File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed
include/clang/Analysis/FlowSensitive
lib/Analysis/FlowSensitive
unittests/Analysis/FlowSensitive Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -139,6 +139,9 @@ struct ReferencedDecls {
139
139
// / All variables with static storage duration, notably including static
140
140
// / member variables and static variables declared within a function.
141
141
llvm::DenseSet<const VarDecl *> Globals;
142
+ // / Local variables, not including parameters or static variables declared
143
+ // / within a function.
144
+ llvm::DenseSet<const VarDecl *> Locals;
142
145
// / Free functions and member functions which are referenced (but not
143
146
// / necessarily called).
144
147
llvm::DenseSet<const FunctionDecl *> Functions;
Original file line number Diff line number Diff line change @@ -170,6 +170,13 @@ static void insertIfGlobal(const Decl &D,
170
170
Globals.insert (V);
171
171
}
172
172
173
+ static void insertIfLocal (const Decl &D,
174
+ llvm::DenseSet<const VarDecl *> &Locals) {
175
+ if (auto *V = dyn_cast<VarDecl>(&D))
176
+ if (V->hasLocalStorage () && !isa<ParmVarDecl>(V))
177
+ Locals.insert (V);
178
+ }
179
+
173
180
static void insertIfFunction (const Decl &D,
174
181
llvm::DenseSet<const FunctionDecl *> &Funcs) {
175
182
if (auto *FD = dyn_cast<FunctionDecl>(&D))
@@ -220,12 +227,14 @@ class ReferencedDeclsVisitor
220
227
221
228
bool VisitDecl (Decl *D) {
222
229
insertIfGlobal (*D, Referenced.Globals );
230
+ insertIfLocal (*D, Referenced.Locals );
223
231
insertIfFunction (*D, Referenced.Functions );
224
232
return true ;
225
233
}
226
234
227
235
bool VisitDeclRefExpr (DeclRefExpr *E) {
228
236
insertIfGlobal (*E->getDecl (), Referenced.Globals );
237
+ insertIfLocal (*E->getDecl (), Referenced.Locals );
229
238
insertIfFunction (*E->getDecl (), Referenced.Functions );
230
239
return true ;
231
240
}
Original file line number Diff line number Diff line change @@ -85,4 +85,26 @@ TEST(ASTOpsTest, ReferencedDeclsOnUnionInitList) {
85
85
UnorderedElementsAre (IDecl));
86
86
}
87
87
88
+ TEST (ASTOpsTest, ReferencedDeclsLocalsNotParamsOrStatics) {
89
+ std::string Code = R"cc(
90
+ void func(int Param) {
91
+ static int Static = 0;
92
+ int Local = Param;
93
+ Local = Static;
94
+ }
95
+ )cc" ;
96
+ std::unique_ptr<ASTUnit> Unit =
97
+ tooling::buildASTFromCodeWithArgs (Code, {" -fsyntax-only" , " -std=c++17" });
98
+ auto &ASTCtx = Unit->getASTContext ();
99
+
100
+ ASSERT_EQ (ASTCtx.getDiagnostics ().getClient ()->getNumErrors (), 0U );
101
+
102
+ auto *Func = cast<FunctionDecl>(findValueDecl (ASTCtx, " func" ));
103
+ ASSERT_NE (Func, nullptr );
104
+ auto *LocalDecl = cast<VarDecl>(findValueDecl (ASTCtx, " Local" ));
105
+
106
+ EXPECT_THAT (getReferencedDecls (*Func).Locals ,
107
+ UnorderedElementsAre (LocalDecl));
108
+ }
109
+
88
110
} // namespace
You can’t perform that action at this time.
0 commit comments