diff --git a/clang-tools-extra/clang-tidy/ClangTidy.cpp b/clang-tools-extra/clang-tidy/ClangTidy.cpp --- a/clang-tools-extra/clang-tidy/ClangTidy.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidy.cpp @@ -409,6 +409,8 @@ } for (auto &Check : Checks) { + if (!Check->isLanguageVersionSupported(Context.getLangOpts())) + continue; Check->registerMatchers(&*Finder); Check->registerPPCallbacks(*SM, PP, ModuleExpanderPP); } diff --git a/clang-tools-extra/clang-tidy/ClangTidyCheck.h b/clang-tools-extra/clang-tidy/ClangTidyCheck.h --- a/clang-tools-extra/clang-tidy/ClangTidyCheck.h +++ b/clang-tools-extra/clang-tidy/ClangTidyCheck.h @@ -53,11 +53,24 @@ /// constructor using the Options.get() methods below. ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context); + /// Override this to disable registering matchers and PP callbacks if an + /// invalid language version is being used. + /// + /// For example if a check is examining overloaded functions then this should + /// be overridden to return false when the CPlusPlus flag is not set in + /// \p LangOpts. + virtual bool isLanguageVersionSupported(const LangOptions &LangOpts) const { + return true; + } + /// Override this to register ``PPCallbacks`` in the preprocessor. /// /// This should be used for clang-tidy checks that analyze preprocessor- /// dependent properties, e.g. include directives and macro definitions. /// + /// This will only be executed if the function isLanguageVersionSupported + /// returns true. + /// /// There are two Preprocessors to choose from that differ in how they handle /// modular #includes: /// - PP is the real Preprocessor. It doesn't walk into modular #includes and @@ -80,6 +93,9 @@ /// "this" will be used as callback, but you can also specify other callback /// classes. Thereby, different matchers can trigger different callbacks. /// + /// This will only be executed if the function isLanguageVersionSupported + /// returns true. + /// /// If you need to merge information between the different matchers, you can /// store these as members of the derived class. However, note that all /// matches occur in the order of the AST traversal. diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h --- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h +++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.h @@ -41,7 +41,7 @@ virtual SmartPtrTypeMatcher getSmartPointerTypeMatcher() const = 0; /// Returns whether the C++ version is compatible with current check. - virtual bool isLanguageVersionSupported(const LangOptions &LangOpts) const; + bool isLanguageVersionSupported(const LangOptions &LangOpts) const override; static const char PointerType[]; diff --git a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp --- a/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/MakeSmartPtrCheck.cpp @@ -68,17 +68,12 @@ void MakeSmartPtrCheck::registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) { - if (isLanguageVersionSupported(getLangOpts())) { Inserter = std::make_unique(SM, getLangOpts(), IncludeStyle); PP->addPPCallbacks(Inserter->CreatePPCallbacks()); - } } void MakeSmartPtrCheck::registerMatchers(ast_matchers::MatchFinder *Finder) { - if (!isLanguageVersionSupported(getLangOpts())) - return; - // Calling make_smart_ptr from within a member function of a type with a // private or protected constructor would be ill-formed. auto CanCallCtor = unless(has(ignoringImpCasts( diff --git a/clang-tools-extra/clangd/ParsedAST.cpp b/clang-tools-extra/clangd/ParsedAST.cpp --- a/clang-tools-extra/clangd/ParsedAST.cpp +++ b/clang-tools-extra/clangd/ParsedAST.cpp @@ -301,6 +301,8 @@ }); Preprocessor *PP = &Clang->getPreprocessor(); for (const auto &Check : CTChecks) { + if (!Check->isLanguageVersionSupported(CTContext->getLangOpts())) + continue; // FIXME: the PP callbacks skip the entire preamble. // Checks that want to see #includes in the main file do not see them. Check->registerPPCallbacks(Clang->getSourceManager(), PP, PP); diff --git a/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h b/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h --- a/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h +++ b/clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h @@ -67,6 +67,8 @@ // `getLangOpts()`). CheckFactory::createChecks(&Context, Checks); for (auto &Check : Checks) { + if (!Check->isLanguageVersionSupported(Context.getLangOpts())) + continue; Check->registerMatchers(&Finder); Check->registerPPCallbacks(Compiler.getSourceManager(), PP, PP); }