blob: b074ff918e8d90b61f0005bbfc3a55ba595756fe [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 {
Ted Kremenek18c66fd2011-08-15 22:09:50 +000038 const ProgramState *state = C.getState();
Ted Kremenek5eca4822012-01-06 22:09:28 +000039 const LocationContext *LCtx = C.getLocationContext();
40 if (state->getSVal(B, LCtx).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000041 // Generate an error node.
Ted Kremenekd048c6e2010-12-20 21:19:09 +000042 ExplodedNode *N = C.generateSink();
Zhongxing Xu668399b2009-11-24 08:24:26 +000043 if (!N)
44 return;
45
46 if (!BT)
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000047 BT.reset(new BuiltinBug("Result of operation is garbage or undefined"));
Zhongxing Xu668399b2009-11-24 08:24:26 +000048
49 llvm::SmallString<256> sbuf;
50 llvm::raw_svector_ostream OS(sbuf);
51 const Expr *Ex = NULL;
52 bool isLeft = true;
53
Ted Kremenek5eca4822012-01-06 22:09:28 +000054 if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000055 Ex = B->getLHS()->IgnoreParenCasts();
56 isLeft = true;
57 }
Ted Kremenek5eca4822012-01-06 22:09:28 +000058 else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
Zhongxing Xu668399b2009-11-24 08:24:26 +000059 Ex = B->getRHS()->IgnoreParenCasts();
60 isLeft = false;
61 }
62
63 if (Ex) {
64 OS << "The " << (isLeft ? "left" : "right")
65 << " operand of '"
66 << BinaryOperator::getOpcodeStr(B->getOpcode())
67 << "' is a garbage value";
68 }
69 else {
70 // Neither operand was undefined, but the result is undefined.
71 OS << "The result of the '"
72 << BinaryOperator::getOpcodeStr(B->getOpcode())
73 << "' expression is undefined";
74 }
Anna Zakse172e8b2011-08-17 23:00:25 +000075 BugReport *report = new BugReport(*BT, OS.str(), N);
Ted Kremenekd4daa7c2009-11-29 06:37:44 +000076 if (Ex) {
77 report->addRange(Ex->getSourceRange());
Anna Zaks50bbc162011-08-19 22:33:38 +000078 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, Ex));
Ted Kremenekd4daa7c2009-11-29 06:37:44 +000079 }
Zhongxing Xu668399b2009-11-24 08:24:26 +000080 else
Anna Zaks50bbc162011-08-19 22:33:38 +000081 report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, B));
Zhongxing Xu668399b2009-11-24 08:24:26 +000082 C.EmitReport(report);
83 }
84}
Argyrios Kyrtzidis180e03f2011-02-28 01:27:22 +000085
86void ento::registerUndefResultChecker(CheckerManager &mgr) {
87 mgr.registerChecker<UndefResultChecker>();
88}