1 //==- ExprInspectionChecker.cpp - Used for regression tests ------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ClangSACheckers.h"
11 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
12 #include "clang/StaticAnalyzer/Core/Checker.h"
13 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
14 #include "llvm/ADT/StringSwitch.h"
16 using namespace clang;
20 class ExprInspectionChecker : public Checker<eval::Call, check::DeadSymbols> {
21 mutable std::unique_ptr<BugType> BT;
23 void analyzerEval(const CallExpr *CE, CheckerContext &C) const;
24 void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const;
25 void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const;
26 void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
27 void analyzerWarnOnDeadSymbol(const CallExpr *CE, CheckerContext &C) const;
29 typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
30 CheckerContext &C) const;
33 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
34 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
38 REGISTER_SET_WITH_PROGRAMSTATE(MarkedSymbols, const void *)
40 bool ExprInspectionChecker::evalCall(const CallExpr *CE,
41 CheckerContext &C) const {
42 // These checks should have no effect on the surrounding environment
43 // (globals should not be invalidated, etc), hence the use of evalCall.
44 FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
45 .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
46 .Case("clang_analyzer_checkInlined",
47 &ExprInspectionChecker::analyzerCheckInlined)
48 .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
49 .Case("clang_analyzer_warnIfReached",
50 &ExprInspectionChecker::analyzerWarnIfReached)
51 .Case("clang_analyzer_warnOnDeadSymbol",
52 &ExprInspectionChecker::analyzerWarnOnDeadSymbol)
58 (this->*Handler)(CE, C);
62 static const char *getArgumentValueString(const CallExpr *CE,
64 if (CE->getNumArgs() == 0)
65 return "Missing assertion argument";
67 ExplodedNode *N = C.getPredecessor();
68 const LocationContext *LC = N->getLocationContext();
69 ProgramStateRef State = N->getState();
71 const Expr *Assertion = CE->getArg(0);
72 SVal AssertionVal = State->getSVal(Assertion, LC);
74 if (AssertionVal.isUndef())
77 ProgramStateRef StTrue, StFalse;
78 std::tie(StTrue, StFalse) =
79 State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
90 llvm_unreachable("Invalid constraint; neither true or false.");
94 void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
95 CheckerContext &C) const {
96 const LocationContext *LC = C.getPredecessor()->getLocationContext();
98 // A specific instantiation of an inlined function may have more constrained
99 // values than can generally be assumed. Skip the check.
100 if (LC->getCurrentStackFrame()->getParent() != nullptr)
104 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
106 ExplodedNode *N = C.generateNonFatalErrorNode();
110 llvm::make_unique<BugReport>(*BT, getArgumentValueString(CE, C), N));
113 void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE,
114 CheckerContext &C) const {
117 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
119 ExplodedNode *N = C.generateNonFatalErrorNode();
122 C.emitReport(llvm::make_unique<BugReport>(*BT, "REACHABLE", N));
125 void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
126 CheckerContext &C) const {
127 const LocationContext *LC = C.getPredecessor()->getLocationContext();
129 // An inlined function could conceivably also be analyzed as a top-level
130 // function. We ignore this case and only emit a message (TRUE or FALSE)
131 // when we are analyzing it as an inlined function. This means that
132 // clang_analyzer_checkInlined(true) should always print TRUE, but
133 // clang_analyzer_checkInlined(false) should never actually print anything.
134 if (LC->getCurrentStackFrame()->getParent() == nullptr)
138 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
140 ExplodedNode *N = C.generateNonFatalErrorNode();
144 llvm::make_unique<BugReport>(*BT, getArgumentValueString(CE, C), N));
147 void ExprInspectionChecker::analyzerWarnOnDeadSymbol(const CallExpr *CE,
148 CheckerContext &C) const {
149 if (CE->getNumArgs() == 0)
151 SVal Val = C.getSVal(CE->getArg(0));
152 SymbolRef Sym = Val.getAsSymbol();
156 ProgramStateRef State = C.getState();
157 State = State->add<MarkedSymbols>(Sym);
158 C.addTransition(State);
161 void ExprInspectionChecker::checkDeadSymbols(SymbolReaper &SymReaper,
162 CheckerContext &C) const {
163 ProgramStateRef State = C.getState();
164 const MarkedSymbolsTy &Syms = State->get<MarkedSymbols>();
165 for (auto I = Syms.begin(), E = Syms.end(); I != E; ++I) {
166 SymbolRef Sym = static_cast<SymbolRef>(*I);
167 if (!SymReaper.isDead(Sym))
171 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
173 ExplodedNode *N = C.generateNonFatalErrorNode();
177 C.emitReport(llvm::make_unique<BugReport>(*BT, "SYMBOL DEAD", N));
178 C.addTransition(State->remove<MarkedSymbols>(Sym), N);
182 void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
183 CheckerContext &C) const {
187 void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
188 Mgr.registerChecker<ExprInspectionChecker>();