blob: 9522d1d6387dba1a1b171c2d54d462c934aec499 [file] [log] [blame]
Jordy Rose93a9d822012-05-16 16:01:07 +00001//==- ExprInspectionChecker.cpp - Used for regression tests ------*- C++ -*-==//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ClangSACheckers.h"
Jordy Rose93a9d822012-05-16 16:01:07 +000011#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000012#include "clang/StaticAnalyzer/Core/Checker.h"
Jordy Rose93a9d822012-05-16 16:01:07 +000013#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000014#include "llvm/ADT/StringSwitch.h"
Jordy Rose93a9d822012-05-16 16:01:07 +000015
16using namespace clang;
17using namespace ento;
18
19namespace {
20class ExprInspectionChecker : public Checker< eval::Call > {
21 mutable OwningPtr<BugType> BT;
Jordan Rosee5399f12012-08-10 22:26:29 +000022
23 void analyzerEval(const CallExpr *CE, CheckerContext &C) const;
24 void analyzerCheckInlined(const CallExpr *CE, CheckerContext &C) const;
Jordan Roseac7cc2d2013-07-19 00:59:08 +000025 void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
Jordan Rosee5399f12012-08-10 22:26:29 +000026
27 typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
28 CheckerContext &C) const;
29
Jordy Rose93a9d822012-05-16 16:01:07 +000030public:
31 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
32};
33}
34
35bool ExprInspectionChecker::evalCall(const CallExpr *CE,
Jordan Rosee5399f12012-08-10 22:26:29 +000036 CheckerContext &C) const {
Jordy Rose93a9d822012-05-16 16:01:07 +000037 // These checks should have no effect on the surrounding environment
Jordan Rosee5399f12012-08-10 22:26:29 +000038 // (globals should not be invalidated, etc), hence the use of evalCall.
39 FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
40 .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
41 .Case("clang_analyzer_checkInlined",
42 &ExprInspectionChecker::analyzerCheckInlined)
Jordan Roseac7cc2d2013-07-19 00:59:08 +000043 .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
Jordan Rosee5399f12012-08-10 22:26:29 +000044 .Default(0);
45
46 if (!Handler)
47 return false;
48
49 (this->*Handler)(CE, C);
50 return true;
51}
52
53static const char *getArgumentValueString(const CallExpr *CE,
54 CheckerContext &C) {
55 if (CE->getNumArgs() == 0)
56 return "Missing assertion argument";
57
58 ExplodedNode *N = C.getPredecessor();
59 const LocationContext *LC = N->getLocationContext();
60 ProgramStateRef State = N->getState();
61
62 const Expr *Assertion = CE->getArg(0);
63 SVal AssertionVal = State->getSVal(Assertion, LC);
64
65 if (AssertionVal.isUndef())
66 return "UNDEFINED";
67
68 ProgramStateRef StTrue, StFalse;
69 llvm::tie(StTrue, StFalse) =
David Blaikie5251abe2013-02-20 05:52:05 +000070 State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
Jordan Rosee5399f12012-08-10 22:26:29 +000071
72 if (StTrue) {
73 if (StFalse)
74 return "UNKNOWN";
75 else
76 return "TRUE";
77 } else {
78 if (StFalse)
79 return "FALSE";
80 else
81 llvm_unreachable("Invalid constraint; neither true or false.");
82 }
83}
84
85void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
86 CheckerContext &C) const {
Jordy Rose93a9d822012-05-16 16:01:07 +000087 ExplodedNode *N = C.getPredecessor();
88 const LocationContext *LC = N->getLocationContext();
89
Jordy Rose93a9d822012-05-16 16:01:07 +000090 // A specific instantiation of an inlined function may have more constrained
91 // values than can generally be assumed. Skip the check.
Jordan Rosee5399f12012-08-10 22:26:29 +000092 if (LC->getCurrentStackFrame()->getParent() != 0)
93 return;
Jordy Rose93a9d822012-05-16 16:01:07 +000094
95 if (!BT)
96 BT.reset(new BugType("Checking analyzer assumptions", "debug"));
97
Jordan Rosee5399f12012-08-10 22:26:29 +000098 BugReport *R = new BugReport(*BT, getArgumentValueString(CE, C), N);
Jordan Rose785950e2012-11-02 01:53:40 +000099 C.emitReport(R);
Jordan Rosee5399f12012-08-10 22:26:29 +0000100}
Jordy Rose93a9d822012-05-16 16:01:07 +0000101
Jordan Rosee5399f12012-08-10 22:26:29 +0000102void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
103 CheckerContext &C) const {
104 ExplodedNode *N = C.getPredecessor();
105 const LocationContext *LC = N->getLocationContext();
106
107 // An inlined function could conceivably also be analyzed as a top-level
108 // function. We ignore this case and only emit a message (TRUE or FALSE)
109 // when we are analyzing it as an inlined function. This means that
110 // clang_analyzer_checkInlined(true) should always print TRUE, but
111 // clang_analyzer_checkInlined(false) should never actually print anything.
112 if (LC->getCurrentStackFrame()->getParent() == 0)
113 return;
114
115 if (!BT)
116 BT.reset(new BugType("Checking analyzer assumptions", "debug"));
117
118 BugReport *R = new BugReport(*BT, getArgumentValueString(CE, C), N);
Jordan Rose785950e2012-11-02 01:53:40 +0000119 C.emitReport(R);
Jordy Rose93a9d822012-05-16 16:01:07 +0000120}
121
Jordan Roseac7cc2d2013-07-19 00:59:08 +0000122void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
123 CheckerContext &C) const {
124 LLVM_BUILTIN_TRAP;
125}
126
Jordy Rose93a9d822012-05-16 16:01:07 +0000127void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
128 Mgr.registerChecker<ExprInspectionChecker>();
129}
130