Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,17 @@ BM_DEFINE_int32(v, 0);

namespace internal {

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::map<std::string, std::string>* global_context = nullptr;

BENCHMARK_EXPORT std::map<std::string, std::string>*& GetGlobalContext() {
return global_context;
}

static void const volatile* volatile global_force_escape_pointer;
namespace {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
void const volatile* volatile global_force_escape_pointer;
} // namespace

// FIXME: Verify if LTO still messes this up?
void UseCharPointer(char const volatile* const v) {
Expand Down
5 changes: 4 additions & 1 deletion src/check.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace benchmark {
namespace internal {

static AbortHandlerT* handler = &std::abort;
namespace {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
AbortHandlerT* handler = &std::abort;
} // namespace

BENCHMARK_EXPORT AbortHandlerT*& GetAbortHandler() { return handler; }

Expand Down
4 changes: 4 additions & 0 deletions src/commandlineflags.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
#define FLAG(name) FLAGS_##name

// Macros for declaring flags.
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
#define BM_DECLARE_bool(name) BENCHMARK_EXPORT extern bool FLAG(name)
#define BM_DECLARE_int32(name) BENCHMARK_EXPORT extern int32_t FLAG(name)
#define BM_DECLARE_double(name) BENCHMARK_EXPORT extern double FLAG(name)
#define BM_DECLARE_string(name) BENCHMARK_EXPORT extern std::string FLAG(name)
#define BM_DECLARE_kvpairs(name) \
BENCHMARK_EXPORT extern std::map<std::string, std::string> FLAG(name)
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)

// Macros for defining flags.
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
#define BM_DEFINE_bool(name, default_val) \
BENCHMARK_EXPORT bool FLAG(name) = benchmark::BoolFromEnv(#name, default_val)
#define BM_DEFINE_int32(name, default_val) \
Expand All @@ -33,6 +36,7 @@
#define BM_DEFINE_kvpairs(name, default_val) \
BENCHMARK_EXPORT std::map<std::string, std::string> FLAG(name) = \
benchmark::KvPairsFromEnv(#name, default_val)
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)

namespace benchmark {

Expand Down
8 changes: 4 additions & 4 deletions src/statistics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace benchmark {

auto StatisticsSum = [](const std::vector<double>& v) {
const auto StatisticsSum = [](const std::vector<double>& v) {
return std::accumulate(v.begin(), v.end(), 0.0);
};

Expand Down Expand Up @@ -59,12 +59,12 @@ double StatisticsMedian(const std::vector<double>& v) {
}

// Return the sum of the squares of this sample set
auto SumSquares = [](const std::vector<double>& v) {
const auto SumSquares = [](const std::vector<double>& v) {
return std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
};

auto Sqr = [](const double dat) { return dat * dat; };
auto Sqrt = [](const double dat) {
const auto Sqr = [](const double dat) { return dat * dat; };
const auto Sqrt = [](const double dat) {
// Avoid NaN due to imprecision in the calculations
if (dat < 0.0) {
return 0.0;
Expand Down
3 changes: 2 additions & 1 deletion test/benchmark_random_interleaving_gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class EventQueue : public std::queue<std::string> {
}
};

EventQueue* queue = new EventQueue();
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
EventQueue* const queue = new EventQueue();

class NullReporter : public BenchmarkReporter {
public:
Expand Down
6 changes: 6 additions & 0 deletions test/benchmark_setup_teardown_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

// Test that Setup() and Teardown() are called exactly once
// for each benchmark run (single-threaded).
namespace {
namespace singlethreaded {
static int setup_call = 0;
static int teardown_call = 0;
} // namespace singlethreaded
} // namespace
static void DoSetup1(const benchmark::State& state) {
++singlethreaded::setup_call;

Expand All @@ -40,11 +42,13 @@ BENCHMARK(BM_with_setup)
->Teardown(DoTeardown1);

// Test that Setup() and Teardown() are called once for each group of threads.
namespace {
namespace concurrent {
static std::atomic<int> setup_call(0);
static std::atomic<int> teardown_call(0);
static std::atomic<int> func_call(0);
} // namespace concurrent
} // namespace

static void DoSetup2(const benchmark::State& state) {
concurrent::setup_call.fetch_add(1, std::memory_order_acquire);
Expand All @@ -71,10 +75,12 @@ BENCHMARK(BM_concurrent)
->Threads(15);

// Testing interaction with Fixture::Setup/Teardown
namespace {
namespace fixture_interaction {
int setup = 0;
int fixture_setup = 0;
} // namespace fixture_interaction
} // namespace

#define FIXTURE_BECHMARK_NAME MyFixture

Expand Down
5 changes: 4 additions & 1 deletion test/benchmark_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ std::set<int64_t> ConstructRandomSet(int64_t size) {
return s;
}

// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
std::mutex test_vector_mu;
std::optional<std::vector<int>> test_vector;
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)

} // end namespace

Expand Down Expand Up @@ -307,7 +309,8 @@ static void BM_templated_test(benchmark::State& state) {
}
}

static auto BM_templated_test_double = BM_templated_test<std::complex<double>>;
static const auto BM_templated_test_double =
BM_templated_test<std::complex<double>>;
BENCHMARK(BM_templated_test_double);

BENCHMARK_MAIN();
34 changes: 18 additions & 16 deletions test/complexity_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace {

#define ADD_COMPLEXITY_CASES(...) \
int CONCAT(dummy, __LINE__) = AddComplexityTest(__VA_ARGS__)
const int CONCAT(dummy, __LINE__) = AddComplexityTest(__VA_ARGS__)

int AddComplexityTest(const std::string &test_name,
const std::string &big_o_test_name,
Expand Down Expand Up @@ -94,11 +94,11 @@ BENCHMARK(BM_Complexity_O1)
->UseManualTime()
->Complexity([](benchmark::IterationCount) { return 1.0; });

const char *one_test_name = "BM_Complexity_O1/manual_time";
const char *big_o_1_test_name = "BM_Complexity_O1/manual_time_BigO";
const char *rms_o_1_test_name = "BM_Complexity_O1/manual_time_RMS";
const char *enum_auto_big_o_1 = "\\([0-9]+\\)";
const char *lambda_big_o_1 = "f\\(N\\)";
constexpr char one_test_name[] = "BM_Complexity_O1/manual_time";
constexpr char big_o_1_test_name[] = "BM_Complexity_O1/manual_time_BigO";
constexpr char rms_o_1_test_name[] = "BM_Complexity_O1/manual_time_RMS";
constexpr char enum_auto_big_o_1[] = "\\([0-9]+\\)";
constexpr char lambda_big_o_1[] = "f\\(N\\)";

// Add enum tests
ADD_COMPLEXITY_CASES(one_test_name, big_o_1_test_name, rms_o_1_test_name,
Expand Down Expand Up @@ -151,11 +151,11 @@ BENCHMARK(BM_Complexity_O_N)
return static_cast<double>(n);
});

const char *n_test_name = "BM_Complexity_O_N/manual_time";
const char *big_o_n_test_name = "BM_Complexity_O_N/manual_time_BigO";
const char *rms_o_n_test_name = "BM_Complexity_O_N/manual_time_RMS";
const char *enum_auto_big_o_n = "N";
const char *lambda_big_o_n = "f\\(N\\)";
constexpr char n_test_name[] = "BM_Complexity_O_N/manual_time";
constexpr char big_o_n_test_name[] = "BM_Complexity_O_N/manual_time_BigO";
constexpr char rms_o_n_test_name[] = "BM_Complexity_O_N/manual_time_RMS";
constexpr char enum_auto_big_o_n[] = "N";
constexpr char lambda_big_o_n[] = "f\\(N\\)";

// Add enum tests
ADD_COMPLEXITY_CASES(n_test_name, big_o_n_test_name, rms_o_n_test_name,
Expand Down Expand Up @@ -209,11 +209,13 @@ BENCHMARK(BM_Complexity_O_N_log_N)
return kLog2E * static_cast<double>(n) * std::log(static_cast<double>(n));
});

const char *n_lg_n_test_name = "BM_Complexity_O_N_log_N/manual_time";
const char *big_o_n_lg_n_test_name = "BM_Complexity_O_N_log_N/manual_time_BigO";
const char *rms_o_n_lg_n_test_name = "BM_Complexity_O_N_log_N/manual_time_RMS";
const char *enum_auto_big_o_n_lg_n = "NlgN";
const char *lambda_big_o_n_lg_n = "f\\(N\\)";
constexpr char n_lg_n_test_name[] = "BM_Complexity_O_N_log_N/manual_time";
constexpr char big_o_n_lg_n_test_name[] =
"BM_Complexity_O_N_log_N/manual_time_BigO";
constexpr char rms_o_n_lg_n_test_name[] =
"BM_Complexity_O_N_log_N/manual_time_RMS";
constexpr char enum_auto_big_o_n_lg_n[] = "NlgN";
constexpr char lambda_big_o_n_lg_n[] = "f\\(N\\)";

// Add enum tests
ADD_COMPLEXITY_CASES(n_lg_n_test_name, big_o_n_lg_n_test_name,
Expand Down
8 changes: 5 additions & 3 deletions test/output_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
#define CONCAT2(x, y) x##y
#define CONCAT(x, y) CONCAT2(x, y)

#define ADD_CASES(...) int CONCAT(dummy, __LINE__) = ::AddCases(__VA_ARGS__)
#define ADD_CASES(...) \
const int CONCAT(dummy, __LINE__) = ::AddCases(__VA_ARGS__)

#define SET_SUBSTITUTIONS(...) \
int CONCAT(dummy, __LINE__) = ::SetSubstitutions(__VA_ARGS__)
const int CONCAT(dummy, __LINE__) = ::SetSubstitutions(__VA_ARGS__)

enum MatchRules : uint8_t {
MR_Default, // Skip non-matching lines until a match is found.
Expand Down Expand Up @@ -80,7 +81,8 @@ std::string GetFileReporterOutput(int argc, char* argv[]);
// will be the subject of a call to checker_function
// checker_function: should be of type ResultsCheckFn (see below)
#define CHECK_BENCHMARK_RESULTS(bm_name_pattern, checker_function) \
size_t CONCAT(dummy, __LINE__) = AddChecker(bm_name_pattern, checker_function)
const size_t CONCAT(dummy, __LINE__) = \
AddChecker(bm_name_pattern, checker_function)

struct Results;
typedef std::function<void(Results const&)> ResultsCheckFn;
Expand Down
7 changes: 4 additions & 3 deletions test/register_benchmark_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ int AddCases(std::initializer_list<TestCase> const& v) {

#define CONCAT(x, y) CONCAT2(x, y)
#define CONCAT2(x, y) x##y
#define ADD_CASES(...) int CONCAT(dummy, __LINE__) = AddCases({__VA_ARGS__})
#define ADD_CASES(...) \
const int CONCAT(dummy, __LINE__) = AddCases({__VA_ARGS__})

} // end namespace

typedef benchmark::internal::Benchmark* ReturnVal;
using ReturnVal = benchmark::internal::Benchmark const* const;

//----------------------------------------------------------------------------//
// Test RegisterBenchmark with no additional arguments
Expand Down Expand Up @@ -91,7 +92,7 @@ int RegisterFromFunction() {
}
return 0;
}
int dummy2 = RegisterFromFunction();
const int dummy2 = RegisterFromFunction();
ADD_CASES({"test1", "One"}, {"test2", "Two"}, {"test3", "Three"});

#endif // BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
Expand Down
2 changes: 1 addition & 1 deletion test/reporter_output_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static int AddContextCases() {
AddCases(TC_JSONOut, {{"\"json_schema_version\": 1$", MR_Next}});
return 0;
}
int dummy_register = AddContextCases();
const int dummy_register = AddContextCases();
ADD_CASES(TC_CSVOut, {{"%csv_header"}});

// ========================================================================= //
Expand Down
3 changes: 2 additions & 1 deletion test/skip_with_error_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct TestCase {
}
};

// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::vector<TestCase> ExpectedResults;

int AddCases(const std::string& base_name,
Expand All @@ -59,7 +60,7 @@ int AddCases(const std::string& base_name,

#define CONCAT(x, y) CONCAT2(x, y)
#define CONCAT2(x, y) x##y
#define ADD_CASES(...) int CONCAT(dummy, __LINE__) = AddCases(__VA_ARGS__)
#define ADD_CASES(...) const int CONCAT(dummy, __LINE__) = AddCases(__VA_ARGS__)

} // end namespace

Expand Down