[clang] [clang-tools-extra] [analysis] assume expr is not mutated after analysis to avoid recursive (PR #90581)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 30 09:18:33 PDT 2024
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/90581
>From f0d640d4c1ba2ede182fdf31cc7030aad01de8b8 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Tue, 30 Apr 2024 17:46:37 +0800
Subject: [PATCH 1/2] [analysis] assume expr is not mutated after analysis to
avoid recursive
Fixes: #89376.
---
clang/lib/Analysis/ExprMutationAnalyzer.cpp | 6 +++--
.../Analysis/ExprMutationAnalyzerTest.cpp | 26 +++++++++++++++++--
2 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Analysis/ExprMutationAnalyzer.cpp b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
index 941322be8f870b..3b3782fa1db9a0 100644
--- a/clang/lib/Analysis/ExprMutationAnalyzer.cpp
+++ b/clang/lib/Analysis/ExprMutationAnalyzer.cpp
@@ -235,15 +235,17 @@ const Stmt *ExprMutationAnalyzer::Analyzer::findMutationMemoized(
if (Memoized != MemoizedResults.end())
return Memoized->second;
+ // Assume Exp is not mutated before analyzing Exp.
+ MemoizedResults[Exp] = nullptr;
if (isUnevaluated(Exp))
- return MemoizedResults[Exp] = nullptr;
+ return nullptr;
for (const auto &Finder : Finders) {
if (const Stmt *S = (this->*Finder)(Exp))
return MemoizedResults[Exp] = S;
}
- return MemoizedResults[Exp] = nullptr;
+ return nullptr;
}
const Stmt *
diff --git a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
index 9c1dc1a76db63d..79ccb024283c35 100644
--- a/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
+++ b/clang/unittests/Analysis/ExprMutationAnalyzerTest.cpp
@@ -10,8 +10,8 @@
#include "clang/AST/TypeLoc.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Frontend/ASTUnit.h"
#include "clang/Tooling/Tooling.h"
-#include "llvm/ADT/SmallString.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <cctype>
@@ -43,7 +43,7 @@ std::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code) {
}
ExprMatcher declRefTo(StringRef Name) {
- return declRefExpr(to(namedDecl(hasName(Name))));
+ return declRefExpr(to(namedDecl(hasName(Name)).bind("decl")));
}
StmtMatcher withEnclosingCompound(ExprMatcher Matcher) {
@@ -57,6 +57,13 @@ bool isMutated(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
return ExprMutationAnalyzer(*S, AST->getASTContext()).isMutated(E);
}
+bool isDeclMutated(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
+ const auto *const S = selectFirst<Stmt>("stmt", Results);
+ const auto *const D = selectFirst<Decl>("decl", Results);
+ TraversalKindScope RAII(AST->getASTContext(), TK_AsIs);
+ return ExprMutationAnalyzer(*S, AST->getASTContext()).isMutated(D);
+}
+
SmallVector<std::string, 1>
mutatedBy(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
const auto *const S = selectFirst<Stmt>("stmt", Results);
@@ -1552,6 +1559,21 @@ TEST(ExprMutationAnalyzerTest, UniquePtr) {
// section: complex problems detected on real code
+TEST(ExprMutationAnalyzerTest, SelfRef) {
+ std::unique_ptr<ASTUnit> AST{};
+ SmallVector<BoundNodes, 1> Results{};
+
+ AST = buildASTFromCodeWithArgs("void f() { int &x = x; }",
+ {"-Wno-unused-value", "-Wno-uninitialized"});
+ Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
+ EXPECT_FALSE(isDeclMutated(Results, AST.get()));
+
+ AST = buildASTFromCodeWithArgs("void f() { int &x = x; x = 1; }",
+ {"-Wno-unused-value", "-Wno-uninitialized"});
+ Results = match(withEnclosingCompound(declRefTo("x")), AST->getASTContext());
+ EXPECT_TRUE(isDeclMutated(Results, AST.get()));
+}
+
TEST(ExprMutationAnalyzerTest, UnevaluatedContext) {
const std::string Example =
"template <typename T>"
>From 86df475be8d19f10e3c40636ae6902a5469cb3e0 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Wed, 1 May 2024 00:18:18 +0800
Subject: [PATCH 2/2] update release note
---
clang-tools-extra/docs/ReleaseNotes.rst | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 3038d2b125f20d..5956ccb925485c 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -261,7 +261,8 @@ Changes in existing checks
- Improved :doc:`misc-const-correctness
<clang-tidy/checks/misc/const-correctness>` check by avoiding infinite recursion
- for recursive forwarding reference.
+ for recursive functions with forwarding reference parameters and reference
+ variables which refer to themselves.
- Improved :doc:`misc-definitions-in-headers
<clang-tidy/checks/misc/definitions-in-headers>` check by replacing the local
More information about the cfe-commits
mailing list