diff options
author | Juan Gu <juan.gu@mongodb.com> | 2023-03-24 22:45:53 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-03-25 00:09:04 +0000 |
commit | ecb455872f68bfc6ba99c2c663e82f48ce99a1f0 (patch) | |
tree | 4cf2659fef6844062204bbb276ac99ecc3c721af | |
parent | 43082c0051351f8be3555a6326fb8a3b186cd85b (diff) | |
download | mongo-ecb455872f68bfc6ba99c2c663e82f48ce99a1f0.tar.gz |
SERVER-71736 Move tracing support lint to clang-tidy
-rw-r--r-- | .clang-tidy.in | 1 | ||||
-rw-r--r-- | buildscripts/linter/simplecpplint.py | 9 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp | 2 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/MongoTraceCheck.cpp | 59 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/MongoTraceCheck.h | 54 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py | 15 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/tests/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/tests/test_MongoTraceCheck.cpp | 17 | ||||
-rw-r--r-- | src/mongo/util/tracing_support.cpp | 4 |
10 files changed, 152 insertions, 11 deletions
diff --git a/.clang-tidy.in b/.clang-tidy.in index 0150d1c1b92..2230cf40dc3 100644 --- a/.clang-tidy.in +++ b/.clang-tidy.in @@ -38,6 +38,7 @@ Checks: '-*, mongo-std-optional-check, mongo-uninterruptible-lock-guard-check, mongo-volatile-check, + mongo-trace-check, performance-faster-string-find, performance-implicit-conversion-in-loop, performance-inefficient-algorithm, diff --git a/buildscripts/linter/simplecpplint.py b/buildscripts/linter/simplecpplint.py index 399ad2893b0..615f6a2bd82 100644 --- a/buildscripts/linter/simplecpplint.py +++ b/buildscripts/linter/simplecpplint.py @@ -57,7 +57,6 @@ _RE_PATTERN_MONGO_POLYFILL = _make_polyfill_regex() _RE_MUTEX = re.compile('(^|[ ({,])stdx?::mutex[ ({]') _RE_ASSERT = re.compile(r'\bassert\s*\(') _RE_UNSTRUCTURED_LOG = re.compile(r'\blogd\s*\(') -_RE_TRACING_SUPPORT = re.compile(r'\bTracerProvider::(get|initialize)\b') _RE_COLLECTION_SHARDING_RUNTIME = re.compile(r'\bCollectionShardingRuntime\b') _RE_RAND = re.compile(r'\b(srand\(|rand\(\))') @@ -159,7 +158,6 @@ class Linter: self._check_for_nonmongo_assert(linenum) self._check_for_mongo_unstructured_log(linenum) self._check_for_mongo_config_header(linenum) - self._check_for_tracing_support(linenum) self._check_for_collection_sharding_runtime(linenum) self._check_for_rand(linenum) self._check_for_c_stdlib_headers(linenum) @@ -272,13 +270,6 @@ class Linter: linenum, 'mongodb/unstructuredlog', 'Illegal use of unstructured logging, ' 'this is only for local development use and should not be committed.') - def _check_for_tracing_support(self, linenum): - line = self.clean_lines[linenum] - if _RE_TRACING_SUPPORT.search(line): - self._error( - linenum, 'mongodb/tracing', 'Illegal use of tracing support, ' - 'this is only for local development use and should not be committed.') - def _check_for_collection_sharding_runtime(self, linenum): line = self.clean_lines[linenum] if _RE_COLLECTION_SHARDING_RUNTIME.search( diff --git a/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp b/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp index e7f41fd487b..430b3d980ea 100644 --- a/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp +++ b/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp @@ -30,6 +30,7 @@ #include "MongoCctypeCheck.h" #include "MongoHeaderBracketCheck.h" #include "MongoStdOptionalCheck.h" +#include "MongoTraceCheck.h" #include "MongoUninterruptibleLockGuardCheck.h" #include "MongoVolatileCheck.h" @@ -50,6 +51,7 @@ public: CheckFactories.registerCheck<MongoCctypeCheck>("mongo-cctype-check"); CheckFactories.registerCheck<MongoStdOptionalCheck>("mongo-std-optional-check"); CheckFactories.registerCheck<MongoVolatileCheck>("mongo-volatile-check"); + CheckFactories.registerCheck<MongoTraceCheck>("mongo-trace-check"); } }; diff --git a/src/mongo/tools/mongo_tidy_checks/MongoTraceCheck.cpp b/src/mongo/tools/mongo_tidy_checks/MongoTraceCheck.cpp new file mode 100644 index 00000000000..c16a1297161 --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/MongoTraceCheck.cpp @@ -0,0 +1,59 @@ +/** + * Copyright (C) 2023-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "MongoTraceCheck.h" + +namespace mongo::tidy { + +using namespace clang; +using namespace clang::ast_matchers; + +MongoTraceCheck::MongoTraceCheck(StringRef Name, clang::tidy::ClangTidyContext* Context) + : ClangTidyCheck(Name, Context) {} + +void MongoTraceCheck::registerMatchers(ast_matchers::MatchFinder* Finder) { + // Matcher for TracerProvider::get and TracerProvider::initialize + Finder->addMatcher(callExpr(anyOf(callee(functionDecl(hasName("TracerProvider::get"))), + callee(functionDecl(hasName("TracerProvider::initialize"))))) + .bind("tracing_support"), + this); +} + +void MongoTraceCheck::check(const ast_matchers::MatchFinder::MatchResult& Result) { + + // Get the matched TracerProvider::get and TracerProvider::initialize + const auto* matchedTraceSupport = Result.Nodes.getNodeAs<CallExpr>("tracing_support"); + if (matchedTraceSupport) { + diag(matchedTraceSupport->getBeginLoc(), + "Illegal use of prohibited tracing support, this is only for local development use " + "and should not be committed."); + } +} + +} // namespace mongo::tidy diff --git a/src/mongo/tools/mongo_tidy_checks/MongoTraceCheck.h b/src/mongo/tools/mongo_tidy_checks/MongoTraceCheck.h new file mode 100644 index 00000000000..fcdf92c3beb --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/MongoTraceCheck.h @@ -0,0 +1,54 @@ +/** + * Copyright (C) 2023-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ +#pragma once + +#include <clang-tidy/ClangTidy.h> +#include <clang-tidy/ClangTidyCheck.h> + +namespace mongo::tidy { + +/** + * MongoTraceCheck is a custom clang-tidy check for detecting + * the usage of TracerProvider::get and TracerProvider::initialize + * in the source code. + * + * It extends ClangTidyCheck and overrides the registerMatchers + * and check functions. The registerMatchers function adds matchers + * to identify the usage of racerProvider::get and TracerProvider::initialize, + * while the check function flags the matched occurrences + */ +class MongoTraceCheck : public clang::tidy::ClangTidyCheck { + +public: + MongoTraceCheck(clang::StringRef Name, clang::tidy::ClangTidyContext* Context); + void registerMatchers(clang::ast_matchers::MatchFinder* Finder) override; + void check(const clang::ast_matchers::MatchFinder::MatchResult& Result) override; +}; + +} // namespace mongo::tidy diff --git a/src/mongo/tools/mongo_tidy_checks/SConscript b/src/mongo/tools/mongo_tidy_checks/SConscript index 9992021f656..4da27589622 100644 --- a/src/mongo/tools/mongo_tidy_checks/SConscript +++ b/src/mongo/tools/mongo_tidy_checks/SConscript @@ -127,6 +127,7 @@ mongo_custom_check = env.SharedLibrary( "MongoStdOptionalCheck.cpp", "MongoVolatileCheck.cpp", "MongoTidyModule.cpp", + "MongoTraceCheck.cpp", ], LIBDEPS_NO_INHERIT=[ '$BUILD_DIR/third_party/shim_allocator', diff --git a/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py b/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py index 85a280dd249..55da0bcf0d2 100644 --- a/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py +++ b/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py @@ -167,6 +167,21 @@ class MongoTidyTests(unittest.TestCase): self.run_clang_tidy() + def test_MongoTraceCheck(self): + + self.write_config( + textwrap.dedent("""\ + Checks: '-*,mongo-trace-check' + WarningsAsErrors: '*' + """)) + + self.expected_output = [ + "Illegal use of prohibited tracing support, this is only for local development use and should not be committed. [mongo-trace-check,-warnings-as-errors]\n TracerProvider::initialize();", + "Illegal use of prohibited tracing support, this is only for local development use and should not be committed. [mongo-trace-check,-warnings-as-errors]\n TracerProvider provider = TracerProvider::get();", + ] + + self.run_clang_tidy() + if __name__ == '__main__': diff --git a/src/mongo/tools/mongo_tidy_checks/tests/SConscript b/src/mongo/tools/mongo_tidy_checks/tests/SConscript index 19d4ad8487a..8903f0366b8 100644 --- a/src/mongo/tools/mongo_tidy_checks/tests/SConscript +++ b/src/mongo/tools/mongo_tidy_checks/tests/SConscript @@ -30,6 +30,7 @@ if env.GetOption('ninja') == 'disabled': 'test_MongoUninterruptibleLockGuardCheck.cpp', 'test_MongoCctypeCheck.cpp', 'test_MongoStdOptionalCheck.cpp', + 'test_MongoTraceCheck.cpp', ] # So that we can do fast runs, we will generate a separate compilation database file for each diff --git a/src/mongo/tools/mongo_tidy_checks/tests/test_MongoTraceCheck.cpp b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoTraceCheck.cpp new file mode 100644 index 00000000000..5965d5b9ed9 --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoTraceCheck.cpp @@ -0,0 +1,17 @@ +namespace mongo { + +class TracerProvider { +public: + static void initialize() {} + static TracerProvider get() { + return TracerProvider(); + } +}; + +int testTraceProviderClass() { + TracerProvider::initialize(); + TracerProvider provider = TracerProvider::get(); + return 0; +} + +} // namespace mongo diff --git a/src/mongo/util/tracing_support.cpp b/src/mongo/util/tracing_support.cpp index b7484bcbb51..4415c2f14c2 100644 --- a/src/mongo/util/tracing_support.cpp +++ b/src/mongo/util/tracing_support.cpp @@ -256,13 +256,13 @@ Tracer::Tracer(std::string name, _factory = maker(std::move(name), this); } -void TracerProvider::initialize(std::unique_ptr<TickSource> tickSource) { // NOLINT +void TracerProvider::initialize(std::unique_ptr<TickSource> tickSource) { auto& provider = getTraceProvider(); invariant(!provider.has_value(), "already initialized"); provider.emplace(TracerProvider(std::move(tickSource))); } -TracerProvider& TracerProvider::get() { // NOLINT +TracerProvider& TracerProvider::get() { auto& provider = getTraceProvider(); invariant(provider.has_value(), "not initialized"); return provider.value(); |