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