Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 1 | //===--- DiagnosticRenderer.cpp - Diagnostic Pretty-Printing --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Frontend/DiagnosticRenderer.h" |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 11 | #include "clang/Basic/DiagnosticOptions.h" |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 12 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 13 | #include "clang/Edit/Commit.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Edit/EditedSource.h" |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 15 | #include "clang/Edit/EditsReceiver.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Lexer.h" |
Eli Friedman | 34ff0ea | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallSet.h" |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | using namespace clang; |
| 23 | |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 24 | DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts, |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 25 | DiagnosticOptions *DiagOpts) |
| 26 | : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {} |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 27 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 28 | DiagnosticRenderer::~DiagnosticRenderer() {} |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 29 | |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | class FixitReceiver : public edit::EditsReceiver { |
| 33 | SmallVectorImpl<FixItHint> &MergedFixits; |
| 34 | |
| 35 | public: |
| 36 | FixitReceiver(SmallVectorImpl<FixItHint> &MergedFixits) |
| 37 | : MergedFixits(MergedFixits) { } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 38 | void insert(SourceLocation loc, StringRef text) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 39 | MergedFixits.push_back(FixItHint::CreateInsertion(loc, text)); |
| 40 | } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 41 | void replace(CharSourceRange range, StringRef text) override { |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 42 | MergedFixits.push_back(FixItHint::CreateReplacement(range, text)); |
| 43 | } |
| 44 | }; |
| 45 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 46 | } |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 47 | |
| 48 | static void mergeFixits(ArrayRef<FixItHint> FixItHints, |
| 49 | const SourceManager &SM, const LangOptions &LangOpts, |
| 50 | SmallVectorImpl<FixItHint> &MergedFixits) { |
| 51 | edit::Commit commit(SM, LangOpts); |
| 52 | for (ArrayRef<FixItHint>::const_iterator |
| 53 | I = FixItHints.begin(), E = FixItHints.end(); I != E; ++I) { |
| 54 | const FixItHint &Hint = *I; |
| 55 | if (Hint.CodeToInsert.empty()) { |
| 56 | if (Hint.InsertFromRange.isValid()) |
| 57 | commit.insertFromRange(Hint.RemoveRange.getBegin(), |
| 58 | Hint.InsertFromRange, /*afterToken=*/false, |
| 59 | Hint.BeforePreviousInsertions); |
| 60 | else |
| 61 | commit.remove(Hint.RemoveRange); |
| 62 | } else { |
| 63 | if (Hint.RemoveRange.isTokenRange() || |
| 64 | Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd()) |
| 65 | commit.replace(Hint.RemoveRange, Hint.CodeToInsert); |
| 66 | else |
| 67 | commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert, |
| 68 | /*afterToken=*/false, Hint.BeforePreviousInsertions); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | edit::EditedSource Editor(SM, LangOpts); |
| 73 | if (Editor.commit(commit)) { |
| 74 | FixitReceiver Rec(MergedFixits); |
| 75 | Editor.applyRewrites(Rec); |
| 76 | } |
| 77 | } |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 78 | |
| 79 | void DiagnosticRenderer::emitDiagnostic(SourceLocation Loc, |
| 80 | DiagnosticsEngine::Level Level, |
| 81 | StringRef Message, |
| 82 | ArrayRef<CharSourceRange> Ranges, |
| 83 | ArrayRef<FixItHint> FixItHints, |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 84 | const SourceManager *SM, |
Ted Kremenek | 0964cca | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 85 | DiagOrStoredDiag D) { |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 86 | assert(SM || Loc.isInvalid()); |
Richard Smith | c01cca2 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | 0964cca | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 88 | beginDiagnostic(D, Level); |
Richard Smith | c01cca2 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 89 | |
| 90 | if (!Loc.isValid()) |
| 91 | // If we have no source location, just emit the diagnostic message. |
| 92 | emitDiagnosticMessage(Loc, PresumedLoc(), Level, Message, Ranges, SM, D); |
| 93 | else { |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 94 | // Get the ranges into a local array we can hack on. |
| 95 | SmallVector<CharSourceRange, 20> MutableRanges(Ranges.begin(), |
| 96 | Ranges.end()); |
Richard Smith | c01cca2 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 97 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 98 | SmallVector<FixItHint, 8> MergedFixits; |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 99 | if (!FixItHints.empty()) { |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 100 | mergeFixits(FixItHints, *SM, LangOpts, MergedFixits); |
Ted Kremenek | f7639e1 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 101 | FixItHints = MergedFixits; |
| 102 | } |
| 103 | |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 104 | for (ArrayRef<FixItHint>::const_iterator I = FixItHints.begin(), |
| 105 | E = FixItHints.end(); |
| 106 | I != E; ++I) |
| 107 | if (I->RemoveRange.isValid()) |
| 108 | MutableRanges.push_back(I->RemoveRange); |
Richard Smith | aebee68 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 109 | |
Richard Smith | c01cca2 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 110 | SourceLocation UnexpandedLoc = Loc; |
Richard Smith | aebee68 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | 372735f | 2012-12-19 01:16:49 +0000 | [diff] [blame] | 112 | // Find the ultimate expansion location for the diagnostic. |
| 113 | Loc = SM->getFileLoc(Loc); |
Richard Smith | c01cca2 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 114 | |
| 115 | PresumedLoc PLoc = SM->getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc); |
| 116 | |
| 117 | // First, if this diagnostic is not in the main file, print out the |
| 118 | // "included from" lines. |
| 119 | emitIncludeStack(Loc, PLoc, Level, *SM); |
| 120 | |
| 121 | // Next, emit the actual diagnostic message and caret. |
| 122 | emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, SM, D); |
| 123 | emitCaret(Loc, Level, MutableRanges, FixItHints, *SM); |
| 124 | |
| 125 | // If this location is within a macro, walk from UnexpandedLoc up to Loc |
| 126 | // and produce a macro backtrace. |
| 127 | if (UnexpandedLoc.isValid() && UnexpandedLoc.isMacroID()) { |
Richard Trieu | 97c45b6 | 2015-07-28 20:53:46 +0000 | [diff] [blame] | 128 | emitMacroExpansions(UnexpandedLoc, Level, MutableRanges, FixItHints, *SM); |
Richard Smith | aebee68 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 129 | } |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 130 | } |
Richard Smith | c01cca2 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 132 | LastLoc = Loc; |
| 133 | LastLevel = Level; |
Richard Smith | c01cca2 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 0964cca | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 135 | endDiagnostic(D, Level); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) { |
| 140 | emitDiagnostic(Diag.getLocation(), Diag.getLevel(), Diag.getMessage(), |
| 141 | Diag.getRanges(), Diag.getFixIts(), |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 142 | Diag.getLocation().isValid() ? &Diag.getLocation().getManager() |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 143 | : nullptr, |
Ted Kremenek | 0964cca | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 144 | &Diag); |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Alp Toker | 4db87ab | 2014-06-21 23:31:59 +0000 | [diff] [blame] | 147 | void DiagnosticRenderer::emitBasicNote(StringRef Message) { |
| 148 | emitDiagnosticMessage( |
| 149 | SourceLocation(), PresumedLoc(), DiagnosticsEngine::Note, Message, |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 150 | None, nullptr, DiagOrStoredDiag()); |
Alp Toker | 4db87ab | 2014-06-21 23:31:59 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 153 | /// \brief Prints an include stack when appropriate for a particular |
| 154 | /// diagnostic level and location. |
| 155 | /// |
| 156 | /// This routine handles all the logic of suppressing particular include |
| 157 | /// stacks (such as those for notes) and duplicate include stacks when |
| 158 | /// repeated warnings occur within the same file. It also handles the logic |
| 159 | /// of customizing the formatting and display of the include stack. |
| 160 | /// |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 161 | /// \param Loc The diagnostic location. |
| 162 | /// \param PLoc The presumed location of the diagnostic location. |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 163 | /// \param Level The diagnostic level of the message this stack pertains to. |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 164 | void DiagnosticRenderer::emitIncludeStack(SourceLocation Loc, |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 165 | PresumedLoc PLoc, |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 166 | DiagnosticsEngine::Level Level, |
| 167 | const SourceManager &SM) { |
Richard Smith | 41e6629 | 2016-04-28 18:26:32 +0000 | [diff] [blame] | 168 | SourceLocation IncludeLoc = |
| 169 | PLoc.isInvalid() ? SourceLocation() : PLoc.getIncludeLoc(); |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 170 | |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 171 | // Skip redundant include stacks altogether. |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 172 | if (LastIncludeLoc == IncludeLoc) |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 173 | return; |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 174 | |
| 175 | LastIncludeLoc = IncludeLoc; |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 177 | if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note) |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 178 | return; |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 179 | |
| 180 | if (IncludeLoc.isValid()) |
| 181 | emitIncludeStackRecursively(IncludeLoc, SM); |
| 182 | else { |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 183 | emitModuleBuildStack(SM); |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 184 | emitImportStack(Loc, SM); |
| 185 | } |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /// \brief Helper to recursivly walk up the include stack and print each layer |
| 189 | /// on the way back down. |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 190 | void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc, |
| 191 | const SourceManager &SM) { |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 192 | if (Loc.isInvalid()) { |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 193 | emitModuleBuildStack(SM); |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 194 | return; |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 195 | } |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 196 | |
Richard Smith | 0b50cb7 | 2012-11-14 23:55:25 +0000 | [diff] [blame] | 197 | PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc); |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 198 | if (PLoc.isInvalid()) |
| 199 | return; |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 200 | |
| 201 | // If this source location was imported from a module, print the module |
| 202 | // import stack rather than the |
| 203 | // FIXME: We want submodule granularity here. |
| 204 | std::pair<SourceLocation, StringRef> Imported = SM.getModuleImportLoc(Loc); |
Richard Smith | a24ff55 | 2015-08-11 00:05:21 +0000 | [diff] [blame] | 205 | if (!Imported.second.empty()) { |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 206 | // This location was imported by a module. Emit the module import stack. |
| 207 | emitImportStackRecursively(Imported.first, Imported.second, SM); |
| 208 | return; |
| 209 | } |
| 210 | |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 211 | // Emit the other include frames first. |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 212 | emitIncludeStackRecursively(PLoc.getIncludeLoc(), SM); |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 213 | |
| 214 | // Emit the inclusion text/note. |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 215 | emitIncludeLocation(Loc, PLoc, SM); |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 218 | /// \brief Emit the module import stack associated with the current location. |
| 219 | void DiagnosticRenderer::emitImportStack(SourceLocation Loc, |
| 220 | const SourceManager &SM) { |
| 221 | if (Loc.isInvalid()) { |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 222 | emitModuleBuildStack(SM); |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 223 | return; |
| 224 | } |
| 225 | |
| 226 | std::pair<SourceLocation, StringRef> NextImportLoc |
| 227 | = SM.getModuleImportLoc(Loc); |
| 228 | emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM); |
| 229 | } |
| 230 | |
| 231 | /// \brief Helper to recursivly walk up the import stack and print each layer |
| 232 | /// on the way back down. |
| 233 | void DiagnosticRenderer::emitImportStackRecursively(SourceLocation Loc, |
| 234 | StringRef ModuleName, |
| 235 | const SourceManager &SM) { |
Richard Smith | a24ff55 | 2015-08-11 00:05:21 +0000 | [diff] [blame] | 236 | if (ModuleName.empty()) { |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | |
| 240 | PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc); |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 241 | |
| 242 | // Emit the other import frames first. |
| 243 | std::pair<SourceLocation, StringRef> NextImportLoc |
| 244 | = SM.getModuleImportLoc(Loc); |
| 245 | emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM); |
| 246 | |
| 247 | // Emit the inclusion text/note. |
| 248 | emitImportLocation(Loc, PLoc, ModuleName, SM); |
| 249 | } |
| 250 | |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 251 | /// \brief Emit the module build stack, for cases where a module is (re-)built |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 252 | /// on demand. |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 253 | void DiagnosticRenderer::emitModuleBuildStack(const SourceManager &SM) { |
| 254 | ModuleBuildStack Stack = SM.getModuleBuildStack(); |
| 255 | for (unsigned I = 0, N = Stack.size(); I != N; ++I) { |
| 256 | const SourceManager &CurSM = Stack[I].second.getManager(); |
| 257 | SourceLocation CurLoc = Stack[I].second; |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 258 | emitBuildingModuleLocation(CurLoc, |
| 259 | CurSM.getPresumedLoc(CurLoc, |
| 260 | DiagOpts->ShowPresumedLoc), |
Douglas Gregor | 6336543 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 261 | Stack[I].first, |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 262 | CurSM); |
| 263 | } |
| 264 | } |
| 265 | |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 266 | /// A recursive function to trace all possible backtrace locations |
| 267 | /// to match the \p CaretLocFileID. |
Reid Kleckner | da30cff | 2015-12-08 01:08:09 +0000 | [diff] [blame] | 268 | static SourceLocation |
| 269 | retrieveMacroLocation(SourceLocation Loc, FileID MacroFileID, |
| 270 | FileID CaretFileID, |
| 271 | const SmallVectorImpl<FileID> &CommonArgExpansions, |
| 272 | bool IsBegin, const SourceManager *SM) { |
| 273 | assert(SM->getFileID(Loc) == MacroFileID); |
| 274 | if (MacroFileID == CaretFileID) |
| 275 | return Loc; |
| 276 | if (!Loc.isMacroID()) |
| 277 | return SourceLocation(); |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 278 | |
| 279 | SourceLocation MacroLocation, MacroArgLocation; |
| 280 | |
| 281 | if (SM->isMacroArgExpansion(Loc)) { |
Reid Kleckner | da30cff | 2015-12-08 01:08:09 +0000 | [diff] [blame] | 282 | // Only look at the immediate spelling location of this macro argument if |
| 283 | // the other location in the source range is also present in that expansion. |
| 284 | if (std::binary_search(CommonArgExpansions.begin(), |
| 285 | CommonArgExpansions.end(), MacroFileID)) |
| 286 | MacroLocation = SM->getImmediateSpellingLoc(Loc); |
| 287 | MacroArgLocation = IsBegin ? SM->getImmediateExpansionRange(Loc).first |
| 288 | : SM->getImmediateExpansionRange(Loc).second; |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 289 | } else { |
Reid Kleckner | da30cff | 2015-12-08 01:08:09 +0000 | [diff] [blame] | 290 | MacroLocation = IsBegin ? SM->getImmediateExpansionRange(Loc).first |
| 291 | : SM->getImmediateExpansionRange(Loc).second; |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 292 | MacroArgLocation = SM->getImmediateSpellingLoc(Loc); |
| 293 | } |
| 294 | |
Reid Kleckner | da30cff | 2015-12-08 01:08:09 +0000 | [diff] [blame] | 295 | if (MacroLocation.isValid()) { |
| 296 | MacroFileID = SM->getFileID(MacroLocation); |
| 297 | MacroLocation = |
| 298 | retrieveMacroLocation(MacroLocation, MacroFileID, CaretFileID, |
| 299 | CommonArgExpansions, IsBegin, SM); |
| 300 | if (MacroLocation.isValid()) |
| 301 | return MacroLocation; |
| 302 | } |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 303 | |
| 304 | MacroFileID = SM->getFileID(MacroArgLocation); |
| 305 | return retrieveMacroLocation(MacroArgLocation, MacroFileID, CaretFileID, |
Reid Kleckner | da30cff | 2015-12-08 01:08:09 +0000 | [diff] [blame] | 306 | CommonArgExpansions, IsBegin, SM); |
| 307 | } |
| 308 | |
| 309 | /// Walk up the chain of macro expansions and collect the FileIDs identifying the |
| 310 | /// expansions. |
| 311 | static void getMacroArgExpansionFileIDs(SourceLocation Loc, |
| 312 | SmallVectorImpl<FileID> &IDs, |
| 313 | bool IsBegin, const SourceManager *SM) { |
| 314 | while (Loc.isMacroID()) { |
| 315 | if (SM->isMacroArgExpansion(Loc)) { |
| 316 | IDs.push_back(SM->getFileID(Loc)); |
| 317 | Loc = SM->getImmediateSpellingLoc(Loc); |
| 318 | } else { |
| 319 | auto ExpRange = SM->getImmediateExpansionRange(Loc); |
| 320 | Loc = IsBegin ? ExpRange.first : ExpRange.second; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /// Collect the expansions of the begin and end locations and compute the set |
| 326 | /// intersection. Produces a sorted vector of FileIDs in CommonArgExpansions. |
| 327 | static void computeCommonMacroArgExpansionFileIDs( |
| 328 | SourceLocation Begin, SourceLocation End, const SourceManager *SM, |
| 329 | SmallVectorImpl<FileID> &CommonArgExpansions) { |
| 330 | SmallVector<FileID, 4> BeginArgExpansions; |
| 331 | SmallVector<FileID, 4> EndArgExpansions; |
| 332 | getMacroArgExpansionFileIDs(Begin, BeginArgExpansions, /*IsBegin=*/true, SM); |
| 333 | getMacroArgExpansionFileIDs(End, EndArgExpansions, /*IsBegin=*/false, SM); |
| 334 | std::sort(BeginArgExpansions.begin(), BeginArgExpansions.end()); |
| 335 | std::sort(EndArgExpansions.begin(), EndArgExpansions.end()); |
| 336 | std::set_intersection(BeginArgExpansions.begin(), BeginArgExpansions.end(), |
| 337 | EndArgExpansions.begin(), EndArgExpansions.end(), |
| 338 | std::back_inserter(CommonArgExpansions)); |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Eli Friedman | 34ff0ea | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 341 | // Helper function to fix up source ranges. It takes in an array of ranges, |
| 342 | // and outputs an array of ranges where we want to draw the range highlighting |
| 343 | // around the location specified by CaretLoc. |
| 344 | // |
| 345 | // To find locations which correspond to the caret, we crawl the macro caller |
| 346 | // chain for the beginning and end of each range. If the caret location |
| 347 | // is in a macro expansion, we search each chain for a location |
| 348 | // in the same expansion as the caret; otherwise, we crawl to the top of |
| 349 | // each chain. Two locations are part of the same macro expansion |
| 350 | // iff the FileID is the same. |
| 351 | static void mapDiagnosticRanges( |
| 352 | SourceLocation CaretLoc, |
Richard Smith | aebee68 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 353 | ArrayRef<CharSourceRange> Ranges, |
Richard Smith | c01cca2 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 354 | SmallVectorImpl<CharSourceRange> &SpellingRanges, |
Eli Friedman | 34ff0ea | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 355 | const SourceManager *SM) { |
| 356 | FileID CaretLocFileID = SM->getFileID(CaretLoc); |
| 357 | |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 358 | for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { |
| 359 | if (I->isInvalid()) continue; |
| 360 | |
Eli Friedman | 34ff0ea | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 361 | SourceLocation Begin = I->getBegin(), End = I->getEnd(); |
| 362 | bool IsTokenRange = I->isTokenRange(); |
| 363 | |
Eli Friedman | dea98de | 2012-11-30 06:19:40 +0000 | [diff] [blame] | 364 | FileID BeginFileID = SM->getFileID(Begin); |
Nadav Rotem | b893734 | 2012-12-13 19:58:10 +0000 | [diff] [blame] | 365 | FileID EndFileID = SM->getFileID(End); |
Eli Friedman | dea98de | 2012-11-30 06:19:40 +0000 | [diff] [blame] | 366 | |
Nadav Rotem | b893734 | 2012-12-13 19:58:10 +0000 | [diff] [blame] | 367 | // Find the common parent for the beginning and end of the range. |
| 368 | |
| 369 | // First, crawl the expansion chain for the beginning of the range. |
| 370 | llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap; |
| 371 | while (Begin.isMacroID() && BeginFileID != EndFileID) { |
| 372 | BeginLocsMap[BeginFileID] = Begin; |
| 373 | Begin = SM->getImmediateExpansionRange(Begin).first; |
| 374 | BeginFileID = SM->getFileID(Begin); |
| 375 | } |
| 376 | |
| 377 | // Then, crawl the expansion chain for the end of the range. |
| 378 | if (BeginFileID != EndFileID) { |
| 379 | while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) { |
Eli Friedman | 34ff0ea | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 380 | End = SM->getImmediateExpansionRange(End).second; |
Nadav Rotem | b893734 | 2012-12-13 19:58:10 +0000 | [diff] [blame] | 381 | EndFileID = SM->getFileID(End); |
| 382 | } |
| 383 | if (End.isMacroID()) { |
| 384 | Begin = BeginLocsMap[EndFileID]; |
| 385 | BeginFileID = EndFileID; |
Eli Friedman | 34ff0ea | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 386 | } |
Eli Friedman | cdb135a | 2012-12-13 00:14:59 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 389 | // Do the backtracking. |
Reid Kleckner | da30cff | 2015-12-08 01:08:09 +0000 | [diff] [blame] | 390 | SmallVector<FileID, 4> CommonArgExpansions; |
| 391 | computeCommonMacroArgExpansionFileIDs(Begin, End, SM, CommonArgExpansions); |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 392 | Begin = retrieveMacroLocation(Begin, BeginFileID, CaretLocFileID, |
Reid Kleckner | da30cff | 2015-12-08 01:08:09 +0000 | [diff] [blame] | 393 | CommonArgExpansions, /*IsBegin=*/true, SM); |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 394 | End = retrieveMacroLocation(End, BeginFileID, CaretLocFileID, |
Reid Kleckner | da30cff | 2015-12-08 01:08:09 +0000 | [diff] [blame] | 395 | CommonArgExpansions, /*IsBegin=*/false, SM); |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 396 | if (Begin.isInvalid() || End.isInvalid()) continue; |
Eli Friedman | 34ff0ea | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 397 | |
| 398 | // Return the spelling location of the beginning and end of the range. |
| 399 | Begin = SM->getSpellingLoc(Begin); |
| 400 | End = SM->getSpellingLoc(End); |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 401 | |
Eli Friedman | 34ff0ea | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 402 | SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End), |
| 403 | IsTokenRange)); |
| 404 | } |
| 405 | } |
| 406 | |
Richard Smith | aebee68 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 407 | void DiagnosticRenderer::emitCaret(SourceLocation Loc, |
| 408 | DiagnosticsEngine::Level Level, |
| 409 | ArrayRef<CharSourceRange> Ranges, |
| 410 | ArrayRef<FixItHint> Hints, |
| 411 | const SourceManager &SM) { |
| 412 | SmallVector<CharSourceRange, 4> SpellingRanges; |
| 413 | mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM); |
| 414 | emitCodeContext(Loc, Level, SpellingRanges, Hints, SM); |
| 415 | } |
| 416 | |
Richard Trieu | 97c45b6 | 2015-07-28 20:53:46 +0000 | [diff] [blame] | 417 | /// \brief A helper function for emitMacroExpansion to print the |
| 418 | /// macro expansion message |
| 419 | void DiagnosticRenderer::emitSingleMacroExpansion( |
| 420 | SourceLocation Loc, |
| 421 | DiagnosticsEngine::Level Level, |
| 422 | ArrayRef<CharSourceRange> Ranges, |
| 423 | const SourceManager &SM) { |
Richard Smith | 7a2d40d | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 424 | // Find the spelling location for the macro definition. We must use the |
Richard Trieu | 97c45b6 | 2015-07-28 20:53:46 +0000 | [diff] [blame] | 425 | // spelling location here to avoid emitting a macro backtrace for the note. |
Richard Trieu | a1d7ece | 2015-08-27 23:38:45 +0000 | [diff] [blame] | 426 | SourceLocation SpellingLoc = SM.getSpellingLoc(Loc); |
Richard Smith | 7a2d40d | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 427 | |
| 428 | // Map the ranges into the FileID of the diagnostic location. |
Eli Friedman | 34ff0ea | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 429 | SmallVector<CharSourceRange, 4> SpellingRanges; |
Richard Smith | 7a2d40d | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 430 | mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM); |
Eli Friedman | 34ff0ea | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 431 | |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 432 | SmallString<100> MessageStorage; |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 433 | llvm::raw_svector_ostream Message(MessageStorage); |
Richard Trieu | 3a5c958 | 2016-01-26 02:51:55 +0000 | [diff] [blame] | 434 | StringRef MacroName = |
| 435 | Lexer::getImmediateMacroNameForDiagnostics(Loc, SM, LangOpts); |
Richard Smith | f89e2e2 | 2012-12-05 11:04:55 +0000 | [diff] [blame] | 436 | if (MacroName.empty()) |
| 437 | Message << "expanded from here"; |
| 438 | else |
| 439 | Message << "expanded from macro '" << MacroName << "'"; |
Richard Trieu | 97c45b6 | 2015-07-28 20:53:46 +0000 | [diff] [blame] | 440 | |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 441 | emitDiagnostic(SpellingLoc, DiagnosticsEngine::Note, Message.str(), |
| 442 | SpellingRanges, None, &SM); |
Ted Kremenek | c4bbd85 | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 445 | /// Check that the macro argument location of Loc starts with ArgumentLoc. |
| 446 | /// The starting location of the macro expansions is used to differeniate |
| 447 | /// different macro expansions. |
| 448 | static bool checkLocForMacroArgExpansion(SourceLocation Loc, |
| 449 | const SourceManager &SM, |
| 450 | SourceLocation ArgumentLoc) { |
| 451 | SourceLocation MacroLoc; |
| 452 | if (SM.isMacroArgExpansion(Loc, &MacroLoc)) { |
| 453 | if (ArgumentLoc == MacroLoc) return true; |
| 454 | } |
| 455 | |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | /// Check if all the locations in the range have the same macro argument |
| 460 | /// expansion, and that that expansion starts with ArgumentLoc. |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 461 | static bool checkRangeForMacroArgExpansion(CharSourceRange Range, |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 462 | const SourceManager &SM, |
| 463 | SourceLocation ArgumentLoc) { |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 464 | SourceLocation BegLoc = Range.getBegin(), EndLoc = Range.getEnd(); |
| 465 | while (BegLoc != EndLoc) { |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 466 | if (!checkLocForMacroArgExpansion(BegLoc, SM, ArgumentLoc)) |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 467 | return false; |
| 468 | BegLoc.getLocWithOffset(1); |
| 469 | } |
| 470 | |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 471 | return checkLocForMacroArgExpansion(BegLoc, SM, ArgumentLoc); |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 474 | /// A helper function to check if the current ranges are all inside the same |
| 475 | /// macro argument expansion as Loc. |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 476 | static bool checkRangesForMacroArgExpansion(SourceLocation Loc, |
| 477 | ArrayRef<CharSourceRange> Ranges, |
| 478 | const SourceManager &SM) { |
| 479 | assert(Loc.isMacroID() && "Must be a macro expansion!"); |
| 480 | |
| 481 | SmallVector<CharSourceRange, 4> SpellingRanges; |
| 482 | mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM); |
| 483 | |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 484 | /// Count all valid ranges. |
| 485 | unsigned ValidCount = 0; |
| 486 | for (auto I : Ranges) |
| 487 | if (I.isValid()) ValidCount++; |
| 488 | |
| 489 | if (ValidCount > SpellingRanges.size()) |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 490 | return false; |
| 491 | |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 492 | /// To store the source location of the argument location. |
| 493 | SourceLocation ArgumentLoc; |
| 494 | |
| 495 | /// Set the ArgumentLoc to the beginning location of the expansion of Loc |
| 496 | /// so to check if the ranges expands to the same beginning location. |
| 497 | if (!SM.isMacroArgExpansion(Loc,&ArgumentLoc)) |
| 498 | return false; |
| 499 | |
| 500 | for (auto I = SpellingRanges.begin(), E = SpellingRanges.end(); I != E; ++I) { |
| 501 | if (!checkRangeForMacroArgExpansion(*I, SM, ArgumentLoc)) |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 502 | return false; |
Richard Trieu | c309624 | 2015-09-24 01:21:01 +0000 | [diff] [blame] | 503 | } |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 504 | |
| 505 | return true; |
| 506 | } |
| 507 | |
Richard Trieu | 97c45b6 | 2015-07-28 20:53:46 +0000 | [diff] [blame] | 508 | /// \brief Recursively emit notes for each macro expansion and caret |
| 509 | /// diagnostics where appropriate. |
| 510 | /// |
| 511 | /// Walks up the macro expansion stack printing expansion notes, the code |
| 512 | /// snippet, caret, underlines and FixItHint display as appropriate at each |
| 513 | /// level. |
| 514 | /// |
| 515 | /// \param Loc The location for this caret. |
| 516 | /// \param Level The diagnostic level currently being emitted. |
| 517 | /// \param Ranges The underlined ranges for this code snippet. |
| 518 | /// \param Hints The FixIt hints active for this diagnostic. |
| 519 | void DiagnosticRenderer::emitMacroExpansions(SourceLocation Loc, |
| 520 | DiagnosticsEngine::Level Level, |
| 521 | ArrayRef<CharSourceRange> Ranges, |
| 522 | ArrayRef<FixItHint> Hints, |
| 523 | const SourceManager &SM) { |
Yaron Keren | ed1fe5d | 2015-10-03 05:15:57 +0000 | [diff] [blame] | 524 | assert(Loc.isValid() && "must have a valid source location here"); |
Richard Trieu | 97c45b6 | 2015-07-28 20:53:46 +0000 | [diff] [blame] | 525 | |
| 526 | // Produce a stack of macro backtraces. |
| 527 | SmallVector<SourceLocation, 8> LocationStack; |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 528 | unsigned IgnoredEnd = 0; |
Richard Trieu | 97c45b6 | 2015-07-28 20:53:46 +0000 | [diff] [blame] | 529 | while (Loc.isMacroID()) { |
Richard Trieu | a1d7ece | 2015-08-27 23:38:45 +0000 | [diff] [blame] | 530 | // If this is the expansion of a macro argument, point the caret at the |
| 531 | // use of the argument in the definition of the macro, not the expansion. |
| 532 | if (SM.isMacroArgExpansion(Loc)) |
| 533 | LocationStack.push_back(SM.getImmediateExpansionRange(Loc).first); |
| 534 | else |
| 535 | LocationStack.push_back(Loc); |
| 536 | |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 537 | if (checkRangesForMacroArgExpansion(Loc, Ranges, SM)) |
| 538 | IgnoredEnd = LocationStack.size(); |
| 539 | |
Richard Trieu | 97c45b6 | 2015-07-28 20:53:46 +0000 | [diff] [blame] | 540 | Loc = SM.getImmediateMacroCallerLoc(Loc); |
Richard Trieu | a1d7ece | 2015-08-27 23:38:45 +0000 | [diff] [blame] | 541 | |
| 542 | // Once the location no longer points into a macro, try stepping through |
| 543 | // the last found location. This sometimes produces additional useful |
| 544 | // backtraces. |
| 545 | if (Loc.isFileID()) |
| 546 | Loc = SM.getImmediateMacroCallerLoc(LocationStack.back()); |
Yaron Keren | ed1fe5d | 2015-10-03 05:15:57 +0000 | [diff] [blame] | 547 | assert(Loc.isValid() && "must have a valid source location here"); |
Richard Trieu | 97c45b6 | 2015-07-28 20:53:46 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Richard Trieu | ecd36ee | 2015-08-12 18:24:59 +0000 | [diff] [blame] | 550 | LocationStack.erase(LocationStack.begin(), |
| 551 | LocationStack.begin() + IgnoredEnd); |
| 552 | |
Richard Trieu | 97c45b6 | 2015-07-28 20:53:46 +0000 | [diff] [blame] | 553 | unsigned MacroDepth = LocationStack.size(); |
| 554 | unsigned MacroLimit = DiagOpts->MacroBacktraceLimit; |
| 555 | if (MacroDepth <= MacroLimit || MacroLimit == 0) { |
| 556 | for (auto I = LocationStack.rbegin(), E = LocationStack.rend(); |
| 557 | I != E; ++I) |
| 558 | emitSingleMacroExpansion(*I, Level, Ranges, SM); |
| 559 | return; |
| 560 | } |
| 561 | |
| 562 | unsigned MacroStartMessages = MacroLimit / 2; |
| 563 | unsigned MacroEndMessages = MacroLimit / 2 + MacroLimit % 2; |
| 564 | |
| 565 | for (auto I = LocationStack.rbegin(), |
| 566 | E = LocationStack.rbegin() + MacroStartMessages; |
| 567 | I != E; ++I) |
| 568 | emitSingleMacroExpansion(*I, Level, Ranges, SM); |
| 569 | |
| 570 | SmallString<200> MessageStorage; |
| 571 | llvm::raw_svector_ostream Message(MessageStorage); |
| 572 | Message << "(skipping " << (MacroDepth - MacroLimit) |
| 573 | << " expansions in backtrace; use -fmacro-backtrace-limit=0 to " |
| 574 | "see all)"; |
| 575 | emitBasicNote(Message.str()); |
| 576 | |
| 577 | for (auto I = LocationStack.rend() - MacroEndMessages, |
| 578 | E = LocationStack.rend(); |
| 579 | I != E; ++I) |
| 580 | emitSingleMacroExpansion(*I, Level, Ranges, SM); |
| 581 | } |
| 582 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 583 | DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {} |
Ted Kremenek | 0964cca | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 584 | |
| 585 | void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc, |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 586 | PresumedLoc PLoc, |
| 587 | const SourceManager &SM) { |
Ted Kremenek | 0964cca | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 588 | // Generate a note indicating the include location. |
| 589 | SmallString<200> MessageStorage; |
| 590 | llvm::raw_svector_ostream Message(MessageStorage); |
| 591 | Message << "in file included from " << PLoc.getFilename() << ':' |
| 592 | << PLoc.getLine() << ":"; |
Argyrios Kyrtzidis | b16ff5d | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 593 | emitNote(Loc, Message.str(), &SM); |
Ted Kremenek | 0964cca | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 596 | void DiagnosticNoteRenderer::emitImportLocation(SourceLocation Loc, |
| 597 | PresumedLoc PLoc, |
| 598 | StringRef ModuleName, |
| 599 | const SourceManager &SM) { |
| 600 | // Generate a note indicating the include location. |
| 601 | SmallString<200> MessageStorage; |
| 602 | llvm::raw_svector_ostream Message(MessageStorage); |
Richard Smith | a24ff55 | 2015-08-11 00:05:21 +0000 | [diff] [blame] | 603 | Message << "in module '" << ModuleName; |
Yaron Keren | ed1fe5d | 2015-10-03 05:15:57 +0000 | [diff] [blame] | 604 | if (PLoc.isValid()) |
Richard Smith | a24ff55 | 2015-08-11 00:05:21 +0000 | [diff] [blame] | 605 | Message << "' imported from " << PLoc.getFilename() << ':' |
| 606 | << PLoc.getLine(); |
| 607 | Message << ":"; |
Douglas Gregor | 22103e3 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 608 | emitNote(Loc, Message.str(), &SM); |
| 609 | } |
| 610 | |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 611 | void |
| 612 | DiagnosticNoteRenderer::emitBuildingModuleLocation(SourceLocation Loc, |
| 613 | PresumedLoc PLoc, |
| 614 | StringRef ModuleName, |
| 615 | const SourceManager &SM) { |
| 616 | // Generate a note indicating the include location. |
| 617 | SmallString<200> MessageStorage; |
| 618 | llvm::raw_svector_ostream Message(MessageStorage); |
Jordan Rose | 602ac14 | 2016-06-28 01:02:31 +0000 | [diff] [blame] | 619 | if (PLoc.isValid()) |
Richard Smith | 7bea1d4 | 2014-03-05 20:55:36 +0000 | [diff] [blame] | 620 | Message << "while building module '" << ModuleName << "' imported from " |
| 621 | << PLoc.getFilename() << ':' << PLoc.getLine() << ":"; |
| 622 | else |
Jordan Rose | 6dcdaa6 | 2014-07-26 01:22:02 +0000 | [diff] [blame] | 623 | Message << "while building module '" << ModuleName << "':"; |
Douglas Gregor | af8f026 | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 624 | emitNote(Loc, Message.str(), &SM); |
| 625 | } |