blob: a89ad2164b30b761efefe260ce9e1bc8c338f990 [file] [log] [blame]
Zhongxing Xu7c9624b2009-12-08 09:07:59 +00001//=== BuiltinFunctionChecker.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//
10// This checker evaluates clang builtin functions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "GRExprEngineInternalChecks.h"
15#include "clang/Analysis/PathSensitive/Checker.h"
16#include "clang/Basic/Builtins.h"
17#include "llvm/ADT/StringSwitch.h"
18
19using namespace clang;
20
21namespace {
22
23class BuiltinFunctionChecker : public Checker {
24public:
25 static void *getTag() { static int tag = 0; return &tag; }
26 virtual bool EvalCallExpr(CheckerContext &C, const CallExpr *CE);
27};
28
29}
30
31void clang::RegisterBuiltinFunctionChecker(GRExprEngine &Eng) {
32 Eng.registerCheck(new BuiltinFunctionChecker());
33}
34
35bool BuiltinFunctionChecker::EvalCallExpr(CheckerContext &C,const CallExpr *CE){
36 const GRState *state = C.getState();
37 const Expr *Callee = CE->getCallee();
38 SVal L = state->getSVal(Callee);
39 const FunctionDecl *FD = L.getAsFunctionDecl();
40
41 if (!FD)
42 return false;
43
44 unsigned id = FD->getBuiltinID();
45
46 if (!id)
47 return false;
48
49 switch (id) {
50 case Builtin::BI__builtin_expect: {
51 // For __builtin_expect, just return the value of the subexpression.
52 assert (CE->arg_begin() != CE->arg_end());
53 SVal X = state->getSVal(*(CE->arg_begin()));
54 C.GenerateNode(state->BindExpr(CE, X));
55 return true;
56 }
57
58 case Builtin::BI__builtin_alloca: {
59 // FIXME: Refactor into StoreManager itself?
60 MemRegionManager& RM = C.getStoreManager().getRegionManager();
61 const MemRegion* R =
62 RM.getAllocaRegion(CE, C.getNodeBuilder().getCurrentBlockCount(),
63 C.getPredecessor()->getLocationContext());
64
65 // Set the extent of the region in bytes. This enables us to use the
66 // SVal of the argument directly. If we save the extent in bits, we
67 // cannot represent values like symbol*8.
68 SVal Extent = state->getSVal(*(CE->arg_begin()));
69 state = C.getStoreManager().setExtent(state, R, Extent);
70 C.GenerateNode(state->BindExpr(CE, loc::MemRegionVal(R)));
71 return true;
72 }
73 }
74
75 return false;
76}