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