Shih-wei Liao | f8fd82b | 2010-02-10 11:10:31 -0800 | [diff] [blame^] | 1 | //===--- 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 "clang/Frontend/AnalysisConsumer.h" |
| 15 | #include "clang/AST/ASTConsumer.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclCXX.h" |
| 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/ParentMap.h" |
| 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 21 | #include "clang/Analysis/Analyses/UninitializedValues.h" |
| 22 | #include "clang/Analysis/CFG.h" |
| 23 | #include "clang/Checker/Checkers/LocalCheckers.h" |
| 24 | #include "clang/Checker/ManagerRegistry.h" |
| 25 | #include "clang/Checker/BugReporter/PathDiagnostic.h" |
| 26 | #include "clang/Checker/PathSensitive/AnalysisManager.h" |
| 27 | #include "clang/Checker/BugReporter/BugReporter.h" |
| 28 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
| 29 | #include "clang/Checker/PathSensitive/GRTransferFuncs.h" |
| 30 | #include "clang/Basic/FileManager.h" |
| 31 | #include "clang/Basic/SourceManager.h" |
| 32 | #include "clang/Frontend/PathDiagnosticClients.h" |
| 33 | #include "clang/Lex/Preprocessor.h" |
| 34 | #include "llvm/Support/raw_ostream.h" |
| 35 | #include "llvm/System/Path.h" |
| 36 | #include "llvm/System/Program.h" |
| 37 | #include "llvm/ADT/OwningPtr.h" |
| 38 | |
| 39 | using namespace clang; |
| 40 | |
| 41 | static ExplodedNode::Auditor* CreateUbiViz(); |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | // Basic type definitions. |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | // Special PathDiagnosticClients. |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
| 51 | static PathDiagnosticClient* |
| 52 | CreatePlistHTMLDiagnosticClient(const std::string& prefix, |
| 53 | const Preprocessor &PP) { |
| 54 | llvm::sys::Path F(prefix); |
| 55 | PathDiagnosticClient *PD = CreateHTMLDiagnosticClient(F.getDirname(), PP); |
| 56 | return CreatePlistDiagnosticClient(prefix, PP, PD); |
| 57 | } |
| 58 | |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | // AnalysisConsumer declaration. |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
| 63 | namespace { |
| 64 | |
| 65 | class AnalysisConsumer : public ASTConsumer { |
| 66 | public: |
| 67 | typedef void (*CodeAction)(AnalysisConsumer &C, AnalysisManager &M, Decl *D); |
| 68 | |
| 69 | private: |
| 70 | typedef std::vector<CodeAction> Actions; |
| 71 | Actions FunctionActions; |
| 72 | Actions ObjCMethodActions; |
| 73 | Actions ObjCImplementationActions; |
| 74 | Actions TranslationUnitActions; |
| 75 | |
| 76 | public: |
| 77 | ASTContext* Ctx; |
| 78 | const Preprocessor &PP; |
| 79 | const std::string OutDir; |
| 80 | AnalyzerOptions Opts; |
| 81 | bool declDisplayed; |
| 82 | |
| 83 | |
| 84 | // PD is owned by AnalysisManager. |
| 85 | PathDiagnosticClient *PD; |
| 86 | |
| 87 | StoreManagerCreator CreateStoreMgr; |
| 88 | ConstraintManagerCreator CreateConstraintMgr; |
| 89 | |
| 90 | llvm::OwningPtr<AnalysisManager> Mgr; |
| 91 | |
| 92 | AnalysisConsumer(const Preprocessor& pp, |
| 93 | const std::string& outdir, |
| 94 | const AnalyzerOptions& opts) |
| 95 | : Ctx(0), PP(pp), OutDir(outdir), |
| 96 | Opts(opts), declDisplayed(false), PD(0) { |
| 97 | DigestAnalyzerOptions(); |
| 98 | } |
| 99 | |
| 100 | void DigestAnalyzerOptions() { |
| 101 | // Create the PathDiagnosticClient. |
| 102 | if (!OutDir.empty()) { |
| 103 | switch (Opts.AnalysisDiagOpt) { |
| 104 | default: |
| 105 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \ |
| 106 | case PD_##NAME: PD = CREATEFN(OutDir, PP); break; |
| 107 | #include "clang/Frontend/Analyses.def" |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Create the analyzer component creators. |
| 112 | if (ManagerRegistry::StoreMgrCreator != 0) { |
| 113 | CreateStoreMgr = ManagerRegistry::StoreMgrCreator; |
| 114 | } |
| 115 | else { |
| 116 | switch (Opts.AnalysisStoreOpt) { |
| 117 | default: |
| 118 | assert(0 && "Unknown store manager."); |
| 119 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \ |
| 120 | case NAME##Model: CreateStoreMgr = CREATEFN; break; |
| 121 | #include "clang/Frontend/Analyses.def" |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (ManagerRegistry::ConstraintMgrCreator != 0) |
| 126 | CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator; |
| 127 | else { |
| 128 | switch (Opts.AnalysisConstraintsOpt) { |
| 129 | default: |
| 130 | assert(0 && "Unknown store manager."); |
| 131 | #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \ |
| 132 | case NAME##Model: CreateConstraintMgr = CREATEFN; break; |
| 133 | #include "clang/Frontend/Analyses.def" |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void DisplayFunction(const Decl *D) { |
| 139 | if (!Opts.AnalyzerDisplayProgress || declDisplayed) |
| 140 | return; |
| 141 | |
| 142 | declDisplayed = true; |
| 143 | SourceManager &SM = Mgr->getASTContext().getSourceManager(); |
| 144 | PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); |
| 145 | llvm::errs() << "ANALYZE: " << Loc.getFilename(); |
| 146 | |
| 147 | if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { |
| 148 | const NamedDecl *ND = cast<NamedDecl>(D); |
| 149 | llvm::errs() << ' ' << ND->getNameAsString() << '\n'; |
| 150 | } |
| 151 | else if (isa<BlockDecl>(D)) { |
| 152 | llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:" |
| 153 | << Loc.getColumn() << '\n'; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void addCodeAction(CodeAction action) { |
| 158 | FunctionActions.push_back(action); |
| 159 | ObjCMethodActions.push_back(action); |
| 160 | } |
| 161 | |
| 162 | void addObjCImplementationAction(CodeAction action) { |
| 163 | ObjCImplementationActions.push_back(action); |
| 164 | } |
| 165 | |
| 166 | void addTranslationUnitAction(CodeAction action) { |
| 167 | TranslationUnitActions.push_back(action); |
| 168 | } |
| 169 | |
| 170 | virtual void Initialize(ASTContext &Context) { |
| 171 | Ctx = &Context; |
| 172 | Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(), |
| 173 | PP.getLangOptions(), PD, |
| 174 | CreateStoreMgr, CreateConstraintMgr, |
| 175 | Opts.VisualizeEGDot, Opts.VisualizeEGUbi, |
| 176 | Opts.PurgeDead, Opts.EagerlyAssume, |
| 177 | Opts.TrimGraph)); |
| 178 | } |
| 179 | |
| 180 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
| 181 | declDisplayed = false; |
| 182 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) |
| 183 | HandleTopLevelSingleDecl(*I); |
| 184 | } |
| 185 | |
| 186 | void HandleTopLevelSingleDecl(Decl *D); |
| 187 | virtual void HandleTranslationUnit(ASTContext &C); |
| 188 | |
| 189 | void HandleCode(Decl* D, Stmt* Body, Actions& actions); |
| 190 | }; |
| 191 | } // end anonymous namespace |
| 192 | |
| 193 | namespace llvm { |
| 194 | template <> struct FoldingSetTrait<AnalysisConsumer::CodeAction> { |
| 195 | static inline void Profile(AnalysisConsumer::CodeAction X, |
| 196 | FoldingSetNodeID& ID) { |
| 197 | ID.AddPointer(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(X))); |
| 198 | } |
| 199 | }; |
| 200 | } |
| 201 | |
| 202 | //===----------------------------------------------------------------------===// |
| 203 | // AnalysisConsumer implementation. |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | |
| 206 | void AnalysisConsumer::HandleTopLevelSingleDecl(Decl *D) { |
| 207 | switch (D->getKind()) { |
| 208 | case Decl::Function: { |
| 209 | FunctionDecl* FD = cast<FunctionDecl>(D); |
| 210 | |
| 211 | if (!Opts.AnalyzeSpecificFunction.empty() && |
| 212 | Opts.AnalyzeSpecificFunction != FD->getIdentifier()->getName()) |
| 213 | break; |
| 214 | |
| 215 | Stmt* Body = FD->getBody(); |
| 216 | if (Body) HandleCode(FD, Body, FunctionActions); |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | case Decl::ObjCMethod: { |
| 221 | ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D); |
| 222 | |
| 223 | if (Opts.AnalyzeSpecificFunction.size() > 0 && |
| 224 | Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString()) |
| 225 | return; |
| 226 | |
| 227 | Stmt* Body = MD->getBody(); |
| 228 | if (Body) HandleCode(MD, Body, ObjCMethodActions); |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | case Decl::CXXConstructor: |
| 233 | case Decl::CXXDestructor: |
| 234 | case Decl::CXXConversion: |
| 235 | case Decl::CXXMethod: { |
| 236 | CXXMethodDecl *CXXMD = cast<CXXMethodDecl>(D); |
| 237 | |
| 238 | if (Opts.AnalyzeSpecificFunction.size() > 0 && |
| 239 | Opts.AnalyzeSpecificFunction != CXXMD->getName()) |
| 240 | return; |
| 241 | |
| 242 | Stmt *Body = CXXMD->getBody(); |
| 243 | if (Body) HandleCode(CXXMD, Body, FunctionActions); |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | default: |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) { |
| 253 | |
| 254 | TranslationUnitDecl *TU = C.getTranslationUnitDecl(); |
| 255 | |
| 256 | if (!TranslationUnitActions.empty()) { |
| 257 | // Find the entry function definition (if any). |
| 258 | FunctionDecl *FD = 0; |
| 259 | // Must specify an entry function. |
| 260 | if (!Opts.AnalyzeSpecificFunction.empty()) { |
| 261 | for (DeclContext::decl_iterator I=TU->decls_begin(), E=TU->decls_end(); |
| 262 | I != E; ++I) { |
| 263 | if (FunctionDecl *fd = dyn_cast<FunctionDecl>(*I)) |
| 264 | if (fd->isThisDeclarationADefinition() && |
| 265 | fd->getNameAsString() == Opts.AnalyzeSpecificFunction) { |
| 266 | FD = fd; |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | if (FD) { |
| 273 | for (Actions::iterator I = TranslationUnitActions.begin(), |
| 274 | E = TranslationUnitActions.end(); I != E; ++I) |
| 275 | (*I)(*this, *Mgr, FD); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (!ObjCImplementationActions.empty()) { |
| 280 | for (DeclContext::decl_iterator I = TU->decls_begin(), |
| 281 | E = TU->decls_end(); |
| 282 | I != E; ++I) |
| 283 | if (ObjCImplementationDecl* ID = dyn_cast<ObjCImplementationDecl>(*I)) |
| 284 | HandleCode(ID, 0, ObjCImplementationActions); |
| 285 | } |
| 286 | |
| 287 | // Explicitly destroy the PathDiagnosticClient. This will flush its output. |
| 288 | // FIXME: This should be replaced with something that doesn't rely on |
| 289 | // side-effects in PathDiagnosticClient's destructor. This is required when |
| 290 | // used with option -disable-free. |
| 291 | Mgr.reset(NULL); |
| 292 | } |
| 293 | |
| 294 | static void FindBlocks(DeclContext *D, llvm::SmallVectorImpl<Decl*> &WL) { |
| 295 | if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 296 | WL.push_back(BD); |
| 297 | |
| 298 | for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); |
| 299 | I!=E; ++I) |
| 300 | if (DeclContext *DC = dyn_cast<DeclContext>(*I)) |
| 301 | FindBlocks(DC, WL); |
| 302 | } |
| 303 | |
| 304 | void AnalysisConsumer::HandleCode(Decl *D, Stmt* Body, Actions& actions) { |
| 305 | |
| 306 | // Don't run the actions if an error has occured with parsing the file. |
| 307 | if (PP.getDiagnostics().hasErrorOccurred()) |
| 308 | return; |
| 309 | |
| 310 | // Don't run the actions on declarations in header files unless |
| 311 | // otherwise specified. |
| 312 | if (!Opts.AnalyzeAll && |
| 313 | !Ctx->getSourceManager().isFromMainFile(D->getLocation())) |
| 314 | return; |
| 315 | |
| 316 | // Clear the AnalysisManager of old AnalysisContexts. |
| 317 | Mgr->ClearContexts(); |
| 318 | |
| 319 | // Dispatch on the actions. |
| 320 | llvm::SmallVector<Decl*, 10> WL; |
| 321 | WL.push_back(D); |
| 322 | |
| 323 | if (Body && Opts.AnalyzeNestedBlocks) |
| 324 | FindBlocks(cast<DeclContext>(D), WL); |
| 325 | |
| 326 | for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I) |
| 327 | for (llvm::SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end(); |
| 328 | WI != WE; ++WI) |
| 329 | (*I)(*this, *Mgr, *WI); |
| 330 | } |
| 331 | |
| 332 | //===----------------------------------------------------------------------===// |
| 333 | // Analyses |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
| 336 | static void ActionWarnDeadStores(AnalysisConsumer &C, AnalysisManager& mgr, |
| 337 | Decl *D) { |
| 338 | if (LiveVariables *L = mgr.getLiveVariables(D)) { |
| 339 | C.DisplayFunction(D); |
| 340 | BugReporter BR(mgr); |
| 341 | CheckDeadStores(*mgr.getCFG(D), *L, mgr.getParentMap(D), BR); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static void ActionWarnUninitVals(AnalysisConsumer &C, AnalysisManager& mgr, |
| 346 | Decl *D) { |
| 347 | if (CFG* c = mgr.getCFG(D)) { |
| 348 | C.DisplayFunction(D); |
| 349 | CheckUninitializedValues(*c, mgr.getASTContext(), mgr.getDiagnostic()); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | |
| 354 | static void ActionGRExprEngine(AnalysisConsumer &C, AnalysisManager& mgr, |
| 355 | Decl *D, |
| 356 | GRTransferFuncs* tf) { |
| 357 | |
| 358 | llvm::OwningPtr<GRTransferFuncs> TF(tf); |
| 359 | |
| 360 | // Display progress. |
| 361 | C.DisplayFunction(D); |
| 362 | |
| 363 | // Construct the analysis engine. We first query for the LiveVariables |
| 364 | // information to see if the CFG is valid. |
| 365 | // FIXME: Inter-procedural analysis will need to handle invalid CFGs. |
| 366 | if (!mgr.getLiveVariables(D)) |
| 367 | return; |
| 368 | |
| 369 | GRExprEngine Eng(mgr, TF.take()); |
| 370 | |
| 371 | if (C.Opts.EnableExperimentalInternalChecks) |
| 372 | RegisterExperimentalInternalChecks(Eng); |
| 373 | |
| 374 | RegisterAppleChecks(Eng, *D); |
| 375 | |
| 376 | if (C.Opts.EnableExperimentalChecks) |
| 377 | RegisterExperimentalChecks(Eng); |
| 378 | |
| 379 | // Set the graph auditor. |
| 380 | llvm::OwningPtr<ExplodedNode::Auditor> Auditor; |
| 381 | if (mgr.shouldVisualizeUbigraph()) { |
| 382 | Auditor.reset(CreateUbiViz()); |
| 383 | ExplodedNode::SetAuditor(Auditor.get()); |
| 384 | } |
| 385 | |
| 386 | // Execute the worklist algorithm. |
| 387 | Eng.ExecuteWorkList(mgr.getStackFrame(D)); |
| 388 | |
| 389 | // Release the auditor (if any) so that it doesn't monitor the graph |
| 390 | // created BugReporter. |
| 391 | ExplodedNode::SetAuditor(0); |
| 392 | |
| 393 | // Visualize the exploded graph. |
| 394 | if (mgr.shouldVisualizeGraphviz()) |
| 395 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
| 396 | |
| 397 | // Display warnings. |
| 398 | Eng.getBugReporter().FlushReports(); |
| 399 | } |
| 400 | |
| 401 | static void ActionObjCMemCheckerAux(AnalysisConsumer &C, AnalysisManager& mgr, |
| 402 | Decl *D, bool GCEnabled) { |
| 403 | |
| 404 | GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getASTContext(), |
| 405 | GCEnabled, |
| 406 | mgr.getLangOptions()); |
| 407 | |
| 408 | ActionGRExprEngine(C, mgr, D, TF); |
| 409 | } |
| 410 | |
| 411 | static void ActionObjCMemChecker(AnalysisConsumer &C, AnalysisManager& mgr, |
| 412 | Decl *D) { |
| 413 | |
| 414 | switch (mgr.getLangOptions().getGCMode()) { |
| 415 | default: |
| 416 | assert (false && "Invalid GC mode."); |
| 417 | case LangOptions::NonGC: |
| 418 | ActionObjCMemCheckerAux(C, mgr, D, false); |
| 419 | break; |
| 420 | |
| 421 | case LangOptions::GCOnly: |
| 422 | ActionObjCMemCheckerAux(C, mgr, D, true); |
| 423 | break; |
| 424 | |
| 425 | case LangOptions::HybridGC: |
| 426 | ActionObjCMemCheckerAux(C, mgr, D, false); |
| 427 | ActionObjCMemCheckerAux(C, mgr, D, true); |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | static void ActionDisplayLiveVariables(AnalysisConsumer &C, |
| 433 | AnalysisManager& mgr, Decl *D) { |
| 434 | if (LiveVariables* L = mgr.getLiveVariables(D)) { |
| 435 | C.DisplayFunction(D); |
| 436 | L->dumpBlockLiveness(mgr.getSourceManager()); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | static void ActionCFGDump(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) { |
| 441 | if (CFG *cfg = mgr.getCFG(D)) { |
| 442 | C.DisplayFunction(D); |
| 443 | cfg->dump(mgr.getLangOptions()); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | static void ActionCFGView(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) { |
| 448 | if (CFG *cfg = mgr.getCFG(D)) { |
| 449 | C.DisplayFunction(D); |
| 450 | cfg->viewCFG(mgr.getLangOptions()); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | static void ActionSecuritySyntacticChecks(AnalysisConsumer &C, |
| 455 | AnalysisManager &mgr, Decl *D) { |
| 456 | C.DisplayFunction(D); |
| 457 | BugReporter BR(mgr); |
| 458 | CheckSecuritySyntaxOnly(D, BR); |
| 459 | } |
| 460 | |
| 461 | static void ActionWarnObjCDealloc(AnalysisConsumer &C, AnalysisManager& mgr, |
| 462 | Decl *D) { |
| 463 | if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 464 | return; |
| 465 | |
| 466 | C.DisplayFunction(D); |
| 467 | BugReporter BR(mgr); |
| 468 | CheckObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOptions(), BR); |
| 469 | } |
| 470 | |
| 471 | static void ActionWarnObjCUnusedIvars(AnalysisConsumer &C, AnalysisManager& mgr, |
| 472 | Decl *D) { |
| 473 | C.DisplayFunction(D); |
| 474 | BugReporter BR(mgr); |
| 475 | CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(D), BR); |
| 476 | } |
| 477 | |
| 478 | static void ActionWarnObjCMethSigs(AnalysisConsumer &C, AnalysisManager& mgr, |
| 479 | Decl *D) { |
| 480 | C.DisplayFunction(D); |
| 481 | BugReporter BR(mgr); |
| 482 | CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(D), BR); |
| 483 | } |
| 484 | |
| 485 | static void ActionWarnSizeofPointer(AnalysisConsumer &C, AnalysisManager &mgr, |
| 486 | Decl *D) { |
| 487 | C.DisplayFunction(D); |
| 488 | BugReporter BR(mgr); |
| 489 | CheckSizeofPointer(D, BR); |
| 490 | } |
| 491 | |
| 492 | static void ActionInlineCall(AnalysisConsumer &C, AnalysisManager &mgr, |
| 493 | Decl *D) { |
| 494 | // FIXME: This is largely copy of ActionGRExprEngine. Needs cleanup. |
| 495 | // Display progress. |
| 496 | C.DisplayFunction(D); |
| 497 | |
| 498 | // FIXME: Make a fake transfer function. The GRTransferFunc interface |
| 499 | // eventually will be removed. |
| 500 | GRExprEngine Eng(mgr, new GRTransferFuncs()); |
| 501 | |
| 502 | if (C.Opts.EnableExperimentalInternalChecks) |
| 503 | RegisterExperimentalInternalChecks(Eng); |
| 504 | |
| 505 | RegisterAppleChecks(Eng, *D); |
| 506 | |
| 507 | if (C.Opts.EnableExperimentalChecks) |
| 508 | RegisterExperimentalChecks(Eng); |
| 509 | |
| 510 | // Register call inliner as the last checker. |
| 511 | RegisterCallInliner(Eng); |
| 512 | |
| 513 | // Execute the worklist algorithm. |
| 514 | Eng.ExecuteWorkList(mgr.getStackFrame(D)); |
| 515 | |
| 516 | // Visualize the exploded graph. |
| 517 | if (mgr.shouldVisualizeGraphviz()) |
| 518 | Eng.ViewGraph(mgr.shouldTrimGraph()); |
| 519 | |
| 520 | // Display warnings. |
| 521 | Eng.getBugReporter().FlushReports(); |
| 522 | } |
| 523 | |
| 524 | //===----------------------------------------------------------------------===// |
| 525 | // AnalysisConsumer creation. |
| 526 | //===----------------------------------------------------------------------===// |
| 527 | |
| 528 | ASTConsumer* clang::CreateAnalysisConsumer(const Preprocessor& pp, |
| 529 | const std::string& OutDir, |
| 530 | const AnalyzerOptions& Opts) { |
| 531 | llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(pp, OutDir, Opts)); |
| 532 | |
| 533 | for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i) |
| 534 | switch (Opts.AnalysisList[i]) { |
| 535 | #define ANALYSIS(NAME, CMD, DESC, SCOPE)\ |
| 536 | case NAME:\ |
| 537 | C->add ## SCOPE ## Action(&Action ## NAME);\ |
| 538 | break; |
| 539 | #include "clang/Frontend/Analyses.def" |
| 540 | default: break; |
| 541 | } |
| 542 | |
| 543 | // Last, disable the effects of '-Werror' when using the AnalysisConsumer. |
| 544 | pp.getDiagnostics().setWarningsAsErrors(false); |
| 545 | |
| 546 | return C.take(); |
| 547 | } |
| 548 | |
| 549 | //===----------------------------------------------------------------------===// |
| 550 | // Ubigraph Visualization. FIXME: Move to separate file. |
| 551 | //===----------------------------------------------------------------------===// |
| 552 | |
| 553 | namespace { |
| 554 | |
| 555 | class UbigraphViz : public ExplodedNode::Auditor { |
| 556 | llvm::OwningPtr<llvm::raw_ostream> Out; |
| 557 | llvm::sys::Path Dir, Filename; |
| 558 | unsigned Cntr; |
| 559 | |
| 560 | typedef llvm::DenseMap<void*,unsigned> VMap; |
| 561 | VMap M; |
| 562 | |
| 563 | public: |
| 564 | UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
| 565 | llvm::sys::Path& filename); |
| 566 | |
| 567 | ~UbigraphViz(); |
| 568 | |
| 569 | virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst); |
| 570 | }; |
| 571 | |
| 572 | } // end anonymous namespace |
| 573 | |
| 574 | static ExplodedNode::Auditor* CreateUbiViz() { |
| 575 | std::string ErrMsg; |
| 576 | |
| 577 | llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); |
| 578 | if (!ErrMsg.empty()) |
| 579 | return 0; |
| 580 | |
| 581 | llvm::sys::Path Filename = Dir; |
| 582 | Filename.appendComponent("llvm_ubi"); |
| 583 | Filename.makeUnique(true,&ErrMsg); |
| 584 | |
| 585 | if (!ErrMsg.empty()) |
| 586 | return 0; |
| 587 | |
| 588 | llvm::errs() << "Writing '" << Filename.str() << "'.\n"; |
| 589 | |
| 590 | llvm::OwningPtr<llvm::raw_fd_ostream> Stream; |
| 591 | Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg)); |
| 592 | |
| 593 | if (!ErrMsg.empty()) |
| 594 | return 0; |
| 595 | |
| 596 | return new UbigraphViz(Stream.take(), Dir, Filename); |
| 597 | } |
| 598 | |
| 599 | void UbigraphViz::AddEdge(ExplodedNode* Src, ExplodedNode* Dst) { |
| 600 | |
| 601 | assert (Src != Dst && "Self-edges are not allowed."); |
| 602 | |
| 603 | // Lookup the Src. If it is a new node, it's a root. |
| 604 | VMap::iterator SrcI= M.find(Src); |
| 605 | unsigned SrcID; |
| 606 | |
| 607 | if (SrcI == M.end()) { |
| 608 | M[Src] = SrcID = Cntr++; |
| 609 | *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; |
| 610 | } |
| 611 | else |
| 612 | SrcID = SrcI->second; |
| 613 | |
| 614 | // Lookup the Dst. |
| 615 | VMap::iterator DstI= M.find(Dst); |
| 616 | unsigned DstID; |
| 617 | |
| 618 | if (DstI == M.end()) { |
| 619 | M[Dst] = DstID = Cntr++; |
| 620 | *Out << "('vertex', " << DstID << ")\n"; |
| 621 | } |
| 622 | else { |
| 623 | // We have hit DstID before. Change its style to reflect a cache hit. |
| 624 | DstID = DstI->second; |
| 625 | *Out << "('change_vertex_style', " << DstID << ", 1)\n"; |
| 626 | } |
| 627 | |
| 628 | // Add the edge. |
| 629 | *Out << "('edge', " << SrcID << ", " << DstID |
| 630 | << ", ('arrow','true'), ('oriented', 'true'))\n"; |
| 631 | } |
| 632 | |
| 633 | UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, |
| 634 | llvm::sys::Path& filename) |
| 635 | : Out(out), Dir(dir), Filename(filename), Cntr(0) { |
| 636 | |
| 637 | *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; |
| 638 | *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," |
| 639 | " ('size', '1.5'))\n"; |
| 640 | } |
| 641 | |
| 642 | UbigraphViz::~UbigraphViz() { |
| 643 | Out.reset(0); |
| 644 | llvm::errs() << "Running 'ubiviz' program... "; |
| 645 | std::string ErrMsg; |
| 646 | llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); |
| 647 | std::vector<const char*> args; |
| 648 | args.push_back(Ubiviz.c_str()); |
| 649 | args.push_back(Filename.c_str()); |
| 650 | args.push_back(0); |
| 651 | |
| 652 | if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { |
| 653 | llvm::errs() << "Error viewing graph: " << ErrMsg << "\n"; |
| 654 | } |
| 655 | |
| 656 | // Delete the directory. |
| 657 | Dir.eraseFromDisk(true); |
| 658 | } |