Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 1 | //===- PathDiagnostic.cpp - Path-Specific Diagnostic Handling -------------===// |
Ted Kremenek | 4fa20c9 | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 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 | 4fa20c9 | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines the PathDiagnostic-related interfaces. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Artem Dergachev | f0bb45f | 2019-09-11 20:54:27 +0000 | [diff] [blame] | 13 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 14 | #include "clang/AST/Decl.h" |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclBase.h" |
Jordan Rose | a4c0d21 | 2012-07-26 20:04:05 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclCXX.h" |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Kristof Umann | 3ea7442 | 2018-05-25 13:18:38 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/OperationKinds.h" |
Anna Zaks | 615e3a6 | 2011-09-14 17:48:01 +0000 | [diff] [blame] | 22 | #include "clang/AST/ParentMap.h" |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 23 | #include "clang/AST/Stmt.h" |
| 24 | #include "clang/AST/Type.h" |
| 25 | #include "clang/Analysis/AnalysisDeclContext.h" |
| 26 | #include "clang/Analysis/CFG.h" |
| 27 | #include "clang/Analysis/ProgramPoint.h" |
| 28 | #include "clang/Basic/FileManager.h" |
| 29 | #include "clang/Basic/LLVM.h" |
| 30 | #include "clang/Basic/SourceLocation.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "clang/Basic/SourceManager.h" |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/ArrayRef.h" |
| 33 | #include "llvm/ADT/FoldingSet.h" |
| 34 | #include "llvm/ADT/None.h" |
| 35 | #include "llvm/ADT/Optional.h" |
| 36 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 23be067 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/SmallVector.h" |
Jordan Rose | c102b35 | 2012-09-22 01:24:42 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/StringRef.h" |
| 41 | #include "llvm/Support/Casting.h" |
| 42 | #include "llvm/Support/ErrorHandling.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 44 | #include <cassert> |
| 45 | #include <cstring> |
| 46 | #include <memory> |
| 47 | #include <utility> |
| 48 | #include <vector> |
Ted Kremenek | bb597cd | 2009-06-26 00:43:22 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | 4fa20c9 | 2008-03-27 03:49:32 +0000 | [diff] [blame] | 50 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 51 | using namespace ento; |
Ted Kremenek | b4947e4 | 2009-03-10 05:16:17 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 53 | static StringRef StripTrailingDots(StringRef s) { |
| 54 | for (StringRef::size_type i = s.size(); i != 0; --i) |
Benjamin Kramer | 4dab76a | 2009-12-11 21:09:27 +0000 | [diff] [blame] | 55 | if (s[i - 1] != '.') |
| 56 | return s.substr(0, i); |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 57 | return {}; |
Ted Kremenek | e9817aa | 2009-02-26 21:30:32 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 60 | PathDiagnosticPiece::PathDiagnosticPiece(StringRef s, |
Ted Kremenek | 1f8140d | 2009-03-02 19:39:50 +0000 | [diff] [blame] | 61 | Kind k, DisplayHint hint) |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 62 | : str(StripTrailingDots(s)), kind(k), Hint(hint) {} |
Ted Kremenek | 55f46f6 | 2009-03-06 07:53:30 +0000 | [diff] [blame] | 63 | |
Ted Kremenek | 3d46b1c | 2009-03-26 21:21:35 +0000 | [diff] [blame] | 64 | PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint) |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 65 | : kind(k), Hint(hint) {} |
Ted Kremenek | 9d1ce8f | 2009-03-12 18:41:53 +0000 | [diff] [blame] | 66 | |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 67 | PathDiagnosticPiece::~PathDiagnosticPiece() = default; |
| 68 | |
| 69 | PathDiagnosticEventPiece::~PathDiagnosticEventPiece() = default; |
| 70 | |
| 71 | PathDiagnosticCallPiece::~PathDiagnosticCallPiece() = default; |
| 72 | |
| 73 | PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() = default; |
| 74 | |
| 75 | PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() = default; |
| 76 | |
| 77 | PathDiagnosticNotePiece::~PathDiagnosticNotePiece() = default; |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 78 | |
Csaba Dabis | 1d7ca67 | 2019-05-29 19:21:59 +0000 | [diff] [blame] | 79 | PathDiagnosticPopUpPiece::~PathDiagnosticPopUpPiece() = default; |
| 80 | |
Jordan Rose | 3eb3cd4 | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 81 | void PathPieces::flattenTo(PathPieces &Primary, PathPieces &Current, |
| 82 | bool ShouldFlattenMacros) const { |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 83 | for (auto &Piece : *this) { |
Jordan Rose | 3eb3cd4 | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 84 | switch (Piece->getKind()) { |
| 85 | case PathDiagnosticPiece::Call: { |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 86 | auto &Call = cast<PathDiagnosticCallPiece>(*Piece); |
| 87 | if (auto CallEnter = Call.getCallEnterEvent()) |
| 88 | Current.push_back(std::move(CallEnter)); |
| 89 | Call.path.flattenTo(Primary, Primary, ShouldFlattenMacros); |
| 90 | if (auto callExit = Call.getCallExitEvent()) |
| 91 | Current.push_back(std::move(callExit)); |
Jordan Rose | 3eb3cd4 | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 92 | break; |
| 93 | } |
| 94 | case PathDiagnosticPiece::Macro: { |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 95 | auto &Macro = cast<PathDiagnosticMacroPiece>(*Piece); |
Jordan Rose | 3eb3cd4 | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 96 | if (ShouldFlattenMacros) { |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 97 | Macro.subPieces.flattenTo(Primary, Primary, ShouldFlattenMacros); |
Jordan Rose | 3eb3cd4 | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 98 | } else { |
| 99 | Current.push_back(Piece); |
| 100 | PathPieces NewPath; |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 101 | Macro.subPieces.flattenTo(Primary, NewPath, ShouldFlattenMacros); |
Jordan Rose | 3eb3cd4 | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 102 | // FIXME: This probably shouldn't mutate the original path piece. |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 103 | Macro.subPieces = NewPath; |
Jordan Rose | 3eb3cd4 | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 104 | } |
| 105 | break; |
| 106 | } |
| 107 | case PathDiagnosticPiece::Event: |
| 108 | case PathDiagnosticPiece::ControlFlow: |
Artem Dergachev | 8b70c4e | 2016-10-07 15:23:02 +0000 | [diff] [blame] | 109 | case PathDiagnosticPiece::Note: |
Csaba Dabis | 1d7ca67 | 2019-05-29 19:21:59 +0000 | [diff] [blame] | 110 | case PathDiagnosticPiece::PopUp: |
Jordan Rose | 3eb3cd4 | 2012-08-03 23:08:54 +0000 | [diff] [blame] | 111 | Current.push_back(Piece); |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 117 | PathDiagnostic::~PathDiagnostic() = default; |
Ted Kremenek | 3116c4e | 2012-02-08 04:32:27 +0000 | [diff] [blame] | 118 | |
George Karpenkov | a5ddd3c | 2018-01-17 02:59:11 +0000 | [diff] [blame] | 119 | PathDiagnostic::PathDiagnostic( |
Kristof Umann | 7264942 | 2019-09-12 19:09:24 +0000 | [diff] [blame] | 120 | StringRef CheckerName, const Decl *declWithIssue, StringRef bugtype, |
George Karpenkov | a5ddd3c | 2018-01-17 02:59:11 +0000 | [diff] [blame] | 121 | StringRef verboseDesc, StringRef shortDesc, StringRef category, |
| 122 | PathDiagnosticLocation LocationToUnique, const Decl *DeclToUnique, |
| 123 | std::unique_ptr<FilesToLineNumsMap> ExecutedLines) |
Kristof Umann | 7264942 | 2019-09-12 19:09:24 +0000 | [diff] [blame] | 124 | : CheckerName(CheckerName), DeclWithIssue(declWithIssue), |
George Karpenkov | a5ddd3c | 2018-01-17 02:59:11 +0000 | [diff] [blame] | 125 | BugType(StripTrailingDots(bugtype)), |
| 126 | VerboseDesc(StripTrailingDots(verboseDesc)), |
| 127 | ShortDesc(StripTrailingDots(shortDesc)), |
| 128 | Category(StripTrailingDots(category)), UniqueingLoc(LocationToUnique), |
| 129 | UniqueingDecl(DeclToUnique), ExecutedLines(std::move(ExecutedLines)), |
| 130 | path(pathImpl) {} |
Ted Kremenek | 5b9e7b8 | 2009-03-06 07:08:50 +0000 | [diff] [blame] | 131 | |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 132 | void PathDiagnosticConsumer::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 134 | PathDiagnosticConsumer::~PathDiagnosticConsumer() { |
| 135 | // Delete the contents of the FoldingSet if it isn't empty already. |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 136 | for (auto &Diag : Diags) |
| 137 | delete &Diag; |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 138 | } |
| 139 | |
David Blaikie | c9950cb | 2014-08-29 20:06:10 +0000 | [diff] [blame] | 140 | void PathDiagnosticConsumer::HandlePathDiagnostic( |
| 141 | std::unique_ptr<PathDiagnostic> D) { |
Ted Kremenek | 0f70a6f | 2012-02-28 23:27:39 +0000 | [diff] [blame] | 142 | if (!D || D->path.empty()) |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 143 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 145 | // We need to flatten the locations (convert Stmt* to locations) because |
| 146 | // the referenced statements may be freed by the time the diagnostics |
| 147 | // are emitted. |
| 148 | D->flattenLocations(); |
| 149 | |
Ted Kremenek | 0f70a6f | 2012-02-28 23:27:39 +0000 | [diff] [blame] | 150 | // If the PathDiagnosticConsumer does not support diagnostics that |
| 151 | // cross file boundaries, prune out such diagnostics now. |
| 152 | if (!supportsCrossFileDiagnostics()) { |
| 153 | // Verify that the entire path is from the same FileID. |
| 154 | FileID FID; |
David Blaikie | c9950cb | 2014-08-29 20:06:10 +0000 | [diff] [blame] | 155 | const SourceManager &SMgr = D->path.front()->getLocation().getManager(); |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 156 | SmallVector<const PathPieces *, 5> WorkList; |
Ted Kremenek | 0f0cc35 | 2012-02-29 23:59:20 +0000 | [diff] [blame] | 157 | WorkList.push_back(&D->path); |
Anton Yartsev | 0509d04 | 2016-10-06 21:42:21 +0000 | [diff] [blame] | 158 | SmallString<128> buf; |
| 159 | llvm::raw_svector_ostream warning(buf); |
| 160 | warning << "warning: Path diagnostic report is not generated. Current " |
| 161 | << "output format does not support diagnostics that cross file " |
| 162 | << "boundaries. Refer to --analyzer-output for valid output " |
| 163 | << "formats\n"; |
Ted Kremenek | 0f70a6f | 2012-02-28 23:27:39 +0000 | [diff] [blame] | 164 | |
Ted Kremenek | 0f0cc35 | 2012-02-29 23:59:20 +0000 | [diff] [blame] | 165 | while (!WorkList.empty()) { |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 166 | const PathPieces &path = *WorkList.pop_back_val(); |
Ted Kremenek | 0f0cc35 | 2012-02-29 23:59:20 +0000 | [diff] [blame] | 167 | |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 168 | for (const auto &I : path) { |
| 169 | const PathDiagnosticPiece *piece = I.get(); |
Ted Kremenek | 0f0cc35 | 2012-02-29 23:59:20 +0000 | [diff] [blame] | 170 | FullSourceLoc L = piece->getLocation().asLocation().getExpansionLoc(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 171 | |
Ted Kremenek | 0f0cc35 | 2012-02-29 23:59:20 +0000 | [diff] [blame] | 172 | if (FID.isInvalid()) { |
| 173 | FID = SMgr.getFileID(L); |
Anton Yartsev | 0509d04 | 2016-10-06 21:42:21 +0000 | [diff] [blame] | 174 | } else if (SMgr.getFileID(L) != FID) { |
| 175 | llvm::errs() << warning.str(); |
| 176 | return; |
| 177 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 0f0cc35 | 2012-02-29 23:59:20 +0000 | [diff] [blame] | 179 | // Check the source ranges. |
Ted Kremenek | 1e60273 | 2012-08-16 17:45:29 +0000 | [diff] [blame] | 180 | ArrayRef<SourceRange> Ranges = piece->getRanges(); |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 181 | for (const auto &I : Ranges) { |
| 182 | SourceLocation L = SMgr.getExpansionLoc(I.getBegin()); |
Anton Yartsev | 0509d04 | 2016-10-06 21:42:21 +0000 | [diff] [blame] | 183 | if (!L.isFileID() || SMgr.getFileID(L) != FID) { |
| 184 | llvm::errs() << warning.str(); |
| 185 | return; |
| 186 | } |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 187 | L = SMgr.getExpansionLoc(I.getEnd()); |
Anton Yartsev | 0509d04 | 2016-10-06 21:42:21 +0000 | [diff] [blame] | 188 | if (!L.isFileID() || SMgr.getFileID(L) != FID) { |
| 189 | llvm::errs() << warning.str(); |
| 190 | return; |
| 191 | } |
Ted Kremenek | 0f0cc35 | 2012-02-29 23:59:20 +0000 | [diff] [blame] | 192 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 193 | |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 194 | if (const auto *call = dyn_cast<PathDiagnosticCallPiece>(piece)) |
Ted Kremenek | 0f0cc35 | 2012-02-29 23:59:20 +0000 | [diff] [blame] | 195 | WorkList.push_back(&call->path); |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 196 | else if (const auto *macro = dyn_cast<PathDiagnosticMacroPiece>(piece)) |
Ted Kremenek | 0f0cc35 | 2012-02-29 23:59:20 +0000 | [diff] [blame] | 197 | WorkList.push_back(¯o->subPieces); |
Ted Kremenek | 0f70a6f | 2012-02-28 23:27:39 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 200 | |
Ted Kremenek | 0f70a6f | 2012-02-28 23:27:39 +0000 | [diff] [blame] | 201 | if (FID.isInvalid()) |
| 202 | return; // FIXME: Emit a warning? |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 203 | } |
Ted Kremenek | 0f70a6f | 2012-02-28 23:27:39 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 205 | // Profile the node to see if we already have something matching it |
| 206 | llvm::FoldingSetNodeID profile; |
| 207 | D->Profile(profile); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 208 | void *InsertPos = nullptr; |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 209 | |
| 210 | if (PathDiagnostic *orig = Diags.FindNodeOrInsertPos(profile, InsertPos)) { |
| 211 | // Keep the PathDiagnostic with the shorter path. |
Anna Zaks | 4bd96c4 | 2012-08-03 18:30:20 +0000 | [diff] [blame] | 212 | // Note, the enclosing routine is called in deterministic order, so the |
Anna Zaks | 4c03dfd | 2012-08-02 23:41:05 +0000 | [diff] [blame] | 213 | // results will be consistent between runs (no reason to break ties if the |
| 214 | // size is the same). |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 215 | const unsigned orig_size = orig->full_size(); |
| 216 | const unsigned new_size = D->full_size(); |
Anna Zaks | 4c03dfd | 2012-08-02 23:41:05 +0000 | [diff] [blame] | 217 | if (orig_size <= new_size) |
| 218 | return; |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 219 | |
David Blaikie | c9950cb | 2014-08-29 20:06:10 +0000 | [diff] [blame] | 220 | assert(orig != D.get()); |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 221 | Diags.RemoveNode(orig); |
| 222 | delete orig; |
| 223 | } |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 224 | |
David Blaikie | c9950cb | 2014-08-29 20:06:10 +0000 | [diff] [blame] | 225 | Diags.InsertNode(D.release()); |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 226 | } |
| 227 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 228 | static Optional<bool> comparePath(const PathPieces &X, const PathPieces &Y); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 229 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 230 | static Optional<bool> |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 231 | compareControlFlow(const PathDiagnosticControlFlowPiece &X, |
| 232 | const PathDiagnosticControlFlowPiece &Y) { |
| 233 | FullSourceLoc XSL = X.getStartLocation().asLocation(); |
| 234 | FullSourceLoc YSL = Y.getStartLocation().asLocation(); |
| 235 | if (XSL != YSL) |
| 236 | return XSL.isBeforeInTranslationUnitThan(YSL); |
| 237 | FullSourceLoc XEL = X.getEndLocation().asLocation(); |
| 238 | FullSourceLoc YEL = Y.getEndLocation().asLocation(); |
| 239 | if (XEL != YEL) |
| 240 | return XEL.isBeforeInTranslationUnitThan(YEL); |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 241 | return None; |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 242 | } |
| 243 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 244 | static Optional<bool> compareMacro(const PathDiagnosticMacroPiece &X, |
| 245 | const PathDiagnosticMacroPiece &Y) { |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 246 | return comparePath(X.subPieces, Y.subPieces); |
| 247 | } |
| 248 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 249 | static Optional<bool> compareCall(const PathDiagnosticCallPiece &X, |
| 250 | const PathDiagnosticCallPiece &Y) { |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 251 | FullSourceLoc X_CEL = X.callEnter.asLocation(); |
| 252 | FullSourceLoc Y_CEL = Y.callEnter.asLocation(); |
| 253 | if (X_CEL != Y_CEL) |
| 254 | return X_CEL.isBeforeInTranslationUnitThan(Y_CEL); |
| 255 | FullSourceLoc X_CEWL = X.callEnterWithin.asLocation(); |
| 256 | FullSourceLoc Y_CEWL = Y.callEnterWithin.asLocation(); |
| 257 | if (X_CEWL != Y_CEWL) |
| 258 | return X_CEWL.isBeforeInTranslationUnitThan(Y_CEWL); |
| 259 | FullSourceLoc X_CRL = X.callReturn.asLocation(); |
| 260 | FullSourceLoc Y_CRL = Y.callReturn.asLocation(); |
| 261 | if (X_CRL != Y_CRL) |
| 262 | return X_CRL.isBeforeInTranslationUnitThan(Y_CRL); |
| 263 | return comparePath(X.path, Y.path); |
| 264 | } |
| 265 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 266 | static Optional<bool> comparePiece(const PathDiagnosticPiece &X, |
| 267 | const PathDiagnosticPiece &Y) { |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 268 | if (X.getKind() != Y.getKind()) |
| 269 | return X.getKind() < Y.getKind(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 271 | FullSourceLoc XL = X.getLocation().asLocation(); |
| 272 | FullSourceLoc YL = Y.getLocation().asLocation(); |
| 273 | if (XL != YL) |
| 274 | return XL.isBeforeInTranslationUnitThan(YL); |
| 275 | |
| 276 | if (X.getString() != Y.getString()) |
| 277 | return X.getString() < Y.getString(); |
| 278 | |
| 279 | if (X.getRanges().size() != Y.getRanges().size()) |
| 280 | return X.getRanges().size() < Y.getRanges().size(); |
| 281 | |
Ted Kremenek | 54fd079 | 2012-09-10 19:02:33 +0000 | [diff] [blame] | 282 | const SourceManager &SM = XL.getManager(); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 283 | |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 284 | for (unsigned i = 0, n = X.getRanges().size(); i < n; ++i) { |
| 285 | SourceRange XR = X.getRanges()[i]; |
| 286 | SourceRange YR = Y.getRanges()[i]; |
| 287 | if (XR != YR) { |
| 288 | if (XR.getBegin() != YR.getBegin()) |
Ted Kremenek | 54fd079 | 2012-09-10 19:02:33 +0000 | [diff] [blame] | 289 | return SM.isBeforeInTranslationUnit(XR.getBegin(), YR.getBegin()); |
| 290 | return SM.isBeforeInTranslationUnit(XR.getEnd(), YR.getEnd()); |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 294 | switch (X.getKind()) { |
Artem Dergachev | 8b70c4e | 2016-10-07 15:23:02 +0000 | [diff] [blame] | 295 | case PathDiagnosticPiece::ControlFlow: |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 296 | return compareControlFlow(cast<PathDiagnosticControlFlowPiece>(X), |
| 297 | cast<PathDiagnosticControlFlowPiece>(Y)); |
Artem Dergachev | 8b70c4e | 2016-10-07 15:23:02 +0000 | [diff] [blame] | 298 | case PathDiagnosticPiece::Macro: |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 299 | return compareMacro(cast<PathDiagnosticMacroPiece>(X), |
| 300 | cast<PathDiagnosticMacroPiece>(Y)); |
Artem Dergachev | 8b70c4e | 2016-10-07 15:23:02 +0000 | [diff] [blame] | 301 | case PathDiagnosticPiece::Call: |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 302 | return compareCall(cast<PathDiagnosticCallPiece>(X), |
| 303 | cast<PathDiagnosticCallPiece>(Y)); |
Csaba Dabis | 1d7ca67 | 2019-05-29 19:21:59 +0000 | [diff] [blame] | 304 | case PathDiagnosticPiece::Event: |
| 305 | case PathDiagnosticPiece::Note: |
| 306 | case PathDiagnosticPiece::PopUp: |
| 307 | return None; |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 308 | } |
| 309 | llvm_unreachable("all cases handled"); |
| 310 | } |
| 311 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 312 | static Optional<bool> comparePath(const PathPieces &X, const PathPieces &Y) { |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 313 | if (X.size() != Y.size()) |
| 314 | return X.size() < Y.size(); |
Ted Kremenek | 518e781 | 2013-04-29 22:38:22 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | eba09fa | 2013-04-29 23:12:59 +0000 | [diff] [blame] | 316 | PathPieces::const_iterator X_I = X.begin(), X_end = X.end(); |
| 317 | PathPieces::const_iterator Y_I = Y.begin(), Y_end = Y.end(); |
Ted Kremenek | 518e781 | 2013-04-29 22:38:22 +0000 | [diff] [blame] | 318 | |
| 319 | for ( ; X_I != X_end && Y_I != Y_end; ++X_I, ++Y_I) { |
Ted Kremenek | eba09fa | 2013-04-29 23:12:59 +0000 | [diff] [blame] | 320 | Optional<bool> b = comparePiece(**X_I, **Y_I); |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 321 | if (b.hasValue()) |
| 322 | return b.getValue(); |
| 323 | } |
Ted Kremenek | 518e781 | 2013-04-29 22:38:22 +0000 | [diff] [blame] | 324 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 325 | return None; |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Ilya Biryukov | 8b9b3bd | 2018-03-01 14:54:16 +0000 | [diff] [blame] | 328 | static bool compareCrossTUSourceLocs(FullSourceLoc XL, FullSourceLoc YL) { |
| 329 | std::pair<FileID, unsigned> XOffs = XL.getDecomposedLoc(); |
| 330 | std::pair<FileID, unsigned> YOffs = YL.getDecomposedLoc(); |
| 331 | const SourceManager &SM = XL.getManager(); |
| 332 | std::pair<bool, bool> InSameTU = SM.isInTheSameTranslationUnit(XOffs, YOffs); |
| 333 | if (InSameTU.first) |
| 334 | return XL.isBeforeInTranslationUnitThan(YL); |
Rafael Stahl | 67676e9 | 2018-07-04 14:12:58 +0000 | [diff] [blame] | 335 | const FileEntry *XFE = SM.getFileEntryForID(XL.getSpellingLoc().getFileID()); |
| 336 | const FileEntry *YFE = SM.getFileEntryForID(YL.getSpellingLoc().getFileID()); |
Ilya Biryukov | 8b9b3bd | 2018-03-01 14:54:16 +0000 | [diff] [blame] | 337 | if (!XFE || !YFE) |
| 338 | return XFE && !YFE; |
Rafael Stahl | 67676e9 | 2018-07-04 14:12:58 +0000 | [diff] [blame] | 339 | int NameCmp = XFE->getName().compare(YFE->getName()); |
| 340 | if (NameCmp != 0) |
| 341 | return NameCmp == -1; |
| 342 | // Last resort: Compare raw file IDs that are possibly expansions. |
| 343 | return XL.getFileID() < YL.getFileID(); |
Ilya Biryukov | 8b9b3bd | 2018-03-01 14:54:16 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 346 | static bool compare(const PathDiagnostic &X, const PathDiagnostic &Y) { |
| 347 | FullSourceLoc XL = X.getLocation().asLocation(); |
| 348 | FullSourceLoc YL = Y.getLocation().asLocation(); |
| 349 | if (XL != YL) |
Ilya Biryukov | 8b9b3bd | 2018-03-01 14:54:16 +0000 | [diff] [blame] | 350 | return compareCrossTUSourceLocs(XL, YL); |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 351 | if (X.getBugType() != Y.getBugType()) |
| 352 | return X.getBugType() < Y.getBugType(); |
| 353 | if (X.getCategory() != Y.getCategory()) |
| 354 | return X.getCategory() < Y.getCategory(); |
| 355 | if (X.getVerboseDescription() != Y.getVerboseDescription()) |
| 356 | return X.getVerboseDescription() < Y.getVerboseDescription(); |
| 357 | if (X.getShortDescription() != Y.getShortDescription()) |
| 358 | return X.getShortDescription() < Y.getShortDescription(); |
| 359 | if (X.getDeclWithIssue() != Y.getDeclWithIssue()) { |
| 360 | const Decl *XD = X.getDeclWithIssue(); |
| 361 | if (!XD) |
| 362 | return true; |
| 363 | const Decl *YD = Y.getDeclWithIssue(); |
| 364 | if (!YD) |
| 365 | return false; |
| 366 | SourceLocation XDL = XD->getLocation(); |
| 367 | SourceLocation YDL = YD->getLocation(); |
Ted Kremenek | a0fa5d6 | 2012-09-10 19:07:56 +0000 | [diff] [blame] | 368 | if (XDL != YDL) { |
| 369 | const SourceManager &SM = XL.getManager(); |
Ilya Biryukov | 8b9b3bd | 2018-03-01 14:54:16 +0000 | [diff] [blame] | 370 | return compareCrossTUSourceLocs(FullSourceLoc(XDL, SM), |
| 371 | FullSourceLoc(YDL, SM)); |
Ted Kremenek | a0fa5d6 | 2012-09-10 19:07:56 +0000 | [diff] [blame] | 372 | } |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 373 | } |
| 374 | PathDiagnostic::meta_iterator XI = X.meta_begin(), XE = X.meta_end(); |
| 375 | PathDiagnostic::meta_iterator YI = Y.meta_begin(), YE = Y.meta_end(); |
| 376 | if (XE - XI != YE - YI) |
| 377 | return (XE - XI) < (YE - YI); |
| 378 | for ( ; XI != XE ; ++XI, ++YI) { |
| 379 | if (*XI != *YI) |
| 380 | return (*XI) < (*YI); |
| 381 | } |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 382 | Optional<bool> b = comparePath(X.path, Y.path); |
Ted Kremenek | 6c7a5ea | 2012-09-10 14:50:55 +0000 | [diff] [blame] | 383 | assert(b.hasValue()); |
| 384 | return b.getValue(); |
| 385 | } |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 386 | |
Ted Kremenek | 9bf9af9 | 2012-08-16 17:45:23 +0000 | [diff] [blame] | 387 | void PathDiagnosticConsumer::FlushDiagnostics( |
| 388 | PathDiagnosticConsumer::FilesMade *Files) { |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 389 | if (flushed) |
| 390 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 391 | |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 392 | flushed = true; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 394 | std::vector<const PathDiagnostic *> BatchDiags; |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 395 | for (const auto &D : Diags) |
| 396 | BatchDiags.push_back(&D); |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 397 | |
| 398 | // Sort the diagnostics so that they are always emitted in a deterministic |
| 399 | // order. |
Benjamin Kramer | 5caa50e | 2015-03-14 12:39:22 +0000 | [diff] [blame] | 400 | int (*Comp)(const PathDiagnostic *const *, const PathDiagnostic *const *) = |
| 401 | [](const PathDiagnostic *const *X, const PathDiagnostic *const *Y) { |
| 402 | assert(*X != *Y && "PathDiagnostics not uniqued!"); |
| 403 | if (compare(**X, **Y)) |
| 404 | return -1; |
| 405 | assert(compare(**Y, **X) && "Not a total order!"); |
| 406 | return 1; |
| 407 | }; |
| 408 | array_pod_sort(BatchDiags.begin(), BatchDiags.end(), Comp); |
Benjamin Kramer | bbdd764 | 2014-03-01 14:48:57 +0000 | [diff] [blame] | 409 | |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 410 | FlushDiagnosticsImpl(BatchDiags, Files); |
| 411 | |
| 412 | // Delete the flushed diagnostics. |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 413 | for (const auto D : BatchDiags) |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 414 | delete D; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | b0d1c70 | 2012-09-08 04:26:37 +0000 | [diff] [blame] | 416 | // Clear out the FoldingSet. |
| 417 | Diags.clear(); |
Ted Kremenek | 996bfab | 2011-08-27 21:39:14 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Chandler Carruth | 7ff0772 | 2014-05-03 10:39:05 +0000 | [diff] [blame] | 420 | PathDiagnosticConsumer::FilesMade::~FilesMade() { |
Benjamin Kramer | b474c04 | 2015-03-22 18:16:22 +0000 | [diff] [blame] | 421 | for (PDFileEntry &Entry : Set) |
Chandler Carruth | 7ff0772 | 2014-05-03 10:39:05 +0000 | [diff] [blame] | 422 | Entry.~PDFileEntry(); |
| 423 | } |
| 424 | |
Ted Kremenek | 5bc38ba | 2012-08-24 19:35:19 +0000 | [diff] [blame] | 425 | void PathDiagnosticConsumer::FilesMade::addDiagnostic(const PathDiagnostic &PD, |
| 426 | StringRef ConsumerName, |
| 427 | StringRef FileName) { |
| 428 | llvm::FoldingSetNodeID NodeID; |
Jordan Rose | cc0b1bf | 2012-08-31 00:36:26 +0000 | [diff] [blame] | 429 | NodeID.Add(PD); |
Ted Kremenek | 5bc38ba | 2012-08-24 19:35:19 +0000 | [diff] [blame] | 430 | void *InsertPos; |
Benjamin Kramer | b474c04 | 2015-03-22 18:16:22 +0000 | [diff] [blame] | 431 | PDFileEntry *Entry = Set.FindNodeOrInsertPos(NodeID, InsertPos); |
Ted Kremenek | 5bc38ba | 2012-08-24 19:35:19 +0000 | [diff] [blame] | 432 | if (!Entry) { |
| 433 | Entry = Alloc.Allocate<PDFileEntry>(); |
| 434 | Entry = new (Entry) PDFileEntry(NodeID); |
Benjamin Kramer | b474c04 | 2015-03-22 18:16:22 +0000 | [diff] [blame] | 435 | Set.InsertNode(Entry, InsertPos); |
Ted Kremenek | 5bc38ba | 2012-08-24 19:35:19 +0000 | [diff] [blame] | 436 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | 5bc38ba | 2012-08-24 19:35:19 +0000 | [diff] [blame] | 438 | // Allocate persistent storage for the file name. |
| 439 | char *FileName_cstr = (char*) Alloc.Allocate(FileName.size(), 1); |
| 440 | memcpy(FileName_cstr, FileName.data(), FileName.size()); |
| 441 | |
| 442 | Entry->files.push_back(std::make_pair(ConsumerName, |
| 443 | StringRef(FileName_cstr, |
| 444 | FileName.size()))); |
| 445 | } |
| 446 | |
| 447 | PathDiagnosticConsumer::PDFileEntry::ConsumerFiles * |
| 448 | PathDiagnosticConsumer::FilesMade::getFiles(const PathDiagnostic &PD) { |
| 449 | llvm::FoldingSetNodeID NodeID; |
Jordan Rose | cc0b1bf | 2012-08-31 00:36:26 +0000 | [diff] [blame] | 450 | NodeID.Add(PD); |
Ted Kremenek | 5bc38ba | 2012-08-24 19:35:19 +0000 | [diff] [blame] | 451 | void *InsertPos; |
Benjamin Kramer | b474c04 | 2015-03-22 18:16:22 +0000 | [diff] [blame] | 452 | PDFileEntry *Entry = Set.FindNodeOrInsertPos(NodeID, InsertPos); |
Ted Kremenek | 5bc38ba | 2012-08-24 19:35:19 +0000 | [diff] [blame] | 453 | if (!Entry) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 454 | return nullptr; |
Ted Kremenek | 5bc38ba | 2012-08-24 19:35:19 +0000 | [diff] [blame] | 455 | return &Entry->files; |
| 456 | } |
| 457 | |
Ted Kremenek | bade06e | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 458 | //===----------------------------------------------------------------------===// |
| 459 | // PathDiagnosticLocation methods. |
| 460 | //===----------------------------------------------------------------------===// |
| 461 | |
Artem Dergachev | 6b85f8e | 2019-09-11 20:54:21 +0000 | [diff] [blame] | 462 | SourceLocation PathDiagnosticLocation::getValidSourceLocation( |
| 463 | const Stmt *S, LocationOrAnalysisDeclContext LAC, bool UseEndOfStatement) { |
| 464 | SourceLocation L = UseEndOfStatement ? S->getEndLoc() : S->getBeginLoc(); |
| 465 | assert(!LAC.isNull() && |
| 466 | "A valid LocationContext or AnalysisDeclContext should be passed to " |
| 467 | "PathDiagnosticLocation upon creation."); |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 468 | |
| 469 | // S might be a temporary statement that does not have a location in the |
Jordan Rose | a4c0d21 | 2012-07-26 20:04:05 +0000 | [diff] [blame] | 470 | // source code, so find an enclosing statement and use its location. |
Anna Zaks | de03d72 | 2011-09-20 16:37:36 +0000 | [diff] [blame] | 471 | if (!L.isValid()) { |
Jordan Rose | 25bc20f | 2012-07-26 20:04:30 +0000 | [diff] [blame] | 472 | AnalysisDeclContext *ADC; |
Anna Zaks | 83128bc | 2011-09-20 18:23:52 +0000 | [diff] [blame] | 473 | if (LAC.is<const LocationContext*>()) |
Jordan Rose | 25bc20f | 2012-07-26 20:04:30 +0000 | [diff] [blame] | 474 | ADC = LAC.get<const LocationContext*>()->getAnalysisDeclContext(); |
Anna Zaks | 83128bc | 2011-09-20 18:23:52 +0000 | [diff] [blame] | 475 | else |
Jordan Rose | 25bc20f | 2012-07-26 20:04:30 +0000 | [diff] [blame] | 476 | ADC = LAC.get<AnalysisDeclContext*>(); |
Anna Zaks | de03d72 | 2011-09-20 16:37:36 +0000 | [diff] [blame] | 477 | |
Jordan Rose | 25bc20f | 2012-07-26 20:04:30 +0000 | [diff] [blame] | 478 | ParentMap &PM = ADC->getParentMap(); |
| 479 | |
| 480 | const Stmt *Parent = S; |
| 481 | do { |
| 482 | Parent = PM.getParent(Parent); |
| 483 | |
| 484 | // In rare cases, we have implicit top-level expressions, |
| 485 | // such as arguments for implicit member initializers. |
| 486 | // In this case, fall back to the start of the body (even if we were |
| 487 | // asked for the statement end location). |
| 488 | if (!Parent) { |
| 489 | const Stmt *Body = ADC->getBody(); |
| 490 | if (Body) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 491 | L = Body->getBeginLoc(); |
Jordan Rose | 25bc20f | 2012-07-26 20:04:30 +0000 | [diff] [blame] | 492 | else |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 493 | L = ADC->getDecl()->getEndLoc(); |
Jordan Rose | 25bc20f | 2012-07-26 20:04:30 +0000 | [diff] [blame] | 494 | break; |
| 495 | } |
| 496 | |
Artem Dergachev | 6b85f8e | 2019-09-11 20:54:21 +0000 | [diff] [blame] | 497 | L = UseEndOfStatement ? Parent->getEndLoc() : Parent->getBeginLoc(); |
Jordan Rose | 25bc20f | 2012-07-26 20:04:30 +0000 | [diff] [blame] | 498 | } while (!L.isValid()); |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Kristof Umann | 4962816 | 2019-03-14 16:10:29 +0000 | [diff] [blame] | 501 | // FIXME: Ironically, this assert actually fails in some cases. |
| 502 | //assert(L.isValid()); |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 503 | return L; |
| 504 | } |
| 505 | |
Jordan Rose | 80880ac | 2012-08-03 23:08:44 +0000 | [diff] [blame] | 506 | static PathDiagnosticLocation |
| 507 | getLocationForCaller(const StackFrameContext *SFC, |
| 508 | const LocationContext *CallerCtx, |
| 509 | const SourceManager &SM) { |
| 510 | const CFGBlock &Block = *SFC->getCallSiteBlock(); |
| 511 | CFGElement Source = Block[SFC->getIndex()]; |
| 512 | |
| 513 | switch (Source.getKind()) { |
Jordan Rose | 80880ac | 2012-08-03 23:08:44 +0000 | [diff] [blame] | 514 | case CFGElement::Statement: |
Artem Dergachev | 41ffb30 | 2018-02-08 22:58:15 +0000 | [diff] [blame] | 515 | case CFGElement::Constructor: |
Artem Dergachev | 1527dec | 2018-03-12 23:12:40 +0000 | [diff] [blame] | 516 | case CFGElement::CXXRecordTypedCall: |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 517 | return PathDiagnosticLocation(Source.castAs<CFGStmt>().getStmt(), |
Jordan Rose | 80880ac | 2012-08-03 23:08:44 +0000 | [diff] [blame] | 518 | SM, CallerCtx); |
| 519 | case CFGElement::Initializer: { |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 520 | const CFGInitializer &Init = Source.castAs<CFGInitializer>(); |
Jordan Rose | 80880ac | 2012-08-03 23:08:44 +0000 | [diff] [blame] | 521 | return PathDiagnosticLocation(Init.getInitializer()->getInit(), |
| 522 | SM, CallerCtx); |
| 523 | } |
| 524 | case CFGElement::AutomaticObjectDtor: { |
David Blaikie | 2a01f5d | 2013-02-21 20:58:29 +0000 | [diff] [blame] | 525 | const CFGAutomaticObjDtor &Dtor = Source.castAs<CFGAutomaticObjDtor>(); |
Jordan Rose | 80880ac | 2012-08-03 23:08:44 +0000 | [diff] [blame] | 526 | return PathDiagnosticLocation::createEnd(Dtor.getTriggerStmt(), |
| 527 | SM, CallerCtx); |
| 528 | } |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 529 | case CFGElement::DeleteDtor: { |
Jordan Rose | 1417a7b | 2013-10-31 18:41:15 +0000 | [diff] [blame] | 530 | const CFGDeleteDtor &Dtor = Source.castAs<CFGDeleteDtor>(); |
| 531 | return PathDiagnosticLocation(Dtor.getDeleteExpr(), SM, CallerCtx); |
Jordan Rose | d2f4079 | 2013-09-03 17:00:57 +0000 | [diff] [blame] | 532 | } |
Jordan Rose | 80880ac | 2012-08-03 23:08:44 +0000 | [diff] [blame] | 533 | case CFGElement::BaseDtor: |
| 534 | case CFGElement::MemberDtor: { |
| 535 | const AnalysisDeclContext *CallerInfo = CallerCtx->getAnalysisDeclContext(); |
| 536 | if (const Stmt *CallerBody = CallerInfo->getBody()) |
| 537 | return PathDiagnosticLocation::createEnd(CallerBody, SM, CallerCtx); |
| 538 | return PathDiagnosticLocation::create(CallerInfo->getDecl(), SM); |
| 539 | } |
Artem Dergachev | e769fb7 | 2018-01-18 00:10:21 +0000 | [diff] [blame] | 540 | case CFGElement::NewAllocator: { |
| 541 | const CFGNewAllocator &Alloc = Source.castAs<CFGNewAllocator>(); |
| 542 | return PathDiagnosticLocation(Alloc.getAllocatorExpr(), SM, CallerCtx); |
| 543 | } |
Artem Dergachev | 60f5aab | 2018-02-15 19:28:21 +0000 | [diff] [blame] | 544 | case CFGElement::TemporaryDtor: { |
| 545 | // Temporary destructors are for temporaries. They die immediately at around |
| 546 | // the location of CXXBindTemporaryExpr. If they are lifetime-extended, |
| 547 | // they'd be dealt with via an AutomaticObjectDtor instead. |
| 548 | const auto &Dtor = Source.castAs<CFGTemporaryDtor>(); |
| 549 | return PathDiagnosticLocation::createEnd(Dtor.getBindTemporaryExpr(), SM, |
| 550 | CallerCtx); |
| 551 | } |
Maxim Ostapenko | debca45 | 2018-03-12 12:26:15 +0000 | [diff] [blame] | 552 | case CFGElement::ScopeBegin: |
| 553 | case CFGElement::ScopeEnd: |
| 554 | llvm_unreachable("not yet implemented!"); |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 555 | case CFGElement::LifetimeEnds: |
| 556 | case CFGElement::LoopExit: |
| 557 | llvm_unreachable("CFGElement kind should not be on callsite!"); |
Jordan Rose | 80880ac | 2012-08-03 23:08:44 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | llvm_unreachable("Unknown CFGElement kind"); |
| 561 | } |
| 562 | |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 563 | PathDiagnosticLocation |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 564 | PathDiagnosticLocation::createBegin(const Decl *D, |
| 565 | const SourceManager &SM) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 566 | return PathDiagnosticLocation(D->getBeginLoc(), SM, SingleLocK); |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | PathDiagnosticLocation |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 570 | PathDiagnosticLocation::createBegin(const Stmt *S, |
| 571 | const SourceManager &SM, |
| 572 | LocationOrAnalysisDeclContext LAC) { |
Anna Zaks | 83128bc | 2011-09-20 18:23:52 +0000 | [diff] [blame] | 573 | return PathDiagnosticLocation(getValidSourceLocation(S, LAC), |
Anna Zaks | 745486a | 2011-09-20 01:51:40 +0000 | [diff] [blame] | 574 | SM, SingleLocK); |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Jordan Rose | a4c0d21 | 2012-07-26 20:04:05 +0000 | [diff] [blame] | 577 | PathDiagnosticLocation |
| 578 | PathDiagnosticLocation::createEnd(const Stmt *S, |
| 579 | const SourceManager &SM, |
| 580 | LocationOrAnalysisDeclContext LAC) { |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 581 | if (const auto *CS = dyn_cast<CompoundStmt>(S)) |
Jordan Rose | a4c0d21 | 2012-07-26 20:04:05 +0000 | [diff] [blame] | 582 | return createEndBrace(CS, SM); |
| 583 | return PathDiagnosticLocation(getValidSourceLocation(S, LAC, /*End=*/true), |
| 584 | SM, SingleLocK); |
| 585 | } |
| 586 | |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 587 | PathDiagnosticLocation |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 588 | PathDiagnosticLocation::createOperatorLoc(const BinaryOperator *BO, |
| 589 | const SourceManager &SM) { |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 590 | return PathDiagnosticLocation(BO->getOperatorLoc(), SM, SingleLocK); |
| 591 | } |
| 592 | |
| 593 | PathDiagnosticLocation |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 594 | PathDiagnosticLocation::createConditionalColonLoc( |
Jordan Rose | 60bd88d | 2013-12-10 18:18:06 +0000 | [diff] [blame] | 595 | const ConditionalOperator *CO, |
| 596 | const SourceManager &SM) { |
| 597 | return PathDiagnosticLocation(CO->getColonLoc(), SM, SingleLocK); |
| 598 | } |
| 599 | |
Jordan Rose | 60bd88d | 2013-12-10 18:18:06 +0000 | [diff] [blame] | 600 | PathDiagnosticLocation |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 601 | PathDiagnosticLocation::createMemberLoc(const MemberExpr *ME, |
| 602 | const SourceManager &SM) { |
Kristof Umann | 4962816 | 2019-03-14 16:10:29 +0000 | [diff] [blame] | 603 | |
| 604 | assert(ME->getMemberLoc().isValid() || ME->getBeginLoc().isValid()); |
| 605 | |
| 606 | // In some cases, getMemberLoc isn't valid -- in this case we'll return with |
| 607 | // some other related valid SourceLocation. |
| 608 | if (ME->getMemberLoc().isValid()) |
| 609 | return PathDiagnosticLocation(ME->getMemberLoc(), SM, SingleLocK); |
| 610 | |
| 611 | return PathDiagnosticLocation(ME->getBeginLoc(), SM, SingleLocK); |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | PathDiagnosticLocation |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 615 | PathDiagnosticLocation::createBeginBrace(const CompoundStmt *CS, |
| 616 | const SourceManager &SM) { |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 617 | SourceLocation L = CS->getLBracLoc(); |
| 618 | return PathDiagnosticLocation(L, SM, SingleLocK); |
| 619 | } |
| 620 | |
| 621 | PathDiagnosticLocation |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 622 | PathDiagnosticLocation::createEndBrace(const CompoundStmt *CS, |
| 623 | const SourceManager &SM) { |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 624 | SourceLocation L = CS->getRBracLoc(); |
| 625 | return PathDiagnosticLocation(L, SM, SingleLocK); |
| 626 | } |
| 627 | |
| 628 | PathDiagnosticLocation |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 629 | PathDiagnosticLocation::createDeclBegin(const LocationContext *LC, |
| 630 | const SourceManager &SM) { |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 631 | // FIXME: Should handle CXXTryStmt if analyser starts supporting C++. |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 632 | if (const auto *CS = dyn_cast_or_null<CompoundStmt>(LC->getDecl()->getBody())) |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 633 | if (!CS->body_empty()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 634 | SourceLocation Loc = (*CS->body_begin())->getBeginLoc(); |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 635 | return PathDiagnosticLocation(Loc, SM, SingleLocK); |
| 636 | } |
| 637 | |
| 638 | return PathDiagnosticLocation(); |
| 639 | } |
| 640 | |
| 641 | PathDiagnosticLocation |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 642 | PathDiagnosticLocation::createDeclEnd(const LocationContext *LC, |
| 643 | const SourceManager &SM) { |
Anna Zaks | 921f049 | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 644 | SourceLocation L = LC->getDecl()->getBodyRBrace(); |
Anna Zaks | efd182d | 2011-09-16 19:18:30 +0000 | [diff] [blame] | 645 | return PathDiagnosticLocation(L, SM, SingleLocK); |
Anna Zaks | 921f049 | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Anna Zaks | de8c5ed | 2011-09-20 01:38:47 +0000 | [diff] [blame] | 648 | PathDiagnosticLocation |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 649 | PathDiagnosticLocation::create(const ProgramPoint& P, |
| 650 | const SourceManager &SMng) { |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 651 | const Stmt* S = nullptr; |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 652 | if (Optional<BlockEdge> BE = P.getAs<BlockEdge>()) { |
Anna Zaks | 921f049 | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 653 | const CFGBlock *BSrc = BE->getSrc(); |
Artem Dergachev | ef0aab3 | 2019-05-24 23:37:11 +0000 | [diff] [blame] | 654 | if (BSrc->getTerminator().isVirtualBaseBranch()) { |
| 655 | // TODO: VirtualBaseBranches should also appear for destructors. |
| 656 | // In this case we should put the diagnostic at the end of decl. |
| 657 | return PathDiagnosticLocation::createBegin( |
| 658 | P.getLocationContext()->getDecl(), SMng); |
| 659 | |
| 660 | } else { |
| 661 | S = BSrc->getTerminatorCondition(); |
Adam Balogh | 9ed4b31 | 2019-05-28 13:07:09 +0000 | [diff] [blame] | 662 | if (!S) { |
| 663 | // If the BlockEdge has no terminator condition statement but its |
| 664 | // source is the entry of the CFG (e.g. a checker crated the branch at |
| 665 | // the beginning of a function), use the function's declaration instead. |
| 666 | assert(BSrc == &BSrc->getParent()->getEntry() && "CFGBlock has no " |
| 667 | "TerminatorCondition and is not the enrty block of the CFG"); |
| 668 | return PathDiagnosticLocation::createBegin( |
| 669 | P.getLocationContext()->getDecl(), SMng); |
| 670 | } |
Artem Dergachev | ef0aab3 | 2019-05-24 23:37:11 +0000 | [diff] [blame] | 671 | } |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 672 | } else if (Optional<StmtPoint> SP = P.getAs<StmtPoint>()) { |
Jordan Rose | ec44ac6 | 2012-10-29 17:31:59 +0000 | [diff] [blame] | 673 | S = SP->getStmt(); |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 674 | if (P.getAs<PostStmtPurgeDeadSymbols>()) |
Jordan Rose | e37ab50 | 2012-11-15 19:11:43 +0000 | [diff] [blame] | 675 | return PathDiagnosticLocation::createEnd(S, SMng, P.getLocationContext()); |
Anna Zaks | ece622a | 2013-04-05 00:59:33 +0000 | [diff] [blame] | 676 | } else if (Optional<PostInitializer> PIP = P.getAs<PostInitializer>()) { |
| 677 | return PathDiagnosticLocation(PIP->getInitializer()->getSourceLocation(), |
| 678 | SMng); |
Artem Dergachev | fc72007 | 2019-01-10 23:44:44 +0000 | [diff] [blame] | 679 | } else if (Optional<PreImplicitCall> PIC = P.getAs<PreImplicitCall>()) { |
| 680 | return PathDiagnosticLocation(PIC->getLocation(), SMng); |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 681 | } else if (Optional<PostImplicitCall> PIE = P.getAs<PostImplicitCall>()) { |
Jordan Rose | 80880ac | 2012-08-03 23:08:44 +0000 | [diff] [blame] | 682 | return PathDiagnosticLocation(PIE->getLocation(), SMng); |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 683 | } else if (Optional<CallEnter> CE = P.getAs<CallEnter>()) { |
Jordan Rose | 80880ac | 2012-08-03 23:08:44 +0000 | [diff] [blame] | 684 | return getLocationForCaller(CE->getCalleeContext(), |
| 685 | CE->getLocationContext(), |
| 686 | SMng); |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 687 | } else if (Optional<CallExitEnd> CEE = P.getAs<CallExitEnd>()) { |
Jordan Rose | 80880ac | 2012-08-03 23:08:44 +0000 | [diff] [blame] | 688 | return getLocationForCaller(CEE->getCalleeContext(), |
| 689 | CEE->getLocationContext(), |
| 690 | SMng); |
George Karpenkov | 2c2d0b6 | 2019-01-18 19:24:55 +0000 | [diff] [blame] | 691 | } else if (auto CEB = P.getAs<CallExitBegin>()) { |
| 692 | if (const ReturnStmt *RS = CEB->getReturnStmt()) |
| 693 | return PathDiagnosticLocation::createBegin(RS, SMng, |
| 694 | CEB->getLocationContext()); |
| 695 | return PathDiagnosticLocation( |
| 696 | CEB->getLocationContext()->getDecl()->getSourceRange().getEnd(), SMng); |
Devin Coughlin | 9a2c14a | 2017-11-29 18:25:37 +0000 | [diff] [blame] | 697 | } else if (Optional<BlockEntrance> BE = P.getAs<BlockEntrance>()) { |
Kristof Umann | b8ac93c | 2019-09-18 22:24:26 +0000 | [diff] [blame] | 698 | if (Optional<CFGElement> BlockFront = BE->getFirstElement()) { |
| 699 | if (auto StmtElt = BlockFront->getAs<CFGStmt>()) { |
| 700 | return PathDiagnosticLocation(StmtElt->getStmt()->getBeginLoc(), SMng); |
| 701 | } else if (auto NewAllocElt = BlockFront->getAs<CFGNewAllocator>()) { |
| 702 | return PathDiagnosticLocation( |
| 703 | NewAllocElt->getAllocatorExpr()->getBeginLoc(), SMng); |
| 704 | } |
| 705 | llvm_unreachable("Unexpected CFG element at front of block"); |
Devin Coughlin | 9a2c14a | 2017-11-29 18:25:37 +0000 | [diff] [blame] | 706 | } |
Kristof Umann | b8ac93c | 2019-09-18 22:24:26 +0000 | [diff] [blame] | 707 | |
| 708 | return PathDiagnosticLocation( |
| 709 | BE->getBlock()->getTerminatorStmt()->getBeginLoc(), SMng); |
Csaba Dabis | 57835bc | 2019-07-04 00:50:50 +0000 | [diff] [blame] | 710 | } else if (Optional<FunctionExitPoint> FE = P.getAs<FunctionExitPoint>()) { |
| 711 | return PathDiagnosticLocation(FE->getStmt(), SMng, |
| 712 | FE->getLocationContext()); |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 713 | } else { |
Jordan Rose | ec44ac6 | 2012-10-29 17:31:59 +0000 | [diff] [blame] | 714 | llvm_unreachable("Unexpected ProgramPoint"); |
| 715 | } |
Anna Zaks | 921f049 | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 716 | |
Anna Zaks | de8c5ed | 2011-09-20 01:38:47 +0000 | [diff] [blame] | 717 | return PathDiagnosticLocation(S, SMng, P.getLocationContext()); |
Anna Zaks | 921f049 | 2011-09-15 18:56:07 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Anna Zaks | de8c5ed | 2011-09-20 01:38:47 +0000 | [diff] [blame] | 720 | PathDiagnosticLocation PathDiagnosticLocation::createSingleLocation( |
| 721 | const PathDiagnosticLocation &PDL) { |
| 722 | FullSourceLoc L = PDL.asLocation(); |
| 723 | return PathDiagnosticLocation(L, L.getManager(), SingleLocK); |
| 724 | } |
| 725 | |
Anna Zaks | 745486a | 2011-09-20 01:51:40 +0000 | [diff] [blame] | 726 | FullSourceLoc |
Anna Zaks | 15ca5e7 | 2011-09-21 00:35:58 +0000 | [diff] [blame] | 727 | PathDiagnosticLocation::genLocation(SourceLocation L, |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 728 | LocationOrAnalysisDeclContext LAC) const { |
Ted Kremenek | 0bb0909 | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 729 | assert(isValid()); |
Ted Kremenek | c251165 | 2009-03-26 21:42:51 +0000 | [diff] [blame] | 730 | // Note that we want a 'switch' here so that the compiler can warn us in |
| 731 | // case we add more cases. |
Ted Kremenek | bade06e | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 732 | switch (K) { |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 733 | case SingleLocK: |
| 734 | case RangeK: |
Ted Kremenek | 7c36d90 | 2009-03-26 21:42:00 +0000 | [diff] [blame] | 735 | break; |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 736 | case StmtK: |
Ted Kremenek | c07e34c | 2012-01-10 15:26:13 +0000 | [diff] [blame] | 737 | // Defensive checking. |
| 738 | if (!S) |
| 739 | break; |
Anna Zaks | 83128bc | 2011-09-20 18:23:52 +0000 | [diff] [blame] | 740 | return FullSourceLoc(getValidSourceLocation(S, LAC), |
Anna Zaks | 615e3a6 | 2011-09-14 17:48:01 +0000 | [diff] [blame] | 741 | const_cast<SourceManager&>(*SM)); |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 742 | case DeclK: |
Ted Kremenek | c07e34c | 2012-01-10 15:26:13 +0000 | [diff] [blame] | 743 | // Defensive checking. |
| 744 | if (!D) |
| 745 | break; |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 746 | return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM)); |
Ted Kremenek | bade06e | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 747 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 748 | |
Anna Zaks | d70da08 | 2011-09-20 23:27:32 +0000 | [diff] [blame] | 749 | return FullSourceLoc(L, const_cast<SourceManager&>(*SM)); |
Ted Kremenek | bade06e | 2009-03-26 21:39:39 +0000 | [diff] [blame] | 750 | } |
Ted Kremenek | 96110d5 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 751 | |
Anna Zaks | 745486a | 2011-09-20 01:51:40 +0000 | [diff] [blame] | 752 | PathDiagnosticRange |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 753 | PathDiagnosticLocation::genRange(LocationOrAnalysisDeclContext LAC) const { |
Ted Kremenek | 0bb0909 | 2009-04-01 06:13:56 +0000 | [diff] [blame] | 754 | assert(isValid()); |
Ted Kremenek | 96110d5 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 755 | // Note that we want a 'switch' here so that the compiler can warn us in |
| 756 | // case we add more cases. |
| 757 | switch (K) { |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 758 | case SingleLocK: |
Anna Zaks | 15ca5e7 | 2011-09-21 00:35:58 +0000 | [diff] [blame] | 759 | return PathDiagnosticRange(SourceRange(Loc,Loc), true); |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 760 | case RangeK: |
Ted Kremenek | 96110d5 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 761 | break; |
Ted Kremenek | 22579c4 | 2009-04-22 18:03:00 +0000 | [diff] [blame] | 762 | case StmtK: { |
| 763 | const Stmt *S = asStmt(); |
| 764 | switch (S->getStmtClass()) { |
| 765 | default: |
| 766 | break; |
Ted Kremenek | 18665fe | 2009-05-15 02:05:25 +0000 | [diff] [blame] | 767 | case Stmt::DeclStmtClass: { |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 768 | const auto *DS = cast<DeclStmt>(S); |
Ted Kremenek | 18665fe | 2009-05-15 02:05:25 +0000 | [diff] [blame] | 769 | if (DS->isSingleDecl()) { |
| 770 | // Should always be the case, but we'll be defensive. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 771 | return SourceRange(DS->getBeginLoc(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 772 | DS->getSingleDecl()->getLocation()); |
Ted Kremenek | 18665fe | 2009-05-15 02:05:25 +0000 | [diff] [blame] | 773 | } |
| 774 | break; |
| 775 | } |
Ted Kremenek | 22579c4 | 2009-04-22 18:03:00 +0000 | [diff] [blame] | 776 | // FIXME: Provide better range information for different |
| 777 | // terminators. |
| 778 | case Stmt::IfStmtClass: |
| 779 | case Stmt::WhileStmtClass: |
| 780 | case Stmt::DoStmtClass: |
| 781 | case Stmt::ForStmtClass: |
| 782 | case Stmt::ChooseExprClass: |
| 783 | case Stmt::IndirectGotoStmtClass: |
| 784 | case Stmt::SwitchStmtClass: |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 785 | case Stmt::BinaryConditionalOperatorClass: |
Ted Kremenek | 22579c4 | 2009-04-22 18:03:00 +0000 | [diff] [blame] | 786 | case Stmt::ConditionalOperatorClass: |
| 787 | case Stmt::ObjCForCollectionStmtClass: { |
Anna Zaks | 83128bc | 2011-09-20 18:23:52 +0000 | [diff] [blame] | 788 | SourceLocation L = getValidSourceLocation(S, LAC); |
Ted Kremenek | 22579c4 | 2009-04-22 18:03:00 +0000 | [diff] [blame] | 789 | return SourceRange(L, L); |
| 790 | } |
| 791 | } |
Anna Zaks | 15ca5e7 | 2011-09-21 00:35:58 +0000 | [diff] [blame] | 792 | SourceRange R = S->getSourceRange(); |
| 793 | if (R.isValid()) |
| 794 | return R; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 795 | break; |
Ted Kremenek | 22579c4 | 2009-04-22 18:03:00 +0000 | [diff] [blame] | 796 | } |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 797 | case DeclK: |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 798 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 799 | return MD->getSourceRange(); |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 800 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | 0b6a06a | 2010-07-07 12:24:14 +0000 | [diff] [blame] | 801 | if (Stmt *Body = FD->getBody()) |
| 802 | return Body->getSourceRange(); |
Douglas Gregor | e3dcb2d | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 803 | } |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 804 | else { |
| 805 | SourceLocation L = D->getLocation(); |
Ted Kremenek | 63e29cc | 2009-04-22 22:26:10 +0000 | [diff] [blame] | 806 | return PathDiagnosticRange(SourceRange(L, L), true); |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 807 | } |
Ted Kremenek | 96110d5 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 808 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 809 | |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 810 | return SourceRange(Loc, Loc); |
Ted Kremenek | 96110d5 | 2009-03-26 21:48:17 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 813 | void PathDiagnosticLocation::flatten() { |
| 814 | if (K == StmtK) { |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 815 | K = RangeK; |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 816 | S = nullptr; |
| 817 | D = nullptr; |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 818 | } |
| 819 | else if (K == DeclK) { |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 820 | K = SingleLocK; |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 821 | S = nullptr; |
| 822 | D = nullptr; |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 826 | //===----------------------------------------------------------------------===// |
| 827 | // Manipulation of PathDiagnosticCallPieces. |
| 828 | //===----------------------------------------------------------------------===// |
| 829 | |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 830 | std::shared_ptr<PathDiagnosticCallPiece> |
George Karpenkov | c82d457 | 2018-09-28 18:49:41 +0000 | [diff] [blame] | 831 | PathDiagnosticCallPiece::construct(const CallExitEnd &CE, |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 832 | const SourceManager &SM) { |
Anna Zaks | 7e53bd6 | 2012-04-20 21:59:08 +0000 | [diff] [blame] | 833 | const Decl *caller = CE.getLocationContext()->getDecl(); |
Jordan Rose | a4c0d21 | 2012-07-26 20:04:05 +0000 | [diff] [blame] | 834 | PathDiagnosticLocation pos = getLocationForCaller(CE.getCalleeContext(), |
| 835 | CE.getLocationContext(), |
| 836 | SM); |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 837 | return std::shared_ptr<PathDiagnosticCallPiece>( |
| 838 | new PathDiagnosticCallPiece(caller, pos)); |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | PathDiagnosticCallPiece * |
Anna Zaks | d4e9059 | 2012-03-14 18:58:28 +0000 | [diff] [blame] | 842 | PathDiagnosticCallPiece::construct(PathPieces &path, |
| 843 | const Decl *caller) { |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 844 | std::shared_ptr<PathDiagnosticCallPiece> C( |
| 845 | new PathDiagnosticCallPiece(path, caller)); |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 846 | path.clear(); |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 847 | auto *R = C.get(); |
| 848 | path.push_front(std::move(C)); |
| 849 | return R; |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | void PathDiagnosticCallPiece::setCallee(const CallEnter &CE, |
| 853 | const SourceManager &SM) { |
Jordan Rose | a4c0d21 | 2012-07-26 20:04:05 +0000 | [diff] [blame] | 854 | const StackFrameContext *CalleeCtx = CE.getCalleeContext(); |
| 855 | Callee = CalleeCtx->getDecl(); |
| 856 | |
| 857 | callEnterWithin = PathDiagnosticLocation::createBegin(Callee, SM); |
| 858 | callEnter = getLocationForCaller(CalleeCtx, CE.getLocationContext(), SM); |
Artem Dergachev | fbe891ee | 2017-06-05 12:40:03 +0000 | [diff] [blame] | 859 | |
| 860 | // Autosynthesized property accessors are special because we'd never |
| 861 | // pop back up to non-autosynthesized code until we leave them. |
| 862 | // This is not generally true for autosynthesized callees, which may call |
| 863 | // non-autosynthesized callbacks. |
| 864 | // Unless set here, the IsCalleeAnAutosynthesizedPropertyAccessor flag |
| 865 | // defaults to false. |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 866 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(Callee)) |
Artem Dergachev | fbe891ee | 2017-06-05 12:40:03 +0000 | [diff] [blame] | 867 | IsCalleeAnAutosynthesizedPropertyAccessor = ( |
| 868 | MD->isPropertyAccessor() && |
| 869 | CalleeCtx->getAnalysisDeclContext()->isBodyAutosynthesized()); |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Kristof Umann | 3ea7442 | 2018-05-25 13:18:38 +0000 | [diff] [blame] | 872 | static void describeTemplateParameters(raw_ostream &Out, |
| 873 | const ArrayRef<TemplateArgument> TAList, |
| 874 | const LangOptions &LO, |
| 875 | StringRef Prefix = StringRef(), |
| 876 | StringRef Postfix = StringRef()); |
| 877 | |
| 878 | static void describeTemplateParameter(raw_ostream &Out, |
| 879 | const TemplateArgument &TArg, |
| 880 | const LangOptions &LO) { |
| 881 | |
| 882 | if (TArg.getKind() == TemplateArgument::ArgKind::Pack) { |
| 883 | describeTemplateParameters(Out, TArg.getPackAsArray(), LO); |
| 884 | } else { |
| 885 | TArg.print(PrintingPolicy(LO), Out); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | static void describeTemplateParameters(raw_ostream &Out, |
| 890 | const ArrayRef<TemplateArgument> TAList, |
| 891 | const LangOptions &LO, |
| 892 | StringRef Prefix, StringRef Postfix) { |
| 893 | if (TAList.empty()) |
| 894 | return; |
| 895 | |
| 896 | Out << Prefix; |
| 897 | for (int I = 0, Last = TAList.size() - 1; I != Last; ++I) { |
| 898 | describeTemplateParameter(Out, TAList[I], LO); |
| 899 | Out << ", "; |
| 900 | } |
| 901 | describeTemplateParameter(Out, TAList[TAList.size() - 1], LO); |
| 902 | Out << Postfix; |
| 903 | } |
| 904 | |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 905 | static void describeClass(raw_ostream &Out, const CXXRecordDecl *D, |
| 906 | StringRef Prefix = StringRef()) { |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 907 | if (!D->getIdentifier()) |
| 908 | return; |
Kristof Umann | 3ea7442 | 2018-05-25 13:18:38 +0000 | [diff] [blame] | 909 | Out << Prefix << '\'' << *D; |
| 910 | if (const auto T = dyn_cast<ClassTemplateSpecializationDecl>(D)) |
| 911 | describeTemplateParameters(Out, T->getTemplateArgs().asArray(), |
| 912 | D->getASTContext().getLangOpts(), "<", ">"); |
| 913 | |
| 914 | Out << '\''; |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 917 | static bool describeCodeDecl(raw_ostream &Out, const Decl *D, |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 918 | bool ExtendedDescription, |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 919 | StringRef Prefix = StringRef()) { |
| 920 | if (!D) |
| 921 | return false; |
| 922 | |
| 923 | if (isa<BlockDecl>(D)) { |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 924 | if (ExtendedDescription) |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 925 | Out << Prefix << "anonymous block"; |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 926 | return ExtendedDescription; |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 929 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 930 | Out << Prefix; |
| 931 | if (ExtendedDescription && !MD->isUserProvided()) { |
| 932 | if (MD->isExplicitlyDefaulted()) |
| 933 | Out << "defaulted "; |
| 934 | else |
| 935 | Out << "implicit "; |
| 936 | } |
| 937 | |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 938 | if (const auto *CD = dyn_cast<CXXConstructorDecl>(MD)) { |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 939 | if (CD->isDefaultConstructor()) |
| 940 | Out << "default "; |
| 941 | else if (CD->isCopyConstructor()) |
| 942 | Out << "copy "; |
| 943 | else if (CD->isMoveConstructor()) |
| 944 | Out << "move "; |
| 945 | |
| 946 | Out << "constructor"; |
| 947 | describeClass(Out, MD->getParent(), " for "); |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 948 | } else if (isa<CXXDestructorDecl>(MD)) { |
| 949 | if (!MD->isUserProvided()) { |
| 950 | Out << "destructor"; |
| 951 | describeClass(Out, MD->getParent(), " for "); |
| 952 | } else { |
| 953 | // Use ~Foo for explicitly-written destructors. |
| 954 | Out << "'" << *MD << "'"; |
| 955 | } |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 956 | } else if (MD->isCopyAssignmentOperator()) { |
| 957 | Out << "copy assignment operator"; |
| 958 | describeClass(Out, MD->getParent(), " for "); |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 959 | } else if (MD->isMoveAssignmentOperator()) { |
| 960 | Out << "move assignment operator"; |
| 961 | describeClass(Out, MD->getParent(), " for "); |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 962 | } else { |
| 963 | if (MD->getParent()->getIdentifier()) |
| 964 | Out << "'" << *MD->getParent() << "::" << *MD << "'"; |
| 965 | else |
| 966 | Out << "'" << *MD << "'"; |
| 967 | } |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 968 | |
| 969 | return true; |
| 970 | } |
| 971 | |
Kristof Umann | 3ea7442 | 2018-05-25 13:18:38 +0000 | [diff] [blame] | 972 | Out << Prefix << '\'' << cast<NamedDecl>(*D); |
| 973 | |
| 974 | // Adding template parameters. |
| 975 | if (const auto FD = dyn_cast<FunctionDecl>(D)) |
| 976 | if (const TemplateArgumentList *TAList = |
| 977 | FD->getTemplateSpecializationArgs()) |
| 978 | describeTemplateParameters(Out, TAList->asArray(), |
| 979 | FD->getASTContext().getLangOpts(), "<", ">"); |
| 980 | |
| 981 | Out << '\''; |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 982 | return true; |
| 983 | } |
| 984 | |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 985 | std::shared_ptr<PathDiagnosticEventPiece> |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 986 | PathDiagnosticCallPiece::getCallEnterEvent() const { |
Artem Dergachev | fbe891ee | 2017-06-05 12:40:03 +0000 | [diff] [blame] | 987 | // We do not produce call enters and call exits for autosynthesized property |
| 988 | // accessors. We do generally produce them for other functions coming from |
| 989 | // the body farm because they may call callbacks that bring us back into |
| 990 | // visible code. |
| 991 | if (!Callee || IsCalleeAnAutosynthesizedPropertyAccessor) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 992 | return nullptr; |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 993 | |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 994 | SmallString<256> buf; |
| 995 | llvm::raw_svector_ostream Out(buf); |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 996 | |
| 997 | Out << "Calling "; |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 998 | describeCodeDecl(Out, Callee, /*ExtendedDescription=*/true); |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 999 | |
Jordan Rose | 2d98b97 | 2012-11-15 02:07:23 +0000 | [diff] [blame] | 1000 | assert(callEnter.asLocation().isValid()); |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 1001 | return std::make_shared<PathDiagnosticEventPiece>(callEnter, Out.str()); |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 1004 | std::shared_ptr<PathDiagnosticEventPiece> |
Ted Kremenek | 04e860d | 2012-03-06 01:25:01 +0000 | [diff] [blame] | 1005 | PathDiagnosticCallPiece::getCallEnterWithinCallerEvent() const { |
Jordan Rose | 2d98b97 | 2012-11-15 02:07:23 +0000 | [diff] [blame] | 1006 | if (!callEnterWithin.asLocation().isValid()) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 1007 | return nullptr; |
Jordan Rose | 5613826 | 2013-05-24 21:43:11 +0000 | [diff] [blame] | 1008 | if (Callee->isImplicit() || !Callee->hasBody()) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 1009 | return nullptr; |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 1010 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Callee)) |
Jordan Rose | d8876a7 | 2013-01-19 19:52:57 +0000 | [diff] [blame] | 1011 | if (MD->isDefaulted()) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 1012 | return nullptr; |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 1013 | |
Ted Kremenek | 04e860d | 2012-03-06 01:25:01 +0000 | [diff] [blame] | 1014 | SmallString<256> buf; |
| 1015 | llvm::raw_svector_ostream Out(buf); |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 1016 | |
| 1017 | Out << "Entered call"; |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 1018 | describeCodeDecl(Out, Caller, /*ExtendedDescription=*/false, " from "); |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 1019 | |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 1020 | return std::make_shared<PathDiagnosticEventPiece>(callEnterWithin, Out.str()); |
Ted Kremenek | 04e860d | 2012-03-06 01:25:01 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 1023 | std::shared_ptr<PathDiagnosticEventPiece> |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 1024 | PathDiagnosticCallPiece::getCallExitEvent() const { |
Artem Dergachev | fbe891ee | 2017-06-05 12:40:03 +0000 | [diff] [blame] | 1025 | // We do not produce call enters and call exits for autosynthesized property |
| 1026 | // accessors. We do generally produce them for other functions coming from |
| 1027 | // the body farm because they may call callbacks that bring us back into |
| 1028 | // visible code. |
| 1029 | if (NoExit || IsCalleeAnAutosynthesizedPropertyAccessor) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 1030 | return nullptr; |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 1031 | |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 1032 | SmallString<256> buf; |
| 1033 | llvm::raw_svector_ostream Out(buf); |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 1034 | |
| 1035 | if (!CallStackMessage.empty()) { |
Anna Zaks | a7f457a | 2012-03-16 23:44:28 +0000 | [diff] [blame] | 1036 | Out << CallStackMessage; |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 1037 | } else { |
Jordan Rose | 1dc3940 | 2013-01-18 18:27:21 +0000 | [diff] [blame] | 1038 | bool DidDescribe = describeCodeDecl(Out, Callee, |
| 1039 | /*ExtendedDescription=*/false, |
Jordan Rose | fe856d5 | 2013-01-18 18:27:14 +0000 | [diff] [blame] | 1040 | "Returning from "); |
| 1041 | if (!DidDescribe) |
| 1042 | Out << "Returning to caller"; |
| 1043 | } |
| 1044 | |
Jordan Rose | 2d98b97 | 2012-11-15 02:07:23 +0000 | [diff] [blame] | 1045 | assert(callReturn.asLocation().isValid()); |
David Blaikie | 0a0c275 | 2017-01-05 17:26:53 +0000 | [diff] [blame] | 1046 | return std::make_shared<PathDiagnosticEventPiece>(callReturn, Out.str()); |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | static void compute_path_size(const PathPieces &pieces, unsigned &size) { |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 1050 | for (const auto &I : pieces) { |
| 1051 | const PathDiagnosticPiece *piece = I.get(); |
| 1052 | if (const auto *cp = dyn_cast<PathDiagnosticCallPiece>(piece)) |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 1053 | compute_path_size(cp->path, size); |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 1054 | else |
| 1055 | ++size; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | unsigned PathDiagnostic::full_size() { |
| 1060 | unsigned size = 0; |
| 1061 | compute_path_size(path, size); |
| 1062 | return size; |
| 1063 | } |
| 1064 | |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1065 | //===----------------------------------------------------------------------===// |
| 1066 | // FoldingSet profiling methods. |
| 1067 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 92820d2 | 2009-04-06 22:33:35 +0000 | [diff] [blame] | 1068 | |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1069 | void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const { |
Anna Zaks | b2dfc2b | 2011-09-20 21:25:00 +0000 | [diff] [blame] | 1070 | ID.AddInteger(Range.getBegin().getRawEncoding()); |
| 1071 | ID.AddInteger(Range.getEnd().getRawEncoding()); |
| 1072 | ID.AddInteger(Loc.getRawEncoding()); |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 1076 | ID.AddInteger((unsigned) getKind()); |
| 1077 | ID.AddString(str); |
| 1078 | // FIXME: Add profiling support for code hints. |
| 1079 | ID.AddInteger((unsigned) getDisplayHint()); |
Ted Kremenek | 1e60273 | 2012-08-16 17:45:29 +0000 | [diff] [blame] | 1080 | ArrayRef<SourceRange> Ranges = getRanges(); |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 1081 | for (const auto &I : Ranges) { |
| 1082 | ID.AddInteger(I.getBegin().getRawEncoding()); |
| 1083 | ID.AddInteger(I.getEnd().getRawEncoding()); |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 1084 | } |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 1087 | void PathDiagnosticCallPiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 1088 | PathDiagnosticPiece::Profile(ID); |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 1089 | for (const auto &I : path) |
| 1090 | ID.Add(*I); |
Ted Kremenek | 60a7820 | 2012-02-24 06:00:00 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1093 | void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 1094 | PathDiagnosticPiece::Profile(ID); |
| 1095 | ID.Add(Pos); |
| 1096 | } |
| 1097 | |
| 1098 | void PathDiagnosticControlFlowPiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 1099 | PathDiagnosticPiece::Profile(ID); |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 1100 | for (const auto &I : *this) |
| 1101 | ID.Add(I); |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 1105 | PathDiagnosticSpotPiece::Profile(ID); |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 1106 | for (const auto &I : subPieces) |
| 1107 | ID.Add(*I); |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Artem Dergachev | 8b70c4e | 2016-10-07 15:23:02 +0000 | [diff] [blame] | 1110 | void PathDiagnosticNotePiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 1111 | PathDiagnosticSpotPiece::Profile(ID); |
| 1112 | } |
| 1113 | |
Csaba Dabis | 1d7ca67 | 2019-05-29 19:21:59 +0000 | [diff] [blame] | 1114 | void PathDiagnosticPopUpPiece::Profile(llvm::FoldingSetNodeID &ID) const { |
| 1115 | PathDiagnosticSpotPiece::Profile(ID); |
| 1116 | } |
| 1117 | |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1118 | void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const { |
Jordan Rose | cc0b1bf | 2012-08-31 00:36:26 +0000 | [diff] [blame] | 1119 | ID.Add(getLocation()); |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1120 | ID.AddString(BugType); |
Jordan Rose | cc0b1bf | 2012-08-31 00:36:26 +0000 | [diff] [blame] | 1121 | ID.AddString(VerboseDesc); |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1122 | ID.AddString(Category); |
Ted Kremenek | 8e4c426 | 2012-01-25 23:47:14 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | void PathDiagnostic::FullProfile(llvm::FoldingSetNodeID &ID) const { |
| 1126 | Profile(ID); |
Eugene Zelenko | 9f103a1 | 2018-03-08 22:45:13 +0000 | [diff] [blame] | 1127 | for (const auto &I : path) |
| 1128 | ID.Add(*I); |
Ted Kremenek | 3d8f862 | 2009-09-18 22:33:39 +0000 | [diff] [blame] | 1129 | for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I) |
| 1130 | ID.AddString(*I); |
| 1131 | } |
Anna Zaks | cba4f29 | 2012-03-16 23:24:20 +0000 | [diff] [blame] | 1132 | |
George Karpenkov | 1cf8cdc | 2018-06-12 20:51:01 +0000 | [diff] [blame] | 1133 | LLVM_DUMP_METHOD void PathPieces::dump() const { |
| 1134 | unsigned index = 0; |
| 1135 | for (PathPieces::const_iterator I = begin(), E = end(); I != E; ++I) { |
| 1136 | llvm::errs() << "[" << index++ << "] "; |
| 1137 | (*I)->dump(); |
| 1138 | llvm::errs() << "\n"; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | LLVM_DUMP_METHOD void PathDiagnosticCallPiece::dump() const { |
| 1143 | llvm::errs() << "CALL\n--------------\n"; |
| 1144 | |
| 1145 | if (const Stmt *SLoc = getLocation().getStmtOrNull()) |
| 1146 | SLoc->dump(); |
| 1147 | else if (const auto *ND = dyn_cast_or_null<NamedDecl>(getCallee())) |
| 1148 | llvm::errs() << *ND << "\n"; |
| 1149 | else |
| 1150 | getLocation().dump(); |
| 1151 | } |
| 1152 | |
| 1153 | LLVM_DUMP_METHOD void PathDiagnosticEventPiece::dump() const { |
| 1154 | llvm::errs() << "EVENT\n--------------\n"; |
| 1155 | llvm::errs() << getString() << "\n"; |
| 1156 | llvm::errs() << " ---- at ----\n"; |
| 1157 | getLocation().dump(); |
| 1158 | } |
| 1159 | |
| 1160 | LLVM_DUMP_METHOD void PathDiagnosticControlFlowPiece::dump() const { |
| 1161 | llvm::errs() << "CONTROL\n--------------\n"; |
| 1162 | getStartLocation().dump(); |
| 1163 | llvm::errs() << " ---- to ----\n"; |
| 1164 | getEndLocation().dump(); |
| 1165 | } |
| 1166 | |
| 1167 | LLVM_DUMP_METHOD void PathDiagnosticMacroPiece::dump() const { |
| 1168 | llvm::errs() << "MACRO\n--------------\n"; |
| 1169 | // FIXME: Print which macro is being invoked. |
| 1170 | } |
| 1171 | |
| 1172 | LLVM_DUMP_METHOD void PathDiagnosticNotePiece::dump() const { |
| 1173 | llvm::errs() << "NOTE\n--------------\n"; |
| 1174 | llvm::errs() << getString() << "\n"; |
| 1175 | llvm::errs() << " ---- at ----\n"; |
| 1176 | getLocation().dump(); |
| 1177 | } |
| 1178 | |
Csaba Dabis | 1d7ca67 | 2019-05-29 19:21:59 +0000 | [diff] [blame] | 1179 | LLVM_DUMP_METHOD void PathDiagnosticPopUpPiece::dump() const { |
| 1180 | llvm::errs() << "POP-UP\n--------------\n"; |
| 1181 | llvm::errs() << getString() << "\n"; |
| 1182 | llvm::errs() << " ---- at ----\n"; |
| 1183 | getLocation().dump(); |
| 1184 | } |
| 1185 | |
George Karpenkov | 1cf8cdc | 2018-06-12 20:51:01 +0000 | [diff] [blame] | 1186 | LLVM_DUMP_METHOD void PathDiagnosticLocation::dump() const { |
| 1187 | if (!isValid()) { |
| 1188 | llvm::errs() << "<INVALID>\n"; |
| 1189 | return; |
| 1190 | } |
| 1191 | |
| 1192 | switch (K) { |
| 1193 | case RangeK: |
| 1194 | // FIXME: actually print the range. |
| 1195 | llvm::errs() << "<range>\n"; |
| 1196 | break; |
| 1197 | case SingleLocK: |
| 1198 | asLocation().dump(); |
| 1199 | llvm::errs() << "\n"; |
| 1200 | break; |
| 1201 | case StmtK: |
| 1202 | if (S) |
| 1203 | S->dump(); |
| 1204 | else |
| 1205 | llvm::errs() << "<NULL STMT>\n"; |
| 1206 | break; |
| 1207 | case DeclK: |
| 1208 | if (const auto *ND = dyn_cast_or_null<NamedDecl>(D)) |
| 1209 | llvm::errs() << *ND << "\n"; |
| 1210 | else if (isa<BlockDecl>(D)) |
| 1211 | // FIXME: Make this nicer. |
| 1212 | llvm::errs() << "<block>\n"; |
| 1213 | else if (D) |
| 1214 | llvm::errs() << "<unknown decl>\n"; |
| 1215 | else |
| 1216 | llvm::errs() << "<NULL DECL>\n"; |
| 1217 | break; |
| 1218 | } |
| 1219 | } |