blob: 60c6aa534d12768304ad0bdab7eaee39f0277031 [file] [log] [blame]
Jordy Rose31ae2592012-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 Rose31ae2592012-05-16 16:01:07 +000011#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000012#include "clang/StaticAnalyzer/Core/Checker.h"
Jordy Rose31ae2592012-05-16 16:01:07 +000013#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000014#include "llvm/ADT/StringSwitch.h"
Jordy Rose31ae2592012-05-16 16:01:07 +000015
16using namespace clang;
17using namespace ento;
18
19namespace {
20class ExprInspectionChecker : public Checker< eval::Call > {
Ahmed Charlesb8984322014-03-07 20:03:18 +000021 mutable std::unique_ptr<BugType> BT;
Jordan Rose13937b12012-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 Rose9db2d9a2013-10-03 16:57:03 +000025 void analyzerWarnIfReached(const CallExpr *CE, CheckerContext &C) const;
Jordan Rosee9c57222013-07-19 00:59:08 +000026 void analyzerCrash(const CallExpr *CE, CheckerContext &C) const;
Jordan Rose13937b12012-08-10 22:26:29 +000027
28 typedef void (ExprInspectionChecker::*FnCheck)(const CallExpr *,
29 CheckerContext &C) const;
30
Jordy Rose31ae2592012-05-16 16:01:07 +000031public:
32 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
33};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000034}
Jordy Rose31ae2592012-05-16 16:01:07 +000035
36bool ExprInspectionChecker::evalCall(const CallExpr *CE,
Jordan Rose13937b12012-08-10 22:26:29 +000037 CheckerContext &C) const {
Jordy Rose31ae2592012-05-16 16:01:07 +000038 // These checks should have no effect on the surrounding environment
Jordan Rose13937b12012-08-10 22:26:29 +000039 // (globals should not be invalidated, etc), hence the use of evalCall.
40 FnCheck Handler = llvm::StringSwitch<FnCheck>(C.getCalleeName(CE))
41 .Case("clang_analyzer_eval", &ExprInspectionChecker::analyzerEval)
42 .Case("clang_analyzer_checkInlined",
43 &ExprInspectionChecker::analyzerCheckInlined)
Jordan Rosee9c57222013-07-19 00:59:08 +000044 .Case("clang_analyzer_crash", &ExprInspectionChecker::analyzerCrash)
Jordan Rose9db2d9a2013-10-03 16:57:03 +000045 .Case("clang_analyzer_warnIfReached", &ExprInspectionChecker::analyzerWarnIfReached)
Craig Topper0dbb7832014-05-27 02:45:47 +000046 .Default(nullptr);
Jordan Rose13937b12012-08-10 22:26:29 +000047
48 if (!Handler)
49 return false;
50
51 (this->*Handler)(CE, C);
52 return true;
53}
54
55static const char *getArgumentValueString(const CallExpr *CE,
56 CheckerContext &C) {
57 if (CE->getNumArgs() == 0)
58 return "Missing assertion argument";
59
60 ExplodedNode *N = C.getPredecessor();
61 const LocationContext *LC = N->getLocationContext();
62 ProgramStateRef State = N->getState();
63
64 const Expr *Assertion = CE->getArg(0);
65 SVal AssertionVal = State->getSVal(Assertion, LC);
66
67 if (AssertionVal.isUndef())
68 return "UNDEFINED";
69
70 ProgramStateRef StTrue, StFalse;
Benjamin Kramer867ea1d2014-03-02 13:01:17 +000071 std::tie(StTrue, StFalse) =
David Blaikie2fdacbc2013-02-20 05:52:05 +000072 State->assume(AssertionVal.castAs<DefinedOrUnknownSVal>());
Jordan Rose13937b12012-08-10 22:26:29 +000073
74 if (StTrue) {
75 if (StFalse)
76 return "UNKNOWN";
77 else
78 return "TRUE";
79 } else {
80 if (StFalse)
81 return "FALSE";
82 else
83 llvm_unreachable("Invalid constraint; neither true or false.");
84 }
85}
86
87void ExprInspectionChecker::analyzerEval(const CallExpr *CE,
88 CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +000089 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Jordy Rose31ae2592012-05-16 16:01:07 +000090
Jordy Rose31ae2592012-05-16 16:01:07 +000091 // A specific instantiation of an inlined function may have more constrained
92 // values than can generally be assumed. Skip the check.
Craig Topper0dbb7832014-05-27 02:45:47 +000093 if (LC->getCurrentStackFrame()->getParent() != nullptr)
Jordan Rose13937b12012-08-10 22:26:29 +000094 return;
Jordy Rose31ae2592012-05-16 16:01:07 +000095
96 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000097 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
Jordy Rose31ae2592012-05-16 16:01:07 +000098
Devin Coughline39bd402015-09-16 22:03:05 +000099 ExplodedNode *N = C.generateNonFatalErrorNode();
100 if (!N)
101 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000102 C.emitReport(
103 llvm::make_unique<BugReport>(*BT, getArgumentValueString(CE, C), N));
Jordan Rose13937b12012-08-10 22:26:29 +0000104}
Jordy Rose31ae2592012-05-16 16:01:07 +0000105
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000106void ExprInspectionChecker::analyzerWarnIfReached(const CallExpr *CE,
107 CheckerContext &C) const {
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000108
109 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000110 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000111
Devin Coughline39bd402015-09-16 22:03:05 +0000112 ExplodedNode *N = C.generateNonFatalErrorNode();
113 if (!N)
114 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000115 C.emitReport(llvm::make_unique<BugReport>(*BT, "REACHABLE", N));
Jordan Rose9db2d9a2013-10-03 16:57:03 +0000116}
117
Jordan Rose13937b12012-08-10 22:26:29 +0000118void ExprInspectionChecker::analyzerCheckInlined(const CallExpr *CE,
119 CheckerContext &C) const {
Devin Coughline39bd402015-09-16 22:03:05 +0000120 const LocationContext *LC = C.getPredecessor()->getLocationContext();
Jordan Rose13937b12012-08-10 22:26:29 +0000121
122 // An inlined function could conceivably also be analyzed as a top-level
123 // function. We ignore this case and only emit a message (TRUE or FALSE)
124 // when we are analyzing it as an inlined function. This means that
125 // clang_analyzer_checkInlined(true) should always print TRUE, but
126 // clang_analyzer_checkInlined(false) should never actually print anything.
Craig Topper0dbb7832014-05-27 02:45:47 +0000127 if (LC->getCurrentStackFrame()->getParent() == nullptr)
Jordan Rose13937b12012-08-10 22:26:29 +0000128 return;
129
130 if (!BT)
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000131 BT.reset(new BugType(this, "Checking analyzer assumptions", "debug"));
Jordan Rose13937b12012-08-10 22:26:29 +0000132
Devin Coughline39bd402015-09-16 22:03:05 +0000133 ExplodedNode *N = C.generateNonFatalErrorNode();
134 if (!N)
135 return;
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000136 C.emitReport(
137 llvm::make_unique<BugReport>(*BT, getArgumentValueString(CE, C), N));
Jordy Rose31ae2592012-05-16 16:01:07 +0000138}
139
Jordan Rosee9c57222013-07-19 00:59:08 +0000140void ExprInspectionChecker::analyzerCrash(const CallExpr *CE,
141 CheckerContext &C) const {
142 LLVM_BUILTIN_TRAP;
143}
144
Jordy Rose31ae2592012-05-16 16:01:07 +0000145void ento::registerExprInspectionChecker(CheckerManager &Mgr) {
146 Mgr.registerChecker<ExprInspectionChecker>();
147}
148