diff options
author | Juan Gu <juan.gu@mongodb.com> | 2023-02-23 19:05:45 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-02-23 23:17:58 +0000 |
commit | 248888231318f0423bf78234d2f120585f4fdd6a (patch) | |
tree | dc4a087f4dd4c48b3234fbf7f0a10a25fafd02bc | |
parent | 307fa56fe32f797b13df4b22425868d107447fd4 (diff) | |
download | mongo-248888231318f0423bf78234d2f120585f4fdd6a.tar.gz |
SERVER-71742 Move volatile keyword lint to clang-tidy
-rw-r--r-- | .clang-tidy.in | 1 | ||||
-rw-r--r-- | buildscripts/linter/simplecpplint.py | 10 | ||||
-rw-r--r-- | src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp | 2 | ||||
-rw-r--r-- | src/mongo/platform/decimal128_test.cpp | 70 | ||||
-rw-r--r-- | src/mongo/stdx/condition_variable_bm.cpp | 2 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp | 4 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/MongoVolatileCheck.cpp | 65 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/MongoVolatileCheck.h | 48 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py | 18 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/tests/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/tests/test_MongoVolatileCheck.cpp | 37 | ||||
-rw-r--r-- | src/mongo/util/tcmalloc_set_parameter.cpp | 2 |
13 files changed, 211 insertions, 50 deletions
diff --git a/.clang-tidy.in b/.clang-tidy.in index f2ce9201e93..0150d1c1b92 100644 --- a/.clang-tidy.in +++ b/.clang-tidy.in @@ -37,6 +37,7 @@ Checks: '-*, mongo-header-bracket-check, mongo-std-optional-check, mongo-uninterruptible-lock-guard-check, + mongo-volatile-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 d82b30af217..399ad2893b0 100644 --- a/buildscripts/linter/simplecpplint.py +++ b/buildscripts/linter/simplecpplint.py @@ -54,7 +54,6 @@ _RE_LINT = re.compile("//.*NOLINT") _RE_COMMENT_STRIP = re.compile("//.*") _RE_PATTERN_MONGO_POLYFILL = _make_polyfill_regex() -_RE_VOLATILE = re.compile('[^_]volatile') _RE_MUTEX = re.compile('(^|[ ({,])stdx?::mutex[ ({]') _RE_ASSERT = re.compile(r'\bassert\s*\(') _RE_UNSTRUCTURED_LOG = re.compile(r'\blogd\s*\(') @@ -154,7 +153,6 @@ class Linter: if not self.clean_lines[linenum]: continue - self._check_for_mongo_volatile(linenum) self._check_for_mongo_polyfill(linenum) self._check_for_mongo_atomic(linenum) self._check_for_mongo_mutex(linenum) @@ -235,14 +233,6 @@ class Linter: if def_line is not None: self._error(def_line, 'mongodb/undefmacro', f'Missing "#undef {macro}"') - def _check_for_mongo_volatile(self, linenum): - line = self.clean_lines[linenum] - if _RE_VOLATILE.search(line) and not "__asm__" in line: - self._error( - linenum, 'mongodb/volatile', - 'Illegal use of the volatile storage keyword, use AtomicWord instead ' - 'from "mongo/platform/atomic_word.h"') - def _check_for_mongo_polyfill(self, linenum): line = self.clean_lines[linenum] match = _RE_PATTERN_MONGO_POLYFILL.search(line) diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp index a2872808d5a..ecc732af658 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp @@ -737,7 +737,7 @@ void WiredTigerKVEngine::cleanShutdown() { bool leak_memory = !kAddressSanitizerEnabled; std::string closeConfig = ""; - if (RUNNING_ON_VALGRIND) { + if (RUNNING_ON_VALGRIND) { // NOLINT leak_memory = false; } diff --git a/src/mongo/platform/decimal128_test.cpp b/src/mongo/platform/decimal128_test.cpp index 0a58e22dfa7..f933de4a484 100644 --- a/src/mongo/platform/decimal128_test.cpp +++ b/src/mongo/platform/decimal128_test.cpp @@ -74,41 +74,41 @@ TEST(Decimal128Test, TestConstructor) { TEST_CTOR_COMMON_(vn, HIGH64, LOW64) \ } - TEST_CTOR(int8_t{0}, posHigh, 0); // +0E+0 - TEST_CTOR(Lim<int8_t>::lowest(), negHigh, uint64_t{1} << 7); - TEST_CTOR(Lim<int8_t>::max(), posHigh, (uint64_t{1} << 7) - 1); - TEST_CTOR(int8_t{5}, posHigh, 0x5); - - TEST_CTOR(uint8_t{0}, posHigh, 0); - TEST_CTOR(Lim<uint8_t>::max(), posHigh, (uint64_t{1} << 8) - 1); - TEST_CTOR(uint8_t{5}, posHigh, 0x5); - - TEST_CTOR(int16_t{0}, posHigh, 0); - TEST_CTOR(Lim<int16_t>::lowest(), negHigh, uint64_t{1} << 15); - TEST_CTOR(Lim<int16_t>::max(), posHigh, (uint64_t{1} << 15) - 1); - TEST_CTOR(int16_t{5}, posHigh, 0x5); - - TEST_CTOR(uint16_t{0}, posHigh, 0); - TEST_CTOR(Lim<uint16_t>::max(), posHigh, (uint64_t{1} << 16) - 1); - TEST_CTOR(uint16_t{5}, posHigh, 0x5); - - TEST_CTOR(int32_t{0}, posHigh, 0); - TEST_CTOR(Lim<int32_t>::lowest(), negHigh, uint64_t{1} << 31); - TEST_CTOR(Lim<int32_t>::max(), posHigh, (uint64_t{1} << 31) - 1); - TEST_CTOR(int32_t{5}, posHigh, 0x5); - - TEST_CTOR(uint32_t{0}, posHigh, 0); - TEST_CTOR(Lim<uint32_t>::max(), posHigh, (uint64_t{1} << 32) - 1); - TEST_CTOR(uint32_t{5}, posHigh, 0x5); - - TEST_CTOR(int64_t{0}, posHigh, 0); - TEST_CTOR(Lim<int64_t>::lowest(), negHigh, uint64_t{1} << 63); - TEST_CTOR(Lim<int64_t>::max(), posHigh, (uint64_t{1} << 63) - 1); - TEST_CTOR(int64_t{5}, posHigh, 0x5); - - TEST_CTOR(uint64_t{0}, posHigh, 0); - TEST_CTOR(Lim<uint64_t>::max(), posHigh, Lim<uint64_t>::max()); - TEST_CTOR(uint64_t{5}, posHigh, 0x5); + TEST_CTOR(int8_t{0}, posHigh, 0); // NOLINT + TEST_CTOR(Lim<int8_t>::lowest(), negHigh, uint64_t{1} << 7); // NOLINT + TEST_CTOR(Lim<int8_t>::max(), posHigh, (uint64_t{1} << 7) - 1); // NOLINT + TEST_CTOR(int8_t{5}, posHigh, 0x5); // NOLINT + + TEST_CTOR(uint8_t{0}, posHigh, 0); // NOLINT + TEST_CTOR(Lim<uint8_t>::max(), posHigh, (uint64_t{1} << 8) - 1); // NOLINT + TEST_CTOR(uint8_t{5}, posHigh, 0x5); // NOLINT + + TEST_CTOR(int16_t{0}, posHigh, 0); // NOLINT + TEST_CTOR(Lim<int16_t>::lowest(), negHigh, uint64_t{1} << 15); // NOLINT + TEST_CTOR(Lim<int16_t>::max(), posHigh, (uint64_t{1} << 15) - 1); // NOLINT + TEST_CTOR(int16_t{5}, posHigh, 0x5); // NOLINT + + TEST_CTOR(uint16_t{0}, posHigh, 0); // NOLINT + TEST_CTOR(Lim<uint16_t>::max(), posHigh, (uint64_t{1} << 16) - 1); // NOLINT + TEST_CTOR(uint16_t{5}, posHigh, 0x5); // NOLINT + + TEST_CTOR(int32_t{0}, posHigh, 0); // NOLINT + TEST_CTOR(Lim<int32_t>::lowest(), negHigh, uint64_t{1} << 31); // NOLINT + TEST_CTOR(Lim<int32_t>::max(), posHigh, (uint64_t{1} << 31) - 1); // NOLINT + TEST_CTOR(int32_t{5}, posHigh, 0x5); // NOLINT + + TEST_CTOR(uint32_t{0}, posHigh, 0); // NOLINT + TEST_CTOR(Lim<uint32_t>::max(), posHigh, (uint64_t{1} << 32) - 1); // NOLINT + TEST_CTOR(uint32_t{5}, posHigh, 0x5); // NOLINT + + TEST_CTOR(int64_t{0}, posHigh, 0); // NOLINT + TEST_CTOR(Lim<int64_t>::lowest(), negHigh, uint64_t{1} << 63); // NOLINT + TEST_CTOR(Lim<int64_t>::max(), posHigh, (uint64_t{1} << 63) - 1); // NOLINT + TEST_CTOR(int64_t{5}, posHigh, 0x5); // NOLINT + + TEST_CTOR(uint64_t{0}, posHigh, 0); // NOLINT + TEST_CTOR(Lim<uint64_t>::max(), posHigh, Lim<uint64_t>::max()); // NOLINT + TEST_CTOR(uint64_t{5}, posHigh, 0x5); // NOLINT #undef TEST_CTOR_COMMON_ #undef TEST_CTOR diff --git a/src/mongo/stdx/condition_variable_bm.cpp b/src/mongo/stdx/condition_variable_bm.cpp index 0b032d99bee..92799ad1387 100644 --- a/src/mongo/stdx/condition_variable_bm.cpp +++ b/src/mongo/stdx/condition_variable_bm.cpp @@ -55,7 +55,7 @@ void BM_stdxNotifyOneNoNotifyables(benchmark::State& state) { } } -volatile bool alwaysTrue = true; +volatile bool alwaysTrue = true; // NOLINT void BM_stdWaitWithTruePredicate(benchmark::State& state) { std::condition_variable cv; // NOLINT diff --git a/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp b/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp index 75c31e615b9..e7f41fd487b 100644 --- a/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp +++ b/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp @@ -31,6 +31,7 @@ #include "MongoHeaderBracketCheck.h" #include "MongoStdOptionalCheck.h" #include "MongoUninterruptibleLockGuardCheck.h" +#include "MongoVolatileCheck.h" #include <clang-tidy/ClangTidy.h> #include <clang-tidy/ClangTidyCheck.h> @@ -48,6 +49,7 @@ public: CheckFactories.registerCheck<MongoHeaderBracketCheck>("mongo-header-bracket-check"); CheckFactories.registerCheck<MongoCctypeCheck>("mongo-cctype-check"); CheckFactories.registerCheck<MongoStdOptionalCheck>("mongo-std-optional-check"); + CheckFactories.registerCheck<MongoVolatileCheck>("mongo-volatile-check"); } }; @@ -59,6 +61,6 @@ static clang::tidy::ClangTidyModuleRegistry::Add<MongoTidyModule> X("mongo-tidy- // This anchor is used to force the linker to link in the generated object file // and thus register the MongoTidyModule. -volatile int MongoTidyModuleAnchorSource = 0; +volatile int MongoTidyModuleAnchorSource = 0; // NOLINT } // namespace mongo diff --git a/src/mongo/tools/mongo_tidy_checks/MongoVolatileCheck.cpp b/src/mongo/tools/mongo_tidy_checks/MongoVolatileCheck.cpp new file mode 100644 index 00000000000..98aafd21574 --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/MongoVolatileCheck.cpp @@ -0,0 +1,65 @@ +/** + * 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 "MongoVolatileCheck.h" + +namespace mongo::tidy { + +using namespace clang; +using namespace clang::ast_matchers; + +MongoVolatileCheck::MongoVolatileCheck(StringRef Name, clang::tidy::ClangTidyContext* Context) + : ClangTidyCheck(Name, Context) {} + +void MongoVolatileCheck::registerMatchers(MatchFinder* Finder) { + + // Matcher for finding all instances of variables volatile + Finder->addMatcher(varDecl(hasType(isVolatileQualified())).bind("var_volatile"), this); + + // Matcher for finding all instances of field volatile + Finder->addMatcher(fieldDecl(hasType(isVolatileQualified())).bind("field_volatile"), this); +} + +void MongoVolatileCheck::check(const MatchFinder::MatchResult& Result) { + const auto* var_match = Result.Nodes.getNodeAs<VarDecl>("var_volatile"); + const auto* field_match = Result.Nodes.getNodeAs<FieldDecl>("field_volatile"); + + if (var_match) { + diag(var_match->getBeginLoc(), + "Illegal use of the volatile storage keyword, use AtomicWord instead from " + "\"mongo/platform/atomic_word.h\""); + } + if (field_match) { + diag(field_match->getBeginLoc(), + "Illegal use of the volatile storage keyword, use AtomicWord instead from " + "\"mongo/platform/atomic_word.h\""); + } +} + +} // namespace mongo::tidy diff --git a/src/mongo/tools/mongo_tidy_checks/MongoVolatileCheck.h b/src/mongo/tools/mongo_tidy_checks/MongoVolatileCheck.h new file mode 100644 index 00000000000..35d6a0f4fa7 --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/MongoVolatileCheck.h @@ -0,0 +1,48 @@ +/** + * 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 { + +/** + * check for new instances of Volatile + * Overrides the default registerMatchers function to add matcher to match the + * use of volatile. overrides the default check function to flag the uses of volatile + */ +class MongoVolatileCheck : public clang::tidy::ClangTidyCheck { +public: + MongoVolatileCheck(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 7be8eab2cd6..9992021f656 100644 --- a/src/mongo/tools/mongo_tidy_checks/SConscript +++ b/src/mongo/tools/mongo_tidy_checks/SConscript @@ -125,6 +125,7 @@ mongo_custom_check = env.SharedLibrary( "MongoUninterruptibleLockGuardCheck.cpp", "MongoCctypeCheck.cpp", "MongoStdOptionalCheck.cpp", + "MongoVolatileCheck.cpp", "MongoTidyModule.cpp", ], LIBDEPS_NO_INHERIT=[ 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 355a44787f1..85a280dd249 100644 --- a/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py +++ b/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py @@ -72,7 +72,7 @@ class MongoTidyTests(unittest.TestCase): 'buildscripts/clang_tidy.py', '--disable-reporting', f'--check-module={self.TIDY_MODULE}', - f'--output-dir={os.path.join(os.path.dirname(compiledb), self._testMethodName + "_out")}', + f'--output-dir={os.path.join(os.path.dirname(self.compile_db), self._testMethodName + "_out")}', f'--compile-commands={self.compile_db}', ] else: @@ -151,6 +151,22 @@ class MongoTidyTests(unittest.TestCase): self.run_clang_tidy() + def test_MongoVolatileCheck(self): + + self.write_config( + textwrap.dedent("""\ + Checks: '-*,mongo-volatile-check' + WarningsAsErrors: '*' + """)) + + self.expected_output = [ + "Illegal use of the volatile storage keyword, use AtomicWord instead from \"mongo/platform/atomic_word.h\" [mongo-volatile-check,-warnings-as-errors]\nvolatile int varVolatileTest;", + "Illegal use of the volatile storage keyword, use AtomicWord instead from \"mongo/platform/atomic_word.h\" [mongo-volatile-check,-warnings-as-errors]\n volatile int fieldVolatileTest;", + "Illegal use of the volatile storage keyword, use AtomicWord instead from \"mongo/platform/atomic_word.h\" [mongo-volatile-check,-warnings-as-errors]\nvoid functionName(volatile int varVolatileTest) {}", + ] + + 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 e32789906c5..19d4ad8487a 100644 --- a/src/mongo/tools/mongo_tidy_checks/tests/SConscript +++ b/src/mongo/tools/mongo_tidy_checks/tests/SConscript @@ -26,6 +26,7 @@ if env.GetOption('ninja') == 'disabled': # by a clang tidy check. The issue should be isolated in as minimal way as possible. tests = [ 'test_MongoHeaderBracketCheck.cpp', + 'test_MongoVolatileCheck.cpp', 'test_MongoUninterruptibleLockGuardCheck.cpp', 'test_MongoCctypeCheck.cpp', 'test_MongoStdOptionalCheck.cpp', diff --git a/src/mongo/tools/mongo_tidy_checks/tests/test_MongoVolatileCheck.cpp b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoVolatileCheck.cpp new file mode 100644 index 00000000000..c660a63d20c --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoVolatileCheck.cpp @@ -0,0 +1,37 @@ +/** + * 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. + */ +namespace mongo { + +volatile int varVolatileTest; +class testClass { + volatile int fieldVolatileTest; +}; +void functionName(volatile int varVolatileTest) {} + +} // namespace mongo diff --git a/src/mongo/util/tcmalloc_set_parameter.cpp b/src/mongo/util/tcmalloc_set_parameter.cpp index de3af048939..f67dc450fad 100644 --- a/src/mongo/util/tcmalloc_set_parameter.cpp +++ b/src/mongo/util/tcmalloc_set_parameter.cpp @@ -61,7 +61,7 @@ StatusWith<size_t> getProperty(StringData propname) { } Status setProperty(StringData propname, size_t value) { - if (!RUNNING_ON_VALGRIND) { + if (!RUNNING_ON_VALGRIND) { // NOLINT if (!MallocExtension::instance()->SetNumericProperty(propname.toString().c_str(), value)) { return {ErrorCodes::InternalError, str::stream() << "Failed to set internal tcmalloc property " << propname}; |