Skip to content

use -fno-access-control instead of -Dprivate=public for tests#3346

Merged
chenBright merged 1 commit into
apache:masterfrom
Felix-Gong:fix-std-any-cpp17
Jun 16, 2026
Merged

use -fno-access-control instead of -Dprivate=public for tests#3346
chenBright merged 1 commit into
apache:masterfrom
Felix-Gong:fix-std-any-cpp17

Conversation

@Felix-Gong

@Felix-Gong Felix-Gong commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Replace -Dprivate=public -Dprotected=public macro hack with -fno-access-control compiler flag in CMake and Makefile test builds. The macro approach caused redeclaration errors with standard library headers (<sstream>, <any>, etc.) that use private access specifiers internally.

-fno-access-control is a GCC/Clang compiler flag that cleanly disables access control checking without affecting header parsing. The Bazel build already uses this flag at test/BUILD.bazel:26, so this makes CMake and Makefile builds consistent with Bazel.

Fixes #3345

Changes

  • test/CMakeLists.txt: Replace -Dprivate=public -Dprotected=public -include sstream_workaround.h with -fno-access-control
  • test/Makefile: Same change
  • test/sstream_workaround.h: Deleted (no longer referenced)

Test plan

  • Local x86 tests: 72/75 passed (3 failures are pre-existing: bthread_dispatcher_unittest epoll issue, test_butil StackTraceTest + EndPointTest)
  • All core bthread and brpc tests pass

@chenBright chenBright left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using -fno-access-control, like in test/BUILD.bazel, would be more reasonable.

"-fno-access-control",

Comment thread test/sstream_workaround.h Outdated
# if __cplusplus >= 201703L
# include <any>
# endif
# define private public

@chenBright chenBright Jun 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using -fno-access-control instead of # define private public?

@Felix-Gong

Copy link
Copy Markdown
Contributor Author

Good point! -fno-access-control is indeed a cleaner approach than #define private public. It avoids the redeclaration issues with standard library headers entirely.

I'll update the PR to:

  1. Replace -Dprivate=public with -fno-access-control in CMake build flags
  2. Simplify sstream_workaround.h (the #ifdef private guard becomes unnecessary)

The Bazel build already uses -fno-access-control at test/BUILD.bazel:26, so this makes CMake consistent with Bazel.

@chenBright

Copy link
Copy Markdown
Contributor

test/Makefile also needs to be updated.

CPPFLAGS+=-DBTHREAD_USE_FAST_PTHREAD_MUTEX -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES --include sstream_workaround.h

Comment thread test/sstream_workaround.h Outdated
#else
# include <sstream>
#endif
// Previously, tests used -Dprivate=public to access private members.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file test/sstream_workaround.h can be deleted, right?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the test build configuration to avoid C++ standard library compilation failures caused by the historical -Dprivate=public/-Dprotected=public approach (notably when <any> is pulled in under C++17).

Changes:

  • Remove -Dprivate=public/-Dprotected=public from test builds and switch to -fno-access-control to bypass access checks without rewriting tokens in headers.
  • Drop forced inclusion of test/sstream_workaround.h from test build flags and simplify the header to comments-only.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
test/sstream_workaround.h Removes the old forced-include workaround logic and leaves explanatory comments.
test/Makefile Replaces -Dprivate=public/-Dprotected=public with -fno-access-control for the Makefile-based test build.
test/CMakeLists.txt Replaces -Dprivate=public/-Dprotected=public with -fno-access-control for the CMake-based test build.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/CMakeLists.txt
Comment thread test/Makefile
Comment thread test/CMakeLists.txt
Comment thread test/sstream_workaround.h Outdated
@Felix-Gong Felix-Gong changed the title fix: include <any> in sstream_workaround.h to fix C++17 build use -fno-access-control instead of -Dprivate=public for tests Jun 16, 2026
@chenBright

Copy link
Copy Markdown
Contributor

These all need to be deleted.

brpc/test/BUILD.bazel

Lines 146 to 151 in f04e0e0

cc_library(
name = "sstream_workaround",
hdrs = [
"sstream_workaround.h",
],
)

":sstream_workaround",

":sstream_workaround",

":sstream_workaround",

":sstream_workaround",

@Felix-Gong

Copy link
Copy Markdown
Contributor Author

These all need to be deleted.

Yes, I'm removing all sstream_workaround references from test/BUILD.bazel now. The fix will be pushed shortly.

Replace the -Dprivate=public/-Dprotected=public macro hack with
-fno-access-control compiler flag in test builds. This avoids
redeclaration issues with standard library headers (e.g. <any>
under C++17) while still allowing tests to access private members.

Changes:
- test/CMakeLists.txt: replace -Dprivate=public with -fno-access-control
- test/Makefile: replace -Dprivate=public with -fno-access-control
- test/BUILD.bazel: remove sstream_workaround cc_library and all deps
- test/sstream_workaround.h: remove workaround (no longer needed)
@chenBright chenBright merged commit 11c8ea9 into apache:master Jun 16, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-Dprivate=public in test CMakeLists.txt breaks std::any compilation with C++17

3 participants