diff options
author | Juan Gu <juan.gu@mongodb.com> | 2023-02-16 23:18:25 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-02-17 02:28:44 +0000 |
commit | f5ce056efc33ec6a304b85d7763c2403f5a307f7 (patch) | |
tree | b188d7da984bb12ad4c1bcc421544e0c1b97ba5f | |
parent | 4702824a2cb37317a4af7842f1af48736f0fd08d (diff) | |
download | mongo-f5ce056efc33ec6a304b85d7763c2403f5a307f7.tar.gz |
SERVER-71747 Move mongo ctype lint to clang tidy
-rw-r--r-- | .clang-tidy.in | 1 | ||||
-rwxr-xr-x[-rw-r--r--] | buildscripts/apply_clang_tidy_fixes.py | 0 | ||||
-rw-r--r-- | buildscripts/linter/simplecpplint.py | 7 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.cpp | 84 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.h | 49 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp | 3 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py | 16 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/tests/SConscript | 1 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/tests/test_MongoCctypeCheck.cpp | 2 | ||||
-rw-r--r-- | src/mongo/tools/mongo_tidy_checks/tests/test_MongoCctypeCheck.h | 1 |
11 files changed, 157 insertions, 8 deletions
diff --git a/.clang-tidy.in b/.clang-tidy.in index 18b3085aef7..6f5903c3a67 100644 --- a/.clang-tidy.in +++ b/.clang-tidy.in @@ -33,6 +33,7 @@ Checks: '-*, modernize-replace-random-shuffle, modernize-shrink-to-fit, modernize-unary-static-assert, + mongo-cctype-check, mongo-header-bracket-check, mongo-uninterruptible-lock-guard-check, performance-faster-string-find, diff --git a/buildscripts/apply_clang_tidy_fixes.py b/buildscripts/apply_clang_tidy_fixes.py index 9735a662097..9735a662097 100644..100755 --- a/buildscripts/apply_clang_tidy_fixes.py +++ b/buildscripts/apply_clang_tidy_fixes.py diff --git a/buildscripts/linter/simplecpplint.py b/buildscripts/linter/simplecpplint.py index 8c7d775573c..876ba4e28ca 100644 --- a/buildscripts/linter/simplecpplint.py +++ b/buildscripts/linter/simplecpplint.py @@ -162,7 +162,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_ctype(linenum) self._check_for_std_optional(linenum) self._check_for_tracing_support(linenum) self._check_for_collection_sharding_runtime(linenum) @@ -285,12 +284,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_ctype(self, linenum): - line = self.clean_lines[linenum] - if 'include <cctype>' in line or 'include <ctype.h>' in line: - self._error(linenum, 'mongodb/ctype', - 'Use of prohibited <ctype.h> or <cctype> header, use "mongo/util/ctype.h"') - def _check_for_std_optional(self, linenum): line = self.clean_lines[linenum] if _RE_STD_OPTIONAL.search(line): diff --git a/src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.cpp b/src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.cpp new file mode 100644 index 00000000000..1935b395c4a --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.cpp @@ -0,0 +1,84 @@ +/** + * 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 "MongoCctypeCheck.h" + +#include <clang/Lex/PPCallbacks.h> +#include <clang/Lex/Preprocessor.h> + +namespace mongo::tidy { + +namespace { + +class MongoCctypePPCallbacks : public clang::PPCallbacks { +public: + explicit MongoCctypePPCallbacks(MongoCctypeCheck& Check, clang::LangOptions LangOpts) + : Check(Check), LangOpts(LangOpts) {} + + void InclusionDirective(clang::SourceLocation HashLoc, + const clang::Token& IncludeTok, + llvm::StringRef FileName, + bool IsAngled, + clang::CharSourceRange FilenameRange, + const clang::FileEntry* File, + llvm::StringRef SearchPath, + llvm::StringRef RelativePath, + const clang::Module* Imported, + clang::SrcMgr::CharacteristicKind FileType) override { + + // match following cases + // #include <cctype> + // #include <ctype.h> + if (FileName.equals("cctype") || FileName.equals("ctype.h")) { + std::string Replacement = "\"mongo/util/ctype.h\""; + Check.diag(FilenameRange.getBegin(), + "Use of prohibited %0%1%2 header, use \"mongo/util/ctype.h\"") + << (IsAngled ? "<" : "\"") << FileName << (IsAngled ? ">" : "\"") + << clang::FixItHint::CreateReplacement(FilenameRange.getAsRange(), Replacement); + } + } + +private: + MongoCctypeCheck& Check; + clang::LangOptions LangOpts; +}; + +} // namespace + +MongoCctypeCheck::MongoCctypeCheck(llvm::StringRef Name, clang::tidy::ClangTidyContext* Context) + : ClangTidyCheck(Name, Context) {} + +void MongoCctypeCheck::registerPPCallbacks(const clang::SourceManager& SM, + clang::Preprocessor* PP, + clang::Preprocessor* ModuleExpanderPP) { + PP->addPPCallbacks(::std::make_unique<MongoCctypePPCallbacks>(*this, getLangOpts())); +} + +} // namespace mongo::tidy diff --git a/src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.h b/src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.h new file mode 100644 index 00000000000..3ee5a8e2ab8 --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.h @@ -0,0 +1,49 @@ +/** + * 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 { + +/** + Overrides the default PPCallback class to primarly override + the InclusionDirective call which is called for each include. This + allows the chance to check whether <cctype> or <ctyhe.h> included or not +*/ +class MongoCctypeCheck : public clang::tidy::ClangTidyCheck { +public: + MongoCctypeCheck(clang::StringRef Name, clang::tidy::ClangTidyContext* Context); + void registerPPCallbacks(const clang::SourceManager& SM, + clang::Preprocessor* PP, + clang::Preprocessor* ModuleExpanderPP) override; +}; + +} // namespace mongo::tidy diff --git a/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp b/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp index 02f5c70b3d3..f633ff34e81 100644 --- a/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp +++ b/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp @@ -27,7 +27,7 @@ * it in the license file. */ - +#include "MongoCctypeCheck.h" #include "MongoHeaderBracketCheck.h" #include "MongoTestCheck.h" #include "MongoUninterruptibleLockGuardCheck.h" @@ -46,6 +46,7 @@ public: CheckFactories.registerCheck<MongoUninterruptibleLockGuardCheck>( "mongo-uninterruptible-lock-guard-check"); CheckFactories.registerCheck<MongoHeaderBracketCheck>("mongo-header-bracket-check"); + CheckFactories.registerCheck<MongoCctypeCheck>("mongo-cctype-check"); CheckFactories.registerCheck<MongoTestCheck>("mongo-test-check"); } }; diff --git a/src/mongo/tools/mongo_tidy_checks/SConscript b/src/mongo/tools/mongo_tidy_checks/SConscript index 85f7e35bda8..c613532ec00 100644 --- a/src/mongo/tools/mongo_tidy_checks/SConscript +++ b/src/mongo/tools/mongo_tidy_checks/SConscript @@ -124,6 +124,7 @@ mongo_custom_check = env.SharedLibrary( "MongoTestCheck.cpp", "MongoHeaderBracketCheck.cpp", "MongoUninterruptibleLockGuardCheck.cpp", + "MongoCctypeCheck.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 807d93a75d0..c218ff173cb 100644 --- a/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py +++ b/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py @@ -126,6 +126,22 @@ class MongoTidyTests(unittest.TestCase): self.run_clang_tidy() + def test_MongoCctypeCheck(self): + + self.write_config( + textwrap.dedent("""\ + Checks: '-*,mongo-cctype-check' + WarningsAsErrors: '*' + HeaderFilterRegex: '(mongo/.*)' + """)) + + self.expected_output = [ + "Use of prohibited \"cctype\" header, use \"mongo/util/ctype.h\"", + "Use of prohibited <ctype.h> header, use \"mongo/util/ctype.h\"", + ] + + 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 ebb1d2bb5c8..312e3251e4c 100644 --- a/src/mongo/tools/mongo_tidy_checks/tests/SConscript +++ b/src/mongo/tools/mongo_tidy_checks/tests/SConscript @@ -25,6 +25,7 @@ if env.GetOption('ninja') == 'disabled': 'test_MongoTestCheck.cpp', 'test_MongoHeaderBracketCheck.cpp', 'test_MongoUninterruptibleLockGuardCheck.cpp', + 'test_MongoCctypeCheck.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_MongoCctypeCheck.cpp b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoCctypeCheck.cpp new file mode 100644 index 00000000000..b7df665b98d --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoCctypeCheck.cpp @@ -0,0 +1,2 @@ +#include "cctype" +#include <test_MongoCctypeCheck.h> diff --git a/src/mongo/tools/mongo_tidy_checks/tests/test_MongoCctypeCheck.h b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoCctypeCheck.h new file mode 100644 index 00000000000..b941a38a8c4 --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoCctypeCheck.h @@ -0,0 +1 @@ +#include <ctype.h> |