blob: 2f2e76e67718ef732ccbddb6e80522614eca7979 [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"
Ted Kremenekb35a74a2008-07-02 00:44:58 +000015#include "HTMLDiagnostics.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000016#include "clang/AST/ASTConsumer.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclObjC.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/ADT/ImmutableList.h"
21#include "llvm/ADT/OwningPtr.h"
22#include "clang/AST/CFG.h"
23#include "clang/Analysis/Analyses/LiveVariables.h"
24#include "clang/Analysis/PathDiagnostic.h"
25#include "clang/Basic/SourceManager.h"
26#include "clang/Basic/FileManager.h"
27#include "clang/AST/ParentMap.h"
28#include "clang/Analysis/Analyses/LiveVariables.h"
29#include "clang/Analysis/LocalCheckers.h"
30#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
31#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek34d77342008-07-02 16:49:11 +000032#include "llvm/Support/Streams.h"
Ted Kremenekf4381fd2008-07-02 00:03:09 +000033
34using namespace clang;
35
36
37//===----------------------------------------------------------------------===//
38// Basic type definitions.
39//===----------------------------------------------------------------------===//
40
41namespace {
42
43 class AnalysisManager;
44 typedef void (*CodeAction)(AnalysisManager& Mgr);
45
46} // end anonymous namespace
47
48//===----------------------------------------------------------------------===//
49// AnalysisConsumer declaration.
50//===----------------------------------------------------------------------===//
51
52namespace {
53
54 class VISIBILITY_HIDDEN AnalysisConsumer : public ASTConsumer {
55 typedef llvm::ImmutableList<CodeAction> Actions;
56 Actions FunctionActions;
57 Actions ObjCMethodActions;
58
59 Actions::Factory F;
60
61 public:
62 const bool Visualize;
63 const bool TrimGraph;
64 const LangOptions& LOpts;
65 Diagnostic &Diags;
66 ASTContext* Ctx;
67 Preprocessor* PP;
68 PreprocessorFactory* PPF;
69 const std::string HTMLDir;
70 const std::string FName;
71 llvm::OwningPtr<PathDiagnosticClient> PD;
72 bool AnalyzeAll;
73
74 AnalysisConsumer(Diagnostic &diags, Preprocessor* pp,
75 PreprocessorFactory* ppf,
76 const LangOptions& lopts,
77 const std::string& fname,
78 const std::string& htmldir,
79 bool visualize, bool trim, bool analyzeAll)
80 : FunctionActions(F.GetEmptyList()), ObjCMethodActions(F.GetEmptyList()),
81 Visualize(visualize), TrimGraph(trim), LOpts(lopts), Diags(diags),
82 Ctx(0), PP(pp), PPF(ppf),
83 HTMLDir(htmldir),
84 FName(fname),
85 AnalyzeAll(analyzeAll) {}
86
87 void addCodeAction(CodeAction action) {
88 FunctionActions = F.Concat(action, FunctionActions);
89 ObjCMethodActions = F.Concat(action, ObjCMethodActions);
90 }
91
92 virtual void Initialize(ASTContext &Context) {
93 Ctx = &Context;
94 }
95
96 virtual void HandleTopLevelDecl(Decl *D);
97 void HandleCode(Decl* D, Stmt* Body, Actions actions);
98 };
99
100
101 class VISIBILITY_HIDDEN AnalysisManager {
102 Decl* D;
103 Stmt* Body;
104 AnalysisConsumer& C;
Ted Kremenek34d77342008-07-02 16:49:11 +0000105 bool DisplayedFunction;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000106
107 llvm::OwningPtr<CFG> cfg;
108 llvm::OwningPtr<LiveVariables> liveness;
109 llvm::OwningPtr<ParentMap> PM;
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000110 llvm::OwningPtr<PathDiagnosticClient> PD;
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000111
112 public:
113 AnalysisManager(AnalysisConsumer& c, Decl* d, Stmt* b)
Ted Kremenek34d77342008-07-02 16:49:11 +0000114 : D(d), Body(b), C(c), DisplayedFunction(false) {}
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000115
116
117 Decl* getCodeDecl() const { return D; }
118 Stmt* getBody() const { return Body; }
119
120 CFG* getCFG() {
121 if (!cfg) cfg.reset(CFG::buildCFG(getBody()));
122 return cfg.get();
123 }
124
125 ParentMap* getParentMap() {
126 if (!PM) PM.reset(new ParentMap(getBody()));
127 return PM.get();
128 }
129
130 ASTContext& getContext() {
131 return *C.Ctx;
132 }
133
134 Diagnostic& getDiagnostic() {
135 return C.Diags;
136 }
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000137
138 const LangOptions& getLangOptions() const {
139 return C.LOpts;
140 }
141
142 PathDiagnosticClient* getPathDiagnosticClient() {
143 if (PD.get() == 0 && !C.HTMLDir.empty())
144 PD.reset(CreateHTMLDiagnosticClient(C.HTMLDir, C.PP, C.PPF));
145
146 return PD.get();
147 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000148
149 LiveVariables* getLiveVariables() {
150 if (!liveness) liveness.reset(new LiveVariables(*getCFG()));
151 return liveness.get();
152 }
Ted Kremenek34d77342008-07-02 16:49:11 +0000153
154 bool shouldVisualize() const {
155 return C.Visualize;
156 }
157
158 bool shouldTrimGraph() const {
159 return C.TrimGraph;
160 }
161
162 void DisplayFunction() {
163
164 if (DisplayedFunction)
165 return;
166
167 DisplayedFunction = true;
168
169 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(getCodeDecl())) {
170 llvm::cerr << "ANALYZE: "
171 << getContext().getSourceManager().getSourceName(FD->getLocation())
172 << ' '
173 << FD->getIdentifier()->getName()
174 << '\n';
175 }
176 else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(getCodeDecl())) {
177 llvm::cerr << "ANALYZE (ObjC Method): "
178 << getContext().getSourceManager().getSourceName(MD->getLocation())
179 << " '"
180 << MD->getSelector().getName() << "'\n";
181 }
182 }
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000183 };
184
185} // end anonymous namespace
186
187namespace llvm {
188 template <> struct FoldingSetTrait<CodeAction> {
189 static inline void Profile(CodeAction X, FoldingSetNodeID& ID) {
190 ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X)));
191 }
192 };
193}
194
195//===----------------------------------------------------------------------===//
196// AnalysisConsumer implementation.
197//===----------------------------------------------------------------------===//
198
199void AnalysisConsumer::HandleTopLevelDecl(Decl *D) {
200 switch (D->getKind()) {
201 case Decl::Function: {
202 FunctionDecl* FD = cast<FunctionDecl>(D);
203 Stmt* Body = FD->getBody();
204 if (Body) HandleCode(FD, Body, FunctionActions);
205 break;
206 }
207
208 case Decl::ObjCMethod: {
209 ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D);
210 Stmt* Body = MD->getBody();
211 if (Body) HandleCode(MD, Body, ObjCMethodActions);
212 break;
213 }
214
215 default:
216 break;
217 }
218}
219
220void AnalysisConsumer::HandleCode(Decl* D, Stmt* Body, Actions actions) {
221
222 // Don't run the actions if an error has occured with parsing the file.
223 if (Diags.hasErrorOccurred())
224 return;
225
226 SourceLocation Loc = D->getLocation();
227
228 // Only run actions on declarations defined in actual source.
229 if (!Loc.isFileID())
230 return;
231
232 // Don't run the actions on declarations in header files unless
233 // otherwise specified.
234 if (!AnalyzeAll && !Ctx->getSourceManager().isFromMainFile(Loc))
235 return;
236
237 // Create an AnalysisManager that will manage the state for analyzing
238 // this method/function.
239 AnalysisManager mgr(*this, D, Body);
240
241 // Dispatch on the actions.
242 for (Actions::iterator I = actions.begin(),
243 E = actions.end(); I != E; ++I)
244 ((*I).getHead())(mgr);
245}
246
247//===----------------------------------------------------------------------===//
248// Analyses
249//===----------------------------------------------------------------------===//
250
251static void ActionDeadStores(AnalysisManager& mgr) {
252 CheckDeadStores(*mgr.getCFG(), mgr.getContext(), *mgr.getParentMap(),
253 mgr.getDiagnostic());
254}
255
256static void ActionUninitVals(AnalysisManager& mgr) {
257 CheckUninitializedValues(*mgr.getCFG(), mgr.getContext(),
258 mgr.getDiagnostic());
259}
260
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000261
Ted Kremenekbc46f342008-07-02 16:35:50 +0000262static void ActionGRExprEngine(AnalysisManager& mgr, GRTransferFuncs* tf) {
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000263
Ted Kremenekbc46f342008-07-02 16:35:50 +0000264 llvm::OwningPtr<GRTransferFuncs> TF(tf);
265
Ted Kremenek34d77342008-07-02 16:49:11 +0000266 // Display progress.
267 if (!mgr.shouldVisualize())
268 mgr.DisplayFunction();
269
Ted Kremenekbc46f342008-07-02 16:35:50 +0000270 // Construct the analysis engine.
271 GRExprEngine Eng(*mgr.getCFG(), *mgr.getCodeDecl(), mgr.getContext());
272 Eng.setTransferFunctions(tf);
273
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000274 // Execute the worklist algorithm.
275 Eng.ExecuteWorkList();
Ted Kremenekbc46f342008-07-02 16:35:50 +0000276
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000277 // Display warnings.
Ted Kremenek34d77342008-07-02 16:49:11 +0000278 Eng.EmitWarnings(mgr.getDiagnostic(), mgr.getPathDiagnosticClient());
279
280 // Visualize the exploded graph.
281 if (mgr.shouldVisualize())
282 Eng.ViewGraph(mgr.shouldTrimGraph());
Ted Kremenekbc46f342008-07-02 16:35:50 +0000283}
284
285static void ActionRefLeakCheckerAux(AnalysisManager& mgr, bool GCEnabled,
286 bool StandardWarnings) {
287
288 GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getContext(),
289 GCEnabled,
290 StandardWarnings,
291 mgr.getLangOptions());
292
293 ActionGRExprEngine(mgr, TF);
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000294}
295
296static void ActionRefLeakChecker(AnalysisManager& mgr) {
297
298 switch (mgr.getLangOptions().getGCMode()) {
299 default:
300 assert (false && "Invalid GC mode.");
301 case LangOptions::NonGC:
302 ActionRefLeakCheckerAux(mgr, false, true);
303 break;
304
305 case LangOptions::GCOnly:
306 ActionRefLeakCheckerAux(mgr, true, true);
307 break;
308
309 case LangOptions::HybridGC:
310 ActionRefLeakCheckerAux(mgr, false, true);
311 ActionRefLeakCheckerAux(mgr, true, false);
312 break;
313 }
314}
315
Ted Kremenekbc46f342008-07-02 16:35:50 +0000316static void ActionSimpleChecks(AnalysisManager& mgr) {
317 ActionGRExprEngine(mgr, MakeGRSimpleValsTF());
318}
319
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000320//===----------------------------------------------------------------------===//
321// AnalysisConsumer creation.
322//===----------------------------------------------------------------------===//
323
324ASTConsumer* clang::CreateAnalysisConsumer(Analyses* Beg, Analyses* End,
325 Diagnostic &diags, Preprocessor* pp,
326 PreprocessorFactory* ppf,
327 const LangOptions& lopts,
328 const std::string& fname,
329 const std::string& htmldir,
330 bool visualize, bool trim,
331 bool analyzeAll) {
332
333 llvm::OwningPtr<AnalysisConsumer>
334 C(new AnalysisConsumer(diags, pp, ppf, lopts, fname, htmldir,
335 visualize, trim, analyzeAll));
336
337 for ( ; Beg != End ; ++Beg)
338 switch (*Beg) {
339 case WarnDeadStores:
340 C->addCodeAction(&ActionDeadStores);
341 break;
342
343 case WarnUninitVals:
344 C->addCodeAction(&ActionUninitVals);
345 break;
346
Ted Kremenekb35a74a2008-07-02 00:44:58 +0000347 case CheckerCFRef:
348 C->addCodeAction(&ActionRefLeakChecker);
349 break;
350
Ted Kremenekbc46f342008-07-02 16:35:50 +0000351 case CheckerSimple:
352 C->addCodeAction(&ActionSimpleChecks);
353 break;
354
Ted Kremenekf4381fd2008-07-02 00:03:09 +0000355 default: break;
356 }
357
358 return C.take();
359}
360