AnalysisConsumer.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. //===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // "Meta" ASTConsumer for running different source analyses.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
  13. #include "ModelInjector.h"
  14. #include "clang/Analysis/PathDiagnostic.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/CheckerManager.h"
  31. #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
  32. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  33. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  34. #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
  35. #include "llvm/ADT/PostOrderIterator.h"
  36. #include "llvm/ADT/Statistic.h"
  37. #include "llvm/Support/FileSystem.h"
  38. #include "llvm/Support/Path.h"
  39. #include "llvm/Support/Program.h"
  40. #include "llvm/Support/Timer.h"
  41. #include "llvm/Support/raw_ostream.h"
  42. #include <memory>
  43. #include <queue>
  44. #include <utility>
  45. using namespace clang;
  46. using namespace ento;
  47. #define DEBUG_TYPE "AnalysisConsumer"
  48. STATISTIC(NumFunctionTopLevel, "The # of functions at top level.");
  49. STATISTIC(NumFunctionsAnalyzed,
  50. "The # of functions and blocks analyzed (as top level "
  51. "with inlining turned on).");
  52. STATISTIC(NumBlocksInAnalyzedFunctions,
  53. "The # of basic blocks in the analyzed functions.");
  54. STATISTIC(NumVisitedBlocksInAnalyzedFunctions,
  55. "The # of visited basic blocks in the analyzed functions.");
  56. STATISTIC(PercentReachableBlocks, "The % of reachable basic blocks.");
  57. STATISTIC(MaxCFGSize, "The maximum number of basic blocks in a function.");
  58. //===----------------------------------------------------------------------===//
  59. // Special PathDiagnosticConsumers.
  60. //===----------------------------------------------------------------------===//
  61. void ento::createPlistHTMLDiagnosticConsumer(
  62. AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
  63. const std::string &prefix, const Preprocessor &PP,
  64. const cross_tu::CrossTranslationUnitContext &CTU) {
  65. createHTMLDiagnosticConsumer(AnalyzerOpts, C,
  66. llvm::sys::path::parent_path(prefix), PP, CTU);
  67. createPlistMultiFileDiagnosticConsumer(AnalyzerOpts, C, prefix, PP, CTU);
  68. }
  69. void ento::createTextPathDiagnosticConsumer(
  70. AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C,
  71. const std::string &Prefix, const clang::Preprocessor &PP,
  72. const cross_tu::CrossTranslationUnitContext &CTU) {
  73. llvm_unreachable("'text' consumer should be enabled on ClangDiags");
  74. }
  75. namespace {
  76. class ClangDiagPathDiagConsumer : public PathDiagnosticConsumer {
  77. DiagnosticsEngine &Diag;
  78. bool IncludePath = false, ShouldEmitAsError = false, FixitsAsRemarks = false;
  79. public:
  80. ClangDiagPathDiagConsumer(DiagnosticsEngine &Diag)
  81. : Diag(Diag) {}
  82. ~ClangDiagPathDiagConsumer() override {}
  83. StringRef getName() const override { return "ClangDiags"; }
  84. bool supportsLogicalOpControlFlow() const override { return true; }
  85. bool supportsCrossFileDiagnostics() const override { return true; }
  86. PathGenerationScheme getGenerationScheme() const override {
  87. return IncludePath ? Minimal : None;
  88. }
  89. void enablePaths() { IncludePath = true; }
  90. void enableWerror() { ShouldEmitAsError = true; }
  91. void enableFixitsAsRemarks() { FixitsAsRemarks = true; }
  92. void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
  93. FilesMade *filesMade) override {
  94. unsigned WarnID =
  95. ShouldEmitAsError
  96. ? Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0")
  97. : Diag.getCustomDiagID(DiagnosticsEngine::Warning, "%0");
  98. unsigned NoteID = Diag.getCustomDiagID(DiagnosticsEngine::Note, "%0");
  99. unsigned RemarkID = Diag.getCustomDiagID(DiagnosticsEngine::Remark, "%0");
  100. auto reportPiece =
  101. [&](unsigned ID, SourceLocation Loc, StringRef String,
  102. ArrayRef<SourceRange> Ranges, ArrayRef<FixItHint> Fixits) {
  103. if (!FixitsAsRemarks) {
  104. Diag.Report(Loc, ID) << String << Ranges << Fixits;
  105. } else {
  106. Diag.Report(Loc, ID) << String << Ranges;
  107. for (const FixItHint &Hint : Fixits) {
  108. SourceManager &SM = Diag.getSourceManager();
  109. llvm::SmallString<128> Str;
  110. llvm::raw_svector_ostream OS(Str);
  111. // FIXME: Add support for InsertFromRange and
  112. // BeforePreviousInsertion.
  113. assert(!Hint.InsertFromRange.isValid() && "Not implemented yet!");
  114. assert(!Hint.BeforePreviousInsertions && "Not implemented yet!");
  115. OS << SM.getSpellingColumnNumber(Hint.RemoveRange.getBegin())
  116. << "-" << SM.getSpellingColumnNumber(Hint.RemoveRange.getEnd())
  117. << ": '" << Hint.CodeToInsert << "'";
  118. Diag.Report(Loc, RemarkID) << OS.str();
  119. }
  120. }
  121. };
  122. for (std::vector<const PathDiagnostic *>::iterator I = Diags.begin(),
  123. E = Diags.end();
  124. I != E; ++I) {
  125. const PathDiagnostic *PD = *I;
  126. reportPiece(WarnID, PD->getLocation().asLocation(),
  127. PD->getShortDescription(), PD->path.back()->getRanges(),
  128. PD->path.back()->getFixits());
  129. // First, add extra notes, even if paths should not be included.
  130. for (const auto &Piece : PD->path) {
  131. if (!isa<PathDiagnosticNotePiece>(Piece.get()))
  132. continue;
  133. reportPiece(NoteID, Piece->getLocation().asLocation(),
  134. Piece->getString(), Piece->getRanges(), Piece->getFixits());
  135. }
  136. if (!IncludePath)
  137. continue;
  138. // Then, add the path notes if necessary.
  139. PathPieces FlatPath = PD->path.flatten(/*ShouldFlattenMacros=*/true);
  140. for (const auto &Piece : FlatPath) {
  141. if (isa<PathDiagnosticNotePiece>(Piece.get()))
  142. continue;
  143. reportPiece(NoteID, Piece->getLocation().asLocation(),
  144. Piece->getString(), Piece->getRanges(), Piece->getFixits());
  145. }
  146. }
  147. }
  148. };
  149. } // end anonymous namespace
  150. //===----------------------------------------------------------------------===//
  151. // AnalysisConsumer declaration.
  152. //===----------------------------------------------------------------------===//
  153. namespace {
  154. class AnalysisConsumer : public AnalysisASTConsumer,
  155. public RecursiveASTVisitor<AnalysisConsumer> {
  156. enum {
  157. AM_None = 0,
  158. AM_Syntax = 0x1,
  159. AM_Path = 0x2
  160. };
  161. typedef unsigned AnalysisMode;
  162. /// Mode of the analyzes while recursively visiting Decls.
  163. AnalysisMode RecVisitorMode;
  164. /// Bug Reporter to use while recursively visiting Decls.
  165. BugReporter *RecVisitorBR;
  166. std::vector<std::function<void(CheckerRegistry &)>> CheckerRegistrationFns;
  167. public:
  168. ASTContext *Ctx;
  169. const Preprocessor &PP;
  170. const std::string OutDir;
  171. AnalyzerOptionsRef Opts;
  172. ArrayRef<std::string> Plugins;
  173. CodeInjector *Injector;
  174. cross_tu::CrossTranslationUnitContext CTU;
  175. /// Stores the declarations from the local translation unit.
  176. /// Note, we pre-compute the local declarations at parse time as an
  177. /// optimization to make sure we do not deserialize everything from disk.
  178. /// The local declaration to all declarations ratio might be very small when
  179. /// working with a PCH file.
  180. SetOfDecls LocalTUDecls;
  181. // Set of PathDiagnosticConsumers. Owned by AnalysisManager.
  182. PathDiagnosticConsumers PathConsumers;
  183. StoreManagerCreator CreateStoreMgr;
  184. ConstraintManagerCreator CreateConstraintMgr;
  185. std::unique_ptr<CheckerManager> checkerMgr;
  186. std::unique_ptr<AnalysisManager> Mgr;
  187. /// Time the analyzes time of each translation unit.
  188. std::unique_ptr<llvm::TimerGroup> AnalyzerTimers;
  189. std::unique_ptr<llvm::Timer> SyntaxCheckTimer;
  190. std::unique_ptr<llvm::Timer> ExprEngineTimer;
  191. std::unique_ptr<llvm::Timer> BugReporterTimer;
  192. /// The information about analyzed functions shared throughout the
  193. /// translation unit.
  194. FunctionSummariesTy FunctionSummaries;
  195. AnalysisConsumer(CompilerInstance &CI, const std::string &outdir,
  196. AnalyzerOptionsRef opts, ArrayRef<std::string> plugins,
  197. CodeInjector *injector)
  198. : RecVisitorMode(0), RecVisitorBR(nullptr), Ctx(nullptr),
  199. PP(CI.getPreprocessor()), OutDir(outdir), Opts(std::move(opts)),
  200. Plugins(plugins), Injector(injector), CTU(CI) {
  201. DigestAnalyzerOptions();
  202. if (Opts->PrintStats || Opts->ShouldSerializeStats) {
  203. AnalyzerTimers = std::make_unique<llvm::TimerGroup>(
  204. "analyzer", "Analyzer timers");
  205. SyntaxCheckTimer = std::make_unique<llvm::Timer>(
  206. "syntaxchecks", "Syntax-based analysis time", *AnalyzerTimers);
  207. ExprEngineTimer = std::make_unique<llvm::Timer>(
  208. "exprengine", "Path exploration time", *AnalyzerTimers);
  209. BugReporterTimer = std::make_unique<llvm::Timer>(
  210. "bugreporter", "Path-sensitive report post-processing time",
  211. *AnalyzerTimers);
  212. llvm::EnableStatistics(/* PrintOnExit= */ false);
  213. }
  214. }
  215. ~AnalysisConsumer() override {
  216. if (Opts->PrintStats) {
  217. llvm::PrintStatistics();
  218. }
  219. }
  220. void DigestAnalyzerOptions() {
  221. if (Opts->AnalysisDiagOpt != PD_NONE) {
  222. // Create the PathDiagnosticConsumer.
  223. ClangDiagPathDiagConsumer *clangDiags =
  224. new ClangDiagPathDiagConsumer(PP.getDiagnostics());
  225. PathConsumers.push_back(clangDiags);
  226. if (Opts->AnalyzerWerror)
  227. clangDiags->enableWerror();
  228. if (Opts->ShouldEmitFixItHintsAsRemarks)
  229. clangDiags->enableFixitsAsRemarks();
  230. if (Opts->AnalysisDiagOpt == PD_TEXT) {
  231. clangDiags->enablePaths();
  232. } else if (!OutDir.empty()) {
  233. switch (Opts->AnalysisDiagOpt) {
  234. default:
  235. #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN) \
  236. case PD_##NAME: \
  237. CREATEFN(*Opts.get(), PathConsumers, OutDir, PP, CTU); \
  238. break;
  239. #include "clang/StaticAnalyzer/Core/Analyses.def"
  240. }
  241. }
  242. }
  243. // Create the analyzer component creators.
  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. switch (Opts->AnalysisConstraintsOpt) {
  252. default:
  253. llvm_unreachable("Unknown constraint manager.");
  254. #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \
  255. case NAME##Model: CreateConstraintMgr = CREATEFN; break;
  256. #include "clang/StaticAnalyzer/Core/Analyses.def"
  257. }
  258. }
  259. void DisplayFunction(const Decl *D, AnalysisMode Mode,
  260. ExprEngine::InliningModes IMode) {
  261. if (!Opts->AnalyzerDisplayProgress)
  262. return;
  263. SourceManager &SM = Mgr->getASTContext().getSourceManager();
  264. PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
  265. if (Loc.isValid()) {
  266. llvm::errs() << "ANALYZE";
  267. if (Mode == AM_Syntax)
  268. llvm::errs() << " (Syntax)";
  269. else if (Mode == AM_Path) {
  270. llvm::errs() << " (Path, ";
  271. switch (IMode) {
  272. case ExprEngine::Inline_Minimal:
  273. llvm::errs() << " Inline_Minimal";
  274. break;
  275. case ExprEngine::Inline_Regular:
  276. llvm::errs() << " Inline_Regular";
  277. break;
  278. }
  279. llvm::errs() << ")";
  280. }
  281. else
  282. assert(Mode == (AM_Syntax | AM_Path) && "Unexpected mode!");
  283. llvm::errs() << ": " << Loc.getFilename() << ' '
  284. << getFunctionName(D) << '\n';
  285. }
  286. }
  287. void Initialize(ASTContext &Context) override {
  288. Ctx = &Context;
  289. checkerMgr = createCheckerManager(
  290. *Ctx, *Opts, Plugins, CheckerRegistrationFns, PP.getDiagnostics());
  291. Mgr = std::make_unique<AnalysisManager>(*Ctx, PathConsumers, CreateStoreMgr,
  292. CreateConstraintMgr,
  293. checkerMgr.get(), *Opts, Injector);
  294. }
  295. /// Store the top level decls in the set to be processed later on.
  296. /// (Doing this pre-processing avoids deserialization of data from PCH.)
  297. bool HandleTopLevelDecl(DeclGroupRef D) override;
  298. void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override;
  299. void HandleTranslationUnit(ASTContext &C) override;
  300. /// Determine which inlining mode should be used when this function is
  301. /// analyzed. This allows to redefine the default inlining policies when
  302. /// analyzing a given function.
  303. ExprEngine::InliningModes
  304. getInliningModeForFunction(const Decl *D, const SetOfConstDecls &Visited);
  305. /// Build the call graph for all the top level decls of this TU and
  306. /// use it to define the order in which the functions should be visited.
  307. void HandleDeclsCallGraph(const unsigned LocalTUDeclsSize);
  308. /// Run analyzes(syntax or path sensitive) on the given function.
  309. /// \param Mode - determines if we are requesting syntax only or path
  310. /// sensitive only analysis.
  311. /// \param VisitedCallees - The output parameter, which is populated with the
  312. /// set of functions which should be considered analyzed after analyzing the
  313. /// given root function.
  314. void HandleCode(Decl *D, AnalysisMode Mode,
  315. ExprEngine::InliningModes IMode = ExprEngine::Inline_Minimal,
  316. SetOfConstDecls *VisitedCallees = nullptr);
  317. void RunPathSensitiveChecks(Decl *D,
  318. ExprEngine::InliningModes IMode,
  319. SetOfConstDecls *VisitedCallees);
  320. /// Visitors for the RecursiveASTVisitor.
  321. bool shouldWalkTypesOfTypeLocs() const { return false; }
  322. /// Handle callbacks for arbitrary Decls.
  323. bool VisitDecl(Decl *D) {
  324. AnalysisMode Mode = getModeForDecl(D, RecVisitorMode);
  325. if (Mode & AM_Syntax) {
  326. if (SyntaxCheckTimer)
  327. SyntaxCheckTimer->startTimer();
  328. checkerMgr->runCheckersOnASTDecl(D, *Mgr, *RecVisitorBR);
  329. if (SyntaxCheckTimer)
  330. SyntaxCheckTimer->stopTimer();
  331. }
  332. return true;
  333. }
  334. bool VisitVarDecl(VarDecl *VD) {
  335. if (!Opts->IsNaiveCTUEnabled)
  336. return true;
  337. if (VD->hasExternalStorage() || VD->isStaticDataMember()) {
  338. if (!cross_tu::containsConst(VD, *Ctx))
  339. return true;
  340. } else {
  341. // Cannot be initialized in another TU.
  342. return true;
  343. }
  344. if (VD->getAnyInitializer())
  345. return true;
  346. llvm::Expected<const VarDecl *> CTUDeclOrError =
  347. CTU.getCrossTUDefinition(VD, Opts->CTUDir, Opts->CTUIndexName,
  348. Opts->DisplayCTUProgress);
  349. if (!CTUDeclOrError) {
  350. handleAllErrors(CTUDeclOrError.takeError(),
  351. [&](const cross_tu::IndexError &IE) {
  352. CTU.emitCrossTUDiagnostics(IE);
  353. });
  354. }
  355. return true;
  356. }
  357. bool VisitFunctionDecl(FunctionDecl *FD) {
  358. IdentifierInfo *II = FD->getIdentifier();
  359. if (II && II->getName().startswith("__inline"))
  360. return true;
  361. // We skip function template definitions, as their semantics is
  362. // only determined when they are instantiated.
  363. if (FD->isThisDeclarationADefinition() &&
  364. !FD->isDependentContext()) {
  365. assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
  366. HandleCode(FD, RecVisitorMode);
  367. }
  368. return true;
  369. }
  370. bool VisitObjCMethodDecl(ObjCMethodDecl *MD) {
  371. if (MD->isThisDeclarationADefinition()) {
  372. assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
  373. HandleCode(MD, RecVisitorMode);
  374. }
  375. return true;
  376. }
  377. bool VisitBlockDecl(BlockDecl *BD) {
  378. if (BD->hasBody()) {
  379. assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
  380. // Since we skip function template definitions, we should skip blocks
  381. // declared in those functions as well.
  382. if (!BD->isDependentContext()) {
  383. HandleCode(BD, RecVisitorMode);
  384. }
  385. }
  386. return true;
  387. }
  388. void AddDiagnosticConsumer(PathDiagnosticConsumer *Consumer) override {
  389. PathConsumers.push_back(Consumer);
  390. }
  391. void AddCheckerRegistrationFn(std::function<void(CheckerRegistry&)> Fn) override {
  392. CheckerRegistrationFns.push_back(std::move(Fn));
  393. }
  394. private:
  395. void storeTopLevelDecls(DeclGroupRef DG);
  396. std::string getFunctionName(const Decl *D);
  397. /// Check if we should skip (not analyze) the given function.
  398. AnalysisMode getModeForDecl(Decl *D, AnalysisMode Mode);
  399. void runAnalysisOnTranslationUnit(ASTContext &C);
  400. /// Print \p S to stderr if \c Opts->AnalyzerDisplayProgress is set.
  401. void reportAnalyzerProgress(StringRef S);
  402. };
  403. } // end anonymous namespace
  404. //===----------------------------------------------------------------------===//
  405. // AnalysisConsumer implementation.
  406. //===----------------------------------------------------------------------===//
  407. bool AnalysisConsumer::HandleTopLevelDecl(DeclGroupRef DG) {
  408. storeTopLevelDecls(DG);
  409. return true;
  410. }
  411. void AnalysisConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) {
  412. storeTopLevelDecls(DG);
  413. }
  414. void AnalysisConsumer::storeTopLevelDecls(DeclGroupRef DG) {
  415. for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) {
  416. // Skip ObjCMethodDecl, wait for the objc container to avoid
  417. // analyzing twice.
  418. if (isa<ObjCMethodDecl>(*I))
  419. continue;
  420. LocalTUDecls.push_back(*I);
  421. }
  422. }
  423. static bool shouldSkipFunction(const Decl *D,
  424. const SetOfConstDecls &Visited,
  425. const SetOfConstDecls &VisitedAsTopLevel) {
  426. if (VisitedAsTopLevel.count(D))
  427. return true;
  428. // We want to re-analyse the functions as top level in the following cases:
  429. // - The 'init' methods should be reanalyzed because
  430. // ObjCNonNilReturnValueChecker assumes that '[super init]' never returns
  431. // 'nil' and unless we analyze the 'init' functions as top level, we will
  432. // not catch errors within defensive code.
  433. // - We want to reanalyze all ObjC methods as top level to report Retain
  434. // Count naming convention errors more aggressively.
  435. if (isa<ObjCMethodDecl>(D))
  436. return false;
  437. // We also want to reanalyze all C++ copy and move assignment operators to
  438. // separately check the two cases where 'this' aliases with the parameter and
  439. // where it may not. (cplusplus.SelfAssignmentChecker)
  440. if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
  441. if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())
  442. return false;
  443. }
  444. // Otherwise, if we visited the function before, do not reanalyze it.
  445. return Visited.count(D);
  446. }
  447. ExprEngine::InliningModes
  448. AnalysisConsumer::getInliningModeForFunction(const Decl *D,
  449. const SetOfConstDecls &Visited) {
  450. // We want to reanalyze all ObjC methods as top level to report Retain
  451. // Count naming convention errors more aggressively. But we should tune down
  452. // inlining when reanalyzing an already inlined function.
  453. if (Visited.count(D) && isa<ObjCMethodDecl>(D)) {
  454. const ObjCMethodDecl *ObjCM = cast<ObjCMethodDecl>(D);
  455. if (ObjCM->getMethodFamily() != OMF_init)
  456. return ExprEngine::Inline_Minimal;
  457. }
  458. return ExprEngine::Inline_Regular;
  459. }
  460. void AnalysisConsumer::HandleDeclsCallGraph(const unsigned LocalTUDeclsSize) {
  461. // Build the Call Graph by adding all the top level declarations to the graph.
  462. // Note: CallGraph can trigger deserialization of more items from a pch
  463. // (though HandleInterestingDecl); triggering additions to LocalTUDecls.
  464. // We rely on random access to add the initially processed Decls to CG.
  465. CallGraph CG;
  466. for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) {
  467. CG.addToCallGraph(LocalTUDecls[i]);
  468. }
  469. // Walk over all of the call graph nodes in topological order, so that we
  470. // analyze parents before the children. Skip the functions inlined into
  471. // the previously processed functions. Use external Visited set to identify
  472. // inlined functions. The topological order allows the "do not reanalyze
  473. // previously inlined function" performance heuristic to be triggered more
  474. // often.
  475. SetOfConstDecls Visited;
  476. SetOfConstDecls VisitedAsTopLevel;
  477. llvm::ReversePostOrderTraversal<clang::CallGraph*> RPOT(&CG);
  478. for (llvm::ReversePostOrderTraversal<clang::CallGraph*>::rpo_iterator
  479. I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
  480. NumFunctionTopLevel++;
  481. CallGraphNode *N = *I;
  482. Decl *D = N->getDecl();
  483. // Skip the abstract root node.
  484. if (!D)
  485. continue;
  486. // Skip the functions which have been processed already or previously
  487. // inlined.
  488. if (shouldSkipFunction(D, Visited, VisitedAsTopLevel))
  489. continue;
  490. // Analyze the function.
  491. SetOfConstDecls VisitedCallees;
  492. HandleCode(D, AM_Path, getInliningModeForFunction(D, Visited),
  493. (Mgr->options.InliningMode == All ? nullptr : &VisitedCallees));
  494. // Add the visited callees to the global visited set.
  495. for (const Decl *Callee : VisitedCallees)
  496. // Decls from CallGraph are already canonical. But Decls coming from
  497. // CallExprs may be not. We should canonicalize them manually.
  498. Visited.insert(isa<ObjCMethodDecl>(Callee) ? Callee
  499. : Callee->getCanonicalDecl());
  500. VisitedAsTopLevel.insert(D);
  501. }
  502. }
  503. static bool isBisonFile(ASTContext &C) {
  504. const SourceManager &SM = C.getSourceManager();
  505. FileID FID = SM.getMainFileID();
  506. StringRef Buffer = SM.getBuffer(FID)->getBuffer();
  507. if (Buffer.startswith("/* A Bison parser, made by"))
  508. return true;
  509. return false;
  510. }
  511. void AnalysisConsumer::runAnalysisOnTranslationUnit(ASTContext &C) {
  512. BugReporter BR(*Mgr);
  513. TranslationUnitDecl *TU = C.getTranslationUnitDecl();
  514. if (SyntaxCheckTimer)
  515. SyntaxCheckTimer->startTimer();
  516. checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR);
  517. if (SyntaxCheckTimer)
  518. SyntaxCheckTimer->stopTimer();
  519. // Run the AST-only checks using the order in which functions are defined.
  520. // If inlining is not turned on, use the simplest function order for path
  521. // sensitive analyzes as well.
  522. RecVisitorMode = AM_Syntax;
  523. if (!Mgr->shouldInlineCall())
  524. RecVisitorMode |= AM_Path;
  525. RecVisitorBR = &BR;
  526. // Process all the top level declarations.
  527. //
  528. // Note: TraverseDecl may modify LocalTUDecls, but only by appending more
  529. // entries. Thus we don't use an iterator, but rely on LocalTUDecls
  530. // random access. By doing so, we automatically compensate for iterators
  531. // possibly being invalidated, although this is a bit slower.
  532. const unsigned LocalTUDeclsSize = LocalTUDecls.size();
  533. for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) {
  534. TraverseDecl(LocalTUDecls[i]);
  535. }
  536. if (Mgr->shouldInlineCall())
  537. HandleDeclsCallGraph(LocalTUDeclsSize);
  538. // After all decls handled, run checkers on the entire TranslationUnit.
  539. checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR);
  540. BR.FlushReports();
  541. RecVisitorBR = nullptr;
  542. }
  543. void AnalysisConsumer::reportAnalyzerProgress(StringRef S) {
  544. if (Opts->AnalyzerDisplayProgress)
  545. llvm::errs() << S;
  546. }
  547. void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
  548. // Don't run the actions if an error has occurred with parsing the file.
  549. DiagnosticsEngine &Diags = PP.getDiagnostics();
  550. if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred())
  551. return;
  552. if (isBisonFile(C)) {
  553. reportAnalyzerProgress("Skipping bison-generated file\n");
  554. } else if (Opts->DisableAllCheckers) {
  555. // Don't analyze if the user explicitly asked for no checks to be performed
  556. // on this file.
  557. reportAnalyzerProgress("All checks are disabled using a supplied option\n");
  558. } else {
  559. // Otherwise, just run the analysis.
  560. runAnalysisOnTranslationUnit(C);
  561. }
  562. // Count how many basic blocks we have not covered.
  563. NumBlocksInAnalyzedFunctions = FunctionSummaries.getTotalNumBasicBlocks();
  564. NumVisitedBlocksInAnalyzedFunctions =
  565. FunctionSummaries.getTotalNumVisitedBasicBlocks();
  566. if (NumBlocksInAnalyzedFunctions > 0)
  567. PercentReachableBlocks =
  568. (FunctionSummaries.getTotalNumVisitedBasicBlocks() * 100) /
  569. NumBlocksInAnalyzedFunctions;
  570. // Explicitly destroy the PathDiagnosticConsumer. This will flush its output.
  571. // FIXME: This should be replaced with something that doesn't rely on
  572. // side-effects in PathDiagnosticConsumer's destructor. This is required when
  573. // used with option -disable-free.
  574. Mgr.reset();
  575. }
  576. std::string AnalysisConsumer::getFunctionName(const Decl *D) {
  577. std::string Str;
  578. llvm::raw_string_ostream OS(Str);
  579. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  580. OS << FD->getQualifiedNameAsString();
  581. // In C++, there are overloads.
  582. if (Ctx->getLangOpts().CPlusPlus) {
  583. OS << '(';
  584. for (const auto &P : FD->parameters()) {
  585. if (P != *FD->param_begin())
  586. OS << ", ";
  587. OS << P->getType().getAsString();
  588. }
  589. OS << ')';
  590. }
  591. } else if (isa<BlockDecl>(D)) {
  592. PresumedLoc Loc = Ctx->getSourceManager().getPresumedLoc(D->getLocation());
  593. if (Loc.isValid()) {
  594. OS << "block (line: " << Loc.getLine() << ", col: " << Loc.getColumn()
  595. << ')';
  596. }
  597. } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
  598. // FIXME: copy-pasted from CGDebugInfo.cpp.
  599. OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
  600. const DeclContext *DC = OMD->getDeclContext();
  601. if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) {
  602. OS << OID->getName();
  603. } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) {
  604. OS << OID->getName();
  605. } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) {
  606. if (OC->IsClassExtension()) {
  607. OS << OC->getClassInterface()->getName();
  608. } else {
  609. OS << OC->getIdentifier()->getNameStart() << '('
  610. << OC->getIdentifier()->getNameStart() << ')';
  611. }
  612. } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) {
  613. OS << OCD->getClassInterface()->getName() << '('
  614. << OCD->getName() << ')';
  615. } else if (isa<ObjCProtocolDecl>(DC)) {
  616. // We can extract the type of the class from the self pointer.
  617. if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) {
  618. QualType ClassTy =
  619. cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType();
  620. ClassTy.print(OS, PrintingPolicy(LangOptions()));
  621. }
  622. }
  623. OS << ' ' << OMD->getSelector().getAsString() << ']';
  624. }
  625. return OS.str();
  626. }
  627. AnalysisConsumer::AnalysisMode
  628. AnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) {
  629. if (!Opts->AnalyzeSpecificFunction.empty() &&
  630. getFunctionName(D) != Opts->AnalyzeSpecificFunction)
  631. return AM_None;
  632. // Unless -analyze-all is specified, treat decls differently depending on
  633. // where they came from:
  634. // - Main source file: run both path-sensitive and non-path-sensitive checks.
  635. // - Header files: run non-path-sensitive checks only.
  636. // - System headers: don't run any checks.
  637. SourceManager &SM = Ctx->getSourceManager();
  638. const Stmt *Body = D->getBody();
  639. SourceLocation SL = Body ? Body->getBeginLoc() : D->getLocation();
  640. SL = SM.getExpansionLoc(SL);
  641. if (!Opts->AnalyzeAll && !Mgr->isInCodeFile(SL)) {
  642. if (SL.isInvalid() || SM.isInSystemHeader(SL))
  643. return AM_None;
  644. return Mode & ~AM_Path;
  645. }
  646. return Mode;
  647. }
  648. void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode,
  649. ExprEngine::InliningModes IMode,
  650. SetOfConstDecls *VisitedCallees) {
  651. if (!D->hasBody())
  652. return;
  653. Mode = getModeForDecl(D, Mode);
  654. if (Mode == AM_None)
  655. return;
  656. // Clear the AnalysisManager of old AnalysisDeclContexts.
  657. Mgr->ClearContexts();
  658. // Ignore autosynthesized code.
  659. if (Mgr->getAnalysisDeclContext(D)->isBodyAutosynthesized())
  660. return;
  661. DisplayFunction(D, Mode, IMode);
  662. CFG *DeclCFG = Mgr->getCFG(D);
  663. if (DeclCFG)
  664. MaxCFGSize.updateMax(DeclCFG->size());
  665. BugReporter BR(*Mgr);
  666. if (Mode & AM_Syntax) {
  667. if (SyntaxCheckTimer)
  668. SyntaxCheckTimer->startTimer();
  669. checkerMgr->runCheckersOnASTBody(D, *Mgr, BR);
  670. if (SyntaxCheckTimer)
  671. SyntaxCheckTimer->stopTimer();
  672. }
  673. BR.FlushReports();
  674. if ((Mode & AM_Path) && checkerMgr->hasPathSensitiveCheckers()) {
  675. RunPathSensitiveChecks(D, IMode, VisitedCallees);
  676. if (IMode != ExprEngine::Inline_Minimal)
  677. NumFunctionsAnalyzed++;
  678. }
  679. }
  680. //===----------------------------------------------------------------------===//
  681. // Path-sensitive checking.
  682. //===----------------------------------------------------------------------===//
  683. void AnalysisConsumer::RunPathSensitiveChecks(Decl *D,
  684. ExprEngine::InliningModes IMode,
  685. SetOfConstDecls *VisitedCallees) {
  686. // Construct the analysis engine. First check if the CFG is valid.
  687. // FIXME: Inter-procedural analysis will need to handle invalid CFGs.
  688. if (!Mgr->getCFG(D))
  689. return;
  690. // See if the LiveVariables analysis scales.
  691. if (!Mgr->getAnalysisDeclContext(D)->getAnalysis<RelaxedLiveVariables>())
  692. return;
  693. ExprEngine Eng(CTU, *Mgr, VisitedCallees, &FunctionSummaries, IMode);
  694. // Execute the worklist algorithm.
  695. if (ExprEngineTimer)
  696. ExprEngineTimer->startTimer();
  697. Eng.ExecuteWorkList(Mgr->getAnalysisDeclContextManager().getStackFrame(D),
  698. Mgr->options.MaxNodesPerTopLevelFunction);
  699. if (ExprEngineTimer)
  700. ExprEngineTimer->stopTimer();
  701. if (!Mgr->options.DumpExplodedGraphTo.empty())
  702. Eng.DumpGraph(Mgr->options.TrimGraph, Mgr->options.DumpExplodedGraphTo);
  703. // Visualize the exploded graph.
  704. if (Mgr->options.visualizeExplodedGraphWithGraphViz)
  705. Eng.ViewGraph(Mgr->options.TrimGraph);
  706. // Display warnings.
  707. if (BugReporterTimer)
  708. BugReporterTimer->startTimer();
  709. Eng.getBugReporter().FlushReports();
  710. if (BugReporterTimer)
  711. BugReporterTimer->stopTimer();
  712. }
  713. //===----------------------------------------------------------------------===//
  714. // AnalysisConsumer creation.
  715. //===----------------------------------------------------------------------===//
  716. std::unique_ptr<AnalysisASTConsumer>
  717. ento::CreateAnalysisConsumer(CompilerInstance &CI) {
  718. // Disable the effects of '-Werror' when using the AnalysisConsumer.
  719. CI.getPreprocessor().getDiagnostics().setWarningsAsErrors(false);
  720. AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts();
  721. bool hasModelPath = analyzerOpts->Config.count("model-path") > 0;
  722. return std::make_unique<AnalysisConsumer>(
  723. CI, CI.getFrontendOpts().OutputFile, analyzerOpts,
  724. CI.getFrontendOpts().Plugins,
  725. hasModelPath ? new ModelInjector(CI) : nullptr);
  726. }