Skip to content

Commit e527df0

Browse files
committed
[analyzer] Add a testing facility for testing relationships between symbols.
Tests introduced in r329780 was disabled in r342317 because these tests were accidentally testing dump infrastructure, when all they cared about was how symbols relate to each other. So when dump infrastructure changed, tests became annoying to maintain. Add a new feature to ExprInspection: clang_analyzer_denote() and clang_analyzer_explain(). The former adds a notation to a symbol, the latter expresses another symbol in terms of previously denoted symbols. It's currently a bit wonky - doesn't print parentheses and only supports denoting atomic symbols. But it's even more readable that way. Differential Revision: https://reviews.llvm.org/D52133 llvm-svn: 343048
1 parent 8dfcd83 commit e527df0

File tree

4 files changed

+708
-521
lines changed

4 files changed

+708
-521
lines changed

clang/docs/analyzer/DebugChecks.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,23 @@ ExprInspection checks
255255
clang_analyzer_hashDump(x); // expected-warning{{hashed string for x}}
256256
}
257257

258+
- ``void clang_analyzer_denote(int, const char *);``
259+
260+
Denotes symbols with strings. A subsequent call to clang_analyzer_express()
261+
will expresses another symbol in terms of these string. Useful for testing
262+
relationships between different symbols.
263+
264+
Example usage::
265+
266+
void foo(int x) {
267+
clang_analyzer_denote(x, "$x");
268+
clang_analyzer_express(x + 1); // expected-warning{{$x + 1}}
269+
}
270+
271+
- ``void clang_analyzer_express(int);``
272+
273+
See clang_analyzer_denote().
274+
258275
Statistics
259276
==========
260277

clang/lib/StaticAnalyzer/Checkers/ExprInspectionChecker.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class ExprInspectionChecker : public Checker<eval::Call, check::DeadSymbols,
4343
void analyzerPrintState(const CallExpr *CE, CheckerContext &C) const;
4444
void analyzerGetExtent(const CallExpr *CE, CheckerContext &C) const;
4545
void analyzerHashDump(const CallExpr *CE, CheckerContext &C) const;
46+
void analyzerDenote(const CallExpr *CE, CheckerContext &C) const;
47+
void analyzerExpress(const CallExpr *CE, CheckerContext &C) const;
4648

4749
typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
4850
CheckerContext &C) const;
@@ -60,6 +62,7 @@ class ExprInspectionChecker : public Checker<eval::Call, check::DeadSymbols,
6062
}
6163

6264
REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, SymbolRef)
65+
REGISTER_MAP_WITH_PROGRAMSTATE(DenotedSymbols, SymbolRef, const StringLiteral *)
6366

6467
bool ExprInspectionChecker::evalCall(const CallExpr *CE,
6568
CheckerContext &C) const {
@@ -82,6 +85,8 @@ bool ExprInspectionChecker::evalCall(const CallExpr *CE,
8285
.Case("clang_analyzer_numTimesReached",
8386
&ExprInspectionChecker::analyzerNumTimesReached)
8487
.Case("clang_analyzer_hashDump", &ExprInspectionChecker::analyzerHashDump)
88+
.Case("clang_analyzer_denote", &ExprInspectionChecker::analyzerDenote)
89+
.Case("clang_analyzer_express", &ExprInspectionChecker::analyzerExpress)
8590
.Default(nullptr);
8691

8792
if (!Handler)
@@ -264,6 +269,13 @@ void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper,
264269
N = BugNode;
265270
State = State->remove<MarkedSymbols>(Sym);
266271
}
272+
273+
for (auto I : State->get<DenotedSymbols>()) {
274+
SymbolRef Sym = I.first;
275+
if (!SymReaper.isLive(Sym))
276+
State = State->remove<DenotedSymbols>(Sym);
277+
}
278+
267279
C.addTransition(State, N);
268280
}
269281

@@ -295,6 +307,92 @@ void ExprInspectionChecker::analyzerHashDump(const CallExpr *CE,
295307
reportBug(HashContent, C);
296308
}
297309

310+
void ExprInspectionChecker::analyzerDenote(const CallExpr *CE,
311+
CheckerContext &C) const {
312+
if (CE->getNumArgs() < 2) {
313+
reportBug("clang_analyzer_denote() requires a symbol and a string literal",
314+
C);
315+
return;
316+
}
317+
318+
SymbolRef Sym = C.getSVal(CE->getArg(0)).getAsSymbol();
319+
if (!Sym) {
320+
reportBug("Not a symbol", C);
321+
return;
322+
}
323+
324+
if (!isa<SymbolData>(Sym)) {
325+
reportBug("Not an atomic symbol", C);
326+
return;
327+
}
328+
329+
const auto *E = dyn_cast<StringLiteral>(CE->getArg(1)->IgnoreParenCasts());
330+
if (!E) {
331+
reportBug("Not a string literal", C);
332+
return;
333+
}
334+
335+
ProgramStateRef State = C.getState();
336+
337+
C.addTransition(C.getState()->set<DenotedSymbols>(Sym, E));
338+
}
339+
340+
class SymbolExpressor
341+
: public SymExprVisitor<SymbolExpressor, Optional<std::string>> {
342+
ProgramStateRef State;
343+
344+
public:
345+
SymbolExpressor(ProgramStateRef State) : State(State) {}
346+
347+
Optional<std::string> VisitSymExpr(const SymExpr *S) {
348+
if (const StringLiteral *const *SLPtr = State->get<DenotedSymbols>(S)) {
349+
const StringLiteral *SL = *SLPtr;
350+
return std::string(SL->getBytes());
351+
}
352+
return None;
353+
}
354+
355+
Optional<std::string> VisitSymIntExpr(const SymIntExpr *S) {
356+
if (auto Str = Visit(S->getLHS()))
357+
return (*Str + " " + BinaryOperator::getOpcodeStr(S->getOpcode()) + " " +
358+
std::to_string(S->getRHS().getLimitedValue()) +
359+
(S->getRHS().isUnsigned() ? "U" : ""))
360+
.str();
361+
return None;
362+
}
363+
364+
Optional<std::string> VisitSymSymExpr(const SymSymExpr *S) {
365+
if (auto Str1 = Visit(S->getLHS()))
366+
if (auto Str2 = Visit(S->getRHS()))
367+
return (*Str1 + " " + BinaryOperator::getOpcodeStr(S->getOpcode()) +
368+
" " + *Str2).str();
369+
return None;
370+
}
371+
};
372+
373+
void ExprInspectionChecker::analyzerExpress(const CallExpr *CE,
374+
CheckerContext &C) const {
375+
if (CE->getNumArgs() == 0) {
376+
reportBug("clang_analyzer_express() requires a symbol", C);
377+
return;
378+
}
379+
380+
SymbolRef Sym = C.getSVal(CE->getArg(0)).getAsSymbol();
381+
if (!Sym) {
382+
reportBug("Not a symbol", C);
383+
return;
384+
}
385+
386+
SymbolExpressor V(C.getState());
387+
auto Str = V.Visit(Sym);
388+
if (!Str) {
389+
reportBug("Unable to express", C);
390+
return;
391+
}
392+
393+
reportBug(*Str, C);
394+
}
395+
298396
void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
299397
Mgr.registerChecker<ExprInspectionChecker>();
300398
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %clang_analyze_cc1 -x c++ -analyzer-checker=debug.ExprInspection -verify %s
2+
3+
// Self-tests for the debug.ExprInspection checker.
4+
5+
void clang_analyzer_denote(int x, const char *str);
6+
void clang_analyzer_express(int x);
7+
8+
// Invalid declarations to test sanity checks.
9+
void clang_analyzer_denote();
10+
void clang_analyzer_denote(int x);
11+
void clang_analyzer_express();
12+
13+
void foo(int x, unsigned y) {
14+
clang_analyzer_denote(); // expected-warning{{clang_analyzer_denote() requires a symbol and a string literal}}
15+
clang_analyzer_express(); // expected-warning{{clang_analyzer_express() requires a symbol}}
16+
17+
clang_analyzer_denote(x); // expected-warning{{clang_analyzer_denote() requires a symbol and a string literal}}
18+
clang_analyzer_express(x); // expected-warning{{Unable to express}}
19+
20+
clang_analyzer_denote(x, "$x");
21+
clang_analyzer_denote(y, "$y");
22+
clang_analyzer_express(x + y); // expected-warning{{$x + $y}}
23+
24+
clang_analyzer_denote(1, "$z"); // expected-warning{{Not a symbol}}
25+
clang_analyzer_express(1); // expected-warning{{Not a symbol}}
26+
27+
clang_analyzer_denote(x + 1, "$w"); // expected-warning{{Not an atomic symbol}}
28+
clang_analyzer_express(x + 1); // expected-warning{{$x + 1}}
29+
clang_analyzer_express(y + 1); // expected-warning{{$y + 1U}}
30+
}

0 commit comments

Comments
 (0)