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 | 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 | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 29 | }; |
Zhongxing Xu | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame^] | 32 | void clang::RegisterCallInliner(GRExprEngine &Eng) { |
| 33 | Eng.registerCheck(new CallInliner()); |
| 34 | } |
| 35 | |
| 36 | bool CallInliner::EvalCallExpr(CheckerContext &C, const CallExpr *CE) { |
| 37 | const GRState *state = C.getState(); |
| 38 | const Expr *Callee = CE->getCallee(); |
| 39 | SVal L = state->getSVal(Callee); |
| 40 | |
| 41 | const FunctionDecl *FD = L.getAsFunctionDecl(); |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 42 | if (!FD) |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame^] | 43 | return false; |
| 44 | |
| 45 | if (!FD->isThisDeclarationADefinition()) |
| 46 | return false; |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 47 | |
| 48 | // Make a new LocationContext. |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame^] | 49 | const StackFrameContext *LocCtx = C.getAnalysisManager().getStackFrame(FD, |
| 50 | C.getPredecessor()->getLocationContext(), CE); |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 51 | |
| 52 | CFGBlock const *Entry = &(LocCtx->getCFG()->getEntry()); |
| 53 | |
| 54 | assert (Entry->empty() && "Entry block must be empty."); |
| 55 | |
| 56 | assert (Entry->succ_size() == 1 && "Entry block must have 1 successor."); |
| 57 | |
| 58 | // Get the solitary successor. |
| 59 | CFGBlock const *SuccB = *(Entry->succ_begin()); |
| 60 | |
| 61 | // Construct an edge representing the starting location in the function. |
| 62 | BlockEdge Loc(Entry, SuccB, LocCtx); |
| 63 | |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame^] | 64 | state = C.getStoreManager().EnterStackFrame(state, LocCtx); |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 65 | // This is a hack. We really should not use the GRStmtNodeBuilder. |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame^] | 66 | bool isNew; |
| 67 | GRExprEngine &Eng = C.getEngine(); |
| 68 | ExplodedNode *Pred = C.getPredecessor(); |
| 69 | GRStmtNodeBuilder &Builder = C.getNodeBuilder(); |
| 70 | |
| 71 | ExplodedNode *SuccN = Eng.getGraph().getNode(Loc, state, &isNew); |
| 72 | SuccN->addPredecessor(Pred, Eng.getGraph()); |
| 73 | C.getNodeBuilder().Deferred.erase(Pred); |
| 74 | |
Zhongxing Xu | c9f4af6 | 2009-10-13 02:36:42 +0000 | [diff] [blame] | 75 | if (isNew) |
| 76 | Builder.getWorkList()->Enqueue(SuccN); |
| 77 | |
| 78 | Builder.HasGeneratedNode = true; |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame^] | 79 | |
| 80 | return true; |
Zhongxing Xu | 66847a2 | 2009-09-11 04:13:42 +0000 | [diff] [blame] | 81 | } |
Zhongxing Xu | 3ff8481 | 2009-12-23 08:56:18 +0000 | [diff] [blame^] | 82 | |