blob: b92eb24d7c98622e8d1686af8700d348d20b1560 [file] [log] [blame]
Zhongxing Xu97ab3942009-07-30 01:17:21 +00001//== 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 Stump87a05f12009-07-31 01:10:29 +000021#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000022
23using namespace clang;
24
25AnalysisContext::~AnalysisContext() {
26 delete cfg;
27 delete liveness;
28 delete PM;
29}
30
31Stmt *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 Stump87a05f12009-07-31 01:10:29 +000037 llvm::llvm_unreachable("unknown code decl");
Zhongxing Xu97ab3942009-07-30 01:17:21 +000038}
39
40CFG *AnalysisContext::getCFG() {
41 if (!cfg)
42 cfg = CFG::buildCFG(getBody(), &D->getASTContext());
43 return cfg;
44}
45
46ParentMap &AnalysisContext::getParentMap() {
47 if (!PM)
48 PM = new ParentMap(getBody());
49 return *PM;
50}
51
52LiveVariables *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
66AnalysisContext *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}