blob: 7fa3804639d6c9398ab013d0ada66bbe84fe9355 [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"
Zhongxing Xu668399b2009-11-24 08:24:26 +000021
22using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000023using namespace ento;
Zhongxing Xu668399b2009-11-24 08:24:26 +000024
25namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000026class UndefResultChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000027 : public Checker< check::PostStmt<BinaryOperator> > {
Zhongxing Xu668399b2009-11-24 08:24:26 +000028
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000029 mutable llvm::OwningPtr<BugType> BT;
Zhongxing Xu668399b2009-11-24 08:24:26 +000030
31public:
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000032 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
Zhongxing Xu668399b2009-11-24 08:24:26 +000033};
34} // end anonymous namespace
35
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000036void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
37 CheckerContext &C) const {
Zhongxing Xu668399b2009-11-24 08:24:26 +000038 const GRState *state = C.getState();
Ted Kremenek13976632010-02-08 16:18:51 +000039 if (state->getSVal(B).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000040 // Generate an error node.
Ted Kremenekd048c6e2010-12-20 21:19:09 +000041 ExplodedNode *N = C.generateSink();
Zhongxing Xu668399b2009-11-24 08:24:26 +000042 if (!N)
43 return;
44
45 if (!BT)
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000046 BT.reset(new BuiltinBug("Result of operation is garbage or undefined"));
Zhongxing Xu668399b2009-11-24 08:24:26 +000047
48 llvm::SmallString<256> sbuf;
49 llvm::raw_svector_ostream OS(sbuf);
50 const Expr *Ex = NULL;
51 bool isLeft = true;
52
Ted Kremenek13976632010-02-08 16:18:51 +000053 if (state->getSVal(B->getLHS()).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000054 Ex = B->getLHS()->IgnoreParenCasts();
55 isLeft = true;
56 }
Ted Kremenek13976632010-02-08 16:18:51 +000057 else if (state->getSVal(B->getRHS()).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000058 Ex = B->getRHS()->IgnoreParenCasts();
59 isLeft = false;
60 }
61
62 if (Ex) {
63 OS << "The " << (isLeft ? "left" : "right")
64 << " operand of '"
65 << BinaryOperator::getOpcodeStr(B->getOpcode())
66 << "' is a garbage value";
67 }
68 else {
69 // Neither operand was undefined, but the result is undefined.
70 OS << "The result of the '"
71 << BinaryOperator::getOpcodeStr(B->getOpcode())
72 << "' expression is undefined";
73 }
Benjamin Kramer4988a9a2009-11-29 18:03:28 +000074 EnhancedBugReport *report = new EnhancedBugReport(*BT, OS.str(), N);
Ted Kremenekd4daa7c2009-11-29 06:37:44 +000075 if (Ex) {
76 report->addRange(Ex->getSourceRange());
Zhongxing Xu668399b2009-11-24 08:24:26 +000077 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, Ex);
Ted Kremenekd4daa7c2009-11-29 06:37:44 +000078 }
Zhongxing Xu668399b2009-11-24 08:24:26 +000079 else
80 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, B);
81 C.EmitReport(report);
82 }
83}
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000084
85void ento::registerUndefResultChecker(CheckerManager &mgr) {
86 mgr.registerChecker<UndefResultChecker>();
87}