blob: 923a7e1bed0bd18c57dfbed25aab18a1a3c2b961 [file] [log] [blame]
Zhongxing Xu8958fff2009-11-03 06:46:03 +00001//===--- UndefinedArgChecker.h - Undefined arguments checker ----*- 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// This defines BadCallChecker, a builtin check in GRExprEngine that performs
11// checks for undefined arguments.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekf493f492009-11-11 05:50:44 +000015#include "clang/Analysis/PathSensitive/CheckerVisitor.h"
Zhongxing Xu8958fff2009-11-03 06:46:03 +000016#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenekf493f492009-11-11 05:50:44 +000017#include "GRExprEngineInternalChecks.h"
Zhongxing Xu8958fff2009-11-03 06:46:03 +000018
19using namespace clang;
20
Ted Kremenekf493f492009-11-11 05:50:44 +000021namespace {
22class VISIBILITY_HIDDEN UndefinedArgChecker
23 : public CheckerVisitor<UndefinedArgChecker> {
24 BugType *BT;
25public:
26 UndefinedArgChecker() : BT(0) {}
27 static void *getTag() {
28 static int x = 0;
29 return &x;
30 }
31 void PreVisitCallExpr(CheckerContext &C, const CallExpr *CE);
32};
33} // end anonymous namespace
34
35void clang::RegisterUndefinedArgChecker(GRExprEngine &Eng) {
36 Eng.registerCheck(new UndefinedArgChecker());
Zhongxing Xu8958fff2009-11-03 06:46:03 +000037}
38
39void UndefinedArgChecker::PreVisitCallExpr(CheckerContext &C,
40 const CallExpr *CE){
41 for (CallExpr::const_arg_iterator I = CE->arg_begin(), E = CE->arg_end();
42 I != E; ++I) {
43 if (C.getState()->getSVal(*I).isUndef()) {
44 if (ExplodedNode *N = C.GenerateNode(CE, true)) {
45 if (!BT)
Ted Kremenek2c791bd2009-11-06 00:44:32 +000046 BT = new BuiltinBug("Pass-by-value argument in function call is "
47 "undefined");
Zhongxing Xu8958fff2009-11-03 06:46:03 +000048 // Generate a report for this bug.
Benjamin Kramerd02e2322009-11-14 12:08:24 +000049 EnhancedBugReport *R = new EnhancedBugReport(*BT, BT->getName(), N);
Zhongxing Xu8958fff2009-11-03 06:46:03 +000050 R->addRange((*I)->getSourceRange());
51 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, *I);
52 C.EmitReport(R);
53 }
54 }
55 }
56}