summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Gu <juan.gu@mongodb.com>2023-05-15 21:05:27 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2023-05-16 02:13:03 +0000
commit889119d0075ff717efa935723ccaa55cc9848b0a (patch)
treeaf7d507bdf54385d17d265178714721405f192ef
parent4e07c8d5fa95f781cd049cb1ba5362fe92beeac3 (diff)
downloadmongo-889119d0075ff717efa935723ccaa55cc9848b0a.tar.gz
SERVER-71746 Move mongo config header lint to clang-tidy
-rw-r--r--.clang-tidy.in1
-rw-r--r--buildscripts/linter/simplecpplint.py15
-rw-r--r--src/mongo/tools/mongo_tidy_checks/MongoConfigHeaderCheck.cpp141
-rw-r--r--src/mongo/tools/mongo_tidy_checks/MongoConfigHeaderCheck.h53
-rw-r--r--src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp2
-rw-r--r--src/mongo/tools/mongo_tidy_checks/SConscript1
-rw-r--r--src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py19
-rw-r--r--src/mongo/tools/mongo_tidy_checks/tests/SConscript1
-rw-r--r--src/mongo/tools/mongo_tidy_checks/tests/test_MongoConfigHeaderCheck.cpp18
9 files changed, 236 insertions, 15 deletions
diff --git a/.clang-tidy.in b/.clang-tidy.in
index aa18238713c..012ee05e41b 100644
--- a/.clang-tidy.in
+++ b/.clang-tidy.in
@@ -34,6 +34,7 @@ Checks: '-*,
modernize-shrink-to-fit,
modernize-unary-static-assert,
mongo-cctype-check,
+ mongo-config-header-check,
mongo-cxx20-banned-includes-check,
mongo-header-bracket-check,
mongo-std-atomic-check,
diff --git a/buildscripts/linter/simplecpplint.py b/buildscripts/linter/simplecpplint.py
index 2315f2192e7..6990920076b 100644
--- a/buildscripts/linter/simplecpplint.py
+++ b/buildscripts/linter/simplecpplint.py
@@ -129,8 +129,6 @@ class Linter:
self.feature_flag_ignore_fcv_check_comments = []
self._error_count = 0
- self.found_config_header = False
-
def lint(self):
"""Run linter, returning error count."""
# steps:
@@ -153,7 +151,6 @@ class Linter:
continue
self._check_for_mongo_polyfill(linenum)
- self._check_for_mongo_config_header(linenum)
self._check_for_collection_sharding_runtime(linenum)
self._check_for_rand(linenum)
self._check_for_c_stdlib_headers(linenum)
@@ -290,18 +287,6 @@ class Linter:
return linenum
return linenum
- def _check_for_mongo_config_header(self, linenum):
- """Check for a config file."""
- if self.found_config_header:
- return
-
- line = self.clean_lines[linenum]
- self.found_config_header = line.startswith('#include "mongo/config.h"')
-
- if not self.found_config_header and "MONGO_CONFIG_" in line:
- self._error(linenum, 'build/config_h_include',
- 'MONGO_CONFIG define used without prior inclusion of config.h.')
-
def _check_for_generic_fcv(self, linenum):
line = self.clean_lines[linenum]
if _RE_GENERIC_FCV_REF.search(line):
diff --git a/src/mongo/tools/mongo_tidy_checks/MongoConfigHeaderCheck.cpp b/src/mongo/tools/mongo_tidy_checks/MongoConfigHeaderCheck.cpp
new file mode 100644
index 00000000000..a1a113a366b
--- /dev/null
+++ b/src/mongo/tools/mongo_tidy_checks/MongoConfigHeaderCheck.cpp
@@ -0,0 +1,141 @@
+/**
+ * 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 "MongoConfigHeaderCheck.h"
+
+#include <clang/Lex/PPCallbacks.h>
+#include <clang/Lex/Preprocessor.h>
+
+namespace mongo::tidy {
+
+using namespace clang;
+using namespace clang::ast_matchers;
+
+class MongoConfigHeaderPPCallbacks : public clang::PPCallbacks {
+public:
+ explicit MongoConfigHeaderPPCallbacks(MongoConfigHeaderCheck& Check,
+ clang::LangOptions LangOpts,
+ const clang::SourceManager& SM)
+ : Check(Check), LangOpts(LangOpts), SM(SM) {}
+
+ // Function to check the usage of MONGO_CONFIG_* macros
+ void checkMacroUsage(clang::SourceLocation Loc, const clang::Token& MacroNameTok) {
+ if (ConfigHeaderIncluded)
+ return;
+ llvm::StringRef macroName = MacroNameTok.getIdentifierInfo()->getName();
+ if (macroName.startswith("MONGO_CONFIG_")) {
+ Check.diag(Loc, "MONGO_CONFIG define used without prior inclusion of config.h");
+ }
+ }
+
+ // Callback function for handling macro definitions
+ void MacroDefined(const clang::Token& MacroNameTok, const clang::MacroDirective* MD) override {
+ if (ConfigHeaderIncluded)
+ return;
+ checkMacroUsage(MD->getLocation(), MacroNameTok);
+ }
+
+ // Callback function for handling #ifdef directives
+ void Ifdef(clang::SourceLocation Loc,
+ const clang::Token& MacroNameTok,
+ const clang::MacroDefinition& MD) override {
+ if (ConfigHeaderIncluded)
+ return;
+ checkMacroUsage(Loc, MacroNameTok);
+ }
+
+ // Callback function for handling #ifndef directives
+ void Ifndef(clang::SourceLocation Loc,
+ const clang::Token& MacroNameTok,
+ const clang::MacroDefinition& MD) override {
+ if (ConfigHeaderIncluded)
+ return;
+ checkMacroUsage(Loc, MacroNameTok);
+ }
+
+ // Callback function for handling #if directives
+ void If(clang::SourceLocation Loc,
+ clang::SourceRange ConditionRange,
+ clang::PPCallbacks::ConditionValueKind ConditionValue) override {
+
+ if (ConfigHeaderIncluded)
+ return;
+
+ // Get the beginning and end locations of the condition in the #if directive
+ clang::SourceLocation Start = ConditionRange.getBegin();
+ clang::SourceLocation End = ConditionRange.getEnd();
+
+ // Get the source text of the condition in the #if directive
+ bool Invalid = false;
+ llvm::StringRef ConditionText = Lexer::getSourceText(
+ CharSourceRange::getTokenRange(Start, End), SM, clang::LangOptions(), &Invalid);
+
+ if (!Invalid) {
+ if (ConditionText.contains("MONGO_CONFIG_")) {
+ Check.diag(Loc, "MONGO_CONFIG define used without prior inclusion of config.h");
+ }
+ }
+ }
+
+ // Callback function for handling #include directives
+ 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 {
+
+ if (FileName.equals("mongo/config.h")) {
+ ConfigHeaderIncluded = true;
+ }
+ }
+
+private:
+ MongoConfigHeaderCheck& Check;
+ clang::LangOptions LangOpts;
+ const clang::SourceManager& SM;
+ bool ConfigHeaderIncluded = false;
+};
+
+MongoConfigHeaderCheck::MongoConfigHeaderCheck(llvm::StringRef Name,
+ clang::tidy::ClangTidyContext* Context)
+ : ClangTidyCheck(Name, Context) {}
+
+void MongoConfigHeaderCheck::registerPPCallbacks(const clang::SourceManager& SM,
+ clang::Preprocessor* PP,
+ clang::Preprocessor* ModuleExpanderPP) {
+ PP->addPPCallbacks(::std::make_unique<MongoConfigHeaderPPCallbacks>(*this, getLangOpts(), SM));
+}
+
+} // namespace mongo::tidy
diff --git a/src/mongo/tools/mongo_tidy_checks/MongoConfigHeaderCheck.h b/src/mongo/tools/mongo_tidy_checks/MongoConfigHeaderCheck.h
new file mode 100644
index 00000000000..05861f1c639
--- /dev/null
+++ b/src/mongo/tools/mongo_tidy_checks/MongoConfigHeaderCheck.h
@@ -0,0 +1,53 @@
+/**
+ * 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 {
+
+/**
+ * MongoConfigHeaderCheck is a custom clang-tidy check for detecting
+ * the usage of MONGO_CONFIG_* macros without prior inclusion of "mongo/config.h" header.
+ *
+ * It extends ClangTidyCheck and overrides the registerPPCallbacks function. The registerPPCallbacks
+ * function adds a custom Preprocessor callback class (MongoConfigHeaderPPCallbacks) to handle
+ * preprocessor events and detect MONGO_CONFIG_* macro usages without proper inclusion of
+ * "mongo/config.h".
+ */
+class MongoConfigHeaderCheck : public clang::tidy::ClangTidyCheck {
+public:
+ MongoConfigHeaderCheck(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 267ebcebac5..aa3a4c8224d 100644
--- a/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp
+++ b/src/mongo/tools/mongo_tidy_checks/MongoTidyModule.cpp
@@ -29,6 +29,7 @@
#include "MongoAssertCheck.h"
#include "MongoCctypeCheck.h"
+#include "MongoConfigHeaderCheck.h"
#include "MongoCxx20BannedIncludesCheck.h"
#include "MongoFCVConstantCheck.h"
#include "MongoHeaderBracketCheck.h"
@@ -55,6 +56,7 @@ public:
"mongo-uninterruptible-lock-guard-check");
CheckFactories.registerCheck<MongoHeaderBracketCheck>("mongo-header-bracket-check");
CheckFactories.registerCheck<MongoCctypeCheck>("mongo-cctype-check");
+ CheckFactories.registerCheck<MongoConfigHeaderCheck>("mongo-config-header-check");
CheckFactories.registerCheck<MongoCxx20BannedIncludesCheck>(
"mongo-cxx20-banned-includes-check");
CheckFactories.registerCheck<MongoStdOptionalCheck>("mongo-std-optional-check");
diff --git a/src/mongo/tools/mongo_tidy_checks/SConscript b/src/mongo/tools/mongo_tidy_checks/SConscript
index eab97419218..f2e96c7753f 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(
"MongoHeaderBracketCheck.cpp",
"MongoUninterruptibleLockGuardCheck.cpp",
"MongoCctypeCheck.cpp",
+ "MongoConfigHeaderCheck.cpp",
"MongoCxx20BannedIncludesCheck.cpp",
"MongoStdOptionalCheck.cpp",
"MongoVolatileCheck.cpp",
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 5c8fb880000..d6fa236c719 100644
--- a/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py
+++ b/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py
@@ -276,6 +276,25 @@ class MongoTidyTests(unittest.TestCase):
self.run_clang_tidy()
+ def test_MongoConfigHeaderCheck(self):
+
+ self.write_config(
+ textwrap.dedent("""\
+ Checks: '-*,mongo-config-header-check'
+ WarningsAsErrors: '*'
+ HeaderFilterRegex: '(mongo/.*)'
+ """))
+
+ self.expected_output = [
+ "error: MONGO_CONFIG define used without prior inclusion of config.h [mongo-config-header-check,-warnings-as-errors]\n#define MONGO_CONFIG_TEST1 1",
+ "error: MONGO_CONFIG define used without prior inclusion of config.h [mongo-config-header-check,-warnings-as-errors]\n#ifdef MONGO_CONFIG_TEST1",
+ "error: MONGO_CONFIG define used without prior inclusion of config.h [mongo-config-header-check,-warnings-as-errors]\n#if MONGO_CONFIG_TEST1 == 1",
+ "error: MONGO_CONFIG define used without prior inclusion of config.h [mongo-config-header-check,-warnings-as-errors]\n#ifndef MONGO_CONFIG_TEST2",
+ "error: MONGO_CONFIG define used without prior inclusion of config.h [mongo-config-header-check,-warnings-as-errors]\n#if defined(MONGO_CONFIG_TEST1)",
+ ]
+
+ self.run_clang_tidy()
+
if __name__ == '__main__':
parser = argparse.ArgumentParser()
diff --git a/src/mongo/tools/mongo_tidy_checks/tests/SConscript b/src/mongo/tools/mongo_tidy_checks/tests/SConscript
index 6f553bb6df7..6342314c176 100644
--- a/src/mongo/tools/mongo_tidy_checks/tests/SConscript
+++ b/src/mongo/tools/mongo_tidy_checks/tests/SConscript
@@ -29,6 +29,7 @@ if env.GetOption('ninja') == 'disabled':
'test_MongoVolatileCheck.cpp',
'test_MongoUninterruptibleLockGuardCheck.cpp',
'test_MongoCctypeCheck.cpp',
+ 'test_MongoConfigHeaderCheck.cpp',
'test_MongoCxx20BannedIncludesCheck.cpp',
'test_MongoStdOptionalCheck.cpp',
'test_MongoTraceCheck.cpp',
diff --git a/src/mongo/tools/mongo_tidy_checks/tests/test_MongoConfigHeaderCheck.cpp b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoConfigHeaderCheck.cpp
new file mode 100644
index 00000000000..70553eea0bd
--- /dev/null
+++ b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoConfigHeaderCheck.cpp
@@ -0,0 +1,18 @@
+//#include "mongo/config.h"
+
+namespace mongo {
+#define MONGO_CONFIG_TEST1 1
+
+#ifdef MONGO_CONFIG_TEST1
+#endif
+
+#if MONGO_CONFIG_TEST1 == 1
+#endif
+
+#ifndef MONGO_CONFIG_TEST2
+#endif
+
+#if defined(MONGO_CONFIG_TEST1)
+#endif
+
+} // namespace mongo