Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame^] | 1 | //== AnalysisContext.cpp - Analysis context for Path Sens analysis -*- 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 file defines AnalysisContext, a class that manages the analysis context |
| 11 | // data for path sensitive analysis. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/AnalysisContext.h" |
| 16 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 17 | #include "clang/Analysis/CFG.h" |
| 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/ParentMap.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | AnalysisContext::~AnalysisContext() { |
| 25 | delete cfg; |
| 26 | delete liveness; |
| 27 | delete PM; |
| 28 | } |
| 29 | |
| 30 | Stmt *AnalysisContext::getBody() { |
| 31 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 32 | return FD->getBody(); |
| 33 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 34 | return MD->getBody(); |
| 35 | |
| 36 | assert(0 && "unknown code decl"); |
| 37 | } |
| 38 | |
| 39 | CFG *AnalysisContext::getCFG() { |
| 40 | if (!cfg) |
| 41 | cfg = CFG::buildCFG(getBody(), &D->getASTContext()); |
| 42 | return cfg; |
| 43 | } |
| 44 | |
| 45 | ParentMap &AnalysisContext::getParentMap() { |
| 46 | if (!PM) |
| 47 | PM = new ParentMap(getBody()); |
| 48 | return *PM; |
| 49 | } |
| 50 | |
| 51 | LiveVariables *AnalysisContext::getLiveVariables() { |
| 52 | if (!liveness) { |
| 53 | CFG *c = getCFG(); |
| 54 | if (!c) |
| 55 | return 0; |
| 56 | |
| 57 | liveness = new LiveVariables(D->getASTContext(), *c); |
| 58 | liveness->runOnCFG(*c); |
| 59 | liveness->runOnAllBlocks(*c, 0, true); |
| 60 | } |
| 61 | |
| 62 | return liveness; |
| 63 | } |
| 64 | |
| 65 | AnalysisContext *AnalysisContextManager::getContext(Decl *D) { |
| 66 | iterator I = Contexts.find(D); |
| 67 | if (I != Contexts.end()) |
| 68 | return &(I->second); |
| 69 | |
| 70 | AnalysisContext &Ctx = Contexts[D]; |
| 71 | Ctx.setDecl(D); |
| 72 | return &Ctx; |
| 73 | } |