1f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===//
2f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
3f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//                     The LLVM Compiler Infrastructure
4f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
5f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// This file is distributed under the University of Illinois Open Source
6f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// License. See LICENSE.TXT for details.
7f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
8f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
9f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
10f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// "Meta" ASTConsumer for running different source analyses.
11f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
12f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
13f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
14651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h"
15f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek#include "clang/AST/ASTConsumer.h"
16651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/AST/DataRecursiveASTVisitor.h"
17f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek#include "clang/AST/Decl.h"
18802be99a6817aba6edb166b93c133da4358aa783Zhongxing Xu#include "clang/AST/DeclCXX.h"
19f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek#include "clang/AST/DeclObjC.h"
20efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar#include "clang/AST/ParentMap.h"
2155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Analysis/Analyses/LiveVariables.h"
22efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar#include "clang/Analysis/CFG.h"
23d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks#include "clang/Analysis/CallGraph.h"
2455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/FileManager.h"
2555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/SourceManager.h"
2655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Lex/Preprocessor.h"
2721142581d55918beed544a757e4af3bb865b1812Ted Kremenek#include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
2855fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
2955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
309b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
3155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/CheckerManager.h"
3255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
339b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
349b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
3555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h"
36d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks#include "llvm/ADT/DepthFirstIterator.h"
37b99083e60325a28063fb588f458a871151971fdcChandler Carruth#include "llvm/ADT/PostOrderIterator.h"
386a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks#include "llvm/ADT/SmallPtrSet.h"
3981fb169f42769e02c7425b23885a261c025fd5e6Anna Zaks#include "llvm/ADT/Statistic.h"
40b8e60d9a8ff06110efab37fc123b3c56937367a9Rafael Espindola#include "llvm/Support/FileSystem.h"
4155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/Path.h"
4255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/Program.h"
4355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/Timer.h"
4455fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/raw_ostream.h"
45651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include <memory>
467fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks#include <queue>
477fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
48f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenekusing namespace clang;
499ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremenekusing namespace ento;
50d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaksusing llvm::SmallPtrSet;
51f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#define DEBUG_TYPE "AnalysisConsumer"
536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
54c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xustatic ExplodedNode::Auditor* CreateUbiViz();
55ff944a8c481d6c0f1ad2633e4be9bf8b1dd2a09fZhongxing Xu
563fd5f370a28552976c52e76c3035d79012d78ddaAnna ZaksSTATISTIC(NumFunctionTopLevel, "The # of functions at top level.");
5775f31c4862643ab09479c979fabf754e7ffe1460Anna ZaksSTATISTIC(NumFunctionsAnalyzed,
5839cf781d62daaec09e443c6e66a01cd7543ffb41Anna Zaks                      "The # of functions and blocks analyzed (as top level "
5939cf781d62daaec09e443c6e66a01cd7543ffb41Anna Zaks                      "with inlining turned on).");
60e62f048960645b79363408fdead53fec2a063c52Anna ZaksSTATISTIC(NumBlocksInAnalyzedFunctions,
6139cf781d62daaec09e443c6e66a01cd7543ffb41Anna Zaks                      "The # of basic blocks in the analyzed functions.");
62e62f048960645b79363408fdead53fec2a063c52Anna ZaksSTATISTIC(PercentReachableBlocks, "The % of reachable basic blocks.");
6384c1f4b1edddf74f8080cfc470796c2c498b5d43Anna ZaksSTATISTIC(MaxCFGSize, "The maximum number of basic blocks in a function.");
64d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
65be1fe1eb12a1cb91c8e3a9fcc2db4dfe989def6cTed Kremenek//===----------------------------------------------------------------------===//
66f39d962cf84f46d2c0512157259ae1d41a1a5173David Blaikie// Special PathDiagnosticConsumers.
67f75560670bcdd59b051149bdece3eac14e313853Ted Kremenek//===----------------------------------------------------------------------===//
68f75560670bcdd59b051149bdece3eac14e313853Ted Kremenek
695fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rosevoid ento::createPlistHTMLDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
705fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose                                             PathDiagnosticConsumers &C,
715fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose                                             const std::string &prefix,
725fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose                                             const Preprocessor &PP) {
739fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenek  createHTMLDiagnosticConsumer(AnalyzerOpts, C,
749fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenek                               llvm::sys::path::parent_path(prefix), PP);
759fcc2ab2ec5e00802880e205568ff3afbd70a773Ted Kremenek  createPlistDiagnosticConsumer(AnalyzerOpts, C, prefix, PP);
76f75560670bcdd59b051149bdece3eac14e313853Ted Kremenek}
77f75560670bcdd59b051149bdece3eac14e313853Ted Kremenek
785fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rosevoid ento::createTextPathDiagnosticConsumer(AnalyzerOptions &AnalyzerOpts,
795fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose                                            PathDiagnosticConsumers &C,
805fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose                                            const std::string &Prefix,
815fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose                                            const clang::Preprocessor &PP) {
825fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose  llvm_unreachable("'text' consumer should be enabled on ClangDiags");
835fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose}
845fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose
85c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremeneknamespace {
86c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekclass ClangDiagPathDiagConsumer : public PathDiagnosticConsumer {
87c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  DiagnosticsEngine &Diag;
885fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose  bool IncludePath;
89c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenekpublic:
905fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose  ClangDiagPathDiagConsumer(DiagnosticsEngine &Diag)
915fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose    : Diag(Diag), IncludePath(false) {}
92c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  virtual ~ClangDiagPathDiagConsumer() {}
93651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  StringRef getName() const override { return "ClangDiags"; }
945fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose
95651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool supportsLogicalOpControlFlow() const override { return true; }
96651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool supportsCrossFileDiagnostics() const override { return true; }
975fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose
98651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  PathGenerationScheme getGenerationScheme() const override {
995fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose    return IncludePath ? Minimal : None;
1005fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose  }
1015fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose
1025fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose  void enablePaths() {
1035fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose    IncludePath = true;
1045fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose  }
1055fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose
106c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
107651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                            FilesMade *filesMade) override {
108651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned WarnID = Diag.getCustomDiagID(DiagnosticsEngine::Warning, "%0");
109651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    unsigned NoteID = Diag.getCustomDiagID(DiagnosticsEngine::Note, "%0");
110651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
111c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    for (std::vector<const PathDiagnostic*>::iterator I = Diags.begin(),
112c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek         E = Diags.end(); I != E; ++I) {
113c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      const PathDiagnostic *PD = *I;
114651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      SourceLocation WarnLoc = PD->getLocation().asLocation();
115651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Diag.Report(WarnLoc, WarnID) << PD->getShortDescription()
116651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                   << PD->path.back()->getRanges();
1175fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose
1185fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose      if (!IncludePath)
1195fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose        continue;
120c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
1215fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose      PathPieces FlatPath = PD->path.flatten(/*ShouldFlattenMacros=*/true);
1225fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose      for (PathPieces::const_iterator PI = FlatPath.begin(),
1235fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose                                      PE = FlatPath.end();
1245fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose           PI != PE; ++PI) {
1255fba5a789a238c29ef811a39a39be722443ec1b1Jordan Rose        SourceLocation NoteLoc = (*PI)->getLocation().asLocation();
126651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        Diag.Report(NoteLoc, NoteID) << (*PI)->getString()
127651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                                     << (*PI)->getRanges();
128c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek      }
129c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    }
130c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  }
131c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek};
132c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek} // end anonymous namespace
133c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek
134f75560670bcdd59b051149bdece3eac14e313853Ted Kremenek//===----------------------------------------------------------------------===//
135f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// AnalysisConsumer declaration.
136f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
137f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
138f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremeneknamespace {
139f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
140651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesclass AnalysisConsumer : public AnalysisASTConsumer,
141651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                         public DataRecursiveASTVisitor<AnalysisConsumer> {
1424eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  enum {
1434eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    AM_None = 0,
1444eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    AM_Syntax = 0x1,
1454eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    AM_Path = 0x2
146aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  };
1474eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  typedef unsigned AnalysisMode;
148aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
149aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// Mode of the analyzes while recursively visiting Decls.
150aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  AnalysisMode RecVisitorMode;
151aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// Bug Reporter to use while recursively visiting Decls.
152aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  BugReporter *RecVisitorBR;
153aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
154ed8afacb8118b71bcfa8017059e51da325e7691bZhongxing Xupublic:
1559c378f705405d37f49795d5e915989de774fe11fTed Kremenek  ASTContext *Ctx;
1561d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  const Preprocessor &PP;
1571d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  const std::string OutDir;
15845796b10d11869e86c6b85e24df165410536b313Ted Kremenek  AnalyzerOptionsRef Opts;
15908b86531ade68727c56918f162816075b87c864aJordy Rose  ArrayRef<std::string> Plugins;
160d07a0d0279c09d1017f8450fce575a94dc9703c0Zhongxing Xu
1616a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// \brief Stores the declarations from the local translation unit.
1626a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// Note, we pre-compute the local declarations at parse time as an
1636a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// optimization to make sure we do not deserialize everything from disk.
1646a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// The local declaration to all declarations ratio might be very small when
1656a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// working with a PCH file.
1666a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  SetOfDecls LocalTUDecls;
167cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek
168c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  // Set of PathDiagnosticConsumers.  Owned by AnalysisManager.
169c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek  PathDiagnosticConsumers PathConsumers;
170d07a0d0279c09d1017f8450fce575a94dc9703c0Zhongxing Xu
1711d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  StoreManagerCreator CreateStoreMgr;
1721d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  ConstraintManagerCreator CreateConstraintMgr;
173f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
174651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<CheckerManager> checkerMgr;
175651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<AnalysisManager> Mgr;
176c471e7b44e63ff1b46b480e723c4130aeaef5a8aZhongxing Xu
177d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  /// Time the analyzes time of each translation unit.
178d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  static llvm::Timer* TUTotalTimer;
179d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks
1803bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks  /// The information about analyzed functions shared throughout the
1813bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks  /// translation unit.
1823bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks  FunctionSummariesTy FunctionSummaries;
1833bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks
1841d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  AnalysisConsumer(const Preprocessor& pp,
1851d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek                   const std::string& outdir,
18645796b10d11869e86c6b85e24df165410536b313Ted Kremenek                   AnalyzerOptionsRef opts,
18708b86531ade68727c56918f162816075b87c864aJordy Rose                   ArrayRef<std::string> plugins)
1886bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    : RecVisitorMode(0), RecVisitorBR(nullptr),
1896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      Ctx(nullptr), PP(pp), OutDir(outdir), Opts(opts), Plugins(plugins) {
1901d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    DigestAnalyzerOptions();
19145796b10d11869e86c6b85e24df165410536b313Ted Kremenek    if (Opts->PrintStats) {
192d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks      llvm::EnableStatistics();
193d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks      TUTotalTimer = new llvm::Timer("Analyzer Total Time");
194d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks    }
195d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  }
196d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks
197d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  ~AnalysisConsumer() {
19845796b10d11869e86c6b85e24df165410536b313Ted Kremenek    if (Opts->PrintStats)
199d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks      delete TUTotalTimer;
2001d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  }
201fda7832b000ff8927386f093b52c067641679469Zhongxing Xu
2021d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  void DigestAnalyzerOptions() {
203651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (Opts->AnalysisDiagOpt != PD_NONE) {
204651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      // Create the PathDiagnosticConsumer.
205651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      ClangDiagPathDiagConsumer *clangDiags =
206651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          new ClangDiagPathDiagConsumer(PP.getDiagnostics());
207651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      PathConsumers.push_back(clangDiags);
208651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
209651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (Opts->AnalysisDiagOpt == PD_TEXT) {
210651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        clangDiags->enablePaths();
211651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
212651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      } else if (!OutDir.empty()) {
213651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        switch (Opts->AnalysisDiagOpt) {
214651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        default:
215651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN)                    \
216651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  case PD_##NAME:                                                              \
217ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    CREATEFN(*Opts.get(), PathConsumers, OutDir, PP);                       \
218651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    break;
219987695a5ddd78beca784d4e503ffbc2dc879181aTed Kremenek#include "clang/StaticAnalyzer/Core/Analyses.def"
220651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        }
221fda7832b000ff8927386f093b52c067641679469Zhongxing Xu      }
2221d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    }
223fda7832b000ff8927386f093b52c067641679469Zhongxing Xu
2241d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    // Create the analyzer component creators.
22545796b10d11869e86c6b85e24df165410536b313Ted Kremenek    switch (Opts->AnalysisStoreOpt) {
2265f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis    default:
227b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      llvm_unreachable("Unknown store manager.");
228fda7832b000ff8927386f093b52c067641679469Zhongxing Xu#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN)           \
2295f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis      case NAME##Model: CreateStoreMgr = CREATEFN; break;
230987695a5ddd78beca784d4e503ffbc2dc879181aTed Kremenek#include "clang/StaticAnalyzer/Core/Analyses.def"
2311d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    }
2321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23345796b10d11869e86c6b85e24df165410536b313Ted Kremenek    switch (Opts->AnalysisConstraintsOpt) {
2345f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis    default:
2351c32b81765ed63ae9e3e00e5acef7a4379bba228Anna Zaks      llvm_unreachable("Unknown constraint manager.");
236fda7832b000ff8927386f093b52c067641679469Zhongxing Xu#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN)     \
2375f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis      case NAME##Model: CreateConstraintMgr = CREATEFN; break;
238987695a5ddd78beca784d4e503ffbc2dc879181aTed Kremenek#include "clang/StaticAnalyzer/Core/Analyses.def"
239fda7832b000ff8927386f093b52c067641679469Zhongxing Xu    }
2401d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  }
241f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
242a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks  void DisplayFunction(const Decl *D, AnalysisMode Mode,
243a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks                       ExprEngine::InliningModes IMode) {
24445796b10d11869e86c6b85e24df165410536b313Ted Kremenek    if (!Opts->AnalyzerDisplayProgress)
2451d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek      return;
246f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
247fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek    SourceManager &SM = Mgr->getASTContext().getSourceManager();
248fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek    PresumedLoc Loc = SM.getPresumedLoc(D->getLocation());
249cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor    if (Loc.isValid()) {
250aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      llvm::errs() << "ANALYZE";
2514eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose
2524eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      if (Mode == AM_Syntax)
2534eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose        llvm::errs() << " (Syntax)";
254a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks      else if (Mode == AM_Path) {
255a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks        llvm::errs() << " (Path, ";
256a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks        switch (IMode) {
2578a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks          case ExprEngine::Inline_Minimal:
2588a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks            llvm::errs() << " Inline_Minimal";
259a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks            break;
2608a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks          case ExprEngine::Inline_Regular:
2618a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks            llvm::errs() << " Inline_Regular";
262a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks            break;
263a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks        }
264a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks        llvm::errs() << ")";
265a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks      }
2664eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      else
2674eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose        assert(Mode == (AM_Syntax | AM_Path) && "Unexpected mode!");
2684eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose
269aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      llvm::errs() << ": " << Loc.getFilename();
270cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) {
271cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor        const NamedDecl *ND = cast<NamedDecl>(D);
272b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer        llvm::errs() << ' ' << *ND << '\n';
273cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      }
274cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      else if (isa<BlockDecl>(D)) {
275cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor        llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:"
276cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor                     << Loc.getColumn() << '\n';
277cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      }
278cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
279cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor        Selector S = MD->getSelector();
280cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor        llvm::errs() << ' ' << S.getAsString();
281cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor      }
28235fa76d0bb6fb8c86159a7506efd094a4fe376d2Ted Kremenek    }
2831d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  }
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
285651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void Initialize(ASTContext &Context) override {
2861d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    Ctx = &Context;
28745796b10d11869e86c6b85e24df165410536b313Ted Kremenek    checkerMgr.reset(createCheckerManager(*Opts, PP.getLangOpts(), Plugins,
28808b86531ade68727c56918f162816075b87c864aJordy Rose                                          PP.getDiagnostics()));
289c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek    Mgr.reset(new AnalysisManager(*Ctx,
290c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                  PP.getDiagnostics(),
291c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                  PP.getLangOpts(),
292c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                  PathConsumers,
293c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                  CreateStoreMgr,
294c4bac8e376b98d633bb00ee5f510d5e58449753cTed Kremenek                                  CreateConstraintMgr,
29543dee220252ef0b42c5f8a3bb1eca97f84f2565fArgyrios Kyrtzidis                                  checkerMgr.get(),
29645796b10d11869e86c6b85e24df165410536b313Ted Kremenek                                  *Opts));
2971d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  }
2981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2996a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// \brief Store the top level decls in the set to be processed later on.
3006a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// (Doing this pre-processing avoids deserialization of data from PCH.)
301651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  bool HandleTopLevelDecl(DeclGroupRef D) override;
302651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override;
3036a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
304651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  void HandleTranslationUnit(ASTContext &C) override;
30514cc9451de4a9539bf79e4e5d63248c2377426dbTed Kremenek
30675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  /// \brief Determine which inlining mode should be used when this function is
3078a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks  /// analyzed. This allows to redefine the default inlining policies when
3088a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks  /// analyzing a given function.
30975f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  ExprEngine::InliningModes
3106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  getInliningModeForFunction(const Decl *D, const SetOfConstDecls &Visited);
31175f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
3126a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// \brief Build the call graph for all the top level decls of this TU and
3136a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// use it to define the order in which the functions should be visited.
314469ba550f54577f28e23ea42c293308fbcad6609Jordan Rose  void HandleDeclsCallGraph(const unsigned LocalTUDeclsSize);
315aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
316aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// \brief Run analyzes(syntax or path sensitive) on the given function.
317aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// \param Mode - determines if we are requesting syntax only or path
318aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// sensitive only analysis.
319aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// \param VisitedCallees - The output parameter, which is populated with the
320aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// set of functions which should be considered analyzed after analyzing the
321aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// given root function.
3226a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  void HandleCode(Decl *D, AnalysisMode Mode,
3238a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks                  ExprEngine::InliningModes IMode = ExprEngine::Inline_Minimal,
3246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                  SetOfConstDecls *VisitedCallees = nullptr);
325aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
32675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  void RunPathSensitiveChecks(Decl *D,
32775f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                              ExprEngine::InliningModes IMode,
32875f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                              SetOfConstDecls *VisitedCallees);
3296a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  void ActionExprEngine(Decl *D, bool ObjCGCEnabled,
33075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                        ExprEngine::InliningModes IMode,
3316a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks                        SetOfConstDecls *VisitedCallees);
332aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
333aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// Visitors for the RecursiveASTVisitor.
334c8848f34bbde083b8d89f445eb605eaabf30d6a8Anna Zaks  bool shouldWalkTypesOfTypeLocs() const { return false; }
335aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
336aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  /// Handle callbacks for arbitrary Decls.
337aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  bool VisitDecl(Decl *D) {
3384eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    AnalysisMode Mode = getModeForDecl(D, RecVisitorMode);
3394eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    if (Mode & AM_Syntax)
3404eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      checkerMgr->runCheckersOnASTDecl(D, *Mgr, *RecVisitorBR);
341aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    return true;
342aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  }
343aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
344aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  bool VisitFunctionDecl(FunctionDecl *FD) {
345aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    IdentifierInfo *II = FD->getIdentifier();
346aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    if (II && II->getName().startswith("__inline"))
347aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      return true;
348aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
349aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    // We skip function template definitions, as their semantics is
350aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    // only determined when they are instantiated.
351aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    if (FD->isThisDeclarationADefinition() &&
352aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks        !FD->isDependentContext()) {
35375f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks      assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
354aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      HandleCode(FD, RecVisitorMode);
355aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    }
356aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    return true;
357aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  }
358aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
359aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  bool VisitObjCMethodDecl(ObjCMethodDecl *MD) {
36075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    if (MD->isThisDeclarationADefinition()) {
36175f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks      assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
362aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      HandleCode(MD, RecVisitorMode);
36375f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    }
364aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    return true;
365aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  }
3664f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks
3674f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks  bool VisitBlockDecl(BlockDecl *BD) {
3684f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks    if (BD->hasBody()) {
3694f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks      assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false);
3704f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks      HandleCode(BD, RecVisitorMode);
3714f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks    }
3724f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks    return true;
3734f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks  }
3746a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
375651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  virtual void
376651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  AddDiagnosticConsumer(PathDiagnosticConsumer *Consumer) override {
377651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    PathConsumers.push_back(Consumer);
378651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  }
379651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
3806a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaksprivate:
3816a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  void storeTopLevelDecls(DeclGroupRef DG);
3826a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
3836a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  /// \brief Check if we should skip (not analyze) the given function.
3844eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  AnalysisMode getModeForDecl(Decl *D, AnalysisMode Mode);
3856a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
3861d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek};
387f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek} // end anonymous namespace
388f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
389aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
390f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
391f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// AnalysisConsumer implementation.
392f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
3936bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesllvm::Timer* AnalysisConsumer::TUTotalTimer = nullptr;
394f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
3956a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaksbool AnalysisConsumer::HandleTopLevelDecl(DeclGroupRef DG) {
3966a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  storeTopLevelDecls(DG);
3976a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  return true;
3986a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks}
3996a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
4006a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaksvoid AnalysisConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) {
4016a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  storeTopLevelDecls(DG);
4026a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks}
4036a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
4046a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaksvoid AnalysisConsumer::storeTopLevelDecls(DeclGroupRef DG) {
4056a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) {
4066a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
4076a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks    // Skip ObjCMethodDecl, wait for the objc container to avoid
4086a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks    // analyzing twice.
4096a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks    if (isa<ObjCMethodDecl>(*I))
4106a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks      continue;
4116a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
412577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek    LocalTUDecls.push_back(*I);
4136a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks  }
4146a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks}
4156a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
416b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaksstatic bool shouldSkipFunction(const Decl *D,
4176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                               const SetOfConstDecls &Visited,
4186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                               const SetOfConstDecls &VisitedAsTopLevel) {
419b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  if (VisitedAsTopLevel.count(D))
42075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    return true;
42175f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
42275f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  // We want to re-analyse the functions as top level in the following cases:
423fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks  // - The 'init' methods should be reanalyzed because
424fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks  //   ObjCNonNilReturnValueChecker assumes that '[super init]' never returns
42575f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  //   'nil' and unless we analyze the 'init' functions as top level, we will
42675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  //   not catch errors within defensive code.
427fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks  // - We want to reanalyze all ObjC methods as top level to report Retain
428fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks  //   Count naming convention errors more aggressively.
429b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  if (isa<ObjCMethodDecl>(D))
430fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks    return false;
431fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks
432fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks  // Otherwise, if we visited the function before, do not reanalyze it.
433b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  return Visited.count(D);
434fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks}
435fbcb3f11fc90e9f00e6074e9b118b8dc11ca604cAnna Zaks
43675f31c4862643ab09479c979fabf754e7ffe1460Anna ZaksExprEngine::InliningModes
437b467051a9f212c57335f88f16e839e1dc8afc1c1Anna ZaksAnalysisConsumer::getInliningModeForFunction(const Decl *D,
4386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                             const SetOfConstDecls &Visited) {
43975f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  // We want to reanalyze all ObjC methods as top level to report Retain
4408a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks  // Count naming convention errors more aggressively. But we should tune down
44175f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  // inlining when reanalyzing an already inlined function.
442b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  if (Visited.count(D)) {
443b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    assert(isa<ObjCMethodDecl>(D) &&
44475f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks           "We are only reanalyzing ObjCMethods.");
445b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    const ObjCMethodDecl *ObjCM = cast<ObjCMethodDecl>(D);
44675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    if (ObjCM->getMethodFamily() != OMF_init)
4478a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks      return ExprEngine::Inline_Minimal;
44875f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  }
44975f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
4508a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks  return ExprEngine::Inline_Regular;
45175f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks}
45275f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
453469ba550f54577f28e23ea42c293308fbcad6609Jordan Rosevoid AnalysisConsumer::HandleDeclsCallGraph(const unsigned LocalTUDeclsSize) {
454bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks  // Build the Call Graph by adding all the top level declarations to the graph.
455694a942b0a14e02757a695142c45437eec6e3684Anna Zaks  // Note: CallGraph can trigger deserialization of more items from a pch
456694a942b0a14e02757a695142c45437eec6e3684Anna Zaks  // (though HandleInterestingDecl); triggering additions to LocalTUDecls.
457694a942b0a14e02757a695142c45437eec6e3684Anna Zaks  // We rely on random access to add the initially processed Decls to CG.
458bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks  CallGraph CG;
459694a942b0a14e02757a695142c45437eec6e3684Anna Zaks  for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) {
460eaa069075f060f58840af03e0bd5bd75bb27e809Anna Zaks    CG.addToCallGraph(LocalTUDecls[i]);
461eaa069075f060f58840af03e0bd5bd75bb27e809Anna Zaks  }
462d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
463bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks  // Walk over all of the call graph nodes in topological order, so that we
464bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks  // analyze parents before the children. Skip the functions inlined into
465bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks  // the previously processed functions. Use external Visited set to identify
466bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks  // inlined functions. The topological order allows the "do not reanalyze
467bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks  // previously inlined function" performance heuristic to be triggered more
468bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks  // often.
469b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  SetOfConstDecls Visited;
470b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks  SetOfConstDecls VisitedAsTopLevel;
471bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks  llvm::ReversePostOrderTraversal<clang::CallGraph*> RPOT(&CG);
472bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks  for (llvm::ReversePostOrderTraversal<clang::CallGraph*>::rpo_iterator
473bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks         I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
474bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks    NumFunctionTopLevel++;
475371b477836f289f2e9caaab58530f187b51bc86dTed Kremenek
476bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks    CallGraphNode *N = *I;
477b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    Decl *D = N->getDecl();
478bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks
479bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks    // Skip the abstract root node.
480bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks    if (!D)
481bd80231672a7418aa1a99d3dbbe1774205c88f74Anna Zaks      continue;
482b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks
4837fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    // Skip the functions which have been processed already or previously
4847fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    // inlined.
485b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    if (shouldSkipFunction(D, Visited, VisitedAsTopLevel))
4867fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks      continue;
4877fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
4887fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    // Analyze the function.
4896a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks    SetOfConstDecls VisitedCallees;
49075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks
491b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    HandleCode(D, AM_Path, getInliningModeForFunction(D, Visited),
4926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines               (Mgr->options.InliningMode == All ? nullptr : &VisitedCallees));
4937fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
4947fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    // Add the visited callees to the global visited set.
495cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek    for (SetOfConstDecls::iterator I = VisitedCallees.begin(),
496cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek                                   E = VisitedCallees.end(); I != E; ++I) {
497b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks        Visited.insert(*I);
4987fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    }
499b467051a9f212c57335f88f16e839e1dc8afc1c1Anna Zaks    VisitedAsTopLevel.insert(D);
500d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  }
501fee618af5dd7dee2caaa7347b372eb3dc5fdeffcTed Kremenek}
502fee618af5dd7dee2caaa7347b372eb3dc5fdeffcTed Kremenek
50314cc9451de4a9539bf79e4e5d63248c2377426dbTed Kremenekvoid AnalysisConsumer::HandleTranslationUnit(ASTContext &C) {
504aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // Don't run the actions if an error has occurred with parsing the file.
505aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  DiagnosticsEngine &Diags = PP.getDiagnostics();
506aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred())
507aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    return;
508aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
509c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks  {
510d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks    if (TUTotalTimer) TUTotalTimer->startTimer();
511d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks
512c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    // Introduce a scope to destroy BR before Mgr.
513c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    BugReporter BR(*Mgr);
514c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    TranslationUnitDecl *TU = C.getTranslationUnitDecl();
515c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR);
516aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
517aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    // Run the AST-only checks using the order in which functions are defined.
518aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    // If inlining is not turned on, use the simplest function order for path
519aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    // sensitive analyzes as well.
5204eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    RecVisitorMode = AM_Syntax;
5214eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    if (!Mgr->shouldInlineCall())
5224eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      RecVisitorMode |= AM_Path;
523aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    RecVisitorBR = &BR;
5246a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks
5256a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks    // Process all the top level declarations.
526cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek    //
527577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek    // Note: TraverseDecl may modify LocalTUDecls, but only by appending more
528577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek    // entries.  Thus we don't use an iterator, but rely on LocalTUDecls
529577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek    // random access.  By doing so, we automatically compensate for iterators
530577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek    // possibly being invalidated, although this is a bit slower.
531694a942b0a14e02757a695142c45437eec6e3684Anna Zaks    const unsigned LocalTUDeclsSize = LocalTUDecls.size();
532694a942b0a14e02757a695142c45437eec6e3684Anna Zaks    for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) {
533577f14a34457032523e59dbbbacb88ca2cd4db57Ted Kremenek      TraverseDecl(LocalTUDecls[i]);
534cb0a5039c243f5b0c178e70f424adac334e5789bTed Kremenek    }
535aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
536aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    if (Mgr->shouldInlineCall())
537469ba550f54577f28e23ea42c293308fbcad6609Jordan Rose      HandleDeclsCallGraph(LocalTUDeclsSize);
538b317f8f5ca8737a5bbad97a3f7566a2dbd2ed61bZhongxing Xu
539c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    // After all decls handled, run checkers on the entire TranslationUnit.
540c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks    checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR);
541aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
5426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    RecVisitorBR = nullptr;
543c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks  }
5449be6e7ce5788e50c62d40c59b0bbc2ea423683f7Ted Kremenek
545ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie  // Explicitly destroy the PathDiagnosticConsumer.  This will flush its output.
546690a7f431d6863a101711e67636d51ddd13f35c5Ted Kremenek  // FIXME: This should be replaced with something that doesn't rely on
547ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie  // side-effects in PathDiagnosticConsumer's destructor. This is required when
548da17fd50ad485fd2a1fc5c2f055caacf532992daZhongxing Xu  // used with option -disable-free.
5496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Mgr.reset(nullptr);
550d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks
551d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  if (TUTotalTimer) TUTotalTimer->stopTimer();
5526b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks
5536b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks  // Count how many basic blocks we have not covered.
5546b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks  NumBlocksInAnalyzedFunctions = FunctionSummaries.getTotalNumBasicBlocks();
5556b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks  if (NumBlocksInAnalyzedFunctions > 0)
5566b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks    PercentReachableBlocks =
5576b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks      (FunctionSummaries.getTotalNumVisitedBasicBlocks() * 100) /
5586b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks        NumBlocksInAnalyzedFunctions;
5596b77ce8824cf62c2cfb61cf2d801eb3fcfbecffeAnna Zaks
560db09a4dee28a4515438af60f2d2b4a83e4965c31Ted Kremenek}
561db09a4dee28a4515438af60f2d2b4a83e4965c31Ted Kremenek
562d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaksstatic std::string getFunctionName(const Decl *D) {
563d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) {
564d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    return ID->getSelector().getAsString();
565d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  }
566d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  if (const FunctionDecl *ND = dyn_cast<FunctionDecl>(D)) {
567d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    IdentifierInfo *II = ND->getIdentifier();
568d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    if (II)
569d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks      return II->getName();
570d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  }
571d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  return "";
572d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks}
573d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
5744eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan RoseAnalysisConsumer::AnalysisMode
5754eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan RoseAnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) {
57645796b10d11869e86c6b85e24df165410536b313Ted Kremenek  if (!Opts->AnalyzeSpecificFunction.empty() &&
57745796b10d11869e86c6b85e24df165410536b313Ted Kremenek      getFunctionName(D) != Opts->AnalyzeSpecificFunction)
5784eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    return AM_None;
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5804eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  // Unless -analyze-all is specified, treat decls differently depending on
5814eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  // where they came from:
5824eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  // - Main source file: run both path-sensitive and non-path-sensitive checks.
5834eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  // - Header files: run non-path-sensitive checks only.
5844eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  // - System headers: don't run any checks.
585fcd783d583d270b7ec1ec3e0fcf83cd93d30e381Ted Kremenek  SourceManager &SM = Ctx->getSourceManager();
586402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  SourceLocation SL = SM.getExpansionLoc(D->getLocation());
58724146975f1af8c1b4b14e8545f218129d0e7dfebEli Friedman  if (!Opts->AnalyzeAll && !SM.isInMainFile(SL)) {
5884eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    if (SL.isInvalid() || SM.isInSystemHeader(SL))
5894eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose      return AM_None;
5904eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose    return Mode & ~AM_Path;
5914eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  }
59298520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks
5934eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  return Mode;
59498520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks}
59598520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks
596aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksvoid AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode,
59775f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                                  ExprEngine::InliningModes IMode,
5986a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks                                  SetOfConstDecls *VisitedCallees) {
5994f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks  if (!D->hasBody())
6004f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks    return;
6014eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  Mode = getModeForDecl(D, Mode);
6024eff6b5d6a5cbb6bd669854a84365aa61d70819eJordan Rose  if (Mode == AM_None)
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return;
604f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
605a5245a51593aa45c2a5ef38a10861697d29c7789Anna Zaks  DisplayFunction(D, Mode, IMode);
60684c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks  CFG *DeclCFG = Mgr->getCFG(D);
60784c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks  if (DeclCFG) {
60884c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks    unsigned CFGSize = DeclCFG->size();
60984c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks    MaxCFGSize = MaxCFGSize < CFGSize ? CFGSize : MaxCFGSize;
61084c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks  }
61184c1f4b1edddf74f8080cfc470796c2c498b5d43Anna Zaks
6121d26f48dc2eea1c07431ca1519d7034a21b9bcffTed Kremenek  // Clear the AnalysisManager of old AnalysisDeclContexts.
61358f5ec7d56b1ebf5f90ee11226ebe7663f2821eaTed Kremenek  Mgr->ClearContexts();
6149fb9474c5b267400d4abfbff63c8b39f378235d4Argyrios Kyrtzidis  BugReporter BR(*Mgr);
6154f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks
6164f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks  if (Mode & AM_Syntax)
6174f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks    checkerMgr->runCheckersOnASTBody(D, *Mgr, BR);
6184f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks  if ((Mode & AM_Path) && checkerMgr->hasPathSensitiveCheckers()) {
6194f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks    RunPathSensitiveChecks(D, IMode, VisitedCallees);
6208a660eb1084294a903f6dcc00bf2fa4e3bc92cfcAnna Zaks    if (IMode != ExprEngine::Inline_Minimal)
6214f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks      NumFunctionsAnalyzed++;
6224f858dfd42c89b67200dac0afc228a0baa323691Anna Zaks  }
623f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek}
624f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
625f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
626d655ab28fdf7c940d3f79f8f287954d7f76e0977Argyrios Kyrtzidis// Path-sensitive checking.
627f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
628f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
6293fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaksvoid AnalysisConsumer::ActionExprEngine(Decl *D, bool ObjCGCEnabled,
63075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                                        ExprEngine::InliningModes IMode,
6316a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks                                        SetOfConstDecls *VisitedCallees) {
632a5937bbfd19e61d651a58b0f0ffeef68457902a5Ted Kremenek  // Construct the analysis engine.  First check if the CFG is valid.
63375d03cffe20b5c945ef04eba208efb0437339997Ted Kremenek  // FIXME: Inter-procedural analysis will need to handle invalid CFGs.
6343fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  if (!Mgr->getCFG(D))
635f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek    return;
6363fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks
637d4aeb8050a1d0fe47c53a73361c8b0b8ac310f46Ted Kremenek  // See if the LiveVariables analysis scales.
638d4aeb8050a1d0fe47c53a73361c8b0b8ac310f46Ted Kremenek  if (!Mgr->getAnalysisDeclContext(D)->getAnalysis<RelaxedLiveVariables>())
639d4aeb8050a1d0fe47c53a73361c8b0b8ac310f46Ted Kremenek    return;
640d4aeb8050a1d0fe47c53a73361c8b0b8ac310f46Ted Kremenek
64175f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks  ExprEngine Eng(*Mgr, ObjCGCEnabled, VisitedCallees, &FunctionSummaries,IMode);
642f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
643f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Set the graph auditor.
644651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::unique_ptr<ExplodedNode::Auditor> Auditor;
6453b8a04004afa46057a9af4afbd086be71d619793Ted Kremenek  if (Mgr->options.visualizeExplodedGraphWithUbiGraph) {
646f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    Auditor.reset(CreateUbiViz());
647c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xu    ExplodedNode::SetAuditor(Auditor.get());
648f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  }
6491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
650b35a74a40c1d2656a25e560e773ed48bdf49f9c0Ted Kremenek  // Execute the worklist algorithm.
651d200187bd27f9ad68699693a6e57f9ee3ff260faJordy Rose  Eng.ExecuteWorkList(Mgr->getAnalysisDeclContextManager().getStackFrame(D),
652ac3a3e7a402cd349dd2b7d70cd92c5fe702ae831Anna Zaks                      Mgr->options.getMaxNodesPerTopLevelFunction());
6531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
654f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Release the auditor (if any) so that it doesn't monitor the graph
655f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // created BugReporter.
6566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  ExplodedNode::SetAuditor(nullptr);
6573df6421150271266b5a90fd4c6bfa6566c38c036Ted Kremenek
65834d7734b6ed1d9c0f647405e065251eb67f42badTed Kremenek  // Visualize the exploded graph.
6593b8a04004afa46057a9af4afbd086be71d619793Ted Kremenek  if (Mgr->options.visualizeExplodedGraphWithGraphViz)
660255d4d4226b24036ceb11228fbb74286e58620f7Ted Kremenek    Eng.ViewGraph(Mgr->options.TrimGraph);
6611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6623df6421150271266b5a90fd4c6bfa6566c38c036Ted Kremenek  // Display warnings.
6633df6421150271266b5a90fd4c6bfa6566c38c036Ted Kremenek  Eng.getBugReporter().FlushReports();
664bc46f345838b1c0d420dbd3655c94f5f360fb5b8Ted Kremenek}
665bc46f345838b1c0d420dbd3655c94f5f360fb5b8Ted Kremenek
6666a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaksvoid AnalysisConsumer::RunPathSensitiveChecks(Decl *D,
66775f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks                                              ExprEngine::InliningModes IMode,
6686a86082f3a06a2dcceaaf63f78a0e52d64bcbaa3Anna Zaks                                              SetOfConstDecls *Visited) {
66917a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose
6704e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  switch (Mgr->getLangOpts().getGC()) {
67117a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  case LangOptions::NonGC:
67275f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    ActionExprEngine(D, false, IMode, Visited);
67317a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose    break;
67417a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose
67517a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  case LangOptions::GCOnly:
67675f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    ActionExprEngine(D, true, IMode, Visited);
67717a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose    break;
67817a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose
67917a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  case LangOptions::HybridGC:
68075f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    ActionExprEngine(D, false, IMode, Visited);
68175f31c4862643ab09479c979fabf754e7ffe1460Anna Zaks    ActionExprEngine(D, true, IMode, Visited);
68217a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose    break;
68317a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  }
684b35a74a40c1d2656a25e560e773ed48bdf49f9c0Ted Kremenek}
685b35a74a40c1d2656a25e560e773ed48bdf49f9c0Ted Kremenek
686f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
687f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// AnalysisConsumer creation.
688f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//===----------------------------------------------------------------------===//
689f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
690651f13cea278ec967336033dd032faef0e9fc2ecStephen HinesAnalysisASTConsumer *
691651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinesento::CreateAnalysisConsumer(const Preprocessor &pp, const std::string &outDir,
692651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                             AnalyzerOptionsRef opts,
693651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                             ArrayRef<std::string> plugins) {
69408b86531ade68727c56918f162816075b87c864aJordy Rose  // Disable the effects of '-Werror' when using the AnalysisConsumer.
695efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar  pp.getDiagnostics().setWarningsAsErrors(false);
696be1fe1eb12a1cb91c8e3a9fcc2db4dfe989def6c