blob: 4a332ae82facb624463dee1a48de0d6bb2a0ab53 [file] [log] [blame]
Ted Kremenek2898d4f2011-12-17 05:26:04 +00001//===--- 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 Gregor02c23eb2012-10-23 22:26:28 +000011#include "clang/Basic/DiagnosticOptions.h"
Ted Kremenek2898d4f2011-12-17 05:26:04 +000012#include "clang/Basic/FileManager.h"
13#include "clang/Basic/SourceManager.h"
Ted Kremenek2898d4f2011-12-17 05:26:04 +000014#include "clang/Lex/Lexer.h"
Ted Kremenek30660a82012-03-06 20:06:33 +000015#include "clang/Edit/EditedSource.h"
16#include "clang/Edit/Commit.h"
17#include "clang/Edit/EditsReceiver.h"
Ted Kremenek2898d4f2011-12-17 05:26:04 +000018#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/raw_ostream.h"
20#include "llvm/Support/ErrorHandling.h"
Eli Friedman9cb1c3d2012-11-03 03:36:51 +000021#include "llvm/ADT/SmallSet.h"
Ted Kremenek2898d4f2011-12-17 05:26:04 +000022#include "llvm/ADT/SmallString.h"
23#include <algorithm>
24using namespace clang;
25
Argyrios Kyrtzidis7f6cf972012-01-23 16:58:33 +000026/// \brief Retrieve the name of the immediate macro expansion.
27///
28/// This routine starts from a source location, and finds the name of the macro
29/// responsible for its immediate expansion. It looks through any intervening
30/// macro argument expansions to compute this. It returns a StringRef which
31/// refers to the SourceManager-owned buffer of the source where that macro
32/// name is spelled. Thus, the result shouldn't out-live that SourceManager.
33///
34/// This differs from Lexer::getImmediateMacroName in that any macro argument
35/// location will result in the topmost function macro that accepted it.
36/// e.g.
37/// \code
38/// MAC1( MAC2(foo) )
39/// \endcode
40/// for location of 'foo' token, this function will return "MAC1" while
41/// Lexer::getImmediateMacroName will return "MAC2".
42static StringRef getImmediateMacroName(SourceLocation Loc,
43 const SourceManager &SM,
44 const LangOptions &LangOpts) {
45 assert(Loc.isMacroID() && "Only reasonble to call this on macros");
46 // Walk past macro argument expanions.
47 while (SM.isMacroArgExpansion(Loc))
48 Loc = SM.getImmediateExpansionRange(Loc).first;
49
50 // Find the spelling location of the start of the non-argument expansion
51 // range. This is where the macro name was spelled in order to begin
52 // expanding this macro.
53 Loc = SM.getSpellingLoc(SM.getImmediateExpansionRange(Loc).first);
54
55 // Dig out the buffer where the macro name was spelled and the extents of the
56 // name so that we can render it into the expansion note.
57 std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc);
58 unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
59 StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first);
60 return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength);
61}
62
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +000063DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts,
Douglas Gregor02c23eb2012-10-23 22:26:28 +000064 DiagnosticOptions *DiagOpts)
65 : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {}
Ted Kremenek2898d4f2011-12-17 05:26:04 +000066
67DiagnosticRenderer::~DiagnosticRenderer() {}
68
Ted Kremenek30660a82012-03-06 20:06:33 +000069namespace {
70
71class FixitReceiver : public edit::EditsReceiver {
72 SmallVectorImpl<FixItHint> &MergedFixits;
73
74public:
75 FixitReceiver(SmallVectorImpl<FixItHint> &MergedFixits)
76 : MergedFixits(MergedFixits) { }
77 virtual void insert(SourceLocation loc, StringRef text) {
78 MergedFixits.push_back(FixItHint::CreateInsertion(loc, text));
79 }
80 virtual void replace(CharSourceRange range, StringRef text) {
81 MergedFixits.push_back(FixItHint::CreateReplacement(range, text));
82 }
83};
84
85}
86
87static void mergeFixits(ArrayRef<FixItHint> FixItHints,
88 const SourceManager &SM, const LangOptions &LangOpts,
89 SmallVectorImpl<FixItHint> &MergedFixits) {
90 edit::Commit commit(SM, LangOpts);
91 for (ArrayRef<FixItHint>::const_iterator
92 I = FixItHints.begin(), E = FixItHints.end(); I != E; ++I) {
93 const FixItHint &Hint = *I;
94 if (Hint.CodeToInsert.empty()) {
95 if (Hint.InsertFromRange.isValid())
96 commit.insertFromRange(Hint.RemoveRange.getBegin(),
97 Hint.InsertFromRange, /*afterToken=*/false,
98 Hint.BeforePreviousInsertions);
99 else
100 commit.remove(Hint.RemoveRange);
101 } else {
102 if (Hint.RemoveRange.isTokenRange() ||
103 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
104 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
105 else
106 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
107 /*afterToken=*/false, Hint.BeforePreviousInsertions);
108 }
109 }
110
111 edit::EditedSource Editor(SM, LangOpts);
112 if (Editor.commit(commit)) {
113 FixitReceiver Rec(MergedFixits);
114 Editor.applyRewrites(Rec);
115 }
116}
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000117
118void DiagnosticRenderer::emitDiagnostic(SourceLocation Loc,
119 DiagnosticsEngine::Level Level,
120 StringRef Message,
121 ArrayRef<CharSourceRange> Ranges,
122 ArrayRef<FixItHint> FixItHints,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000123 const SourceManager *SM,
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000124 DiagOrStoredDiag D) {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000125 assert(SM || Loc.isInvalid());
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000126
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000127 beginDiagnostic(D, Level);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000128
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000129 PresumedLoc PLoc;
130 if (Loc.isValid()) {
Richard Smith62221b12012-11-14 23:55:25 +0000131 PLoc = SM->getPresumedLocForDisplay(Loc, DiagOpts->ShowPresumedLoc);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000132
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000133 // First, if this diagnostic is not in the main file, print out the
134 // "included from" lines.
Douglas Gregor6c325432012-11-30 21:58:49 +0000135 emitIncludeStack(Loc, PLoc, Level, *SM);
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000136 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000137
138 // Next, emit the actual diagnostic message.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000139 emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, SM, D);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000140
141 // Only recurse if we have a valid location.
142 if (Loc.isValid()) {
143 // Get the ranges into a local array we can hack on.
144 SmallVector<CharSourceRange, 20> MutableRanges(Ranges.begin(),
145 Ranges.end());
146
Ted Kremenek30660a82012-03-06 20:06:33 +0000147 llvm::SmallVector<FixItHint, 8> MergedFixits;
148 if (!FixItHints.empty()) {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000149 mergeFixits(FixItHints, *SM, LangOpts, MergedFixits);
Ted Kremenek30660a82012-03-06 20:06:33 +0000150 FixItHints = MergedFixits;
151 }
152
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000153 for (ArrayRef<FixItHint>::const_iterator I = FixItHints.begin(),
154 E = FixItHints.end();
155 I != E; ++I)
156 if (I->RemoveRange.isValid())
157 MutableRanges.push_back(I->RemoveRange);
158
159 unsigned MacroDepth = 0;
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000160 emitMacroExpansionsAndCarets(Loc, Level, MutableRanges, FixItHints, *SM,
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000161 MacroDepth);
162 }
163
164 LastLoc = Loc;
165 LastLevel = Level;
166
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000167 endDiagnostic(D, Level);
168}
169
170
171void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) {
172 emitDiagnostic(Diag.getLocation(), Diag.getLevel(), Diag.getMessage(),
173 Diag.getRanges(), Diag.getFixIts(),
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000174 Diag.getLocation().isValid() ? &Diag.getLocation().getManager()
175 : 0,
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000176 &Diag);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000177}
178
179/// \brief Prints an include stack when appropriate for a particular
180/// diagnostic level and location.
181///
182/// This routine handles all the logic of suppressing particular include
183/// stacks (such as those for notes) and duplicate include stacks when
184/// repeated warnings occur within the same file. It also handles the logic
185/// of customizing the formatting and display of the include stack.
186///
Douglas Gregor6c325432012-11-30 21:58:49 +0000187/// \param Loc The diagnostic location.
188/// \param PLoc The presumed location of the diagnostic location.
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000189/// \param Level The diagnostic level of the message this stack pertains to.
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000190void DiagnosticRenderer::emitIncludeStack(SourceLocation Loc,
Douglas Gregor6c325432012-11-30 21:58:49 +0000191 PresumedLoc PLoc,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000192 DiagnosticsEngine::Level Level,
193 const SourceManager &SM) {
Douglas Gregor6c325432012-11-30 21:58:49 +0000194 SourceLocation IncludeLoc = PLoc.getIncludeLoc();
195
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000196 // Skip redundant include stacks altogether.
Douglas Gregor6c325432012-11-30 21:58:49 +0000197 if (LastIncludeLoc == IncludeLoc)
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000198 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000199
200 LastIncludeLoc = IncludeLoc;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000201
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000202 if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000203 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000204
205 if (IncludeLoc.isValid())
206 emitIncludeStackRecursively(IncludeLoc, SM);
207 else {
208 emitModuleBuildPath(SM);
209 emitImportStack(Loc, SM);
210 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000211}
212
213/// \brief Helper to recursivly walk up the include stack and print each layer
214/// on the way back down.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000215void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc,
216 const SourceManager &SM) {
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000217 if (Loc.isInvalid()) {
218 emitModuleBuildPath(SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000219 return;
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000220 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000221
Richard Smith62221b12012-11-14 23:55:25 +0000222 PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000223 if (PLoc.isInvalid())
224 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000225
226 // If this source location was imported from a module, print the module
227 // import stack rather than the
228 // FIXME: We want submodule granularity here.
229 std::pair<SourceLocation, StringRef> Imported = SM.getModuleImportLoc(Loc);
230 if (Imported.first.isValid()) {
231 // This location was imported by a module. Emit the module import stack.
232 emitImportStackRecursively(Imported.first, Imported.second, SM);
233 return;
234 }
235
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000236 // Emit the other include frames first.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000237 emitIncludeStackRecursively(PLoc.getIncludeLoc(), SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000238
239 // Emit the inclusion text/note.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000240 emitIncludeLocation(Loc, PLoc, SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000241}
242
Douglas Gregor6c325432012-11-30 21:58:49 +0000243/// \brief Emit the module import stack associated with the current location.
244void DiagnosticRenderer::emitImportStack(SourceLocation Loc,
245 const SourceManager &SM) {
246 if (Loc.isInvalid()) {
247 emitModuleBuildPath(SM);
248 return;
249 }
250
251 std::pair<SourceLocation, StringRef> NextImportLoc
252 = SM.getModuleImportLoc(Loc);
253 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
254}
255
256/// \brief Helper to recursivly walk up the import stack and print each layer
257/// on the way back down.
258void DiagnosticRenderer::emitImportStackRecursively(SourceLocation Loc,
259 StringRef ModuleName,
260 const SourceManager &SM) {
261 if (Loc.isInvalid()) {
262 return;
263 }
264
265 PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
266 if (PLoc.isInvalid())
267 return;
268
269 // Emit the other import frames first.
270 std::pair<SourceLocation, StringRef> NextImportLoc
271 = SM.getModuleImportLoc(Loc);
272 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
273
274 // Emit the inclusion text/note.
275 emitImportLocation(Loc, PLoc, ModuleName, SM);
276}
277
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000278/// \brief Emit the module build path, for cases where a module is (re-)built
279/// on demand.
280void DiagnosticRenderer::emitModuleBuildPath(const SourceManager &SM) {
281 ModuleBuildPath Path = SM.getModuleBuildPath();
282 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
283 const SourceManager &CurSM = Path[I].second.getManager();
284 SourceLocation CurLoc = Path[I].second;
285 emitBuildingModuleLocation(CurLoc,
286 CurSM.getPresumedLoc(CurLoc,
287 DiagOpts->ShowPresumedLoc),
288 Path[I].first,
289 CurSM);
290 }
291}
292
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000293// Helper function to fix up source ranges. It takes in an array of ranges,
294// and outputs an array of ranges where we want to draw the range highlighting
295// around the location specified by CaretLoc.
296//
297// To find locations which correspond to the caret, we crawl the macro caller
298// chain for the beginning and end of each range. If the caret location
299// is in a macro expansion, we search each chain for a location
300// in the same expansion as the caret; otherwise, we crawl to the top of
301// each chain. Two locations are part of the same macro expansion
302// iff the FileID is the same.
303static void mapDiagnosticRanges(
304 SourceLocation CaretLoc,
305 const SmallVectorImpl<CharSourceRange>& Ranges,
306 SmallVectorImpl<CharSourceRange>& SpellingRanges,
307 const SourceManager *SM) {
308 FileID CaretLocFileID = SM->getFileID(CaretLoc);
309
310 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
311 E = Ranges.end();
312 I != E; ++I) {
313 SourceLocation Begin = I->getBegin(), End = I->getEnd();
314 bool IsTokenRange = I->isTokenRange();
315
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000316 FileID BeginFileID = SM->getFileID(Begin);
317 FileID EndFileID = SM->getFileID(End);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000318
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000319 // Find the common parent for the beginning and end of the range.
320
321 // First, crawl the expansion chain for the beginning of the range.
322 llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap;
323 while (Begin.isMacroID() && BeginFileID != EndFileID) {
324 BeginLocsMap[BeginFileID] = Begin;
325 Begin = SM->getImmediateExpansionRange(Begin).first;
326 BeginFileID = SM->getFileID(Begin);
327 }
328
329 // Then, crawl the expansion chain for the end of the range.
330 if (BeginFileID != EndFileID) {
331 while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) {
332 End = SM->getImmediateExpansionRange(End).second;
333 EndFileID = SM->getFileID(End);
334 }
335 if (End.isMacroID()) {
336 Begin = BeginLocsMap[EndFileID];
337 BeginFileID = EndFileID;
338 }
339 }
340
341 while (Begin.isMacroID() && BeginFileID != CaretLocFileID) {
342 if (SM->isMacroArgExpansion(Begin)) {
343 Begin = SM->getImmediateSpellingLoc(Begin);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000344 End = SM->getImmediateSpellingLoc(End);
345 } else {
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000346 Begin = SM->getImmediateExpansionRange(Begin).first;
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000347 End = SM->getImmediateExpansionRange(End).second;
348 }
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000349 BeginFileID = SM->getFileID(Begin);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000350 }
351
352 // Return the spelling location of the beginning and end of the range.
353 Begin = SM->getSpellingLoc(Begin);
354 End = SM->getSpellingLoc(End);
355 SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End),
356 IsTokenRange));
357 }
358}
359
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000360/// \brief Recursively emit notes for each macro expansion and caret
361/// diagnostics where appropriate.
362///
363/// Walks up the macro expansion stack printing expansion notes, the code
364/// snippet, caret, underlines and FixItHint display as appropriate at each
365/// level.
366///
367/// \param Loc The location for this caret.
368/// \param Level The diagnostic level currently being emitted.
369/// \param Ranges The underlined ranges for this code snippet.
370/// \param Hints The FixIt hints active for this diagnostic.
371/// \param MacroSkipEnd The depth to stop skipping macro expansions.
372/// \param OnMacroInst The current depth of the macro expansion stack.
373void DiagnosticRenderer::emitMacroExpansionsAndCarets(
374 SourceLocation Loc,
375 DiagnosticsEngine::Level Level,
376 SmallVectorImpl<CharSourceRange>& Ranges,
377 ArrayRef<FixItHint> Hints,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000378 const SourceManager &SM,
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000379 unsigned &MacroDepth,
380 unsigned OnMacroInst)
381{
382 assert(!Loc.isInvalid() && "must have a valid source location here");
383
384 // If this is a file source location, directly emit the source snippet and
385 // caret line. Also record the macro depth reached.
386 if (Loc.isFileID()) {
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000387 // Map the ranges.
388 SmallVector<CharSourceRange, 4> SpellingRanges;
389 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
390
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000391 assert(MacroDepth == 0 && "We shouldn't hit a leaf node twice!");
392 MacroDepth = OnMacroInst;
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000393 emitCodeContext(Loc, Level, SpellingRanges, Hints, SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000394 return;
395 }
396 // Otherwise recurse through each macro expansion layer.
397
398 // When processing macros, skip over the expansions leading up to
399 // a macro argument, and trace the argument's expansion stack instead.
Matt Beaumont-Gay29271fb2012-06-18 20:12:05 +0000400 Loc = SM.skipToMacroArgExpansion(Loc);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000401
Matt Beaumont-Gay29271fb2012-06-18 20:12:05 +0000402 SourceLocation OneLevelUp = SM.getImmediateMacroCallerLoc(Loc);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000403
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000404 emitMacroExpansionsAndCarets(OneLevelUp, Level, Ranges, Hints, SM, MacroDepth,
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000405 OnMacroInst + 1);
406
407 // Save the original location so we can find the spelling of the macro call.
408 SourceLocation MacroLoc = Loc;
409
410 // Map the location.
Matt Beaumont-Gay29271fb2012-06-18 20:12:05 +0000411 Loc = SM.getImmediateMacroCalleeLoc(Loc);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000412
413 unsigned MacroSkipStart = 0, MacroSkipEnd = 0;
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000414 if (MacroDepth > DiagOpts->MacroBacktraceLimit &&
415 DiagOpts->MacroBacktraceLimit != 0) {
416 MacroSkipStart = DiagOpts->MacroBacktraceLimit / 2 +
417 DiagOpts->MacroBacktraceLimit % 2;
418 MacroSkipEnd = MacroDepth - DiagOpts->MacroBacktraceLimit / 2;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000419 }
420
421 // Whether to suppress printing this macro expansion.
422 bool Suppressed = (OnMacroInst >= MacroSkipStart &&
423 OnMacroInst < MacroSkipEnd);
424
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000425 if (Suppressed) {
426 // Tell the user that we've skipped contexts.
427 if (OnMacroInst == MacroSkipStart) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000428 SmallString<200> MessageStorage;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000429 llvm::raw_svector_ostream Message(MessageStorage);
430 Message << "(skipping " << (MacroSkipEnd - MacroSkipStart)
431 << " expansions in backtrace; use -fmacro-backtrace-limit=0 to "
432 "see all)";
433 emitBasicNote(Message.str());
434 }
435 return;
436 }
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000437
438 // Map the ranges.
439 SmallVector<CharSourceRange, 4> SpellingRanges;
440 mapDiagnosticRanges(MacroLoc, Ranges, SpellingRanges, &SM);
441
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000442 SmallString<100> MessageStorage;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000443 llvm::raw_svector_ostream Message(MessageStorage);
444 Message << "expanded from macro '"
Argyrios Kyrtzidis7f6cf972012-01-23 16:58:33 +0000445 << getImmediateMacroName(MacroLoc, SM, LangOpts) << "'";
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000446 emitDiagnostic(SM.getSpellingLoc(Loc), DiagnosticsEngine::Note,
447 Message.str(),
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000448 SpellingRanges, ArrayRef<FixItHint>(), &SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000449}
450
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000451DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {}
452
453void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000454 PresumedLoc PLoc,
455 const SourceManager &SM) {
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000456 // Generate a note indicating the include location.
457 SmallString<200> MessageStorage;
458 llvm::raw_svector_ostream Message(MessageStorage);
459 Message << "in file included from " << PLoc.getFilename() << ':'
460 << PLoc.getLine() << ":";
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000461 emitNote(Loc, Message.str(), &SM);
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000462}
463
Douglas Gregor6c325432012-11-30 21:58:49 +0000464void DiagnosticNoteRenderer::emitImportLocation(SourceLocation Loc,
465 PresumedLoc PLoc,
466 StringRef ModuleName,
467 const SourceManager &SM) {
468 // Generate a note indicating the include location.
469 SmallString<200> MessageStorage;
470 llvm::raw_svector_ostream Message(MessageStorage);
471 Message << "in module '" << ModuleName << "' imported from "
472 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
473 emitNote(Loc, Message.str(), &SM);
474}
475
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000476void
477DiagnosticNoteRenderer::emitBuildingModuleLocation(SourceLocation Loc,
478 PresumedLoc PLoc,
479 StringRef ModuleName,
480 const SourceManager &SM) {
481 // Generate a note indicating the include location.
482 SmallString<200> MessageStorage;
483 llvm::raw_svector_ostream Message(MessageStorage);
484 Message << "while building module '" << ModuleName << "' imported from "
485 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
486 emitNote(Loc, Message.str(), &SM);
487}
488
489
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000490void DiagnosticNoteRenderer::emitBasicNote(StringRef Message) {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000491 emitNote(SourceLocation(), Message, 0);
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000492}