| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h" |
| 14 | #include "ModelInjector.h" |
| 15 | #include "clang/AST/Decl.h" |
| 16 | #include "clang/AST/DeclCXX.h" |
| 17 | #include "clang/AST/DeclObjC.h" |
| 18 | #include "clang/AST/RecursiveASTVisitor.h" |
| 19 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 20 | #include "clang/Analysis/CFG.h" |
| 21 | #include "clang/Analysis/CallGraph.h" |
| 22 | #include "clang/Analysis/CodeInjector.h" |
| 23 | #include "clang/Basic/SourceManager.h" |
| 24 | #include "clang/CrossTU/CrossTranslationUnit.h" |
| 25 | #include "clang/Frontend/CompilerInstance.h" |
| 26 | #include "clang/Lex/Preprocessor.h" |
| 27 | #include "clang/StaticAnalyzer/Checkers/LocalCheckers.h" |
| 28 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" |
| 29 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 30 | #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" |
| 31 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 32 | #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h" |
| 33 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
| 34 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
| 35 | #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h" |
| 36 | #include "llvm/ADT/PostOrderIterator.h" |
| 37 | #include "llvm/ADT/Statistic.h" |
| 38 | #include "llvm/Support/FileSystem.h" |
| 39 | #include "llvm/Support/Path.h" |
| 40 | #include "llvm/Support/Program.h" |
| 41 | #include "llvm/Support/Timer.h" |
| 42 | #include "llvm/Support/raw_ostream.h" |
| 43 | #include <memory> |
| 44 | #include <queue> |
| 45 | #include <utility> |
| 46 | |
| 47 | using namespace clang; |
| 48 | using namespace ento; |
| 49 | |
| 50 | #define DEBUG_TYPE "AnalysisConsumer" |
| 51 | |
| 52 | STATISTIC(NumFunctionTopLevel, "The # of functions at top level."); |
| 53 | STATISTIC(NumFunctionsAnalyzed, |
| 54 | "The # of functions and blocks analyzed (as top level " |
| 55 | "with inlining turned on)."); |
| 56 | STATISTIC(NumBlocksInAnalyzedFunctions, |
| 57 | "The # of basic blocks in the analyzed functions."); |
| 58 | STATISTIC(NumVisitedBlocksInAnalyzedFunctions, |
| 59 | "The # of visited basic blocks in the analyzed functions."); |
| 60 | STATISTIC(PercentReachableBlocks, "The % of reachable basic blocks."); |
| 61 | STATISTIC(MaxCFGSize, "The maximum number of basic blocks in a function."); |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | void ento::createPlistHTMLDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts, |
| 68 | PathDiagnosticConsumers &C, |
| 69 | const std::string &prefix, |
| 70 | const Preprocessor &PP) { |
| 71 | createHTMLDiagnosticConsumer(AnalyzerOpts, C, |
| 72 | llvm::sys::path::parent_path(prefix), PP); |
| 73 | createPlistMultiFileDiagnosticConsumer(AnalyzerOpts, C, prefix, PP); |
| 74 | } |
| 75 | |
| 76 | void ento::createTextPathDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts, |
| 77 | PathDiagnosticConsumers &C, |
| 78 | const std::string &Prefix, |
| 79 | const clang::Preprocessor &PP) { |
| 80 | llvm_unreachable("'text' consumer should be enabled on ClangDiags"); |
| 81 | } |
| 82 | |
| 83 | namespace { |
| 84 | class ClangDiagPathDiagConsumer : public PathDiagnosticConsumer { |
| 85 | DiagnosticsEngine &Diag; |
| 86 | bool IncludePath; |
| 87 | public: |
| 88 | ClangDiagPathDiagConsumer(DiagnosticsEngine &Diag) |
| 89 | : Diag(Diag), IncludePath(false) {} |
| 90 | ~ClangDiagPathDiagConsumer() override {} |
| 91 | StringRef getName() const override { return "ClangDiags"; } |
| 92 | |
| 93 | bool supportsLogicalOpControlFlow() const override { return true; } |
| 94 | bool supportsCrossFileDiagnostics() const override { return true; } |
| 95 | |
| 96 | PathGenerationScheme getGenerationScheme() const override { |
| 97 | return IncludePath ? Minimal : None; |
| 98 | } |
| 99 | |
| 100 | void enablePaths() { |
| 101 | IncludePath = true; |
| 102 | } |
| 103 | |
| 104 | void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags, |
| 105 | FilesMade *filesMade) override { |
| 106 | unsigned WarnID = Diag.getCustomDiagID(DiagnosticsEngine::Warning, "%0"); |
| 107 | unsigned NoteID = Diag.getCustomDiagID(DiagnosticsEngine::Note, "%0"); |
| 108 | |
| 109 | for (std::vector<const PathDiagnostic*>::iterator I = Diags.begin(), |
| 110 | E = Diags.end(); I != E; ++I) { |
| 111 | const PathDiagnostic *PD = *I; |
| 112 | SourceLocation WarnLoc = PD->getLocation().asLocation(); |
| 113 | Diag.Report(WarnLoc, WarnID) << PD->getShortDescription() |
| 114 | << PD->path.back()->getRanges(); |
| 115 | |
| 116 | |
| 117 | for (const auto &Piece : PD->path) { |
| 118 | if (!isa<PathDiagnosticNotePiece>(Piece.get())) |
| 119 | continue; |
| 120 | |
| 121 | SourceLocation NoteLoc = Piece->getLocation().asLocation(); |
| 122 | Diag.Report(NoteLoc, NoteID) << Piece->getString() |
| 123 | << Piece->getRanges(); |
| 124 | } |
| 125 | |
| 126 | if (!IncludePath) |
| 127 | continue; |
| 128 | |
| 129 | |
| 130 | PathPieces FlatPath = PD->path.flatten(); |
| 131 | for (const auto &Piece : FlatPath) { |
| 132 | if (isa<PathDiagnosticNotePiece>(Piece.get())) |
| 133 | continue; |
| 134 | |
| 135 | SourceLocation NoteLoc = Piece->getLocation().asLocation(); |
| 136 | Diag.Report(NoteLoc, NoteID) << Piece->getString() |
| 137 | << Piece->getRanges(); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | }; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | namespace { |
| 149 | |
| 150 | class AnalysisConsumer : public AnalysisASTConsumer, |
| 151 | public RecursiveASTVisitor<AnalysisConsumer> { |
| 152 | enum { |
| 153 | AM_None = 0, |
| 154 | AM_Syntax = 0x1, |
| 155 | AM_Path = 0x2 |
| 156 | }; |
| 157 | typedef unsigned AnalysisMode; |
| 158 | |
| 159 | |
| 160 | AnalysisMode RecVisitorMode; |
| 161 | |
| 162 | BugReporter *RecVisitorBR; |
| 163 | |
| 164 | std::vector<std::function<void(CheckerRegistry &)>> CheckerRegistrationFns; |
| 165 | |
| 166 | public: |
| 167 | ASTContext *Ctx; |
| 168 | const Preprocessor &PP; |
| 169 | const std::string OutDir; |
| 170 | AnalyzerOptionsRef Opts; |
| 171 | ArrayRef<std::string> Plugins; |
| 172 | CodeInjector *Injector; |
| 173 | cross_tu::CrossTranslationUnitContext CTU; |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | SetOfDecls LocalTUDecls; |
| 181 | |
| 182 | |
| 183 | PathDiagnosticConsumers PathConsumers; |
| 184 | |
| 185 | StoreManagerCreator CreateStoreMgr; |
| 186 | ConstraintManagerCreator CreateConstraintMgr; |
| 187 | |
| 188 | std::unique_ptr<CheckerManager> checkerMgr; |
| 189 | std::unique_ptr<AnalysisManager> Mgr; |
| 190 | |
| 191 | |
| 192 | std::unique_ptr<llvm::TimerGroup> AnalyzerTimers; |
| 193 | std::unique_ptr<llvm::Timer> TUTotalTimer; |
| 194 | |
| 195 | |
| 196 | |
| 197 | FunctionSummariesTy FunctionSummaries; |
| 198 | |
| 199 | AnalysisConsumer(CompilerInstance &CI, const std::string &outdir, |
| 200 | AnalyzerOptionsRef opts, ArrayRef<std::string> plugins, |
| 201 | CodeInjector *injector) |
| 202 | : RecVisitorMode(0), RecVisitorBR(nullptr), Ctx(nullptr), |
| 203 | PP(CI.getPreprocessor()), OutDir(outdir), Opts(std::move(opts)), |
| 204 | Plugins(plugins), Injector(injector), CTU(CI) { |
| 205 | DigestAnalyzerOptions(); |
| 206 | if (Opts->PrintStats || Opts->ShouldSerializeStats) { |
| 207 | AnalyzerTimers = llvm::make_unique<llvm::TimerGroup>( |
| 208 | "analyzer", "Analyzer timers"); |
| 209 | TUTotalTimer = llvm::make_unique<llvm::Timer>( |
| 210 | "time", "Analyzer total time", *AnalyzerTimers); |
| 211 | llvm::EnableStatistics( false); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | ~AnalysisConsumer() override { |
| 216 | if (Opts->PrintStats) { |
| 217 | llvm::PrintStatistics(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void DigestAnalyzerOptions() { |
| 222 | if (Opts->AnalysisDiagOpt != PD_NONE) { |
| 223 | |
| 224 | ClangDiagPathDiagConsumer *clangDiags = |
| 225 | new ClangDiagPathDiagConsumer(PP.getDiagnostics()); |
| 226 | PathConsumers.push_back(clangDiags); |
| 227 | |
| 228 | if (Opts->AnalysisDiagOpt == PD_TEXT) { |
| 229 | clangDiags->enablePaths(); |
| 230 | |
| 231 | } else if (!OutDir.empty()) { |
| 232 | switch (Opts->AnalysisDiagOpt) { |
| 233 | default: |
| 234 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN) \ |
| 235 | case PD_##NAME: \ |
| 236 | CREATEFN(*Opts.get(), PathConsumers, OutDir, PP); \ |
| 237 | break; |
| 238 | #include "clang/StaticAnalyzer/Core/Analyses.def" |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | switch (Opts->AnalysisStoreOpt) { |
| 245 | default: |
| 246 | llvm_unreachable("Unknown store manager."); |
| 247 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \ |
| 248 | case NAME##Model: CreateStoreMgr = CREATEFN; break; |
| 249 | #include "clang/StaticAnalyzer/Core/Analyses.def" |
| 250 | } |
| 251 | |
| 252 | switch (Opts->AnalysisConstraintsOpt) { |
| 253 | default: |
| 254 | llvm_unreachable("Unknown constraint manager."); |
| 255 | #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \ |
| 256 | case NAME##Model: CreateConstraintMgr = CREATEFN; break; |
| 257 | #include "clang/StaticAnalyzer/Core/Analyses.def" |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void DisplayFunction(const Decl *D, AnalysisMode Mode, |
| 262 | ExprEngine::InliningModes IMode) { |
| 263 | if (!Opts->AnalyzerDisplayProgress) |
| 264 | return; |
| 265 | |
| 266 | SourceManager &SM = Mgr->getASTContext().getSourceManager(); |
| 267 | PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); |
| 268 | if (Loc.isValid()) { |
| 269 | llvm::errs() << "ANALYZE"; |
| 270 | |
| 271 | if (Mode == AM_Syntax) |
| 272 | llvm::errs() << " (Syntax)"; |
| 273 | else if (Mode == AM_Path) { |
| 274 | llvm::errs() << " (Path, "; |
| 275 | switch (IMode) { |
| 276 | case ExprEngine::Inline_Minimal: |
| 277 | llvm::errs() << " Inline_Minimal"; |
| 278 | break; |
| 279 | case ExprEngine::Inline_Regular: |
| 280 | llvm::errs() << " Inline_Regular"; |
| 281 | break; |
| 282 | } |
| 283 | llvm::errs() << ")"; |
| 284 | } |
| 285 | else |
| 286 | (0) . __assert_fail ("Mode == (AM_Syntax | AM_Path) && \"Unexpected mode!\"", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp", 286, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Mode == (AM_Syntax | AM_Path) && "Unexpected mode!"); |
| 287 | |
| 288 | llvm::errs() << ": " << Loc.getFilename() << ' ' |
| 289 | << getFunctionName(D) << '\n'; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | void Initialize(ASTContext &Context) override { |
| 294 | Ctx = &Context; |
| 295 | checkerMgr = createCheckerManager( |
| 296 | *Ctx, *Opts, Plugins, CheckerRegistrationFns, PP.getDiagnostics()); |
| 297 | |
| 298 | Mgr = llvm::make_unique<AnalysisManager>( |
| 299 | *Ctx, PP.getDiagnostics(), PathConsumers, CreateStoreMgr, |
| 300 | CreateConstraintMgr, checkerMgr.get(), *Opts, Injector); |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | bool HandleTopLevelDecl(DeclGroupRef D) override; |
| 306 | void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override; |
| 307 | |
| 308 | void HandleTranslationUnit(ASTContext &C) override; |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | ExprEngine::InliningModes |
| 314 | getInliningModeForFunction(const Decl *D, const SetOfConstDecls &Visited); |
| 315 | |
| 316 | |
| 317 | |
| 318 | void HandleDeclsCallGraph(const unsigned LocalTUDeclsSize); |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | void HandleCode(Decl *D, AnalysisMode Mode, |
| 327 | ExprEngine::InliningModes IMode = ExprEngine::Inline_Minimal, |
| 328 | SetOfConstDecls *VisitedCallees = nullptr); |
| 329 | |
| 330 | void RunPathSensitiveChecks(Decl *D, |
| 331 | ExprEngine::InliningModes IMode, |
| 332 | SetOfConstDecls *VisitedCallees); |
| 333 | |
| 334 | |
| 335 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
| 336 | |
| 337 | |
| 338 | bool VisitDecl(Decl *D) { |
| 339 | AnalysisMode Mode = getModeForDecl(D, RecVisitorMode); |
| 340 | if (Mode & AM_Syntax) |
| 341 | checkerMgr->runCheckersOnASTDecl(D, *Mgr, *RecVisitorBR); |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | bool VisitFunctionDecl(FunctionDecl *FD) { |
| 346 | IdentifierInfo *II = FD->getIdentifier(); |
| 347 | if (II && II->getName().startswith("__inline")) |
| 348 | return true; |
| 349 | |
| 350 | |
| 351 | |
| 352 | if (FD->isThisDeclarationADefinition() && |
| 353 | !FD->isDependentContext()) { |
| 354 | shouldInlineCall() == false", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp", 354, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false); |
| 355 | HandleCode(FD, RecVisitorMode); |
| 356 | } |
| 357 | return true; |
| 358 | } |
| 359 | |
| 360 | bool VisitObjCMethodDecl(ObjCMethodDecl *MD) { |
| 361 | if (MD->isThisDeclarationADefinition()) { |
| 362 | shouldInlineCall() == false", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp", 362, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false); |
| 363 | HandleCode(MD, RecVisitorMode); |
| 364 | } |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | bool VisitBlockDecl(BlockDecl *BD) { |
| 369 | if (BD->hasBody()) { |
| 370 | shouldInlineCall() == false", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp", 370, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false); |
| 371 | |
| 372 | |
| 373 | if (!BD->isDependentContext()) { |
| 374 | HandleCode(BD, RecVisitorMode); |
| 375 | } |
| 376 | } |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | void AddDiagnosticConsumer(PathDiagnosticConsumer *Consumer) override { |
| 381 | PathConsumers.push_back(Consumer); |
| 382 | } |
| 383 | |
| 384 | void AddCheckerRegistrationFn(std::function<void(CheckerRegistry&)> Fn) override { |
| 385 | CheckerRegistrationFns.push_back(std::move(Fn)); |
| 386 | } |
| 387 | |
| 388 | private: |
| 389 | void storeTopLevelDecls(DeclGroupRef DG); |
| 390 | std::string getFunctionName(const Decl *D); |
| 391 | |
| 392 | |
| 393 | AnalysisMode getModeForDecl(Decl *D, AnalysisMode Mode); |
| 394 | void runAnalysisOnTranslationUnit(ASTContext &C); |
| 395 | |
| 396 | |
| 397 | void reportAnalyzerProgress(StringRef S); |
| 398 | }; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | bool AnalysisConsumer::HandleTopLevelDecl(DeclGroupRef DG) { |
| 406 | storeTopLevelDecls(DG); |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | void AnalysisConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) { |
| 411 | storeTopLevelDecls(DG); |
| 412 | } |
| 413 | |
| 414 | void AnalysisConsumer::storeTopLevelDecls(DeclGroupRef DG) { |
| 415 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) { |
| 416 | |
| 417 | |
| 418 | |
| 419 | if (isa<ObjCMethodDecl>(*I)) |
| 420 | continue; |
| 421 | |
| 422 | LocalTUDecls.push_back(*I); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | static bool shouldSkipFunction(const Decl *D, |
| 427 | const SetOfConstDecls &Visited, |
| 428 | const SetOfConstDecls &VisitedAsTopLevel) { |
| 429 | if (VisitedAsTopLevel.count(D)) |
| 430 | return true; |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | if (isa<ObjCMethodDecl>(D)) |
| 440 | return false; |
| 441 | |
| 442 | |
| 443 | |
| 444 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 445 | if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | return Visited.count(D); |
| 451 | } |
| 452 | |
| 453 | ExprEngine::InliningModes |
| 454 | AnalysisConsumer::getInliningModeForFunction(const Decl *D, |
| 455 | const SetOfConstDecls &Visited) { |
| 456 | |
| 457 | |
| 458 | |
| 459 | if (Visited.count(D) && isa<ObjCMethodDecl>(D)) { |
| 460 | const ObjCMethodDecl *ObjCM = cast<ObjCMethodDecl>(D); |
| 461 | if (ObjCM->getMethodFamily() != OMF_init) |
| 462 | return ExprEngine::Inline_Minimal; |
| 463 | } |
| 464 | |
| 465 | return ExprEngine::Inline_Regular; |
| 466 | } |
| 467 | |
| 468 | void AnalysisConsumer::HandleDeclsCallGraph(const unsigned LocalTUDeclsSize) { |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | CallGraph CG; |
| 474 | for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) { |
| 475 | CG.addToCallGraph(LocalTUDecls[i]); |
| 476 | } |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | SetOfConstDecls Visited; |
| 485 | SetOfConstDecls VisitedAsTopLevel; |
| 486 | llvm::ReversePostOrderTraversal<clang::CallGraph*> RPOT(&CG); |
| 487 | for (llvm::ReversePostOrderTraversal<clang::CallGraph*>::rpo_iterator |
| 488 | I = RPOT.begin(), E = RPOT.end(); I != E; ++I) { |
| 489 | NumFunctionTopLevel++; |
| 490 | |
| 491 | CallGraphNode *N = *I; |
| 492 | Decl *D = N->getDecl(); |
| 493 | |
| 494 | |
| 495 | if (!D) |
| 496 | continue; |
| 497 | |
| 498 | |
| 499 | |
| 500 | if (shouldSkipFunction(D, Visited, VisitedAsTopLevel)) |
| 501 | continue; |
| 502 | |
| 503 | |
| 504 | SetOfConstDecls VisitedCallees; |
| 505 | |
| 506 | HandleCode(D, AM_Path, getInliningModeForFunction(D, Visited), |
| 507 | (Mgr->options.InliningMode == All ? nullptr : &VisitedCallees)); |
| 508 | |
| 509 | |
| 510 | for (const Decl *Callee : VisitedCallees) |
| 511 | |
| 512 | |
| 513 | Visited.insert(isa<ObjCMethodDecl>(Callee) ? Callee |
| 514 | : Callee->getCanonicalDecl()); |
| 515 | VisitedAsTopLevel.insert(D); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | static bool isBisonFile(ASTContext &C) { |
| 520 | const SourceManager &SM = C.getSourceManager(); |
| 521 | FileID FID = SM.getMainFileID(); |
| 522 | StringRef Buffer = SM.getBuffer(FID)->getBuffer(); |
| 523 | if (Buffer.startswith("/* A Bison parser, made by")) |
| 524 | return true; |
| 525 | return false; |
| 526 | } |
| 527 | |
| 528 | void AnalysisConsumer::runAnalysisOnTranslationUnit(ASTContext &C) { |
| 529 | BugReporter BR(*Mgr); |
| 530 | TranslationUnitDecl *TU = C.getTranslationUnitDecl(); |
| 531 | checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR); |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | RecVisitorMode = AM_Syntax; |
| 537 | if (!Mgr->shouldInlineCall()) |
| 538 | RecVisitorMode |= AM_Path; |
| 539 | RecVisitorBR = &BR; |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | |
| 547 | const unsigned LocalTUDeclsSize = LocalTUDecls.size(); |
| 548 | for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) { |
| 549 | TraverseDecl(LocalTUDecls[i]); |
| 550 | } |
| 551 | |
| 552 | if (Mgr->shouldInlineCall()) |
| 553 | HandleDeclsCallGraph(LocalTUDeclsSize); |
| 554 | |
| 555 | |
| 556 | checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR); |
| 557 | |
| 558 | RecVisitorBR = nullptr; |
| 559 | } |
| 560 | |
| 561 | void AnalysisConsumer::reportAnalyzerProgress(StringRef S) { |
| 562 | if (Opts->AnalyzerDisplayProgress) |
| 563 | llvm::errs() << S; |
| 564 | } |
| 565 | |
| 566 | void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) { |
| 567 | |
| 568 | |
| 569 | DiagnosticsEngine &Diags = PP.getDiagnostics(); |
| 570 | if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) |
| 571 | return; |
| 572 | |
| 573 | if (TUTotalTimer) TUTotalTimer->startTimer(); |
| 574 | |
| 575 | if (isBisonFile(C)) { |
| 576 | reportAnalyzerProgress("Skipping bison-generated file\n"); |
| 577 | } else if (Opts->DisableAllChecks) { |
| 578 | |
| 579 | |
| 580 | |
| 581 | reportAnalyzerProgress("All checks are disabled using a supplied option\n"); |
| 582 | } else { |
| 583 | |
| 584 | runAnalysisOnTranslationUnit(C); |
| 585 | } |
| 586 | |
| 587 | if (TUTotalTimer) TUTotalTimer->stopTimer(); |
| 588 | |
| 589 | |
| 590 | NumBlocksInAnalyzedFunctions = FunctionSummaries.getTotalNumBasicBlocks(); |
| 591 | NumVisitedBlocksInAnalyzedFunctions = |
| 592 | FunctionSummaries.getTotalNumVisitedBasicBlocks(); |
| 593 | if (NumBlocksInAnalyzedFunctions > 0) |
| 594 | PercentReachableBlocks = |
| 595 | (FunctionSummaries.getTotalNumVisitedBasicBlocks() * 100) / |
| 596 | NumBlocksInAnalyzedFunctions; |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | Mgr.reset(); |
| 603 | } |
| 604 | |
| 605 | std::string AnalysisConsumer::getFunctionName(const Decl *D) { |
| 606 | std::string Str; |
| 607 | llvm::raw_string_ostream OS(Str); |
| 608 | |
| 609 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 610 | OS << FD->getQualifiedNameAsString(); |
| 611 | |
| 612 | |
| 613 | if (Ctx->getLangOpts().CPlusPlus) { |
| 614 | OS << '('; |
| 615 | for (const auto &P : FD->parameters()) { |
| 616 | if (P != *FD->param_begin()) |
| 617 | OS << ", "; |
| 618 | OS << P->getType().getAsString(); |
| 619 | } |
| 620 | OS << ')'; |
| 621 | } |
| 622 | |
| 623 | } else if (isa<BlockDecl>(D)) { |
| 624 | PresumedLoc Loc = Ctx->getSourceManager().getPresumedLoc(D->getLocation()); |
| 625 | |
| 626 | if (Loc.isValid()) { |
| 627 | OS << "block (line: " << Loc.getLine() << ", col: " << Loc.getColumn() |
| 628 | << ')'; |
| 629 | } |
| 630 | |
| 631 | } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) { |
| 632 | |
| 633 | |
| 634 | OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; |
| 635 | const DeclContext *DC = OMD->getDeclContext(); |
| 636 | if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) { |
| 637 | OS << OID->getName(); |
| 638 | } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) { |
| 639 | OS << OID->getName(); |
| 640 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) { |
| 641 | if (OC->IsClassExtension()) { |
| 642 | OS << OC->getClassInterface()->getName(); |
| 643 | } else { |
| 644 | OS << OC->getIdentifier()->getNameStart() << '(' |
| 645 | << OC->getIdentifier()->getNameStart() << ')'; |
| 646 | } |
| 647 | } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) { |
| 648 | OS << OCD->getClassInterface()->getName() << '(' |
| 649 | << OCD->getName() << ')'; |
| 650 | } else if (isa<ObjCProtocolDecl>(DC)) { |
| 651 | |
| 652 | if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) { |
| 653 | QualType ClassTy = |
| 654 | cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType(); |
| 655 | ClassTy.print(OS, PrintingPolicy(LangOptions())); |
| 656 | } |
| 657 | } |
| 658 | OS << ' ' << OMD->getSelector().getAsString() << ']'; |
| 659 | |
| 660 | } |
| 661 | |
| 662 | return OS.str(); |
| 663 | } |
| 664 | |
| 665 | AnalysisConsumer::AnalysisMode |
| 666 | AnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) { |
| 667 | if (!Opts->AnalyzeSpecificFunction.empty() && |
| 668 | getFunctionName(D) != Opts->AnalyzeSpecificFunction) |
| 669 | return AM_None; |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | SourceManager &SM = Ctx->getSourceManager(); |
| 677 | const Stmt *Body = D->getBody(); |
| 678 | SourceLocation SL = Body ? Body->getBeginLoc() : D->getLocation(); |
| 679 | SL = SM.getExpansionLoc(SL); |
| 680 | |
| 681 | if (!Opts->AnalyzeAll && !Mgr->isInCodeFile(SL)) { |
| 682 | if (SL.isInvalid() || SM.isInSystemHeader(SL)) |
| 683 | return AM_None; |
| 684 | return Mode & ~AM_Path; |
| 685 | } |
| 686 | |
| 687 | return Mode; |
| 688 | } |
| 689 | |
| 690 | void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode, |
| 691 | ExprEngine::InliningModes IMode, |
| 692 | SetOfConstDecls *VisitedCallees) { |
| 693 | if (!D->hasBody()) |
| 694 | return; |
| 695 | Mode = getModeForDecl(D, Mode); |
| 696 | if (Mode == AM_None) |
| 697 | return; |
| 698 | |
| 699 | |
| 700 | Mgr->ClearContexts(); |
| 701 | |
| 702 | if (Mgr->getAnalysisDeclContext(D)->isBodyAutosynthesized()) |
| 703 | return; |
| 704 | |
| 705 | DisplayFunction(D, Mode, IMode); |
| 706 | CFG *DeclCFG = Mgr->getCFG(D); |
| 707 | if (DeclCFG) |
| 708 | MaxCFGSize.updateMax(DeclCFG->size()); |
| 709 | |
| 710 | BugReporter BR(*Mgr); |
| 711 | |
| 712 | if (Mode & AM_Syntax) |
| 713 | checkerMgr->runCheckersOnASTBody(D, *Mgr, BR); |
| 714 | if ((Mode & AM_Path) && checkerMgr->hasPathSensitiveCheckers()) { |
| 715 | RunPathSensitiveChecks(D, IMode, VisitedCallees); |
| 716 | if (IMode != ExprEngine::Inline_Minimal) |
| 717 | NumFunctionsAnalyzed++; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | void AnalysisConsumer::RunPathSensitiveChecks(Decl *D, |
| 726 | ExprEngine::InliningModes IMode, |
| 727 | SetOfConstDecls *VisitedCallees) { |
| 728 | |
| 729 | |
| 730 | if (!Mgr->getCFG(D)) |
| 731 | return; |
| 732 | |
| 733 | |
| 734 | if (!Mgr->getAnalysisDeclContext(D)->getAnalysis<RelaxedLiveVariables>()) |
| 735 | return; |
| 736 | |
| 737 | ExprEngine Eng(CTU, *Mgr, VisitedCallees, &FunctionSummaries, IMode); |
| 738 | |
| 739 | |
| 740 | Eng.ExecuteWorkList(Mgr->getAnalysisDeclContextManager().getStackFrame(D), |
| 741 | Mgr->options.MaxNodesPerTopLevelFunction); |
| 742 | |
| 743 | if (!Mgr->options.DumpExplodedGraphTo.empty()) |
| 744 | Eng.DumpGraph(Mgr->options.TrimGraph, Mgr->options.DumpExplodedGraphTo); |
| 745 | |
| 746 | |
| 747 | if (Mgr->options.visualizeExplodedGraphWithGraphViz) |
| 748 | Eng.ViewGraph(Mgr->options.TrimGraph); |
| 749 | |
| 750 | |
| 751 | Eng.getBugReporter().FlushReports(); |
| 752 | } |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | std::unique_ptr<AnalysisASTConsumer> |
| 759 | ento::CreateAnalysisConsumer(CompilerInstance &CI) { |
| 760 | |
| 761 | CI.getPreprocessor().getDiagnostics().setWarningsAsErrors(false); |
| 762 | |
| 763 | AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts(); |
| 764 | bool hasModelPath = analyzerOpts->Config.count("model-path") > 0; |
| 765 | |
| 766 | return llvm::make_unique<AnalysisConsumer>( |
| 767 | CI, CI.getFrontendOpts().OutputFile, analyzerOpts, |
| 768 | CI.getFrontendOpts().Plugins, |
| 769 | hasModelPath ? new ModelInjector(CI) : nullptr); |
| 770 | } |
| 771 | |