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" |
Mike Stump | 87a05f1 | 2009-07-31 01:10:29 +0000 | [diff] [blame^] | 21 | #include "llvm/Support/ErrorHandling.h" |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | AnalysisContext::~AnalysisContext() { |
| 26 | delete cfg; |
| 27 | delete liveness; |
| 28 | delete PM; |
| 29 | } |
| 30 | |
| 31 | Stmt *AnalysisContext::getBody() { |
| 32 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 33 | return FD->getBody(); |
| 34 | else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 35 | return MD->getBody(); |
| 36 | |
Mike Stump | 87a05f1 | 2009-07-31 01:10:29 +0000 | [diff] [blame^] | 37 | llvm::llvm_unreachable("unknown code decl"); |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | CFG *AnalysisContext::getCFG() { |
| 41 | if (!cfg) |
| 42 | cfg = CFG::buildCFG(getBody(), &D->getASTContext()); |
| 43 | return cfg; |
| 44 | } |
| 45 | |
| 46 | ParentMap &AnalysisContext::getParentMap() { |
| 47 | if (!PM) |
| 48 | PM = new ParentMap(getBody()); |
| 49 | return *PM; |
| 50 | } |
| 51 | |
| 52 | LiveVariables *AnalysisContext::getLiveVariables() { |
| 53 | if (!liveness) { |
| 54 | CFG *c = getCFG(); |
| 55 | if (!c) |
| 56 | return 0; |
| 57 | |
| 58 | liveness = new LiveVariables(D->getASTContext(), *c); |
| 59 | liveness->runOnCFG(*c); |
| 60 | liveness->runOnAllBlocks(*c, 0, true); |
| 61 | } |
| 62 | |
| 63 | return liveness; |
| 64 | } |
| 65 | |
| 66 | AnalysisContext *AnalysisContextManager::getContext(Decl *D) { |
| 67 | iterator I = Contexts.find(D); |
| 68 | if (I != Contexts.end()) |
| 69 | return &(I->second); |
| 70 | |
| 71 | AnalysisContext &Ctx = Contexts[D]; |
| 72 | Ctx.setDecl(D); |
| 73 | return &Ctx; |
| 74 | } |