Author: Julian Schmidt Date: 2023-11-14T23:00:18+01:00 New Revision: a3d76b3fa3f8641f515ea8bc5de0844e01f6d7cd
URL: https://github.com/llvm/llvm-project/commit/a3d76b3fa3f8641f515ea8bc5de0844e01f6d7cd DIFF: https://github.com/llvm/llvm-project/commit/a3d76b3fa3f8641f515ea8bc5de0844e01f6d7cd.diff LOG: [clang-tidy] fix match for binaryOperator in ExprMutationAnalyzer for misc-const-correctness (#70559) The `ExprMutationAnalyzer`s matcher of `binaryOperator`s that contained the variable expr, were previously narrowing the variable to be type dependent, when the `binaryOperator` should have been narrowed as dependent. The variable we are trying to find mutations for does not need to be the dependent type, the other operand of the `binaryOperator` could be dependent. Fixes #57297 Added: Modified: clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp clang/lib/Analysis/ExprMutationAnalyzer.cpp clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index f49c412118e7d98..a24e3a735d2d2d5 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -315,7 +315,9 @@ Changes in existing checks - Improved :doc:`misc-const-correctness <clang-tidy/checks/misc/const-correctness>` check to avoid false positive when - using pointer to member function. + using pointer to member function. Additionally, the check no longer emits + a diagnostic when a variable that is not type-dependent is an operand of a + type-dependent binary operator. - Improved :doc:`misc-include-cleaner <clang-tidy/checks/misc/include-cleaner>` check by adding option diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp index 7b5ccabdd6ef611..794578ceeeba8fc 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/misc/const-correctness-templates.cpp @@ -20,3 +20,13 @@ void instantiate_template_cases() { type_dependent_variables<int>(); type_dependent_variables<float>(); } + +namespace gh57297{ +// The expression to check may not be the dependent operand in a dependent +// operator. + +// Explicitly not declaring a (templated) stream operator +// so the `<<` is a `binaryOperator` with a dependent type. +struct Stream { }; +template <typename T> void f() { T t; Stream x; x << t; } +} // namespace gh57297 diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp index f2e1beb025423cf..624a643cc60e4ba 100644 --- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp +++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp @@ -296,9 +296,9 @@ const Stmt *ExprMutationAnalyzer::findDirectMutation(const Expr *Exp) { // resolved and modelled as `binaryOperator` on a dependent type. // Such instances are considered a modification, because they can modify // in diff erent instantiations of the template. - binaryOperator(hasEitherOperand( - allOf(ignoringImpCasts(canResolveToExpr(equalsNode(Exp))), - isTypeDependent()))), + binaryOperator( + hasEitherOperand(ignoringImpCasts(canResolveToExpr(equalsNode(Exp)))), + isTypeDependent()), // Within class templates and member functions the member expression might // not be resolved. In that case, the `callExpr` is considered to be a // modification. diff --git a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp index c0a398394093c48..f1a1f857a0ee5be 100644 --- a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp +++ b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp @@ -343,6 +343,22 @@ TEST(ExprMutationAnalyzerTest, UnresolvedOperator) { EXPECT_TRUE(isMutated(Results, AST.get())); } +TEST(ExprMutationAnalyzerTest, DependentOperatorWithNonDependentOperand) { + // gh57297 + // The expression to check may not be the dependent operand in a dependent + // operator. + + // Explicitly not declaring a (templated) stream operator + // so the `<<` is a `binaryOperator` with a dependent type. + const auto AST = buildASTFromCodeWithArgs( + "struct Stream { };" + "template <typename T> void f() { T t; Stream x; x << t; }", + {"-fno-delayed-template-parsing"}); + const auto Results = + match(withEnclosingCompound(declRefTo("x")), AST->getASTContext()); + EXPECT_THAT(mutatedBy(Results, AST.get()), ElementsAre("x << t")); +} + // Section: expression as call argument TEST(ExprMutationAnalyzerTest, ByValueArgument) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits