Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1 | //===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // "Meta" ASTConsumer for running different source analyses. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Alexander Kornienko | 6de3949 | 2014-01-03 17:23:10 +0000 | [diff] [blame] | 13 | #include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 14 | #include "ModelInjector.h" |
Artem Dergachev | f0bb45f | 2019-09-11 20:54:27 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
Zhongxing Xu | 686b845 | 2009-12-16 05:29:59 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/RecursiveASTVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Daniel Dunbar | b5f2025 | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/CFG.h" |
Anna Zaks | eee9110 | 2012-03-08 23:16:38 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/CallGraph.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/CodeInjector.h" |
Daniel Dunbar | b5f2025 | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Ilya Biryukov | 8b9b3bd | 2018-03-01 14:54:16 +0000 | [diff] [blame] | 25 | #include "clang/CrossTU/CrossTranslationUnit.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CompilerInstance.h" |
Daniel Dunbar | b5f2025 | 2009-11-05 02:41:58 +0000 | [diff] [blame] | 27 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 28 | #include "clang/StaticAnalyzer/Checkers/LocalCheckers.h" |
| 29 | #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h" |
| 30 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 32 | #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h" |
| 33 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
| 34 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" |
| 35 | #include "clang/StaticAnalyzer/Frontend/CheckerRegistration.h" |
Chandler Carruth | 44eb4f6 | 2013-01-02 10:28:36 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/PostOrderIterator.h" |
Anna Zaks | b028654 | 2012-02-27 21:33:16 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/Statistic.h" |
Rafael Espindola | 4168ee7 | 2013-06-26 06:13:06 +0000 | [diff] [blame] | 38 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Path.h" |
| 40 | #include "llvm/Support/Program.h" |
| 41 | #include "llvm/Support/Timer.h" |
| 42 | #include "llvm/Support/raw_ostream.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 43 | #include <memory> |
Anna Zaks | ca70ed5 | 2012-03-13 19:32:13 +0000 | [diff] [blame] | 44 | #include <queue> |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 45 | #include <utility> |
Anna Zaks | ca70ed5 | 2012-03-13 19:32:13 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 47 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 48 | using namespace ento; |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 49 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 50 | #define DEBUG_TYPE "AnalysisConsumer" |
| 51 | |
Anna Zaks | 394d07e | 2012-03-09 21:14:01 +0000 | [diff] [blame] | 52 | STATISTIC(NumFunctionTopLevel, "The # of functions at top level."); |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 53 | STATISTIC(NumFunctionsAnalyzed, |
Anna Zaks | ad3704c | 2012-12-17 20:08:54 +0000 | [diff] [blame] | 54 | "The # of functions and blocks analyzed (as top level " |
| 55 | "with inlining turned on)."); |
Anna Zaks | cc24e45 | 2012-04-03 02:05:47 +0000 | [diff] [blame] | 56 | STATISTIC(NumBlocksInAnalyzedFunctions, |
Anna Zaks | ad3704c | 2012-12-17 20:08:54 +0000 | [diff] [blame] | 57 | "The # of basic blocks in the analyzed functions."); |
George Karpenkov | 2a63985 | 2018-02-09 23:37:47 +0000 | [diff] [blame] | 58 | STATISTIC(NumVisitedBlocksInAnalyzedFunctions, |
| 59 | "The # of visited basic blocks in the analyzed functions."); |
Anna Zaks | cc24e45 | 2012-04-03 02:05:47 +0000 | [diff] [blame] | 60 | STATISTIC(PercentReachableBlocks, "The % of reachable basic blocks."); |
Anna Zaks | 40b87fc | 2012-07-05 20:44:02 +0000 | [diff] [blame] | 61 | STATISTIC(MaxCFGSize, "The maximum number of basic blocks in a function."); |
Anna Zaks | eee9110 | 2012-03-08 23:16:38 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | b535181 | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 63 | //===----------------------------------------------------------------------===// |
David Blaikie | 0cc4943 | 2011-09-27 01:43:33 +0000 | [diff] [blame] | 64 | // Special PathDiagnosticConsumers. |
Ted Kremenek | 04ade6f | 2009-07-27 22:13:39 +0000 | [diff] [blame] | 65 | //===----------------------------------------------------------------------===// |
| 66 | |
Balazs Keri | 32f220c | 2019-07-23 07:04:20 +0000 | [diff] [blame] | 67 | void ento::createPlistHTMLDiagnosticConsumer( |
| 68 | AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C, |
| 69 | const std::string &prefix, const Preprocessor &PP, |
| 70 | const cross_tu::CrossTranslationUnitContext &CTU) { |
Ted Kremenek | 3a081a0 | 2012-12-19 01:35:35 +0000 | [diff] [blame] | 71 | createHTMLDiagnosticConsumer(AnalyzerOpts, C, |
Balazs Keri | 32f220c | 2019-07-23 07:04:20 +0000 | [diff] [blame] | 72 | llvm::sys::path::parent_path(prefix), PP, CTU); |
| 73 | createPlistMultiFileDiagnosticConsumer(AnalyzerOpts, C, prefix, PP, CTU); |
Ted Kremenek | 04ade6f | 2009-07-27 22:13:39 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Balazs Keri | 32f220c | 2019-07-23 07:04:20 +0000 | [diff] [blame] | 76 | void ento::createTextPathDiagnosticConsumer( |
| 77 | AnalyzerOptions &AnalyzerOpts, PathDiagnosticConsumers &C, |
| 78 | const std::string &Prefix, const clang::Preprocessor &PP, |
| 79 | const cross_tu::CrossTranslationUnitContext &CTU) { |
Jordan Rose | 367843a | 2013-08-16 01:06:30 +0000 | [diff] [blame] | 80 | llvm_unreachable("'text' consumer should be enabled on ClangDiags"); |
| 81 | } |
| 82 | |
Ted Kremenek | 9bf9af9 | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 83 | namespace { |
| 84 | class ClangDiagPathDiagConsumer : public PathDiagnosticConsumer { |
| 85 | DiagnosticsEngine &Diag; |
Artem Dergachev | 6cee434 | 2019-09-06 20:55:29 +0000 | [diff] [blame] | 86 | bool IncludePath = false, ShouldEmitAsError = false, FixitsAsRemarks = false; |
Keno Fischer | 6f48c07 | 2019-06-07 23:34:00 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 9bf9af9 | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 88 | public: |
Jordan Rose | 367843a | 2013-08-16 01:06:30 +0000 | [diff] [blame] | 89 | ClangDiagPathDiagConsumer(DiagnosticsEngine &Diag) |
Artem Dergachev | 6cee434 | 2019-09-06 20:55:29 +0000 | [diff] [blame] | 90 | : Diag(Diag) {} |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 91 | ~ClangDiagPathDiagConsumer() override {} |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 92 | StringRef getName() const override { return "ClangDiags"; } |
Jordan Rose | 367843a | 2013-08-16 01:06:30 +0000 | [diff] [blame] | 93 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 94 | bool supportsLogicalOpControlFlow() const override { return true; } |
| 95 | bool supportsCrossFileDiagnostics() const override { return true; } |
Jordan Rose | 367843a | 2013-08-16 01:06:30 +0000 | [diff] [blame] | 96 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 97 | PathGenerationScheme getGenerationScheme() const override { |
Jordan Rose | 367843a | 2013-08-16 01:06:30 +0000 | [diff] [blame] | 98 | return IncludePath ? Minimal : None; |
| 99 | } |
| 100 | |
Artem Dergachev | 6cee434 | 2019-09-06 20:55:29 +0000 | [diff] [blame] | 101 | void enablePaths() { IncludePath = true; } |
Keno Fischer | 6f48c07 | 2019-06-07 23:34:00 +0000 | [diff] [blame] | 102 | void enableWerror() { ShouldEmitAsError = true; } |
Artem Dergachev | 6cee434 | 2019-09-06 20:55:29 +0000 | [diff] [blame] | 103 | void enableFixitsAsRemarks() { FixitsAsRemarks = true; } |
Keno Fischer | 6f48c07 | 2019-06-07 23:34:00 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 9bf9af9 | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 105 | void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags, |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 106 | FilesMade *filesMade) override { |
Keno Fischer | 6f48c07 | 2019-06-07 23:34:00 +0000 | [diff] [blame] | 107 | unsigned WarnID = |
| 108 | ShouldEmitAsError |
| 109 | ? Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0") |
| 110 | : Diag.getCustomDiagID(DiagnosticsEngine::Warning, "%0"); |
Alp Toker | 5c494cb | 2013-12-23 17:59:59 +0000 | [diff] [blame] | 111 | unsigned NoteID = Diag.getCustomDiagID(DiagnosticsEngine::Note, "%0"); |
Artem Dergachev | 6cee434 | 2019-09-06 20:55:29 +0000 | [diff] [blame] | 112 | unsigned RemarkID = Diag.getCustomDiagID(DiagnosticsEngine::Remark, "%0"); |
Alp Toker | 5c494cb | 2013-12-23 17:59:59 +0000 | [diff] [blame] | 113 | |
Artem Dergachev | 6cee434 | 2019-09-06 20:55:29 +0000 | [diff] [blame] | 114 | auto reportPiece = |
| 115 | [&](unsigned ID, SourceLocation Loc, StringRef String, |
| 116 | ArrayRef<SourceRange> Ranges, ArrayRef<FixItHint> Fixits) { |
| 117 | if (!FixitsAsRemarks) { |
| 118 | Diag.Report(Loc, ID) << String << Ranges << Fixits; |
| 119 | } else { |
| 120 | Diag.Report(Loc, ID) << String << Ranges; |
| 121 | for (const FixItHint &Hint : Fixits) { |
| 122 | SourceManager &SM = Diag.getSourceManager(); |
| 123 | llvm::SmallString<128> Str; |
| 124 | llvm::raw_svector_ostream OS(Str); |
| 125 | // FIXME: Add support for InsertFromRange and |
| 126 | // BeforePreviousInsertion. |
| 127 | assert(!Hint.InsertFromRange.isValid() && "Not implemented yet!"); |
| 128 | assert(!Hint.BeforePreviousInsertions && "Not implemented yet!"); |
| 129 | OS << SM.getSpellingColumnNumber(Hint.RemoveRange.getBegin()) |
| 130 | << "-" << SM.getSpellingColumnNumber(Hint.RemoveRange.getEnd()) |
| 131 | << ": '" << Hint.CodeToInsert << "'"; |
| 132 | Diag.Report(Loc, RemarkID) << OS.str(); |
| 133 | } |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | for (std::vector<const PathDiagnostic *>::iterator I = Diags.begin(), |
| 138 | E = Diags.end(); |
| 139 | I != E; ++I) { |
Ted Kremenek | 9bf9af9 | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 140 | const PathDiagnostic *PD = *I; |
Artem Dergachev | 6cee434 | 2019-09-06 20:55:29 +0000 | [diff] [blame] | 141 | reportPiece(WarnID, PD->getLocation().asLocation(), |
| 142 | PD->getShortDescription(), PD->path.back()->getRanges(), |
| 143 | PD->path.back()->getFixits()); |
Ted Kremenek | 9bf9af9 | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 144 | |
Artem Dergachev | b1991c5 | 2016-10-07 19:25:10 +0000 | [diff] [blame] | 145 | // First, add extra notes, even if paths should not be included. |
| 146 | for (const auto &Piece : PD->path) { |
| 147 | if (!isa<PathDiagnosticNotePiece>(Piece.get())) |
| 148 | continue; |
| 149 | |
Artem Dergachev | 6cee434 | 2019-09-06 20:55:29 +0000 | [diff] [blame] | 150 | reportPiece(NoteID, Piece->getLocation().asLocation(), |
| 151 | Piece->getString(), Piece->getRanges(), Piece->getFixits()); |
Artem Dergachev | b1991c5 | 2016-10-07 19:25:10 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Jordan Rose | 367843a | 2013-08-16 01:06:30 +0000 | [diff] [blame] | 154 | if (!IncludePath) |
| 155 | continue; |
Ted Kremenek | 9bf9af9 | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 156 | |
Artem Dergachev | b1991c5 | 2016-10-07 19:25:10 +0000 | [diff] [blame] | 157 | // Then, add the path notes if necessary. |
Jordan Rose | 367843a | 2013-08-16 01:06:30 +0000 | [diff] [blame] | 158 | PathPieces FlatPath = PD->path.flatten(/*ShouldFlattenMacros=*/true); |
Artem Dergachev | b1991c5 | 2016-10-07 19:25:10 +0000 | [diff] [blame] | 159 | for (const auto &Piece : FlatPath) { |
| 160 | if (isa<PathDiagnosticNotePiece>(Piece.get())) |
| 161 | continue; |
| 162 | |
Artem Dergachev | 6cee434 | 2019-09-06 20:55:29 +0000 | [diff] [blame] | 163 | reportPiece(NoteID, Piece->getLocation().asLocation(), |
| 164 | Piece->getString(), Piece->getRanges(), Piece->getFixits()); |
Ted Kremenek | 9bf9af9 | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | }; |
| 169 | } // end anonymous namespace |
| 170 | |
Ted Kremenek | 04ade6f | 2009-07-27 22:13:39 +0000 | [diff] [blame] | 171 | //===----------------------------------------------------------------------===// |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 172 | // AnalysisConsumer declaration. |
| 173 | //===----------------------------------------------------------------------===// |
| 174 | |
| 175 | namespace { |
| 176 | |
Alexander Kornienko | 7a712ce | 2014-02-03 18:37:50 +0000 | [diff] [blame] | 177 | class AnalysisConsumer : public AnalysisASTConsumer, |
Richard Smith | 5066845 | 2015-11-24 03:55:01 +0000 | [diff] [blame] | 178 | public RecursiveASTVisitor<AnalysisConsumer> { |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 179 | enum { |
| 180 | AM_None = 0, |
| 181 | AM_Syntax = 0x1, |
| 182 | AM_Path = 0x2 |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 183 | }; |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 184 | typedef unsigned AnalysisMode; |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 185 | |
| 186 | /// Mode of the analyzes while recursively visiting Decls. |
| 187 | AnalysisMode RecVisitorMode; |
| 188 | /// Bug Reporter to use while recursively visiting Decls. |
| 189 | BugReporter *RecVisitorBR; |
| 190 | |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 191 | std::vector<std::function<void(CheckerRegistry &)>> CheckerRegistrationFns; |
| 192 | |
Zhongxing Xu | 685a1d8 | 2010-04-30 04:14:20 +0000 | [diff] [blame] | 193 | public: |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 194 | ASTContext *Ctx; |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 195 | const Preprocessor &PP; |
| 196 | const std::string OutDir; |
Ted Kremenek | 40ea0ea | 2012-08-31 04:36:05 +0000 | [diff] [blame] | 197 | AnalyzerOptionsRef Opts; |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 198 | ArrayRef<std::string> Plugins; |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 199 | CodeInjector *Injector; |
Ilya Biryukov | 8b9b3bd | 2018-03-01 14:54:16 +0000 | [diff] [blame] | 200 | cross_tu::CrossTranslationUnitContext CTU; |
Zhongxing Xu | b9a45a4 | 2009-08-03 03:27:37 +0000 | [diff] [blame] | 201 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 202 | /// Stores the declarations from the local translation unit. |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 203 | /// Note, we pre-compute the local declarations at parse time as an |
| 204 | /// optimization to make sure we do not deserialize everything from disk. |
| 205 | /// The local declaration to all declarations ratio might be very small when |
| 206 | /// working with a PCH file. |
| 207 | SetOfDecls LocalTUDecls; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | 9bf9af9 | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 209 | // Set of PathDiagnosticConsumers. Owned by AnalysisManager. |
| 210 | PathDiagnosticConsumers PathConsumers; |
Zhongxing Xu | b9a45a4 | 2009-08-03 03:27:37 +0000 | [diff] [blame] | 211 | |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 212 | StoreManagerCreator CreateStoreMgr; |
| 213 | ConstraintManagerCreator CreateConstraintMgr; |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 214 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 215 | std::unique_ptr<CheckerManager> checkerMgr; |
| 216 | std::unique_ptr<AnalysisManager> Mgr; |
Zhongxing Xu | bcbe44f | 2009-08-03 03:13:46 +0000 | [diff] [blame] | 217 | |
Anna Zaks | 9bd4be9 | 2012-03-05 20:53:59 +0000 | [diff] [blame] | 218 | /// Time the analyzes time of each translation unit. |
George Karpenkov | 5a755b3 | 2018-02-10 01:49:20 +0000 | [diff] [blame] | 219 | std::unique_ptr<llvm::TimerGroup> AnalyzerTimers; |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 220 | std::unique_ptr<llvm::Timer> SyntaxCheckTimer; |
| 221 | std::unique_ptr<llvm::Timer> ExprEngineTimer; |
| 222 | std::unique_ptr<llvm::Timer> BugReporterTimer; |
Anna Zaks | 9bd4be9 | 2012-03-05 20:53:59 +0000 | [diff] [blame] | 223 | |
Anna Zaks | 54fd4a0 | 2012-03-30 05:48:10 +0000 | [diff] [blame] | 224 | /// The information about analyzed functions shared throughout the |
| 225 | /// translation unit. |
| 226 | FunctionSummariesTy FunctionSummaries; |
| 227 | |
Ilya Biryukov | 8b9b3bd | 2018-03-01 14:54:16 +0000 | [diff] [blame] | 228 | AnalysisConsumer(CompilerInstance &CI, const std::string &outdir, |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 229 | AnalyzerOptionsRef opts, ArrayRef<std::string> plugins, |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 230 | CodeInjector *injector) |
Ilya Biryukov | 8b9b3bd | 2018-03-01 14:54:16 +0000 | [diff] [blame] | 231 | : RecVisitorMode(0), RecVisitorBR(nullptr), Ctx(nullptr), |
| 232 | PP(CI.getPreprocessor()), OutDir(outdir), Opts(std::move(opts)), |
| 233 | Plugins(plugins), Injector(injector), CTU(CI) { |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 234 | DigestAnalyzerOptions(); |
Kristof Umann | 549f9cd | 2018-11-30 20:44:00 +0000 | [diff] [blame] | 235 | if (Opts->PrintStats || Opts->ShouldSerializeStats) { |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 236 | AnalyzerTimers = std::make_unique<llvm::TimerGroup>( |
George Karpenkov | 5a755b3 | 2018-02-10 01:49:20 +0000 | [diff] [blame] | 237 | "analyzer", "Analyzer timers"); |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 238 | SyntaxCheckTimer = std::make_unique<llvm::Timer>( |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 239 | "syntaxchecks", "Syntax-based analysis time", *AnalyzerTimers); |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 240 | ExprEngineTimer = std::make_unique<llvm::Timer>( |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 241 | "exprengine", "Path exploration time", *AnalyzerTimers); |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 242 | BugReporterTimer = std::make_unique<llvm::Timer>( |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 243 | "bugreporter", "Path-sensitive report post-processing time", |
| 244 | *AnalyzerTimers); |
George Karpenkov | 5a755b3 | 2018-02-10 01:49:20 +0000 | [diff] [blame] | 245 | llvm::EnableStatistics(/* PrintOnExit= */ false); |
Anna Zaks | 9bd4be9 | 2012-03-05 20:53:59 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 249 | ~AnalysisConsumer() override { |
Matthias Braun | abb6eea | 2016-09-26 18:53:34 +0000 | [diff] [blame] | 250 | if (Opts->PrintStats) { |
Matthias Braun | abb6eea | 2016-09-26 18:53:34 +0000 | [diff] [blame] | 251 | llvm::PrintStatistics(); |
| 252 | } |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 253 | } |
Zhongxing Xu | 4b03d49 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 254 | |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 255 | void DigestAnalyzerOptions() { |
Alexander Kornienko | 7a712ce | 2014-02-03 18:37:50 +0000 | [diff] [blame] | 256 | if (Opts->AnalysisDiagOpt != PD_NONE) { |
| 257 | // Create the PathDiagnosticConsumer. |
| 258 | ClangDiagPathDiagConsumer *clangDiags = |
| 259 | new ClangDiagPathDiagConsumer(PP.getDiagnostics()); |
| 260 | PathConsumers.push_back(clangDiags); |
Ted Kremenek | 9bf9af9 | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 261 | |
Keno Fischer | 6f48c07 | 2019-06-07 23:34:00 +0000 | [diff] [blame] | 262 | if (Opts->AnalyzerWerror) |
| 263 | clangDiags->enableWerror(); |
| 264 | |
Artem Dergachev | 6cee434 | 2019-09-06 20:55:29 +0000 | [diff] [blame] | 265 | if (Opts->ShouldEmitFixItHintsAsRemarks) |
| 266 | clangDiags->enableFixitsAsRemarks(); |
| 267 | |
Alexander Kornienko | 7a712ce | 2014-02-03 18:37:50 +0000 | [diff] [blame] | 268 | if (Opts->AnalysisDiagOpt == PD_TEXT) { |
| 269 | clangDiags->enablePaths(); |
Jordan Rose | 367843a | 2013-08-16 01:06:30 +0000 | [diff] [blame] | 270 | |
Alexander Kornienko | 7a712ce | 2014-02-03 18:37:50 +0000 | [diff] [blame] | 271 | } else if (!OutDir.empty()) { |
| 272 | switch (Opts->AnalysisDiagOpt) { |
| 273 | default: |
| 274 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN) \ |
| 275 | case PD_##NAME: \ |
Balazs Keri | 32f220c | 2019-07-23 07:04:20 +0000 | [diff] [blame] | 276 | CREATEFN(*Opts.get(), PathConsumers, OutDir, PP, CTU); \ |
Alexander Kornienko | 7a712ce | 2014-02-03 18:37:50 +0000 | [diff] [blame] | 277 | break; |
Ted Kremenek | a5770cd | 2012-08-31 04:35:58 +0000 | [diff] [blame] | 278 | #include "clang/StaticAnalyzer/Core/Analyses.def" |
Alexander Kornienko | 7a712ce | 2014-02-03 18:37:50 +0000 | [diff] [blame] | 279 | } |
Zhongxing Xu | 4b03d49 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 280 | } |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 281 | } |
Zhongxing Xu | 4b03d49 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 283 | // Create the analyzer component creators. |
Ted Kremenek | 40ea0ea | 2012-08-31 04:36:05 +0000 | [diff] [blame] | 284 | switch (Opts->AnalysisStoreOpt) { |
Argyrios Kyrtzidis | 4ec3cf9 | 2011-02-14 18:13:17 +0000 | [diff] [blame] | 285 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 286 | llvm_unreachable("Unknown store manager."); |
Zhongxing Xu | 4b03d49 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 287 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \ |
Argyrios Kyrtzidis | 4ec3cf9 | 2011-02-14 18:13:17 +0000 | [diff] [blame] | 288 | case NAME##Model: CreateStoreMgr = CREATEFN; break; |
Ted Kremenek | a5770cd | 2012-08-31 04:35:58 +0000 | [diff] [blame] | 289 | #include "clang/StaticAnalyzer/Core/Analyses.def" |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 290 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | 40ea0ea | 2012-08-31 04:36:05 +0000 | [diff] [blame] | 292 | switch (Opts->AnalysisConstraintsOpt) { |
Argyrios Kyrtzidis | 4ec3cf9 | 2011-02-14 18:13:17 +0000 | [diff] [blame] | 293 | default: |
Anna Zaks | 297176c | 2013-02-07 23:29:20 +0000 | [diff] [blame] | 294 | llvm_unreachable("Unknown constraint manager."); |
Zhongxing Xu | 4b03d49 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 295 | #define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \ |
Argyrios Kyrtzidis | 4ec3cf9 | 2011-02-14 18:13:17 +0000 | [diff] [blame] | 296 | case NAME##Model: CreateConstraintMgr = CREATEFN; break; |
Ted Kremenek | a5770cd | 2012-08-31 04:35:58 +0000 | [diff] [blame] | 297 | #include "clang/StaticAnalyzer/Core/Analyses.def" |
Zhongxing Xu | 4b03d49 | 2009-07-30 09:11:52 +0000 | [diff] [blame] | 298 | } |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 299 | } |
Ted Kremenek | 39df94b | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 300 | |
Anna Zaks | e9eb13a | 2013-02-02 00:30:02 +0000 | [diff] [blame] | 301 | void DisplayFunction(const Decl *D, AnalysisMode Mode, |
| 302 | ExprEngine::InliningModes IMode) { |
Ted Kremenek | 40ea0ea | 2012-08-31 04:36:05 +0000 | [diff] [blame] | 303 | if (!Opts->AnalyzerDisplayProgress) |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 304 | return; |
Ted Kremenek | 39df94b | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 305 | |
Ted Kremenek | 6818991 | 2009-12-07 22:06:12 +0000 | [diff] [blame] | 306 | SourceManager &SM = Mgr->getASTContext().getSourceManager(); |
| 307 | PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); |
Douglas Gregor | 453b012 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 308 | if (Loc.isValid()) { |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 309 | llvm::errs() << "ANALYZE"; |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 310 | |
| 311 | if (Mode == AM_Syntax) |
| 312 | llvm::errs() << " (Syntax)"; |
Anna Zaks | e9eb13a | 2013-02-02 00:30:02 +0000 | [diff] [blame] | 313 | else if (Mode == AM_Path) { |
| 314 | llvm::errs() << " (Path, "; |
| 315 | switch (IMode) { |
Anna Zaks | b13d21b | 2013-03-26 18:57:58 +0000 | [diff] [blame] | 316 | case ExprEngine::Inline_Minimal: |
| 317 | llvm::errs() << " Inline_Minimal"; |
Anna Zaks | e9eb13a | 2013-02-02 00:30:02 +0000 | [diff] [blame] | 318 | break; |
Anna Zaks | b13d21b | 2013-03-26 18:57:58 +0000 | [diff] [blame] | 319 | case ExprEngine::Inline_Regular: |
| 320 | llvm::errs() << " Inline_Regular"; |
Anna Zaks | e9eb13a | 2013-02-02 00:30:02 +0000 | [diff] [blame] | 321 | break; |
| 322 | } |
| 323 | llvm::errs() << ")"; |
| 324 | } |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 325 | else |
| 326 | assert(Mode == (AM_Syntax | AM_Path) && "Unexpected mode!"); |
| 327 | |
Artem Dergachev | 6a76a16 | 2016-08-08 16:01:02 +0000 | [diff] [blame] | 328 | llvm::errs() << ": " << Loc.getFilename() << ' ' |
| 329 | << getFunctionName(D) << '\n'; |
Ted Kremenek | 7043fba | 2010-10-22 22:08:29 +0000 | [diff] [blame] | 330 | } |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 331 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 333 | void Initialize(ASTContext &Context) override { |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 334 | Ctx = &Context; |
George Karpenkov | 4ece68a | 2018-08-06 23:09:07 +0000 | [diff] [blame] | 335 | checkerMgr = createCheckerManager( |
| 336 | *Ctx, *Opts, Plugins, CheckerRegistrationFns, PP.getDiagnostics()); |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 337 | |
Dmitri Gribenko | 6b9d7c9 | 2019-08-21 08:48:24 +0000 | [diff] [blame] | 338 | Mgr = std::make_unique<AnalysisManager>(*Ctx, PathConsumers, CreateStoreMgr, |
| 339 | CreateConstraintMgr, |
| 340 | checkerMgr.get(), *Opts, Injector); |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 341 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 343 | /// Store the top level decls in the set to be processed later on. |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 344 | /// (Doing this pre-processing avoids deserialization of data from PCH.) |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 345 | bool HandleTopLevelDecl(DeclGroupRef D) override; |
| 346 | void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override; |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 347 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 348 | void HandleTranslationUnit(ASTContext &C) override; |
Ted Kremenek | a1ec4f3 | 2011-01-20 17:09:48 +0000 | [diff] [blame] | 349 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 350 | /// Determine which inlining mode should be used when this function is |
Anna Zaks | b13d21b | 2013-03-26 18:57:58 +0000 | [diff] [blame] | 351 | /// analyzed. This allows to redefine the default inlining policies when |
| 352 | /// analyzing a given function. |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 353 | ExprEngine::InliningModes |
Anna Zaks | d5478fd | 2014-08-29 20:01:38 +0000 | [diff] [blame] | 354 | getInliningModeForFunction(const Decl *D, const SetOfConstDecls &Visited); |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 355 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 356 | /// Build the call graph for all the top level decls of this TU and |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 357 | /// use it to define the order in which the functions should be visited. |
Jordan Rose | 85c8c81 | 2012-10-10 17:55:37 +0000 | [diff] [blame] | 358 | void HandleDeclsCallGraph(const unsigned LocalTUDeclsSize); |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 359 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 360 | /// Run analyzes(syntax or path sensitive) on the given function. |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 361 | /// \param Mode - determines if we are requesting syntax only or path |
| 362 | /// sensitive only analysis. |
| 363 | /// \param VisitedCallees - The output parameter, which is populated with the |
| 364 | /// set of functions which should be considered analyzed after analyzing the |
| 365 | /// given root function. |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 366 | void HandleCode(Decl *D, AnalysisMode Mode, |
Anna Zaks | b13d21b | 2013-03-26 18:57:58 +0000 | [diff] [blame] | 367 | ExprEngine::InliningModes IMode = ExprEngine::Inline_Minimal, |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 368 | SetOfConstDecls *VisitedCallees = nullptr); |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 369 | |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 370 | void RunPathSensitiveChecks(Decl *D, |
| 371 | ExprEngine::InliningModes IMode, |
| 372 | SetOfConstDecls *VisitedCallees); |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 373 | |
| 374 | /// Visitors for the RecursiveASTVisitor. |
Anna Zaks | e7e7c9e | 2012-05-11 23:15:18 +0000 | [diff] [blame] | 375 | bool shouldWalkTypesOfTypeLocs() const { return false; } |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 376 | |
| 377 | /// Handle callbacks for arbitrary Decls. |
| 378 | bool VisitDecl(Decl *D) { |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 379 | AnalysisMode Mode = getModeForDecl(D, RecVisitorMode); |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 380 | if (Mode & AM_Syntax) { |
| 381 | if (SyntaxCheckTimer) |
| 382 | SyntaxCheckTimer->startTimer(); |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 383 | checkerMgr->runCheckersOnASTDecl(D, *Mgr, *RecVisitorBR); |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 384 | if (SyntaxCheckTimer) |
| 385 | SyntaxCheckTimer->stopTimer(); |
| 386 | } |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 387 | return true; |
| 388 | } |
| 389 | |
Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 390 | bool VisitVarDecl(VarDecl *VD) { |
| 391 | if (!Opts->IsNaiveCTUEnabled) |
| 392 | return true; |
| 393 | |
| 394 | if (VD->hasExternalStorage() || VD->isStaticDataMember()) { |
| 395 | if (!cross_tu::containsConst(VD, *Ctx)) |
| 396 | return true; |
| 397 | } else { |
| 398 | // Cannot be initialized in another TU. |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | if (VD->getAnyInitializer()) |
| 403 | return true; |
| 404 | |
| 405 | llvm::Expected<const VarDecl *> CTUDeclOrError = |
Keno Fischer | 6f48c07 | 2019-06-07 23:34:00 +0000 | [diff] [blame] | 406 | CTU.getCrossTUDefinition(VD, Opts->CTUDir, Opts->CTUIndexName, |
Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame] | 407 | Opts->DisplayCTUProgress); |
| 408 | |
| 409 | if (!CTUDeclOrError) { |
| 410 | handleAllErrors(CTUDeclOrError.takeError(), |
| 411 | [&](const cross_tu::IndexError &IE) { |
| 412 | CTU.emitCrossTUDiagnostics(IE); |
| 413 | }); |
| 414 | } |
| 415 | |
| 416 | return true; |
| 417 | } |
| 418 | |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 419 | bool VisitFunctionDecl(FunctionDecl *FD) { |
| 420 | IdentifierInfo *II = FD->getIdentifier(); |
| 421 | if (II && II->getName().startswith("__inline")) |
| 422 | return true; |
| 423 | |
| 424 | // We skip function template definitions, as their semantics is |
| 425 | // only determined when they are instantiated. |
| 426 | if (FD->isThisDeclarationADefinition() && |
| 427 | !FD->isDependentContext()) { |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 428 | assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false); |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 429 | HandleCode(FD, RecVisitorMode); |
| 430 | } |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | bool VisitObjCMethodDecl(ObjCMethodDecl *MD) { |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 435 | if (MD->isThisDeclarationADefinition()) { |
| 436 | assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false); |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 437 | HandleCode(MD, RecVisitorMode); |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 438 | } |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 439 | return true; |
| 440 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 441 | |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 442 | bool VisitBlockDecl(BlockDecl *BD) { |
| 443 | if (BD->hasBody()) { |
| 444 | assert(RecVisitorMode == AM_Syntax || Mgr->shouldInlineCall() == false); |
Devin Coughlin | 6e644ab | 2015-11-18 22:46:52 +0000 | [diff] [blame] | 445 | // Since we skip function template definitions, we should skip blocks |
| 446 | // declared in those functions as well. |
| 447 | if (!BD->isDependentContext()) { |
| 448 | HandleCode(BD, RecVisitorMode); |
| 449 | } |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 450 | } |
| 451 | return true; |
| 452 | } |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 453 | |
Alexander Kornienko | 34eb207 | 2015-04-11 02:00:23 +0000 | [diff] [blame] | 454 | void AddDiagnosticConsumer(PathDiagnosticConsumer *Consumer) override { |
Alexander Kornienko | 7a712ce | 2014-02-03 18:37:50 +0000 | [diff] [blame] | 455 | PathConsumers.push_back(Consumer); |
| 456 | } |
| 457 | |
Alexander Kornienko | d00ed8e | 2018-06-27 14:56:12 +0000 | [diff] [blame] | 458 | void AddCheckerRegistrationFn(std::function<void(CheckerRegistry&)> Fn) override { |
| 459 | CheckerRegistrationFns.push_back(std::move(Fn)); |
| 460 | } |
| 461 | |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 462 | private: |
| 463 | void storeTopLevelDecls(DeclGroupRef DG); |
Artem Dergachev | 6a76a16 | 2016-08-08 16:01:02 +0000 | [diff] [blame] | 464 | std::string getFunctionName(const Decl *D); |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 465 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 466 | /// Check if we should skip (not analyze) the given function. |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 467 | AnalysisMode getModeForDecl(Decl *D, AnalysisMode Mode); |
George Karpenkov | 1d3e49e | 2018-02-26 22:14:16 +0000 | [diff] [blame] | 468 | void runAnalysisOnTranslationUnit(ASTContext &C); |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 469 | |
George Karpenkov | 1d3e49e | 2018-02-26 22:14:16 +0000 | [diff] [blame] | 470 | /// Print \p S to stderr if \c Opts->AnalyzerDisplayProgress is set. |
| 471 | void reportAnalyzerProgress(StringRef S); |
Ted Kremenek | 55d59bf | 2009-11-11 06:28:42 +0000 | [diff] [blame] | 472 | }; |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 473 | } // end anonymous namespace |
| 474 | |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 475 | |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 476 | //===----------------------------------------------------------------------===// |
| 477 | // AnalysisConsumer implementation. |
| 478 | //===----------------------------------------------------------------------===// |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 479 | bool AnalysisConsumer::HandleTopLevelDecl(DeclGroupRef DG) { |
| 480 | storeTopLevelDecls(DG); |
| 481 | return true; |
| 482 | } |
| 483 | |
| 484 | void AnalysisConsumer::HandleTopLevelDeclInObjCContainer(DeclGroupRef DG) { |
| 485 | storeTopLevelDecls(DG); |
| 486 | } |
| 487 | |
| 488 | void AnalysisConsumer::storeTopLevelDecls(DeclGroupRef DG) { |
| 489 | for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) { |
| 490 | |
| 491 | // Skip ObjCMethodDecl, wait for the objc container to avoid |
| 492 | // analyzing twice. |
| 493 | if (isa<ObjCMethodDecl>(*I)) |
| 494 | continue; |
| 495 | |
Ted Kremenek | 2964aac | 2012-04-27 04:54:28 +0000 | [diff] [blame] | 496 | LocalTUDecls.push_back(*I); |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 500 | static bool shouldSkipFunction(const Decl *D, |
Alexander Kornienko | fe65998 | 2014-04-23 16:39:41 +0000 | [diff] [blame] | 501 | const SetOfConstDecls &Visited, |
| 502 | const SetOfConstDecls &VisitedAsTopLevel) { |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 503 | if (VisitedAsTopLevel.count(D)) |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 504 | return true; |
| 505 | |
| 506 | // We want to re-analyse the functions as top level in the following cases: |
Anna Zaks | a8017ec | 2012-08-30 23:42:02 +0000 | [diff] [blame] | 507 | // - The 'init' methods should be reanalyzed because |
| 508 | // ObjCNonNilReturnValueChecker assumes that '[super init]' never returns |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 509 | // 'nil' and unless we analyze the 'init' functions as top level, we will |
| 510 | // not catch errors within defensive code. |
Anna Zaks | a8017ec | 2012-08-30 23:42:02 +0000 | [diff] [blame] | 511 | // - We want to reanalyze all ObjC methods as top level to report Retain |
| 512 | // Count naming convention errors more aggressively. |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 513 | if (isa<ObjCMethodDecl>(D)) |
Anna Zaks | a8017ec | 2012-08-30 23:42:02 +0000 | [diff] [blame] | 514 | return false; |
Devin Coughlin | f57f90d | 2016-07-21 23:42:31 +0000 | [diff] [blame] | 515 | // We also want to reanalyze all C++ copy and move assignment operators to |
| 516 | // separately check the two cases where 'this' aliases with the parameter and |
| 517 | // where it may not. (cplusplus.SelfAssignmentChecker) |
| 518 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 519 | if (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) |
| 520 | return false; |
| 521 | } |
Anna Zaks | a8017ec | 2012-08-30 23:42:02 +0000 | [diff] [blame] | 522 | |
| 523 | // Otherwise, if we visited the function before, do not reanalyze it. |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 524 | return Visited.count(D); |
Anna Zaks | a8017ec | 2012-08-30 23:42:02 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 527 | ExprEngine::InliningModes |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 528 | AnalysisConsumer::getInliningModeForFunction(const Decl *D, |
Alexander Kornienko | fe65998 | 2014-04-23 16:39:41 +0000 | [diff] [blame] | 529 | const SetOfConstDecls &Visited) { |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 530 | // We want to reanalyze all ObjC methods as top level to report Retain |
Anna Zaks | b13d21b | 2013-03-26 18:57:58 +0000 | [diff] [blame] | 531 | // Count naming convention errors more aggressively. But we should tune down |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 532 | // inlining when reanalyzing an already inlined function. |
Devin Coughlin | f57f90d | 2016-07-21 23:42:31 +0000 | [diff] [blame] | 533 | if (Visited.count(D) && isa<ObjCMethodDecl>(D)) { |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 534 | const ObjCMethodDecl *ObjCM = cast<ObjCMethodDecl>(D); |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 535 | if (ObjCM->getMethodFamily() != OMF_init) |
Anna Zaks | b13d21b | 2013-03-26 18:57:58 +0000 | [diff] [blame] | 536 | return ExprEngine::Inline_Minimal; |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Anna Zaks | b13d21b | 2013-03-26 18:57:58 +0000 | [diff] [blame] | 539 | return ExprEngine::Inline_Regular; |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Jordan Rose | 85c8c81 | 2012-10-10 17:55:37 +0000 | [diff] [blame] | 542 | void AnalysisConsumer::HandleDeclsCallGraph(const unsigned LocalTUDeclsSize) { |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 543 | // Build the Call Graph by adding all the top level declarations to the graph. |
Anna Zaks | 2774f99 | 2012-05-31 18:07:55 +0000 | [diff] [blame] | 544 | // Note: CallGraph can trigger deserialization of more items from a pch |
| 545 | // (though HandleInterestingDecl); triggering additions to LocalTUDecls. |
| 546 | // We rely on random access to add the initially processed Decls to CG. |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 547 | CallGraph CG; |
Anna Zaks | 2774f99 | 2012-05-31 18:07:55 +0000 | [diff] [blame] | 548 | for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) { |
Anna Zaks | 34d89b7 | 2012-05-30 23:14:48 +0000 | [diff] [blame] | 549 | CG.addToCallGraph(LocalTUDecls[i]); |
| 550 | } |
Anna Zaks | eee9110 | 2012-03-08 23:16:38 +0000 | [diff] [blame] | 551 | |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 552 | // Walk over all of the call graph nodes in topological order, so that we |
| 553 | // analyze parents before the children. Skip the functions inlined into |
| 554 | // the previously processed functions. Use external Visited set to identify |
| 555 | // inlined functions. The topological order allows the "do not reanalyze |
| 556 | // previously inlined function" performance heuristic to be triggered more |
| 557 | // often. |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 558 | SetOfConstDecls Visited; |
| 559 | SetOfConstDecls VisitedAsTopLevel; |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 560 | llvm::ReversePostOrderTraversal<clang::CallGraph*> RPOT(&CG); |
| 561 | for (llvm::ReversePostOrderTraversal<clang::CallGraph*>::rpo_iterator |
| 562 | I = RPOT.begin(), E = RPOT.end(); I != E; ++I) { |
| 563 | NumFunctionTopLevel++; |
Anna Zaks | ca70ed5 | 2012-03-13 19:32:13 +0000 | [diff] [blame] | 564 | |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 565 | CallGraphNode *N = *I; |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 566 | Decl *D = N->getDecl(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 567 | |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 568 | // Skip the abstract root node. |
| 569 | if (!D) |
| 570 | continue; |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 571 | |
Anna Zaks | ca70ed5 | 2012-03-13 19:32:13 +0000 | [diff] [blame] | 572 | // Skip the functions which have been processed already or previously |
| 573 | // inlined. |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 574 | if (shouldSkipFunction(D, Visited, VisitedAsTopLevel)) |
Anna Zaks | ca70ed5 | 2012-03-13 19:32:13 +0000 | [diff] [blame] | 575 | continue; |
| 576 | |
| 577 | // Analyze the function. |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 578 | SetOfConstDecls VisitedCallees; |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 579 | |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 580 | HandleCode(D, AM_Path, getInliningModeForFunction(D, Visited), |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 581 | (Mgr->options.InliningMode == All ? nullptr : &VisitedCallees)); |
Anna Zaks | ca70ed5 | 2012-03-13 19:32:13 +0000 | [diff] [blame] | 582 | |
| 583 | // Add the visited callees to the global visited set. |
Yury Gribov | 054873b | 2016-01-11 09:38:48 +0000 | [diff] [blame] | 584 | for (const Decl *Callee : VisitedCallees) |
| 585 | // Decls from CallGraph are already canonical. But Decls coming from |
| 586 | // CallExprs may be not. We should canonicalize them manually. |
| 587 | Visited.insert(isa<ObjCMethodDecl>(Callee) ? Callee |
| 588 | : Callee->getCanonicalDecl()); |
Anna Zaks | 5f2af81 | 2012-12-14 19:08:17 +0000 | [diff] [blame] | 589 | VisitedAsTopLevel.insert(D); |
Anna Zaks | eee9110 | 2012-03-08 23:16:38 +0000 | [diff] [blame] | 590 | } |
Ted Kremenek | 60120fb | 2011-08-27 21:28:09 +0000 | [diff] [blame] | 591 | } |
| 592 | |
George Karpenkov | 1d3e49e | 2018-02-26 22:14:16 +0000 | [diff] [blame] | 593 | static bool isBisonFile(ASTContext &C) { |
| 594 | const SourceManager &SM = C.getSourceManager(); |
| 595 | FileID FID = SM.getMainFileID(); |
| 596 | StringRef Buffer = SM.getBuffer(FID)->getBuffer(); |
| 597 | if (Buffer.startswith("/* A Bison parser, made by")) |
| 598 | return true; |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | void AnalysisConsumer::runAnalysisOnTranslationUnit(ASTContext &C) { |
| 603 | BugReporter BR(*Mgr); |
| 604 | TranslationUnitDecl *TU = C.getTranslationUnitDecl(); |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 605 | if (SyntaxCheckTimer) |
| 606 | SyntaxCheckTimer->startTimer(); |
George Karpenkov | 1d3e49e | 2018-02-26 22:14:16 +0000 | [diff] [blame] | 607 | checkerMgr->runCheckersOnASTDecl(TU, *Mgr, BR); |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 608 | if (SyntaxCheckTimer) |
| 609 | SyntaxCheckTimer->stopTimer(); |
George Karpenkov | 1d3e49e | 2018-02-26 22:14:16 +0000 | [diff] [blame] | 610 | |
| 611 | // Run the AST-only checks using the order in which functions are defined. |
| 612 | // If inlining is not turned on, use the simplest function order for path |
| 613 | // sensitive analyzes as well. |
| 614 | RecVisitorMode = AM_Syntax; |
| 615 | if (!Mgr->shouldInlineCall()) |
| 616 | RecVisitorMode |= AM_Path; |
| 617 | RecVisitorBR = &BR; |
| 618 | |
| 619 | // Process all the top level declarations. |
| 620 | // |
| 621 | // Note: TraverseDecl may modify LocalTUDecls, but only by appending more |
| 622 | // entries. Thus we don't use an iterator, but rely on LocalTUDecls |
| 623 | // random access. By doing so, we automatically compensate for iterators |
| 624 | // possibly being invalidated, although this is a bit slower. |
| 625 | const unsigned LocalTUDeclsSize = LocalTUDecls.size(); |
| 626 | for (unsigned i = 0 ; i < LocalTUDeclsSize ; ++i) { |
| 627 | TraverseDecl(LocalTUDecls[i]); |
| 628 | } |
| 629 | |
| 630 | if (Mgr->shouldInlineCall()) |
| 631 | HandleDeclsCallGraph(LocalTUDeclsSize); |
| 632 | |
| 633 | // After all decls handled, run checkers on the entire TranslationUnit. |
| 634 | checkerMgr->runCheckersOnEndOfTranslationUnit(TU, *Mgr, BR); |
| 635 | |
Artem Dergachev | 3fdc427 | 2019-08-20 21:41:20 +0000 | [diff] [blame] | 636 | BR.FlushReports(); |
George Karpenkov | 1d3e49e | 2018-02-26 22:14:16 +0000 | [diff] [blame] | 637 | RecVisitorBR = nullptr; |
| 638 | } |
| 639 | |
| 640 | void AnalysisConsumer::reportAnalyzerProgress(StringRef S) { |
| 641 | if (Opts->AnalyzerDisplayProgress) |
| 642 | llvm::errs() << S; |
| 643 | } |
| 644 | |
Ted Kremenek | a1ec4f3 | 2011-01-20 17:09:48 +0000 | [diff] [blame] | 645 | void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) { |
George Karpenkov | 1d3e49e | 2018-02-26 22:14:16 +0000 | [diff] [blame] | 646 | |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 647 | // Don't run the actions if an error has occurred with parsing the file. |
| 648 | DiagnosticsEngine &Diags = PP.getDiagnostics(); |
| 649 | if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) |
| 650 | return; |
| 651 | |
George Karpenkov | 1d3e49e | 2018-02-26 22:14:16 +0000 | [diff] [blame] | 652 | if (isBisonFile(C)) { |
| 653 | reportAnalyzerProgress("Skipping bison-generated file\n"); |
Kristof Umann | 7264942 | 2019-09-12 19:09:24 +0000 | [diff] [blame] | 654 | } else if (Opts->DisableAllCheckers) { |
Anna Zaks | 9bd4be9 | 2012-03-05 20:53:59 +0000 | [diff] [blame] | 655 | |
George Karpenkov | 1d3e49e | 2018-02-26 22:14:16 +0000 | [diff] [blame] | 656 | // Don't analyze if the user explicitly asked for no checks to be performed |
| 657 | // on this file. |
| 658 | reportAnalyzerProgress("All checks are disabled using a supplied option\n"); |
| 659 | } else { |
| 660 | // Otherwise, just run the analysis. |
| 661 | runAnalysisOnTranslationUnit(C); |
Anna Zaks | 17f57b0 | 2012-01-07 16:49:46 +0000 | [diff] [blame] | 662 | } |
Ted Kremenek | e69ab05 | 2011-05-05 03:41:17 +0000 | [diff] [blame] | 663 | |
Anna Zaks | 8382e45 | 2012-04-05 02:10:21 +0000 | [diff] [blame] | 664 | // Count how many basic blocks we have not covered. |
| 665 | NumBlocksInAnalyzedFunctions = FunctionSummaries.getTotalNumBasicBlocks(); |
George Karpenkov | 2a63985 | 2018-02-09 23:37:47 +0000 | [diff] [blame] | 666 | NumVisitedBlocksInAnalyzedFunctions = |
| 667 | FunctionSummaries.getTotalNumVisitedBasicBlocks(); |
Anna Zaks | 8382e45 | 2012-04-05 02:10:21 +0000 | [diff] [blame] | 668 | if (NumBlocksInAnalyzedFunctions > 0) |
| 669 | PercentReachableBlocks = |
| 670 | (FunctionSummaries.getTotalNumVisitedBasicBlocks() * 100) / |
| 671 | NumBlocksInAnalyzedFunctions; |
| 672 | |
George Karpenkov | 5a755b3 | 2018-02-10 01:49:20 +0000 | [diff] [blame] | 673 | // Explicitly destroy the PathDiagnosticConsumer. This will flush its output. |
| 674 | // FIXME: This should be replaced with something that doesn't rely on |
| 675 | // side-effects in PathDiagnosticConsumer's destructor. This is required when |
| 676 | // used with option -disable-free. |
| 677 | Mgr.reset(); |
Ted Kremenek | 0e7d252 | 2008-07-03 04:29:21 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Artem Dergachev | 6a76a16 | 2016-08-08 16:01:02 +0000 | [diff] [blame] | 680 | std::string AnalysisConsumer::getFunctionName(const Decl *D) { |
| 681 | std::string Str; |
| 682 | llvm::raw_string_ostream OS(Str); |
| 683 | |
| 684 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 685 | OS << FD->getQualifiedNameAsString(); |
| 686 | |
| 687 | // In C++, there are overloads. |
| 688 | if (Ctx->getLangOpts().CPlusPlus) { |
| 689 | OS << '('; |
| 690 | for (const auto &P : FD->parameters()) { |
| 691 | if (P != *FD->param_begin()) |
| 692 | OS << ", "; |
| 693 | OS << P->getType().getAsString(); |
| 694 | } |
| 695 | OS << ')'; |
| 696 | } |
| 697 | |
| 698 | } else if (isa<BlockDecl>(D)) { |
| 699 | PresumedLoc Loc = Ctx->getSourceManager().getPresumedLoc(D->getLocation()); |
| 700 | |
| 701 | if (Loc.isValid()) { |
| 702 | OS << "block (line: " << Loc.getLine() << ", col: " << Loc.getColumn() |
| 703 | << ')'; |
| 704 | } |
| 705 | |
| 706 | } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) { |
| 707 | |
| 708 | // FIXME: copy-pasted from CGDebugInfo.cpp. |
| 709 | OS << (OMD->isInstanceMethod() ? '-' : '+') << '['; |
| 710 | const DeclContext *DC = OMD->getDeclContext(); |
| 711 | if (const auto *OID = dyn_cast<ObjCImplementationDecl>(DC)) { |
| 712 | OS << OID->getName(); |
| 713 | } else if (const auto *OID = dyn_cast<ObjCInterfaceDecl>(DC)) { |
| 714 | OS << OID->getName(); |
| 715 | } else if (const auto *OC = dyn_cast<ObjCCategoryDecl>(DC)) { |
| 716 | if (OC->IsClassExtension()) { |
| 717 | OS << OC->getClassInterface()->getName(); |
| 718 | } else { |
| 719 | OS << OC->getIdentifier()->getNameStart() << '(' |
| 720 | << OC->getIdentifier()->getNameStart() << ')'; |
| 721 | } |
| 722 | } else if (const auto *OCD = dyn_cast<ObjCCategoryImplDecl>(DC)) { |
Argyrios Kyrtzidis | a166a2b | 2017-03-07 09:26:07 +0000 | [diff] [blame] | 723 | OS << OCD->getClassInterface()->getName() << '(' |
| 724 | << OCD->getName() << ')'; |
Artem Dergachev | 6a76a16 | 2016-08-08 16:01:02 +0000 | [diff] [blame] | 725 | } else if (isa<ObjCProtocolDecl>(DC)) { |
| 726 | // We can extract the type of the class from the self pointer. |
| 727 | if (ImplicitParamDecl *SelfDecl = OMD->getSelfDecl()) { |
| 728 | QualType ClassTy = |
| 729 | cast<ObjCObjectPointerType>(SelfDecl->getType())->getPointeeType(); |
| 730 | ClassTy.print(OS, PrintingPolicy(LangOptions())); |
| 731 | } |
| 732 | } |
| 733 | OS << ' ' << OMD->getSelector().getAsString() << ']'; |
| 734 | |
Anna Zaks | eee9110 | 2012-03-08 23:16:38 +0000 | [diff] [blame] | 735 | } |
Artem Dergachev | 6a76a16 | 2016-08-08 16:01:02 +0000 | [diff] [blame] | 736 | |
| 737 | return OS.str(); |
Anna Zaks | eee9110 | 2012-03-08 23:16:38 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 740 | AnalysisConsumer::AnalysisMode |
| 741 | AnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) { |
Ted Kremenek | 40ea0ea | 2012-08-31 04:36:05 +0000 | [diff] [blame] | 742 | if (!Opts->AnalyzeSpecificFunction.empty() && |
| 743 | getFunctionName(D) != Opts->AnalyzeSpecificFunction) |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 744 | return AM_None; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 746 | // Unless -analyze-all is specified, treat decls differently depending on |
| 747 | // where they came from: |
| 748 | // - Main source file: run both path-sensitive and non-path-sensitive checks. |
| 749 | // - Header files: run non-path-sensitive checks only. |
| 750 | // - System headers: don't run any checks. |
Ted Kremenek | 4c721bf | 2010-06-15 00:55:40 +0000 | [diff] [blame] | 751 | SourceManager &SM = Ctx->getSourceManager(); |
Aaron Ballman | 6569387 | 2015-08-20 21:27:35 +0000 | [diff] [blame] | 752 | const Stmt *Body = D->getBody(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 753 | SourceLocation SL = Body ? Body->getBeginLoc() : D->getLocation(); |
Anna Zaks | 3849dea | 2015-06-26 17:42:58 +0000 | [diff] [blame] | 754 | SL = SM.getExpansionLoc(SL); |
| 755 | |
Artem Dergachev | 516837f | 2018-04-25 21:51:26 +0000 | [diff] [blame] | 756 | if (!Opts->AnalyzeAll && !Mgr->isInCodeFile(SL)) { |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 757 | if (SL.isInvalid() || SM.isInSystemHeader(SL)) |
| 758 | return AM_None; |
| 759 | return Mode & ~AM_Path; |
| 760 | } |
Anna Zaks | aa19abe | 2012-03-13 19:31:54 +0000 | [diff] [blame] | 761 | |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 762 | return Mode; |
Anna Zaks | aa19abe | 2012-03-13 19:31:54 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Anna Zaks | 1418951 | 2012-03-13 19:32:00 +0000 | [diff] [blame] | 765 | void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode, |
Anna Zaks | 5d48478 | 2012-12-07 21:51:47 +0000 | [diff] [blame] | 766 | ExprEngine::InliningModes IMode, |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 767 | SetOfConstDecls *VisitedCallees) { |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 768 | if (!D->hasBody()) |
| 769 | return; |
Jordan Rose | d8665b3 | 2012-10-10 17:55:40 +0000 | [diff] [blame] | 770 | Mode = getModeForDecl(D, Mode); |
| 771 | if (Mode == AM_None) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | return; |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 773 | |
Alexander Shaposhnikov | a1fead2 | 2016-09-23 20:49:01 +0000 | [diff] [blame] | 774 | // Clear the AnalysisManager of old AnalysisDeclContexts. |
| 775 | Mgr->ClearContexts(); |
| 776 | // Ignore autosynthesized code. |
| 777 | if (Mgr->getAnalysisDeclContext(D)->isBodyAutosynthesized()) |
| 778 | return; |
| 779 | |
Anna Zaks | e9eb13a | 2013-02-02 00:30:02 +0000 | [diff] [blame] | 780 | DisplayFunction(D, Mode, IMode); |
Anna Zaks | 40b87fc | 2012-07-05 20:44:02 +0000 | [diff] [blame] | 781 | CFG *DeclCFG = Mgr->getCFG(D); |
Craig Topper | 704b4fb | 2017-05-18 01:11:52 +0000 | [diff] [blame] | 782 | if (DeclCFG) |
| 783 | MaxCFGSize.updateMax(DeclCFG->size()); |
Anna Zaks | 40b87fc | 2012-07-05 20:44:02 +0000 | [diff] [blame] | 784 | |
Argyrios Kyrtzidis | 24ffc08 | 2011-02-17 21:39:24 +0000 | [diff] [blame] | 785 | BugReporter BR(*Mgr); |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 786 | |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 787 | if (Mode & AM_Syntax) { |
| 788 | if (SyntaxCheckTimer) |
| 789 | SyntaxCheckTimer->startTimer(); |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 790 | checkerMgr->runCheckersOnASTBody(D, *Mgr, BR); |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 791 | if (SyntaxCheckTimer) |
| 792 | SyntaxCheckTimer->stopTimer(); |
| 793 | } |
Artem Dergachev | 3fdc427 | 2019-08-20 21:41:20 +0000 | [diff] [blame] | 794 | |
| 795 | BR.FlushReports(); |
| 796 | |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 797 | if ((Mode & AM_Path) && checkerMgr->hasPathSensitiveCheckers()) { |
| 798 | RunPathSensitiveChecks(D, IMode, VisitedCallees); |
Anna Zaks | b13d21b | 2013-03-26 18:57:58 +0000 | [diff] [blame] | 799 | if (IMode != ExprEngine::Inline_Minimal) |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 800 | NumFunctionsAnalyzed++; |
| 801 | } |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 6a1c760 | 2011-02-28 19:49:17 +0000 | [diff] [blame] | 805 | // Path-sensitive checking. |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 806 | //===----------------------------------------------------------------------===// |
| 807 | |
George Karpenkov | c82d457 | 2018-09-28 18:49:41 +0000 | [diff] [blame] | 808 | void AnalysisConsumer::RunPathSensitiveChecks(Decl *D, |
| 809 | ExprEngine::InliningModes IMode, |
| 810 | SetOfConstDecls *VisitedCallees) { |
Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 811 | // Construct the analysis engine. First check if the CFG is valid. |
Ted Kremenek | 1e7f988 | 2009-09-18 22:29:35 +0000 | [diff] [blame] | 812 | // FIXME: Inter-procedural analysis will need to handle invalid CFGs. |
Anna Zaks | 394d07e | 2012-03-09 21:14:01 +0000 | [diff] [blame] | 813 | if (!Mgr->getCFG(D)) |
Ted Kremenek | 39df94b | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 814 | return; |
Anna Zaks | 394d07e | 2012-03-09 21:14:01 +0000 | [diff] [blame] | 815 | |
Ted Kremenek | de21a1c | 2012-07-02 20:21:52 +0000 | [diff] [blame] | 816 | // See if the LiveVariables analysis scales. |
| 817 | if (!Mgr->getAnalysisDeclContext(D)->getAnalysis<RelaxedLiveVariables>()) |
| 818 | return; |
| 819 | |
George Karpenkov | c82d457 | 2018-09-28 18:49:41 +0000 | [diff] [blame] | 820 | ExprEngine Eng(CTU, *Mgr, VisitedCallees, &FunctionSummaries, IMode); |
Ted Kremenek | 39df94b | 2010-02-14 19:08:51 +0000 | [diff] [blame] | 821 | |
Ted Kremenek | 8e631a0 | 2008-07-02 00:44:58 +0000 | [diff] [blame] | 822 | // Execute the worklist algorithm. |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 823 | if (ExprEngineTimer) |
| 824 | ExprEngineTimer->startTimer(); |
Jordy Rose | 4f8198e | 2012-04-28 01:58:08 +0000 | [diff] [blame] | 825 | Eng.ExecuteWorkList(Mgr->getAnalysisDeclContextManager().getStackFrame(D), |
Kristof Umann | 549f9cd | 2018-11-30 20:44:00 +0000 | [diff] [blame] | 826 | Mgr->options.MaxNodesPerTopLevelFunction); |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 827 | if (ExprEngineTimer) |
| 828 | ExprEngineTimer->stopTimer(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | |
George Karpenkov | c704f4f | 2018-09-28 18:49:21 +0000 | [diff] [blame] | 830 | if (!Mgr->options.DumpExplodedGraphTo.empty()) |
| 831 | Eng.DumpGraph(Mgr->options.TrimGraph, Mgr->options.DumpExplodedGraphTo); |
| 832 | |
Ted Kremenek | 71ac832 | 2008-07-02 16:49:11 +0000 | [diff] [blame] | 833 | // Visualize the exploded graph. |
Ted Kremenek | b8f8b35 | 2012-08-30 19:26:53 +0000 | [diff] [blame] | 834 | if (Mgr->options.visualizeExplodedGraphWithGraphViz) |
Ted Kremenek | 8756c4a | 2012-08-30 19:26:43 +0000 | [diff] [blame] | 835 | Eng.ViewGraph(Mgr->options.TrimGraph); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
Ted Kremenek | 39df18f | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 837 | // Display warnings. |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 838 | if (BugReporterTimer) |
| 839 | BugReporterTimer->startTimer(); |
Ted Kremenek | 39df18f | 2009-03-11 01:42:29 +0000 | [diff] [blame] | 840 | Eng.getBugReporter().FlushReports(); |
Artem Dergachev | c48be7f | 2019-06-25 02:16:47 +0000 | [diff] [blame] | 841 | if (BugReporterTimer) |
| 842 | BugReporterTimer->stopTimer(); |
Ted Kremenek | becec2c | 2008-07-02 16:35:50 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 845 | //===----------------------------------------------------------------------===// |
| 846 | // AnalysisConsumer creation. |
| 847 | //===----------------------------------------------------------------------===// |
| 848 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 849 | std::unique_ptr<AnalysisASTConsumer> |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 850 | ento::CreateAnalysisConsumer(CompilerInstance &CI) { |
Jordy Rose | 59cce71 | 2011-08-16 21:24:21 +0000 | [diff] [blame] | 851 | // Disable the effects of '-Werror' when using the AnalysisConsumer. |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 852 | CI.getPreprocessor().getDiagnostics().setWarningsAsErrors(false); |
Ted Kremenek | b535181 | 2009-02-17 04:27:41 +0000 | [diff] [blame] | 853 | |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 854 | AnalyzerOptionsRef analyzerOpts = CI.getAnalyzerOpts(); |
| 855 | bool hasModelPath = analyzerOpts->Config.count("model-path") > 0; |
| 856 | |
Jonas Devlieghere | 2b3d49b | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 857 | return std::make_unique<AnalysisConsumer>( |
Ilya Biryukov | 8b9b3bd | 2018-03-01 14:54:16 +0000 | [diff] [blame] | 858 | CI, CI.getFrontendOpts().OutputFile, analyzerOpts, |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 859 | CI.getFrontendOpts().Plugins, |
| 860 | hasModelPath ? new ModelInjector(CI) : nullptr); |
Ted Kremenek | f0413bf | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 861 | } |