Zhongxing Xu | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 1 | //===--- CallInliner.cpp - Transfer function that inlines callee ----------===// |
| 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 file implements the callee inlining transfer function. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 14 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
| 15 | #include "clang/Checker/PathSensitive/GRState.h" |
Ted Kremenek | 9705309 | 2010-01-26 22:59:55 +0000 | [diff] [blame^] | 16 | #include "clang/Checker/Checkers/LocalCheckers.h" |
Zhongxing Xu | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | |
| 20 | namespace { |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 21 | class CallInliner : public Checker { |
Zhongxing Xu | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 22 | public: |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 23 | static void *getTag() { |
| 24 | static int x; |
| 25 | return &x; |
| 26 | } |
Zhongxing Xu | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 27 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 28 | virtual bool EvalCallExpr(CheckerContext &C, const CallExpr *CE); |
Zhongxing Xu | 598278b | 2009-12-24 02:25:21 +0000 | [diff] [blame] | 29 | virtual void EvalEndPath(GREndPathNodeBuilder &B,void *tag,GRExprEngine &Eng); |
Zhongxing Xu | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 30 | }; |
Zhongxing Xu | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 33 | void clang::RegisterCallInliner(GRExprEngine &Eng) { |
| 34 | Eng.registerCheck(new CallInliner()); |
| 35 | } |
| 36 | |
| 37 | bool CallInliner::EvalCallExpr(CheckerContext &C, const CallExpr *CE) { |
| 38 | const GRState *state = C.getState(); |
| 39 | const Expr *Callee = CE->getCallee(); |
| 40 | SVal L = state->getSVal(Callee); |
Zhongxing Xu | 62d399e | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 41 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 42 | const FunctionDecl *FD = L.getAsFunctionDecl(); |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 43 | if (!FD) |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 44 | return false; |
| 45 | |
| 46 | if (!FD->isThisDeclarationADefinition()) |
| 47 | return false; |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 48 | |
Zhongxing Xu | 62d399e | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 49 | GRStmtNodeBuilder &Builder = C.getNodeBuilder(); |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 50 | // Make a new LocationContext. |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 51 | const StackFrameContext *LocCtx = C.getAnalysisManager().getStackFrame(FD, |
Zhongxing Xu | 62d399e | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 52 | C.getPredecessor()->getLocationContext(), CE, |
| 53 | Builder.getBlock(), Builder.getIndex()); |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 54 | |
| 55 | CFGBlock const *Entry = &(LocCtx->getCFG()->getEntry()); |
| 56 | |
| 57 | assert (Entry->empty() && "Entry block must be empty."); |
| 58 | |
| 59 | assert (Entry->succ_size() == 1 && "Entry block must have 1 successor."); |
| 60 | |
| 61 | // Get the solitary successor. |
| 62 | CFGBlock const *SuccB = *(Entry->succ_begin()); |
| 63 | |
| 64 | // Construct an edge representing the starting location in the function. |
| 65 | BlockEdge Loc(Entry, SuccB, LocCtx); |
| 66 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 67 | state = C.getStoreManager().EnterStackFrame(state, LocCtx); |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 68 | // This is a hack. We really should not use the GRStmtNodeBuilder. |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 69 | bool isNew; |
| 70 | GRExprEngine &Eng = C.getEngine(); |
| 71 | ExplodedNode *Pred = C.getPredecessor(); |
Zhongxing Xu | 62d399e | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 72 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 73 | |
| 74 | ExplodedNode *SuccN = Eng.getGraph().getNode(Loc, state, &isNew); |
| 75 | SuccN->addPredecessor(Pred, Eng.getGraph()); |
| 76 | C.getNodeBuilder().Deferred.erase(Pred); |
| 77 | |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 78 | if (isNew) |
| 79 | Builder.getWorkList()->Enqueue(SuccN); |
| 80 | |
| 81 | Builder.HasGeneratedNode = true; |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 82 | |
| 83 | return true; |
Zhongxing Xu | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 84 | } |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame] | 85 | |
Zhongxing Xu | 598278b | 2009-12-24 02:25:21 +0000 | [diff] [blame] | 86 | void CallInliner::EvalEndPath(GREndPathNodeBuilder &B, void *tag, |
| 87 | GRExprEngine &Eng) { |
| 88 | const GRState *state = B.getState(); |
| 89 | ExplodedNode *Pred = B.getPredecessor(); |
| 90 | const StackFrameContext *LocCtx = |
| 91 | cast<StackFrameContext>(Pred->getLocationContext()); |
| 92 | |
| 93 | const Stmt *CE = LocCtx->getCallSite(); |
| 94 | |
| 95 | // Check if this is the top level stack frame. |
| 96 | if (!LocCtx->getParent()) |
| 97 | return; |
| 98 | |
| 99 | PostStmt NodeLoc(CE, LocCtx->getParent()); |
| 100 | |
| 101 | bool isNew; |
| 102 | ExplodedNode *Succ = Eng.getGraph().getNode(NodeLoc, state, &isNew); |
| 103 | Succ->addPredecessor(Pred, Eng.getGraph()); |
| 104 | |
Zhongxing Xu | 598278b | 2009-12-24 02:25:21 +0000 | [diff] [blame] | 105 | // When creating the new work list unit, increment the statement index to |
| 106 | // point to the statement after the CallExpr. |
| 107 | if (isNew) |
Zhongxing Xu | 62d399e | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 108 | B.getWorkList().Enqueue(Succ, |
| 109 | *const_cast<CFGBlock*>(LocCtx->getCallSiteBlock()), |
| 110 | LocCtx->getIndex() + 1); |
Zhongxing Xu | 598278b | 2009-12-24 02:25:21 +0000 | [diff] [blame] | 111 | |
| 112 | B.HasGeneratedNode = true; |
| 113 | } |