blob: 11fb4a95c1f149252bef2576dbe02ad8f66b4f03 [file] [log] [blame]
Ted Kremenekf4381fd2008-07-02 00:03:09 +00001//===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===//
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// "Meta" ASTConsumer for running different source analyses.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ASTConsumers.h"
15#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclObjC.h"
18#include "llvm/Support/Compiler.h"
19#include "llvm/ADT/ImmutableList.h"
20#include "llvm/ADT/OwningPtr.h"
21#include "clang/AST/CFG.h"
22#include "clang/Analysis/Analyses/LiveVariables.h"
23#include "clang/Analysis/PathDiagnostic.h"
24#include "clang/Basic/SourceManager.h"
25#include "clang/Basic/FileManager.h"
26#include "clang/AST/ParentMap.h"
27#include "clang/Analysis/Analyses/LiveVariables.h"
28#include "clang/Analysis/LocalCheckers.h"
29#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
30#include "clang/Analysis/PathSensitive/GRExprEngine.h"
31
32using namespace clang;
33
34
35//===----------------------------------------------------------------------===//
36// Basic type definitions.
37//===----------------------------------------------------------------------===//
38
39namespace {
40
41 class AnalysisManager;
42 typedef void (*CodeAction)(AnalysisManager& Mgr);
43
44} // end anonymous namespace
45
46//===----------------------------------------------------------------------===//
47// AnalysisConsumer declaration.
48//===----------------------------------------------------------------------===//
49
50namespace {
51
52 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
53 typedef llvm::ImmutableList<CodeAction> Actions;
54 Actions FunctionActions;
55 Actions ObjCMethodActions;
56
57 Actions::Factory F;
58
59 public:
60 const bool Visualize;
61 const bool TrimGraph;
62 const LangOptions& LOpts;
63 Diagnostic &Diags;
64 ASTContext* Ctx;
65 Preprocessor* PP;
66 PreprocessorFactory* PPF;
67 const std::string HTMLDir;
68 const std::string FName;
69 llvm::OwningPtr<PathDiagnosticClient> PD;
70 bool AnalyzeAll;
71
72 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
73 PreprocessorFactory* ppf,
74 const LangOptions& lopts,
75 const std::string& fname,
76 const std::string& htmldir,
77 bool visualize, bool trim, bool analyzeAll)
78 : FunctionActions(F.GetEmptyList()), ObjCMethodActions(F.GetEmptyList()),
79 Visualize(visualize), TrimGraph(trim), LOpts(lopts), Diags(diags),
80 Ctx(0), PP(pp), PPF(ppf),
81 HTMLDir(htmldir),
82 FName(fname),
83 AnalyzeAll(analyzeAll) {}
84
85 void addCodeAction(CodeAction action) {
86 FunctionActions = F.Concat(action, FunctionActions);
87 ObjCMethodActions = F.Concat(action, ObjCMethodActions);
88 }
89
90 virtual void Initialize(ASTContext &Context) {
91 Ctx = &Context;
92 }
93
94 virtual void HandleTopLevelDecl(Decl *D);
95 void HandleCode(Decl* D, Stmt* Body, Actions actions);
96 };
97
98
99 class VISIBILITY_HIDDEN AnalysisManager {
100 Decl* D;
101 Stmt* Body;
102 AnalysisConsumer& C;
103
104 llvm::OwningPtr<CFG> cfg;
105 llvm::OwningPtr<LiveVariables> liveness;
106 llvm::OwningPtr<ParentMap> PM;
107
108 public:
109 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b)
110 : D(d), Body(b), C(c) {}
111
112
113 Decl* getCodeDecl() const { return D; }
114 Stmt* getBody() const { return Body; }
115
116 CFG* getCFG() {
117 if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
118 return cfg.get();
119 }
120
121 ParentMap* getParentMap() {
122 if (!PM) PM.reset(new ParentMap(getBody()));
123 return PM.get();
124 }
125
126 ASTContext& getContext() {
127 return *C.Ctx;
128 }
129
130 Diagnostic& getDiagnostic() {
131 return C.Diags;
132 }
133
134 LiveVariables* getLiveVariables() {
135 if (!liveness) liveness.reset(new LiveVariables(*getCFG()));
136 return liveness.get();
137 }
138 };
139
140} // end anonymous namespace
141
142namespace llvm {
143 template <> struct FoldingSetTrait<CodeAction> {
144 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
145 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
146 }
147 };
148}
149
150//===----------------------------------------------------------------------===//
151// AnalysisConsumer implementation.
152//===----------------------------------------------------------------------===//
153
154void AnalysisConsumer::HandleTopLevelDecl(Decl *D) {
155 switch (D->getKind()) {
156 case Decl::Function: {
157 FunctionDecl* FD = cast<FunctionDecl>(D);
158 Stmt* Body = FD->getBody();
159 if (Body) HandleCode(FD, Body, FunctionActions);
160 break;
161 }
162
163 case Decl::ObjCMethod: {
164 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
165 Stmt* Body = MD->getBody();
166 if (Body) HandleCode(MD, Body, ObjCMethodActions);
167 break;
168 }
169
170 default:
171 break;
172 }
173}
174
175void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions actions) {
176
177 // Don't run the actions if an error has occured with parsing the file.
178 if (Diags.hasErrorOccurred())
179 return;
180
181 SourceLocation Loc = D->getLocation();
182
183 // Only run actions on declarations defined in actual source.
184 if (!Loc.isFileID())
185 return;
186
187 // Don't run the actions on declarations in header files unless
188 // otherwise specified.
189 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
190 return;
191
192 // Create an AnalysisManager that will manage the state for analyzing
193 // this method/function.
194 AnalysisManager mgr(*this, D, Body);
195
196 // Dispatch on the actions.
197 for (Actions::iterator I = actions.begin(),
198 E = actions.end(); I != E; ++I)
199 ((*I).getHead())(mgr);
200}
201
202//===----------------------------------------------------------------------===//
203// Analyses
204//===----------------------------------------------------------------------===//
205
206static void ActionDeadStores(AnalysisManager& mgr) {
207 CheckDeadStores(*mgr.getCFG(), mgr.getContext(), *mgr.getParentMap(),
208 mgr.getDiagnostic());
209}
210
211static void ActionUninitVals(AnalysisManager& mgr) {
212 CheckUninitializedValues(*mgr.getCFG(), mgr.getContext(),
213 mgr.getDiagnostic());
214}
215
216//===----------------------------------------------------------------------===//
217// AnalysisConsumer creation.
218//===----------------------------------------------------------------------===//
219
220ASTConsumer* clang::CreateAnalysisConsumer(Analyses* Beg, Analyses* End,
221 Diagnostic &diags, Preprocessor* pp,
222 PreprocessorFactory* ppf,
223 const LangOptions& lopts,
224 const std::string& fname,
225 const std::string& htmldir,
226 bool visualize, bool trim,
227 bool analyzeAll) {
228
229 llvm::OwningPtr<AnalysisConsumer>
230 C(new AnalysisConsumer(diags, pp, ppf, lopts, fname, htmldir,
231 visualize, trim, analyzeAll));
232
233 for ( ; Beg != End ; ++Beg)
234 switch (*Beg) {
235 case WarnDeadStores:
236 C->addCodeAction(&ActionDeadStores);
237 break;
238
239 case WarnUninitVals:
240 C->addCodeAction(&ActionUninitVals);
241 break;
242
243 default: break;
244 }
245
246 return C.take();
247}
248