blob: e220499d73f4178efea9db6ab4158679d95ec041 [file] [log] [blame]
Zhongxing Xu668399b2009-11-24 08:24:26 +00001//=== UndefResultChecker.cpp ------------------------------------*- 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//
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000010// This defines UndefResultChecker, a builtin check in ExprEngine that
Zhongxing Xu668399b2009-11-24 08:24:26 +000011// performs checks for undefined results of non-assignment binary operators.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000017#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000021#include "llvm/ADT/SmallString.h"
Zhongxing Xu668399b2009-11-24 08:24:26 +000022
23using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000024using namespace ento;
Zhongxing Xu668399b2009-11-24 08:24:26 +000025
26namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000027class UndefResultChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000028 : public Checker< check::PostStmt<BinaryOperator> > {
Zhongxing Xu668399b2009-11-24 08:24:26 +000029
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000030 mutable OwningPtr<BugType> BT;
Zhongxing Xu668399b2009-11-24 08:24:26 +000031
32public:
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000033 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xu668399b2009-11-24 08:24:26 +000034};
35} // end anonymous namespace
36
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000037void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
38 CheckerContext &C) const {
Ted Kremenek8bef8232012-01-26 21:29:00 +000039 ProgramStateRef state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +000040 const LocationContext *LCtx = C.getLocationContext();
41 if (state->getSVal(B, LCtx).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000042 // Generate an error node.
Ted Kremenekd048c6e2010-12-20 21:19:09 +000043 ExplodedNode *N = C.generateSink();
Zhongxing Xu668399b2009-11-24 08:24:26 +000044 if (!N)
45 return;
46
47 if (!BT)
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000048 BT.reset(new BuiltinBug("Result of operation is garbage or undefined"));
Zhongxing Xu668399b2009-11-24 08:24:26 +000049
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000050 SmallString<256> sbuf;
Zhongxing Xu668399b2009-11-24 08:24:26 +000051 llvm::raw_svector_ostream OS(sbuf);
52 const Expr *Ex = NULL;
53 bool isLeft = true;
54
Ted Kremenek5eca4822012-01-06 22:09:28 +000055 if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000056 Ex = B->getLHS()->IgnoreParenCasts();
57 isLeft = true;
58 }
Ted Kremenek5eca4822012-01-06 22:09:28 +000059 else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000060 Ex = B->getRHS()->IgnoreParenCasts();
61 isLeft = false;
62 }
63
64 if (Ex) {
65 OS << "The " << (isLeft ? "left" : "right")
66 << " operand of '"
67 << BinaryOperator::getOpcodeStr(B->getOpcode())
68 << "' is a garbage value";
69 }
70 else {
71 // Neither operand was undefined, but the result is undefined.
72 OS << "The result of the '"
73 << BinaryOperator::getOpcodeStr(B->getOpcode())
74 << "' expression is undefined";
75 }
Anna Zakse172e8b2011-08-17 23:00:25 +000076 BugReport *report = new BugReport(*BT, OS.str(), N);
Ted Kremenekd4daa7c2009-11-29 06:37:44 +000077 if (Ex) {
78 report->addRange(Ex->getSourceRange());
Jordan Rose68537992012-08-03 23:09:01 +000079 bugreporter::addTrackNullOrUndefValueVisitor(N, Ex, report);
Ted Kremenekd4daa7c2009-11-29 06:37:44 +000080 }
Zhongxing Xu668399b2009-11-24 08:24:26 +000081 else
Jordan Rose68537992012-08-03 23:09:01 +000082 bugreporter::addTrackNullOrUndefValueVisitor(N, B, report);
Ted Kremenekce56fd32012-06-06 06:25:37 +000083
84 report->disablePathPruning();
Zhongxing Xu668399b2009-11-24 08:24:26 +000085 C.EmitReport(report);
86 }
87}
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000088
89void ento::registerUndefResultChecker(CheckerManager &mgr) {
90 mgr.registerChecker<UndefResultChecker>();
91}