10
10
#include " clang/AST/TypeLoc.h"
11
11
#include " clang/ASTMatchers/ASTMatchFinder.h"
12
12
#include " clang/ASTMatchers/ASTMatchers.h"
13
+ #include " clang/Frontend/ASTUnit.h"
13
14
#include " clang/Tooling/Tooling.h"
14
- #include " llvm/ADT/SmallString.h"
15
15
#include " gmock/gmock.h"
16
16
#include " gtest/gtest.h"
17
17
#include < cctype>
@@ -43,7 +43,7 @@ std::unique_ptr<ASTUnit> buildASTFromCode(const Twine &Code) {
43
43
}
44
44
45
45
ExprMatcher declRefTo (StringRef Name) {
46
- return declRefExpr (to (namedDecl (hasName (Name))));
46
+ return declRefExpr (to (namedDecl (hasName (Name)). bind ( " decl " ) ));
47
47
}
48
48
49
49
StmtMatcher withEnclosingCompound (ExprMatcher Matcher) {
@@ -57,6 +57,13 @@ bool isMutated(const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
57
57
return ExprMutationAnalyzer (*S, AST->getASTContext ()).isMutated (E);
58
58
}
59
59
60
+ bool isDeclMutated (const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
61
+ const auto *const S = selectFirst<Stmt>(" stmt" , Results);
62
+ const auto *const D = selectFirst<Decl>(" decl" , Results);
63
+ TraversalKindScope RAII (AST->getASTContext (), TK_AsIs);
64
+ return ExprMutationAnalyzer (*S, AST->getASTContext ()).isMutated (D);
65
+ }
66
+
60
67
SmallVector<std::string, 1 >
61
68
mutatedBy (const SmallVectorImpl<BoundNodes> &Results, ASTUnit *AST) {
62
69
const auto *const S = selectFirst<Stmt>(" stmt" , Results);
@@ -1552,6 +1559,21 @@ TEST(ExprMutationAnalyzerTest, UniquePtr) {
1552
1559
1553
1560
// section: complex problems detected on real code
1554
1561
1562
+ TEST (ExprMutationAnalyzerTest, SelfRef) {
1563
+ std::unique_ptr<ASTUnit> AST{};
1564
+ SmallVector<BoundNodes, 1 > Results{};
1565
+
1566
+ AST = buildASTFromCodeWithArgs (" void f() { int &x = x; }" ,
1567
+ {" -Wno-unused-value" , " -Wno-uninitialized" });
1568
+ Results = match (withEnclosingCompound (declRefTo (" x" )), AST->getASTContext ());
1569
+ EXPECT_FALSE (isDeclMutated (Results, AST.get ()));
1570
+
1571
+ AST = buildASTFromCodeWithArgs (" void f() { int &x = x; x = 1; }" ,
1572
+ {" -Wno-unused-value" , " -Wno-uninitialized" });
1573
+ Results = match (withEnclosingCompound (declRefTo (" x" )), AST->getASTContext ());
1574
+ EXPECT_TRUE (isDeclMutated (Results, AST.get ()));
1575
+ }
1576
+
1555
1577
TEST (ExprMutationAnalyzerTest, UnevaluatedContext) {
1556
1578
const std::string Example =
1557
1579
" template <typename T>"
0 commit comments