Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 1 | //=== 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 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 14 | #include "ExprEngineInternalChecks.h" |
Argyrios Kyrtzidis | 98cabba | 2010-12-22 18:51:49 +0000 | [diff] [blame] | 15 | #include "clang/GR/PathSensitive/Checker.h" |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Builtins.h" |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 19 | using namespace GR; |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class BuiltinFunctionChecker : public Checker { |
| 24 | public: |
| 25 | static void *getTag() { static int tag = 0; return &tag; } |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 26 | virtual bool evalCallExpr(CheckerContext &C, const CallExpr *CE); |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | } |
| 30 | |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame^] | 31 | void GR::RegisterBuiltinFunctionChecker(ExprEngine &Eng) { |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 32 | Eng.registerCheck(new BuiltinFunctionChecker()); |
| 33 | } |
| 34 | |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 35 | bool BuiltinFunctionChecker::evalCallExpr(CheckerContext &C,const CallExpr *CE){ |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 36 | const GRState *state = C.getState(); |
| 37 | const Expr *Callee = CE->getCallee(); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 38 | SVal L = state->getSVal(Callee); |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 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()); |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 53 | SVal X = state->getSVal(*(CE->arg_begin())); |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 54 | C.generateNode(state->BindExpr(CE, X)); |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
| 58 | case Builtin::BI__builtin_alloca: { |
| 59 | // FIXME: Refactor into StoreManager itself? |
| 60 | MemRegionManager& RM = C.getStoreManager().getRegionManager(); |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 61 | const AllocaRegion* R = |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 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. |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 68 | DefinedOrUnknownSVal Size = |
| 69 | cast<DefinedOrUnknownSVal>(state->getSVal(*(CE->arg_begin()))); |
| 70 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 71 | SValBuilder& svalBuilder = C.getSValBuilder(); |
| 72 | DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder); |
| 73 | DefinedOrUnknownSVal extentMatchesSizeArg = |
Ted Kremenek | 9c14953 | 2010-12-01 21:57:22 +0000 | [diff] [blame] | 74 | svalBuilder.evalEQ(state, Extent, Size); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 75 | state = state->assume(extentMatchesSizeArg, true); |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 77 | C.generateNode(state->BindExpr(CE, loc::MemRegionVal(R))); |
Zhongxing Xu | 7c9624b | 2009-12-08 09:07:59 +0000 | [diff] [blame] | 78 | return true; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | } |