From f971106004e847065a3d876c75bbceae7899974a Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 15:09:15 -0800 Subject: [PATCH 01/12] clang-tidy --- .github/workflows/clang-tidy.yml | 2 +- include/benchmark/benchmark.h | 63 ++++++++++++++++++-------------- src/check.cc | 10 ++--- src/check.h | 6 +-- src/log.h | 8 ++-- 5 files changed, 47 insertions(+), 42 deletions(-) diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index 6d50543bdc..49151520d0 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -35,4 +35,4 @@ jobs: - name: run shell: bash working-directory: ${{ runner.workspace }}/_build - run: run-clang-tidy -checks=*,-clang-analyzer-deadcode*,-clang-analyzer-optin* + run: run-clang-tidy -checks=*,-clang-analyzer-deadcode*,-clang-analyzer-optin*,-fuchsia-*,-llvmlibc-*,-modernize-use-trailing-return-type diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index 4ea085adff..7f828f0a7c 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -406,13 +406,16 @@ class MemoryManager { int64_t net_heap_growth; }; - virtual ~MemoryManager() {} + MemoryManager() {} + virtual ~MemoryManager() = default; + MemoryManager(MemoryManager const&) = delete; // Implement this to start recording allocation information. virtual void Start() = 0; // Implement this to stop recording and fill out the given Result structure. virtual void Stop(Result& result) = 0; + }; // Register a MemoryManager instance that will be used to collect and report @@ -425,7 +428,9 @@ void RegisterMemoryManager(MemoryManager* memory_manager); // report profile metrics for the run of the benchmark. class ProfilerManager { public: - virtual ~ProfilerManager() {} + ProfilerManager() {} + virtual ~ProfilerManager() = default; + ProfilerManager(ProfilerManager const&) = delete; // This is called after `Setup()` code and right before the benchmark is run. virtual void AfterSetupStart() = 0; @@ -460,7 +465,7 @@ BENCHMARK_EXPORT Benchmark* RegisterBenchmarkInternal(Benchmark*); // Ensure that the standard streams are properly initialized in every TU. BENCHMARK_EXPORT int InitializeStreams(); -[[maybe_unused]] static int stream_init_anchor = InitializeStreams(); +[[maybe_unused]] static const int stream_init_anchor = InitializeStreams(); } // namespace internal @@ -626,11 +631,13 @@ class Counter { OneK oneK; BENCHMARK_ALWAYS_INLINE - Counter(double v = 0., Flags f = kDefaults, OneK k = kIs1000) - : value(v), flags(f), oneK(k) {} + Counter(double val = 0., Flags flg = kDefaults, OneK onek = kIs1000) // NOLINT + : value(val), flags(flg), oneK(onek) {} - BENCHMARK_ALWAYS_INLINE operator double const &() const { return value; } - BENCHMARK_ALWAYS_INLINE operator double&() { return value; } + BENCHMARK_ALWAYS_INLINE operator double const &() const { // NOLINT + return value; + } + BENCHMARK_ALWAYS_INLINE operator double&() { return value; } // NOLINT }; // A helper for user code to create unforeseen combinations of Flags, without @@ -642,7 +649,7 @@ Counter::Flags inline operator|(const Counter::Flags& LHS, } // This is the container for the user-defined counters. -typedef std::map UserCounters; +using UserCounters = std::map; // BigO is passed to a benchmark in order to specify the asymptotic // computational @@ -650,9 +657,9 @@ typedef std::map UserCounters; // calculated automatically to the best fit. enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda }; -typedef int64_t ComplexityN; +using ComplexityN = int64_t; -typedef int64_t IterationCount; +using IterationCount = int64_t; enum StatisticUnit { kTime, kPercentage }; @@ -670,9 +677,9 @@ struct Statistics { StatisticsFunc* compute_; StatisticUnit unit_; - Statistics(const std::string& name, StatisticsFunc* compute, + Statistics(std::string name, StatisticsFunc* compute, StatisticUnit unit = kTime) - : name_(name), compute_(compute), unit_(unit) {} + : name_(std::move(name)), compute_(compute), unit_(unit) {} }; class BenchmarkInstance; @@ -840,9 +847,9 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State { BENCHMARK_ALWAYS_INLINE int64_t bytes_processed() const { - if (counters.find("bytes_per_second") != counters.end()) - return static_cast(counters.at("bytes_per_second")); - return 0; + return (counters.find("bytes_per_second") != counters.end()) ? + static_cast(counters.at("bytes_per_second")) : + 0; } // If this routine is called with complexity_n > 0 and complexity report is @@ -872,9 +879,9 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State { BENCHMARK_ALWAYS_INLINE int64_t items_processed() const { - if (counters.find("items_per_second") != counters.end()) - return static_cast(counters.at("items_per_second")); - return 0; + return (counters.find("items_per_second") != counters.end()) ? + static_cast(counters.at("items_per_second")) : + 0; } // If this routine is called, the specified label is printed at the @@ -1017,11 +1024,11 @@ inline BENCHMARK_ALWAYS_INLINE bool State::KeepRunningInternal(IterationCount n, struct State::StateIterator { struct [[maybe_unused]] Value {}; - typedef std::forward_iterator_tag iterator_category; - typedef Value value_type; - typedef Value reference; - typedef Value pointer; - typedef std::ptrdiff_t difference_type; + using iterator_category = std::forward_iterator_tag; + using value_type = Value; + using reference = Value; + using pointer = Value; + using difference_type = std::ptrdiff_t; private: friend class State; @@ -1034,7 +1041,7 @@ struct State::StateIterator { public: BENCHMARK_ALWAYS_INLINE - Value operator*() const { return Value(); } + Value operator*() const { return {}; } BENCHMARK_ALWAYS_INLINE StateIterator& operator++() { @@ -1045,7 +1052,7 @@ struct State::StateIterator { BENCHMARK_ALWAYS_INLINE bool operator!=(StateIterator const&) const { - if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) return true; + if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) { return true; } parent_->FinishKeepRunning(); return false; } @@ -1060,7 +1067,7 @@ inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::begin() { } inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() { StartKeepRunning(); - return StateIterator(); + return {}; } namespace internal { @@ -1354,6 +1361,7 @@ class LambdaBenchmark : public Benchmark { LambdaBenchmark(const std::string& name, OLambda&& lam) : Benchmark(name), lambda_(std::forward(lam)) {} + virtual ~LambdaBenchmark() = default; LambdaBenchmark(LambdaBenchmark const&) = delete; template // NOLINTNEXTLINE(readability-redundant-declaration) @@ -1721,7 +1729,7 @@ class BENCHMARK_EXPORT BenchmarkReporter { complexity_n(0), report_big_o(false), report_rms(false), - memory_result(NULL), + memory_result(nullptr), allocs_per_iter(0.0) {} std::string benchmark_name() const; @@ -1844,6 +1852,7 @@ class BENCHMARK_EXPORT BenchmarkReporter { std::ostream& GetErrorStream() const { return *error_stream_; } virtual ~BenchmarkReporter(); + BenchmarkReporter(const BenchmarkReporter&) = delete; // Write a human readable string to 'out' representing the specified // 'context'. diff --git a/src/check.cc b/src/check.cc index 5f7526e08d..c34b1ec288 100644 --- a/src/check.cc +++ b/src/check.cc @@ -1,11 +1,11 @@ #include "check.h" -namespace benchmark { -namespace internal { +namespace benchmark::internal { -static AbortHandlerT* handler = &std::abort; +namespace { +AbortHandlerT* handler = &std::abort; +} // namespace BENCHMARK_EXPORT AbortHandlerT*& GetAbortHandler() { return handler; } -} // namespace internal -} // namespace benchmark +} // namespace benchmark::internal diff --git a/src/check.h b/src/check.h index f9f223f2a1..68e33a53f5 100644 --- a/src/check.h +++ b/src/check.h @@ -26,8 +26,7 @@ #define BENCHMARK_NOEXCEPT_OP(x) #endif -namespace benchmark { -namespace internal { +namespace benchmark::internal { typedef void(AbortHandlerT)(); @@ -74,8 +73,7 @@ class CheckHandler { LogType& log_; }; -} // end namespace internal -} // end namespace benchmark +} // namespace benchmark::internal // The BM_CHECK macro returns a std::ostream object that can have extra // information written to it. diff --git a/src/log.h b/src/log.h index 57b7bdfc45..5ff4a0b14e 100644 --- a/src/log.h +++ b/src/log.h @@ -18,14 +18,12 @@ class LogType { friend LogType& operator<<(LogType&, Tp const&); friend LogType& operator<<(LogType&, EndLType*); + LogType(const LogType&) = delete; + LogType& operator=(const LogType&) = delete; + private: LogType(std::ostream* out) : out_(out) {} std::ostream* out_; - - // NOTE: we could use BENCHMARK_DISALLOW_COPY_AND_ASSIGN but we shouldn't have - // a dependency on benchmark.h from here. - LogType(const LogType&) = delete; - LogType& operator=(const LogType&) = delete; }; template From c29812d4f2dfeec22cdbb058e288789a638c7216 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 15:11:39 -0800 Subject: [PATCH 02/12] clang-format --- include/benchmark/benchmark.h | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index 7f828f0a7c..922e8ed842 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -415,7 +415,6 @@ class MemoryManager { // Implement this to stop recording and fill out the given Result structure. virtual void Stop(Result& result) = 0; - }; // Register a MemoryManager instance that will be used to collect and report @@ -631,10 +630,11 @@ class Counter { OneK oneK; BENCHMARK_ALWAYS_INLINE - Counter(double val = 0., Flags flg = kDefaults, OneK onek = kIs1000) // NOLINT + Counter(double val = 0., Flags flg = kDefaults, + OneK onek = kIs1000) // NOLINT : value(val), flags(flg), oneK(onek) {} - BENCHMARK_ALWAYS_INLINE operator double const &() const { // NOLINT + BENCHMARK_ALWAYS_INLINE operator double const&() const { // NOLINT return value; } BENCHMARK_ALWAYS_INLINE operator double&() { return value; } // NOLINT @@ -847,9 +847,9 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State { BENCHMARK_ALWAYS_INLINE int64_t bytes_processed() const { - return (counters.find("bytes_per_second") != counters.end()) ? - static_cast(counters.at("bytes_per_second")) : - 0; + return (counters.find("bytes_per_second") != counters.end()) + ? static_cast(counters.at("bytes_per_second")) + : 0; } // If this routine is called with complexity_n > 0 and complexity report is @@ -879,9 +879,9 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State { BENCHMARK_ALWAYS_INLINE int64_t items_processed() const { - return (counters.find("items_per_second") != counters.end()) ? - static_cast(counters.at("items_per_second")) : - 0; + return (counters.find("items_per_second") != counters.end()) + ? static_cast(counters.at("items_per_second")) + : 0; } // If this routine is called, the specified label is printed at the @@ -1052,7 +1052,9 @@ struct State::StateIterator { BENCHMARK_ALWAYS_INLINE bool operator!=(StateIterator const&) const { - if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) { return true; } + if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) { + return true; + } parent_->FinishKeepRunning(); return false; } From d1a7e5a9ed7b0b28887fbb39d81aa62dc494914a Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 15:14:19 -0800 Subject: [PATCH 03/12] clang-format is never happy --- include/benchmark/benchmark.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index 922e8ed842..ada829c218 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -634,7 +634,7 @@ class Counter { OneK onek = kIs1000) // NOLINT : value(val), flags(flg), oneK(onek) {} - BENCHMARK_ALWAYS_INLINE operator double const&() const { // NOLINT + BENCHMARK_ALWAYS_INLINE operator double const &() const { // NOLINT return value; } BENCHMARK_ALWAYS_INLINE operator double&() { return value; } // NOLINT From 48717109180be5afb2803a8040f6cc5cc65e3e19 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 15:28:12 -0800 Subject: [PATCH 04/12] move clang-tidy config somewhere central and reduce it --- .clang-tidy | 29 +++++++++++++++++++++++---- .github/workflows/clang-tidy.yml | 2 +- test/benchmark_setup_teardown_test.cc | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 1e229e582e..19f04149ba 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,6 +1,27 @@ --- -Checks: 'clang-analyzer-*,readability-redundant-*,performance-*' -WarningsAsErrors: 'clang-analyzer-*,readability-redundant-*,performance-*' -HeaderFilterRegex: '.*' +Checks: 'abseil-*,bugprone-*,clang-analyzer-*,cppcoreguidelines-*,google-*,misc-*,performance-*,readability-*,-clang-analyzer-deadcode*,-clang-analyzer-optin*,-readability-identifier-length' +WarningsAsErrors: '' +HeaderFilterRegex: '' +AnalyzeTemporaryDtors: false FormatStyle: none -User: user +CheckOptions: + llvm-else-after-return.WarnOnConditionVariables: 'false' + modernize-loop-convert.MinConfidence: reasonable + modernize-replace-auto-ptr.IncludeStyle: llvm + cert-str34-c.DiagnoseSignedUnsignedCharComparisons: 'false' + google-readability-namespace-comments.ShortNamespaceLines: '10' + cert-err33-c.CheckedFunctions: '::aligned_alloc;::asctime_s;::at_quick_exit;::atexit;::bsearch;::bsearch_s;::btowc;::c16rtomb;::c32rtomb;::calloc;::clock;::cnd_broadcast;::cnd_init;::cnd_signal;::cnd_timedwait;::cnd_wait;::ctime_s;::fclose;::fflush;::fgetc;::fgetpos;::fgets;::fgetwc;::fopen;::fopen_s;::fprintf;::fprintf_s;::fputc;::fputs;::fputwc;::fputws;::fread;::freopen;::freopen_s;::fscanf;::fscanf_s;::fseek;::fsetpos;::ftell;::fwprintf;::fwprintf_s;::fwrite;::fwscanf;::fwscanf_s;::getc;::getchar;::getenv;::getenv_s;::gets_s;::getwc;::getwchar;::gmtime;::gmtime_s;::localtime;::localtime_s;::malloc;::mbrtoc16;::mbrtoc32;::mbsrtowcs;::mbsrtowcs_s;::mbstowcs;::mbstowcs_s;::memchr;::mktime;::mtx_init;::mtx_lock;::mtx_timedlock;::mtx_trylock;::mtx_unlock;::printf_s;::putc;::putwc;::raise;::realloc;::remove;::rename;::scanf;::scanf_s;::setlocale;::setvbuf;::signal;::snprintf;::snprintf_s;::sprintf;::sprintf_s;::sscanf;::sscanf_s;::strchr;::strerror_s;::strftime;::strpbrk;::strrchr;::strstr;::strtod;::strtof;::strtoimax;::strtok;::strtok_s;::strtol;::strtold;::strtoll;::strtoul;::strtoull;::strtoumax;::strxfrm;::swprintf;::swprintf_s;::swscanf;::swscanf_s;::thrd_create;::thrd_detach;::thrd_join;::thrd_sleep;::time;::timespec_get;::tmpfile;::tmpfile_s;::tmpnam;::tmpnam_s;::tss_create;::tss_get;::tss_set;::ungetc;::ungetwc;::vfprintf;::vfprintf_s;::vfscanf;::vfscanf_s;::vfwprintf;::vfwprintf_s;::vfwscanf;::vfwscanf_s;::vprintf_s;::vscanf;::vscanf_s;::vsnprintf;::vsnprintf_s;::vsprintf;::vsprintf_s;::vsscanf;::vsscanf_s;::vswprintf;::vswprintf_s;::vswscanf;::vswscanf_s;::vwprintf_s;::vwscanf;::vwscanf_s;::wcrtomb;::wcschr;::wcsftime;::wcspbrk;::wcsrchr;::wcsrtombs;::wcsrtombs_s;::wcsstr;::wcstod;::wcstof;::wcstoimax;::wcstok;::wcstok_s;::wcstol;::wcstold;::wcstoll;::wcstombs;::wcstombs_s;::wcstoul;::wcstoull;::wcstoumax;::wcsxfrm;::wctob;::wctrans;::wctype;::wmemchr;::wprintf_s;::wscanf;::wscanf_s;' + cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField: 'false' + cert-dcl16-c.NewSuffixes: 'L;LL;LU;LLU' + google-readability-braces-around-statements.ShortStatementLines: '1' + cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic: 'true' + google-readability-namespace-comments.SpacesBeforeComments: '2' + modernize-loop-convert.MaxCopySize: '16' + modernize-pass-by-value.IncludeStyle: llvm + modernize-use-nullptr.NullMacros: 'NULL' + llvm-qualified-auto.AddConstToQualified: 'false' + modernize-loop-convert.NamingStyle: CamelCase + llvm-else-after-return.WarnOnUnfixable: 'false' + google-readability-function-size.StatementThreshold: '800' +... + diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index 49151520d0..8bfc57e9c7 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -35,4 +35,4 @@ jobs: - name: run shell: bash working-directory: ${{ runner.workspace }}/_build - run: run-clang-tidy -checks=*,-clang-analyzer-deadcode*,-clang-analyzer-optin*,-fuchsia-*,-llvmlibc-*,-modernize-use-trailing-return-type + run: run-clang-tidy -config_file=../.clang-tidy diff --git a/test/benchmark_setup_teardown_test.cc b/test/benchmark_setup_teardown_test.cc index 6c3cc2e58f..d2b5fff1d0 100644 --- a/test/benchmark_setup_teardown_test.cc +++ b/test/benchmark_setup_teardown_test.cc @@ -130,7 +130,7 @@ BENCHMARK(BM_WithRep) int main(int argc, char** argv) { benchmark::Initialize(&argc, argv); - size_t ret = benchmark::RunSpecifiedBenchmarks("."); + const size_t ret = benchmark::RunSpecifiedBenchmarks("."); assert(ret > 0); // Setup/Teardown is called once for each arg group (1,3,5,7). From 9b7e8dacf02b88306c3644cba724e47f156b141c Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 15:30:57 -0800 Subject: [PATCH 05/12] who uses hyphens in CLI arguments? --- .github/workflows/clang-tidy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index 8bfc57e9c7..1ed15104ce 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -35,4 +35,4 @@ jobs: - name: run shell: bash working-directory: ${{ runner.workspace }}/_build - run: run-clang-tidy -config_file=../.clang-tidy + run: run-clang-tidy -config-file=../.clang-tidy From 312509babe8dd3059d778fbcc0dd873bf978c2a9 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 15:37:34 -0800 Subject: [PATCH 06/12] clean some tidy issues --- include/benchmark/benchmark.h | 8 ++++---- test/link_main_test.cc | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index ada829c218..6040acc408 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -1448,10 +1448,10 @@ class Fixture : public internal::Benchmark { #define BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method) \ BaseClass##_##Method##_Benchmark -#define BENCHMARK_PRIVATE_DECLARE(n) \ - /* NOLINTNEXTLINE(misc-use-anonymous-namespace) */ \ - static ::benchmark::internal::Benchmark* BENCHMARK_PRIVATE_NAME(n) \ - [[maybe_unused]] +#define BENCHMARK_PRIVATE_DECLARE(n) \ + /* NOLINTNEXTLINE(misc-use-anonymous-namespace) */ \ + static const ::benchmark::internal::Benchmark* const \ + BENCHMARK_PRIVATE_NAME(n) [[maybe_unused]] #define BENCHMARK(...) \ BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \ diff --git a/test/link_main_test.cc b/test/link_main_test.cc index 131937eebc..b0a37c06e1 100644 --- a/test/link_main_test.cc +++ b/test/link_main_test.cc @@ -2,7 +2,8 @@ void BM_empty(benchmark::State& state) { for (auto _ : state) { - auto iterations = double(state.iterations()) * double(state.iterations()); + auto iterations = static_cast(state.iterations()) * + static_cast(state.iterations()); benchmark::DoNotOptimize(iterations); } } From 2d9c7d235c21e9e2294bbfe48ada1944d344bfe9 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 15:43:37 -0800 Subject: [PATCH 07/12] restore changes to non-clang-tidy config files --- include/benchmark/benchmark.h | 73 ++++++++++++--------------- src/check.cc | 10 ++-- src/check.h | 6 ++- src/log.h | 8 +-- test/benchmark_setup_teardown_test.cc | 2 +- test/link_main_test.cc | 3 +- 6 files changed, 47 insertions(+), 55 deletions(-) diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index 6040acc408..4ea085adff 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -406,9 +406,7 @@ class MemoryManager { int64_t net_heap_growth; }; - MemoryManager() {} - virtual ~MemoryManager() = default; - MemoryManager(MemoryManager const&) = delete; + virtual ~MemoryManager() {} // Implement this to start recording allocation information. virtual void Start() = 0; @@ -427,9 +425,7 @@ void RegisterMemoryManager(MemoryManager* memory_manager); // report profile metrics for the run of the benchmark. class ProfilerManager { public: - ProfilerManager() {} - virtual ~ProfilerManager() = default; - ProfilerManager(ProfilerManager const&) = delete; + virtual ~ProfilerManager() {} // This is called after `Setup()` code and right before the benchmark is run. virtual void AfterSetupStart() = 0; @@ -464,7 +460,7 @@ BENCHMARK_EXPORT Benchmark* RegisterBenchmarkInternal(Benchmark*); // Ensure that the standard streams are properly initialized in every TU. BENCHMARK_EXPORT int InitializeStreams(); -[[maybe_unused]] static const int stream_init_anchor = InitializeStreams(); +[[maybe_unused]] static int stream_init_anchor = InitializeStreams(); } // namespace internal @@ -630,14 +626,11 @@ class Counter { OneK oneK; BENCHMARK_ALWAYS_INLINE - Counter(double val = 0., Flags flg = kDefaults, - OneK onek = kIs1000) // NOLINT - : value(val), flags(flg), oneK(onek) {} + Counter(double v = 0., Flags f = kDefaults, OneK k = kIs1000) + : value(v), flags(f), oneK(k) {} - BENCHMARK_ALWAYS_INLINE operator double const &() const { // NOLINT - return value; - } - BENCHMARK_ALWAYS_INLINE operator double&() { return value; } // NOLINT + BENCHMARK_ALWAYS_INLINE operator double const &() const { return value; } + BENCHMARK_ALWAYS_INLINE operator double&() { return value; } }; // A helper for user code to create unforeseen combinations of Flags, without @@ -649,7 +642,7 @@ Counter::Flags inline operator|(const Counter::Flags& LHS, } // This is the container for the user-defined counters. -using UserCounters = std::map; +typedef std::map UserCounters; // BigO is passed to a benchmark in order to specify the asymptotic // computational @@ -657,9 +650,9 @@ using UserCounters = std::map; // calculated automatically to the best fit. enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda }; -using ComplexityN = int64_t; +typedef int64_t ComplexityN; -using IterationCount = int64_t; +typedef int64_t IterationCount; enum StatisticUnit { kTime, kPercentage }; @@ -677,9 +670,9 @@ struct Statistics { StatisticsFunc* compute_; StatisticUnit unit_; - Statistics(std::string name, StatisticsFunc* compute, + Statistics(const std::string& name, StatisticsFunc* compute, StatisticUnit unit = kTime) - : name_(std::move(name)), compute_(compute), unit_(unit) {} + : name_(name), compute_(compute), unit_(unit) {} }; class BenchmarkInstance; @@ -847,9 +840,9 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State { BENCHMARK_ALWAYS_INLINE int64_t bytes_processed() const { - return (counters.find("bytes_per_second") != counters.end()) - ? static_cast(counters.at("bytes_per_second")) - : 0; + if (counters.find("bytes_per_second") != counters.end()) + return static_cast(counters.at("bytes_per_second")); + return 0; } // If this routine is called with complexity_n > 0 and complexity report is @@ -879,9 +872,9 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State { BENCHMARK_ALWAYS_INLINE int64_t items_processed() const { - return (counters.find("items_per_second") != counters.end()) - ? static_cast(counters.at("items_per_second")) - : 0; + if (counters.find("items_per_second") != counters.end()) + return static_cast(counters.at("items_per_second")); + return 0; } // If this routine is called, the specified label is printed at the @@ -1024,11 +1017,11 @@ inline BENCHMARK_ALWAYS_INLINE bool State::KeepRunningInternal(IterationCount n, struct State::StateIterator { struct [[maybe_unused]] Value {}; - using iterator_category = std::forward_iterator_tag; - using value_type = Value; - using reference = Value; - using pointer = Value; - using difference_type = std::ptrdiff_t; + typedef std::forward_iterator_tag iterator_category; + typedef Value value_type; + typedef Value reference; + typedef Value pointer; + typedef std::ptrdiff_t difference_type; private: friend class State; @@ -1041,7 +1034,7 @@ struct State::StateIterator { public: BENCHMARK_ALWAYS_INLINE - Value operator*() const { return {}; } + Value operator*() const { return Value(); } BENCHMARK_ALWAYS_INLINE StateIterator& operator++() { @@ -1052,9 +1045,7 @@ struct State::StateIterator { BENCHMARK_ALWAYS_INLINE bool operator!=(StateIterator const&) const { - if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) { - return true; - } + if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) return true; parent_->FinishKeepRunning(); return false; } @@ -1069,7 +1060,7 @@ inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::begin() { } inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() { StartKeepRunning(); - return {}; + return StateIterator(); } namespace internal { @@ -1363,7 +1354,6 @@ class LambdaBenchmark : public Benchmark { LambdaBenchmark(const std::string& name, OLambda&& lam) : Benchmark(name), lambda_(std::forward(lam)) {} - virtual ~LambdaBenchmark() = default; LambdaBenchmark(LambdaBenchmark const&) = delete; template // NOLINTNEXTLINE(readability-redundant-declaration) @@ -1448,10 +1438,10 @@ class Fixture : public internal::Benchmark { #define BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method) \ BaseClass##_##Method##_Benchmark -#define BENCHMARK_PRIVATE_DECLARE(n) \ - /* NOLINTNEXTLINE(misc-use-anonymous-namespace) */ \ - static const ::benchmark::internal::Benchmark* const \ - BENCHMARK_PRIVATE_NAME(n) [[maybe_unused]] +#define BENCHMARK_PRIVATE_DECLARE(n) \ + /* NOLINTNEXTLINE(misc-use-anonymous-namespace) */ \ + static ::benchmark::internal::Benchmark* BENCHMARK_PRIVATE_NAME(n) \ + [[maybe_unused]] #define BENCHMARK(...) \ BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \ @@ -1731,7 +1721,7 @@ class BENCHMARK_EXPORT BenchmarkReporter { complexity_n(0), report_big_o(false), report_rms(false), - memory_result(nullptr), + memory_result(NULL), allocs_per_iter(0.0) {} std::string benchmark_name() const; @@ -1854,7 +1844,6 @@ class BENCHMARK_EXPORT BenchmarkReporter { std::ostream& GetErrorStream() const { return *error_stream_; } virtual ~BenchmarkReporter(); - BenchmarkReporter(const BenchmarkReporter&) = delete; // Write a human readable string to 'out' representing the specified // 'context'. diff --git a/src/check.cc b/src/check.cc index c34b1ec288..5f7526e08d 100644 --- a/src/check.cc +++ b/src/check.cc @@ -1,11 +1,11 @@ #include "check.h" -namespace benchmark::internal { +namespace benchmark { +namespace internal { -namespace { -AbortHandlerT* handler = &std::abort; -} // namespace +static AbortHandlerT* handler = &std::abort; BENCHMARK_EXPORT AbortHandlerT*& GetAbortHandler() { return handler; } -} // namespace benchmark::internal +} // namespace internal +} // namespace benchmark diff --git a/src/check.h b/src/check.h index 68e33a53f5..f9f223f2a1 100644 --- a/src/check.h +++ b/src/check.h @@ -26,7 +26,8 @@ #define BENCHMARK_NOEXCEPT_OP(x) #endif -namespace benchmark::internal { +namespace benchmark { +namespace internal { typedef void(AbortHandlerT)(); @@ -73,7 +74,8 @@ class CheckHandler { LogType& log_; }; -} // namespace benchmark::internal +} // end namespace internal +} // end namespace benchmark // The BM_CHECK macro returns a std::ostream object that can have extra // information written to it. diff --git a/src/log.h b/src/log.h index 5ff4a0b14e..57b7bdfc45 100644 --- a/src/log.h +++ b/src/log.h @@ -18,12 +18,14 @@ class LogType { friend LogType& operator<<(LogType&, Tp const&); friend LogType& operator<<(LogType&, EndLType*); - LogType(const LogType&) = delete; - LogType& operator=(const LogType&) = delete; - private: LogType(std::ostream* out) : out_(out) {} std::ostream* out_; + + // NOTE: we could use BENCHMARK_DISALLOW_COPY_AND_ASSIGN but we shouldn't have + // a dependency on benchmark.h from here. + LogType(const LogType&) = delete; + LogType& operator=(const LogType&) = delete; }; template diff --git a/test/benchmark_setup_teardown_test.cc b/test/benchmark_setup_teardown_test.cc index d2b5fff1d0..6c3cc2e58f 100644 --- a/test/benchmark_setup_teardown_test.cc +++ b/test/benchmark_setup_teardown_test.cc @@ -130,7 +130,7 @@ BENCHMARK(BM_WithRep) int main(int argc, char** argv) { benchmark::Initialize(&argc, argv); - const size_t ret = benchmark::RunSpecifiedBenchmarks("."); + size_t ret = benchmark::RunSpecifiedBenchmarks("."); assert(ret > 0); // Setup/Teardown is called once for each arg group (1,3,5,7). diff --git a/test/link_main_test.cc b/test/link_main_test.cc index b0a37c06e1..131937eebc 100644 --- a/test/link_main_test.cc +++ b/test/link_main_test.cc @@ -2,8 +2,7 @@ void BM_empty(benchmark::State& state) { for (auto _ : state) { - auto iterations = static_cast(state.iterations()) * - static_cast(state.iterations()); + auto iterations = double(state.iterations()) * double(state.iterations()); benchmark::DoNotOptimize(iterations); } } From 90a0f4fa5e2945063c1aec3f98eee6c301d694c9 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 15:46:04 -0800 Subject: [PATCH 08/12] try to get right path for config --- .github/workflows/clang-tidy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index 1ed15104ce..f543f50d70 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -35,4 +35,4 @@ jobs: - name: run shell: bash working-directory: ${{ runner.workspace }}/_build - run: run-clang-tidy -config-file=../.clang-tidy + run: run-clang-tidy -config-file=${{ runner.workspace }}/.clang-tidy From 05d02645c1cb248417c8e7a936f56475c32bc3c4 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 15:55:29 -0800 Subject: [PATCH 09/12] hope it picks up the (cleaner) config --- .clang-tidy | 13 ++++++++++++- .github/workflows/clang-tidy.yml | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 19f04149ba..588a525c89 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -1,5 +1,16 @@ --- -Checks: 'abseil-*,bugprone-*,clang-analyzer-*,cppcoreguidelines-*,google-*,misc-*,performance-*,readability-*,-clang-analyzer-deadcode*,-clang-analyzer-optin*,-readability-identifier-length' +Checks: > + abseil-*, + bugprone-*, + clang-analyzer-*, + cppcoreguidelines-*, + google-*, + misc-*, + performance-*, + readability-*, + -clang-analyzer-deadcode*, + -clang-analyzer-optin*, + -readability-identifier-length WarningsAsErrors: '' HeaderFilterRegex: '' AnalyzeTemporaryDtors: false diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index f543f50d70..558375e3ae 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -35,4 +35,4 @@ jobs: - name: run shell: bash working-directory: ${{ runner.workspace }}/_build - run: run-clang-tidy -config-file=${{ runner.workspace }}/.clang-tidy + run: run-clang-tidy From f5e620b9f9619160005d79582cb8126dd535f1f0 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 16:15:33 -0800 Subject: [PATCH 10/12] let's try an all-in-one solution --- .github/workflows/clang-format-lint.yml | 18 ------------ .github/workflows/clang-tidy.yml | 38 ------------------------- .github/workflows/cpp-linter.yml | 26 +++++++++++++++++ 3 files changed, 26 insertions(+), 56 deletions(-) delete mode 100644 .github/workflows/clang-format-lint.yml delete mode 100644 .github/workflows/clang-tidy.yml create mode 100644 .github/workflows/cpp-linter.yml diff --git a/.github/workflows/clang-format-lint.yml b/.github/workflows/clang-format-lint.yml deleted file mode 100644 index 8f089dc8dc..0000000000 --- a/.github/workflows/clang-format-lint.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: clang-format-lint -on: - push: {} - pull_request: {} - -jobs: - job: - name: check-clang-format - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - uses: DoozyX/clang-format-lint-action@v0.15 - with: - source: './include/benchmark ./src ./test' - extensions: 'h,cc' - clangFormatVersion: 12 - style: Google diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml deleted file mode 100644 index 558375e3ae..0000000000 --- a/.github/workflows/clang-tidy.yml +++ /dev/null @@ -1,38 +0,0 @@ -name: clang-tidy - -on: - push: {} - pull_request: {} - -jobs: - job: - name: run-clang-tidy - runs-on: ubuntu-latest - strategy: - fail-fast: false - steps: - - uses: actions/checkout@v4 - - - name: install clang-tidy - run: sudo apt update && sudo apt -y install clang-tidy - - - name: create build environment - run: cmake -E make_directory ${{ runner.workspace }}/_build - - - name: configure cmake - shell: bash - working-directory: ${{ runner.workspace }}/_build - run: > - cmake $GITHUB_WORKSPACE - -DBENCHMARK_ENABLE_ASSEMBLY_TESTS=OFF - -DBENCHMARK_ENABLE_LIBPFM=OFF - -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON - -DCMAKE_C_COMPILER=clang - -DCMAKE_CXX_COMPILER=clang++ - -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - -DGTEST_COMPILE_COMMANDS=OFF - - - name: run - shell: bash - working-directory: ${{ runner.workspace }}/_build - run: run-clang-tidy diff --git a/.github/workflows/cpp-linter.yml b/.github/workflows/cpp-linter.yml new file mode 100644 index 0000000000..b823f0f8c8 --- /dev/null +++ b/.github/workflows/cpp-linter.yml @@ -0,0 +1,26 @@ +name: cpp-linter + +on: + push: {} + pull_request: {} + +jobs: + job: + name: run-clang-tidy + runs-on: ubuntu-latest + strategy: + fail-fast: false + steps: + - uses: actions/checkout@v4 + - uses: cpp-linter/cpp-linter-action@v2 + id: linter + with: + style: 'file' + tidy-checks: '' + extensions: 'c,h,cc' + - name: fail fast format + if: steps.linter.outputs.checks-failed > 0 + run: exit 1 + - name: fail fast tidy + if: steps.linter.outputs.clang-tidy-checks-failed > 0 + run: exit 1 From eb18c8a3a1db045ad4373505056287ba8e2ef5b4 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 16:22:55 -0800 Subject: [PATCH 11/12] action doesn't work. rolling back --- .github/workflows/clang-format-lint.yml | 18 ++++++++++++ .github/workflows/clang-tidy-lint.yml | 38 +++++++++++++++++++++++++ .github/workflows/cpp-linter.yml | 26 ----------------- 3 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/clang-format-lint.yml create mode 100644 .github/workflows/clang-tidy-lint.yml delete mode 100644 .github/workflows/cpp-linter.yml diff --git a/.github/workflows/clang-format-lint.yml b/.github/workflows/clang-format-lint.yml new file mode 100644 index 0000000000..8f089dc8dc --- /dev/null +++ b/.github/workflows/clang-format-lint.yml @@ -0,0 +1,18 @@ +name: clang-format-lint +on: + push: {} + pull_request: {} + +jobs: + job: + name: check-clang-format + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: DoozyX/clang-format-lint-action@v0.15 + with: + source: './include/benchmark ./src ./test' + extensions: 'h,cc' + clangFormatVersion: 12 + style: Google diff --git a/.github/workflows/clang-tidy-lint.yml b/.github/workflows/clang-tidy-lint.yml new file mode 100644 index 0000000000..e38153b823 --- /dev/null +++ b/.github/workflows/clang-tidy-lint.yml @@ -0,0 +1,38 @@ +name: clang-tidy + +on: + push: {} + pull_request: {} + +jobs: + job: + name: run-clang-tidy + runs-on: ubuntu-latest + strategy: + fail-fast: false + steps: + - uses: actions/checkout@v4 + + - name: install clang-tidy + run: sudo apt update && sudo apt -y install clang-tidy + + - name: create build environment + run: cmake -E make_directory ${{ github.workspace }}/_build + + - name: configure cmake + shell: bash + working-directory: ${{ github.workspace }}/_build + run: > + cmake $GITHUB_WORKSPACE + -DBENCHMARK_ENABLE_ASSEMBLY_TESTS=OFF + -DBENCHMARK_ENABLE_LIBPFM=OFF + -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON + -DCMAKE_C_COMPILER=clang + -DCMAKE_CXX_COMPILER=clang++ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + -DGTEST_COMPILE_COMMANDS=OFF + + - name: run + shell: bash + working-directory: ${{ github.workspace }}/_build + run: run-clang-tidy -config-file=$GITHUB_WORKSPACE/.clang-tidy diff --git a/.github/workflows/cpp-linter.yml b/.github/workflows/cpp-linter.yml deleted file mode 100644 index b823f0f8c8..0000000000 --- a/.github/workflows/cpp-linter.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: cpp-linter - -on: - push: {} - pull_request: {} - -jobs: - job: - name: run-clang-tidy - runs-on: ubuntu-latest - strategy: - fail-fast: false - steps: - - uses: actions/checkout@v4 - - uses: cpp-linter/cpp-linter-action@v2 - id: linter - with: - style: 'file' - tidy-checks: '' - extensions: 'c,h,cc' - - name: fail fast format - if: steps.linter.outputs.checks-failed > 0 - run: exit 1 - - name: fail fast tidy - if: steps.linter.outputs.clang-tidy-checks-failed > 0 - run: exit 1 From 4bbe5bb29d14b149b7a1c0c1d9c593c8e9db3366 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 10 Feb 2025 16:24:49 -0800 Subject: [PATCH 12/12] remove old config field --- .clang-tidy | 1 - 1 file changed, 1 deletion(-) diff --git a/.clang-tidy b/.clang-tidy index 588a525c89..d6cd768f6d 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -13,7 +13,6 @@ Checks: > -readability-identifier-length WarningsAsErrors: '' HeaderFilterRegex: '' -AnalyzeTemporaryDtors: false FormatStyle: none CheckOptions: llvm-else-after-return.WarnOnConditionVariables: 'false'