diff options
author | Alexander Neben <alexander.neben@mongodb.com> | 2023-04-12 01:07:12 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-04-12 04:53:51 +0000 |
commit | f77a6c66d004ea593547ce2932c840c028a4b994 (patch) | |
tree | dd209cef417aa3927db761c397857ce6db55cee3 | |
parent | 3cd6a9bc6def4936c489f8f9dffeebc98390b6f5 (diff) | |
download | mongo-f77a6c66d004ea593547ce2932c840c028a4b994.tar.gz |
SERVER-75522 Added banned includes check for c++20
9 files changed, 182 insertions, 2 deletions
diff --git a/.clang-tidy.in b/.clang-tidy.in index 31cd6eb3137..baba5826605 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-cxx20-banned-includes-check, mongo-header-bracket-check, mongo-std-atomic-check, mongo-mutex-check, diff --git a/src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.h b/src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.h index 3ee5a8e2ab8..8e75d594513 100644 --- a/src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.h +++ b/src/mongo/tools/mongo_tidy_checks/MongoCctypeCheck.h @@ -36,7 +36,7 @@ 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 + allows the chance to check whether <cctype> or <ctype.h> included or not */ class MongoCctypeCheck : public clang::tidy::ClangTidyCheck { public: diff --git a/src/mongo/tools/mongo_tidy_checks/MongoCxx20BannedIncludesCheck.cpp b/src/mongo/tools/mongo_tidy_checks/MongoCxx20BannedIncludesCheck.cpp new file mode 100644 index 00000000000..2422a575d29 --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/MongoCxx20BannedIncludesCheck.cpp @@ -0,0 +1,93 @@ +/** + * 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 "MongoCxx20BannedIncludesCheck.h" + +#include <clang/Lex/PPCallbacks.h> +#include <clang/Lex/Preprocessor.h> + +namespace mongo::tidy { + +namespace { + +class MongoCxx20BannedIncludesPPCallbacks : public clang::PPCallbacks { +public: + explicit MongoCxx20BannedIncludesPPCallbacks(MongoCxx20BannedIncludesCheck& 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 { + + if (FileName.equals("coroutine") || FileName.equals("format")) { + Check.diag(FilenameRange.getBegin(), + "Use of prohibited %0%1%2 header. There are no override waivers issued for " + "this header. For questions please reach out to #cxx-discuss.") + << (IsAngled ? "<" : "\"") << FileName << (IsAngled ? ">" : "\""); + } + + if (FileName.equals("syncstream") || FileName.equals("ranges") || + FileName.equals("barrier") || FileName.equals("latch") || + FileName.equals("semaphore")) { + Check.diag(FilenameRange.getBegin(), + "Use of prohibited %0%1%2 header. There are override waivers issued for " + "this header. You need to follow the process in PM-3140 to override this " + "warning. For questions please reach out to #cxx-discuss.") + << (IsAngled ? "<" : "\"") << FileName << (IsAngled ? ">" : "\""); + } + } + +private: + MongoCxx20BannedIncludesCheck& Check; + clang::LangOptions LangOpts; +}; + +} // namespace + +MongoCxx20BannedIncludesCheck::MongoCxx20BannedIncludesCheck(llvm::StringRef Name, + clang::tidy::ClangTidyContext* Context) + : ClangTidyCheck(Name, Context) {} + +void MongoCxx20BannedIncludesCheck::registerPPCallbacks(const clang::SourceManager& SM, + clang::Preprocessor* PP, + clang::Preprocessor* ModuleExpanderPP) { + PP->addPPCallbacks( + ::std::make_unique<MongoCxx20BannedIncludesPPCallbacks>(*this, getLangOpts())); +} + +} // namespace mongo::tidy diff --git a/src/mongo/tools/mongo_tidy_checks/MongoCxx20BannedIncludesCheck.h b/src/mongo/tools/mongo_tidy_checks/MongoCxx20BannedIncludesCheck.h new file mode 100644 index 00000000000..0cdcffd818b --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/MongoCxx20BannedIncludesCheck.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 checking for several c++20 banned headers. +*/ +class MongoCxx20BannedIncludesCheck : public clang::tidy::ClangTidyCheck { +public: + MongoCxx20BannedIncludesCheck(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 fcf60f737f7..94e9796d42a 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 "MongoCxx20BannedIncludesCheck.h" #include "MongoHeaderBracketCheck.h" #include "MongoMutexCheck.h" #include "MongoStdAtomicCheck.h" @@ -52,6 +53,8 @@ public: "mongo-uninterruptible-lock-guard-check"); CheckFactories.registerCheck<MongoHeaderBracketCheck>("mongo-header-bracket-check"); CheckFactories.registerCheck<MongoCctypeCheck>("mongo-cctype-check"); + CheckFactories.registerCheck<MongoCxx20BannedIncludesCheck>( + "mongo-cxx20-banned-includes-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/SConscript b/src/mongo/tools/mongo_tidy_checks/SConscript index c9d8c589fe0..0fb80e47c0a 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", + "MongoCxx20BannedIncludesCheck.cpp", "MongoStdOptionalCheck.cpp", "MongoVolatileCheck.cpp", "MongoStdAtomicCheck.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 b3fcab7e8b4..b7ea7a3f8b7 100644 --- a/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py +++ b/src/mongo/tools/mongo_tidy_checks/tests/MongoTidyCheck_unittest.py @@ -131,6 +131,25 @@ class MongoTidyTests(unittest.TestCase): self.run_clang_tidy() + def test_MongoCxx20BannedIncludesCheck(self): + + self.write_config( + textwrap.dedent("""\ + Checks: '-*,mongo-cxx20-banned-includes-check' + WarningsAsErrors: '*' + HeaderFilterRegex: '(mongo/.*)' + """)) + + self.expected_output = [ + "Use of prohibited <syncstream> header.", + "Use of prohibited <ranges> header.", + "Use of prohibited <barrier> header.", + "Use of prohibited <latch> header.", + "Use of prohibited <semaphore> header.", + ] + + self.run_clang_tidy() + def test_MongoStdOptionalCheck(self): self.write_config( @@ -181,7 +200,7 @@ class MongoTidyTests(unittest.TestCase): ] self.run_clang_tidy() - + def test_MongoStdAtomicCheck(self): self.write_config( @@ -228,6 +247,7 @@ class MongoTidyTests(unittest.TestCase): 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 a75a7720661..869e386caed 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_MongoCxx20BannedIncludesCheck.cpp', 'test_MongoStdOptionalCheck.cpp', 'test_MongoTraceCheck.cpp', 'test_MongoStdAtomicCheck.cpp', diff --git a/src/mongo/tools/mongo_tidy_checks/tests/test_MongoCxx20BannedIncludesCheck.cpp b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoCxx20BannedIncludesCheck.cpp new file mode 100644 index 00000000000..940ec0d958e --- /dev/null +++ b/src/mongo/tools/mongo_tidy_checks/tests/test_MongoCxx20BannedIncludesCheck.cpp @@ -0,0 +1,12 @@ +#include <barrier> +#include <latch> +#include <ranges> +#include <semaphore> +#include <syncstream> +// This header does not exist in this version of clang +// Based on the other tests I am going to assume this is working +// #include <format> +// This header needs to be compiled with -fcoroutines. This causes the check to not compile. +// Based on the other tests I am going to assume this is working. +// Also no one can include this header without also adding this flag. +// #include <coroutine> |