diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp index 420317e10177..11d5425956e0 100644 --- a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp @@ -539,9 +539,14 @@ void LoopConvertCheck::findAndVerifyUsages( } void LoopConvertCheck::registerMatchers(MatchFinder *Finder) { - Finder->addMatcher(makeArrayLoopMatcher(), this); - Finder->addMatcher(makeIteratorLoopMatcher(), this); - Finder->addMatcher(makePseudoArrayLoopMatcher(), this); + // Only register the matchers for C++. Because this checker is used for + // modernization, it is reasonable to run it on any C++ standard with the + // assumption the user is trying to modernize their codebase. + if (getLangOpts().CPlusPlus) { + Finder->addMatcher(makeArrayLoopMatcher(), this); + Finder->addMatcher(makeIteratorLoopMatcher(), this); + Finder->addMatcher(makePseudoArrayLoopMatcher(), this); + } } void LoopConvertCheck::check(const MatchFinder::MatchResult &Result) { diff --git a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp index d039e0111e0f..0cbb606d09ad 100644 --- a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp @@ -127,35 +127,46 @@ void PassByValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { } void PassByValueCheck::registerMatchers(MatchFinder *Finder) { - Finder->addMatcher( - constructorDecl( - forEachConstructorInitializer( - ctorInitializer( - // Clang builds a CXXConstructExpr only whin it knows which - // constructor will be called. In dependent contexts a - // ParenListExpr is generated instead of a CXXConstructExpr, - // filtering out templates automatically for us. - withInitializer(constructExpr( - has(declRefExpr(to( - parmVarDecl( - hasType(qualType( - // Match only const-ref or a non-const value - // parameters. Rvalues and const-values - // shouldn't be modified. - anyOf(constRefType(), nonConstValueType())))) - .bind("Param")))), - hasDeclaration(constructorDecl( - isCopyConstructor(), unless(isDeleted()), - hasDeclContext(recordDecl(isMoveConstructible()))))))) - .bind("Initializer"))) - .bind("Ctor"), - this); + // Only register the matchers for C++; the functionality currently does not + // provide any benefit to other languages, despite being benign. + if (getLangOpts().CPlusPlus) { + Finder->addMatcher( + constructorDecl( + forEachConstructorInitializer( + ctorInitializer( + // Clang builds a CXXConstructExpr only whin it knows which + // constructor will be called. In dependent contexts a + // ParenListExpr is generated instead of a CXXConstructExpr, + // filtering out templates automatically for us. + withInitializer(constructExpr( + has(declRefExpr(to( + parmVarDecl( + hasType(qualType( + // Match only const-ref or a non-const value + // parameters. Rvalues and const-values + // shouldn't be modified. + anyOf(constRefType(), + nonConstValueType())))) + .bind("Param")))), + hasDeclaration(constructorDecl( + isCopyConstructor(), unless(isDeleted()), + hasDeclContext( + recordDecl(isMoveConstructible()))))))) + .bind("Initializer"))) + .bind("Ctor"), + this); + } } void PassByValueCheck::registerPPCallbacks(CompilerInstance &Compiler) { - Inserter.reset(new IncludeInserter(Compiler.getSourceManager(), - Compiler.getLangOpts(), IncludeStyle)); - Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); + // Only register the preprocessor callbacks for C++; the functionality + // currently does not provide any benefit to other languages, despite being + // benign. + if (getLangOpts().CPlusPlus) { + Inserter.reset(new IncludeInserter(Compiler.getSourceManager(), + Compiler.getLangOpts(), IncludeStyle)); + Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); + } } void PassByValueCheck::check(const MatchFinder::MatchResult &Result) { diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp index 83f12824afe2..4da3efb98883 100644 --- a/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/ReplaceAutoPtrCheck.cpp @@ -198,15 +198,24 @@ void ReplaceAutoPtrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { } void ReplaceAutoPtrCheck::registerMatchers(MatchFinder *Finder) { - Finder->addMatcher(makeAutoPtrTypeLocMatcher(), this); - Finder->addMatcher(makeAutoPtrUsingDeclMatcher(), this); - Finder->addMatcher(makeTransferOwnershipExprMatcher(), this); + // Only register the matchers for C++; the functionality currently does not + // provide any benefit to other languages, despite being benign. + if (getLangOpts().CPlusPlus) { + Finder->addMatcher(makeAutoPtrTypeLocMatcher(), this); + Finder->addMatcher(makeAutoPtrUsingDeclMatcher(), this); + Finder->addMatcher(makeTransferOwnershipExprMatcher(), this); + } } void ReplaceAutoPtrCheck::registerPPCallbacks(CompilerInstance &Compiler) { - Inserter.reset(new IncludeInserter(Compiler.getSourceManager(), - Compiler.getLangOpts(), IncludeStyle)); - Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); + // Only register the preprocessor callbacks for C++; the functionality + // currently does not provide any benefit to other languages, despite being + // benign. + if (getLangOpts().CPlusPlus) { + Inserter.reset(new IncludeInserter(Compiler.getSourceManager(), + Compiler.getLangOpts(), IncludeStyle)); + Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); + } } void ReplaceAutoPtrCheck::check(const MatchFinder::MatchResult &Result) { diff --git a/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.cpp index b688f1086dca..6aa57e907d69 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseAutoCheck.cpp @@ -238,8 +238,12 @@ StatementMatcher makeDeclWithNewMatcher() { } // namespace void UseAutoCheck::registerMatchers(MatchFinder *Finder) { - Finder->addMatcher(makeIteratorDeclMatcher(), this); - Finder->addMatcher(makeDeclWithNewMatcher(), this); + // Only register the matchers for C++; the functionality currently does not + // provide any benefit to other languages, despite being benign. + if (getLangOpts().CPlusPlus) { + Finder->addMatcher(makeIteratorDeclMatcher(), this); + Finder->addMatcher(makeDeclWithNewMatcher(), this); + } } void UseAutoCheck::replaceIterators(const DeclStmt *D, ASTContext *Context) { diff --git a/clang-tools-extra/test/clang-tidy/modernize-loop-convert.c b/clang-tools-extra/test/clang-tidy/modernize-loop-convert.c new file mode 100644 index 000000000000..4c7d956c31d1 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/modernize-loop-convert.c @@ -0,0 +1,12 @@ +// RUN: clang-tidy %s -checks=-*,modernize-loop-convert -- -std=c11 | count 0 + +// Note: this test expects no diagnostics, but FileCheck cannot handle that, +// hence the use of | count 0. + +int arr[6] = {1, 2, 3, 4, 5, 6}; + +void f(void) { + for (int i = 0; i < 6; ++i) { + (void)arr[i]; + } +}