From febb5a13d0a3b20de8a67264a91dacd368eb25f4 Mon Sep 17 00:00:00 2001 From: Felix-Gong Date: Tue, 16 Jun 2026 03:38:33 +0000 Subject: [PATCH] Use -fno-access-control instead of -Dprivate=public for tests 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. 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) --- test/BUILD.bazel | 11 ----------- test/CMakeLists.txt | 4 ++-- test/Makefile | 4 ++-- test/sstream_workaround.h | 33 --------------------------------- 4 files changed, 4 insertions(+), 48 deletions(-) delete mode 100644 test/sstream_workaround.h diff --git a/test/BUILD.bazel b/test/BUILD.bazel index d442d3b764..3efe804c4d 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -143,13 +143,6 @@ brpc_proto_library( visibility = ["//visibility:public"], ) -cc_library( - name = "sstream_workaround", - hdrs = [ - "sstream_workaround.h", - ], -) - cc_library( name = "gperftools_helper", hdrs = [ @@ -182,7 +175,6 @@ cc_test( defines = ["HAS_NLOHMANN_JSON=1"], deps = [ ":cc_test_proto", - ":sstream_workaround", ":gperftools_helper", "//:butil", "//:bthread", @@ -195,7 +187,6 @@ cc_test( name = "bvar_unittests", srcs = glob(["bvar_*_unittest.cpp"]), deps = [ - ":sstream_workaround", ":gperftools_helper", "//:bvar", "@com_google_googletest//:gtest", @@ -210,7 +201,6 @@ generate_unittests( "bthread*_unittest.cpp", ]), deps = [ - ":sstream_workaround", ":gperftools_helper", "//:bthread", "@com_google_googletest//:gtest", @@ -239,7 +229,6 @@ generate_unittests( "brpc_*_unittest.cpp", ]), deps = [ - ":sstream_workaround", ":gperftools_helper", "//:brpc", ":cc_test_proto", diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ade7350f5a..df8fedce49 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -53,8 +53,8 @@ else() endif() set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} ${DEFINE_CLOCK_GETTIME} -DBRPC_WITH_GLOG=${WITH_GLOG_VAL} -DBRPC_WITH_RDMA=${WITH_RDMA_VAL}") -set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__=__unused__ -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 -D__STRICT_ANSI__ -include ${PROJECT_SOURCE_DIR}/test/sstream_workaround.h") -set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -g -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer") +set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -DBTHREAD_USE_FAST_PTHREAD_MUTEX -D__const__=__unused__ -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -DBVAR_NOT_LINK_DEFAULT_VARIABLES -D__STRICT_ANSI__") +set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -g -O2 -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -fno-access-control") use_cxx11() if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") diff --git a/test/Makefile b/test/Makefile index 30e196bbce..15169937ef 100644 --- a/test/Makefile +++ b/test/Makefile @@ -18,8 +18,8 @@ NEED_GPERFTOOLS=1 NEED_GTEST=1 include ../config.mk -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 -CXXFLAGS+=$(CPPFLAGS) -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -std=c++0x +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 -DBVAR_NOT_LINK_DEFAULT_VARIABLES +CXXFLAGS+=$(CPPFLAGS) -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -fno-access-control -std=c++0x # On Darwin, only gtest library is needed, other libraries have been linked in `brpc.dbg.dylib` ifeq ($(SYSTEM),Darwin) diff --git a/test/sstream_workaround.h b/test/sstream_workaround.h deleted file mode 100644 index 05934e5597..0000000000 --- a/test/sstream_workaround.h +++ /dev/null @@ -1,33 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -#ifndef BUTIL_TEST_SSTREAM_WORKAROUND -#define BUTIL_TEST_SSTREAM_WORKAROUND - -// defining private as public makes it fail to compile sstream with gcc5.x like this: -// "error: ‘struct std::__cxx11::basic_stringbuf<_CharT, _Traits, _Alloc>:: -// __xfer_bufptrs’ redeclared with different access" - -#ifdef private -# undef private -# include -# define private public -#else -# include -#endif - -#endif // BUTIL_TEST_SSTREAM_WORKAROUND