blob: 89d3867aaddbd185ee0358dbb75fa38f3ad3d855 [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 Kremenek30660a82012-03-06 20:06:33 +000014#include "clang/Edit/Commit.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "clang/Edit/EditedSource.h"
Ted Kremenek30660a82012-03-06 20:06:33 +000016#include "clang/Edit/EditsReceiver.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Lex/Lexer.h"
Eli Friedman9cb1c3d2012-11-03 03:36:51 +000018#include "llvm/ADT/SmallSet.h"
Ted Kremenek2898d4f2011-12-17 05:26:04 +000019#include "llvm/ADT/SmallString.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000020#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/raw_ostream.h"
Ted Kremenek2898d4f2011-12-17 05:26:04 +000023#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
Richard Smith5b10af72012-12-05 11:04:55 +000050 // If the macro's spelling has no FileID, then it's actually a token paste
51 // or stringization (or similar) and not a macro at all.
52 if (!SM.getFileEntryForID(SM.getFileID(SM.getSpellingLoc(Loc))))
53 return StringRef();
54
Argyrios Kyrtzidis7f6cf972012-01-23 16:58:33 +000055 // Find the spelling location of the start of the non-argument expansion
56 // range. This is where the macro name was spelled in order to begin
57 // expanding this macro.
58 Loc = SM.getSpellingLoc(SM.getImmediateExpansionRange(Loc).first);
59
60 // Dig out the buffer where the macro name was spelled and the extents of the
61 // name so that we can render it into the expansion note.
62 std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc);
63 unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
64 StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first);
65 return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength);
66}
67
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +000068DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts,
Douglas Gregor02c23eb2012-10-23 22:26:28 +000069 DiagnosticOptions *DiagOpts)
70 : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {}
Ted Kremenek2898d4f2011-12-17 05:26:04 +000071
72DiagnosticRenderer::~DiagnosticRenderer() {}
73
Ted Kremenek30660a82012-03-06 20:06:33 +000074namespace {
75
76class FixitReceiver : public edit::EditsReceiver {
77 SmallVectorImpl<FixItHint> &MergedFixits;
78
79public:
80 FixitReceiver(SmallVectorImpl<FixItHint> &MergedFixits)
81 : MergedFixits(MergedFixits) { }
82 virtual void insert(SourceLocation loc, StringRef text) {
83 MergedFixits.push_back(FixItHint::CreateInsertion(loc, text));
84 }
85 virtual void replace(CharSourceRange range, StringRef text) {
86 MergedFixits.push_back(FixItHint::CreateReplacement(range, text));
87 }
88};
89
90}
91
92static void mergeFixits(ArrayRef<FixItHint> FixItHints,
93 const SourceManager &SM, const LangOptions &LangOpts,
94 SmallVectorImpl<FixItHint> &MergedFixits) {
95 edit::Commit commit(SM, LangOpts);
96 for (ArrayRef<FixItHint>::const_iterator
97 I = FixItHints.begin(), E = FixItHints.end(); I != E; ++I) {
98 const FixItHint &Hint = *I;
99 if (Hint.CodeToInsert.empty()) {
100 if (Hint.InsertFromRange.isValid())
101 commit.insertFromRange(Hint.RemoveRange.getBegin(),
102 Hint.InsertFromRange, /*afterToken=*/false,
103 Hint.BeforePreviousInsertions);
104 else
105 commit.remove(Hint.RemoveRange);
106 } else {
107 if (Hint.RemoveRange.isTokenRange() ||
108 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
109 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
110 else
111 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
112 /*afterToken=*/false, Hint.BeforePreviousInsertions);
113 }
114 }
115
116 edit::EditedSource Editor(SM, LangOpts);
117 if (Editor.commit(commit)) {
118 FixitReceiver Rec(MergedFixits);
119 Editor.applyRewrites(Rec);
120 }
121}
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000122
123void DiagnosticRenderer::emitDiagnostic(SourceLocation Loc,
124 DiagnosticsEngine::Level Level,
125 StringRef Message,
126 ArrayRef<CharSourceRange> Ranges,
127 ArrayRef<FixItHint> FixItHints,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000128 const SourceManager *SM,
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000129 DiagOrStoredDiag D) {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000130 assert(SM || Loc.isInvalid());
Richard Smith98cffc62012-12-05 09:47:49 +0000131
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000132 beginDiagnostic(D, Level);
Richard Smith98cffc62012-12-05 09:47:49 +0000133
134 if (!Loc.isValid())
135 // If we have no source location, just emit the diagnostic message.
136 emitDiagnosticMessage(Loc, PresumedLoc(), Level, Message, Ranges, SM, D);
137 else {
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000138 // Get the ranges into a local array we can hack on.
139 SmallVector<CharSourceRange, 20> MutableRanges(Ranges.begin(),
140 Ranges.end());
Richard Smith98cffc62012-12-05 09:47:49 +0000141
Ted Kremenek30660a82012-03-06 20:06:33 +0000142 llvm::SmallVector<FixItHint, 8> MergedFixits;
143 if (!FixItHints.empty()) {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000144 mergeFixits(FixItHints, *SM, LangOpts, MergedFixits);
Ted Kremenek30660a82012-03-06 20:06:33 +0000145 FixItHints = MergedFixits;
146 }
147
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000148 for (ArrayRef<FixItHint>::const_iterator I = FixItHints.begin(),
149 E = FixItHints.end();
150 I != E; ++I)
151 if (I->RemoveRange.isValid())
152 MutableRanges.push_back(I->RemoveRange);
Richard Smithac31c832012-12-05 06:20:58 +0000153
Richard Smith98cffc62012-12-05 09:47:49 +0000154 SourceLocation UnexpandedLoc = Loc;
Richard Smithac31c832012-12-05 06:20:58 +0000155
Richard Smith98cffc62012-12-05 09:47:49 +0000156 // Perform the same walk as emitMacroExpansions, to find the ultimate
157 // expansion location for the diagnostic.
158 while (Loc.isMacroID())
159 Loc = SM->getImmediateMacroCallerLoc(Loc);
160
161 PresumedLoc PLoc = SM->getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
162
163 // First, if this diagnostic is not in the main file, print out the
164 // "included from" lines.
165 emitIncludeStack(Loc, PLoc, Level, *SM);
166
167 // Next, emit the actual diagnostic message and caret.
168 emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, SM, D);
169 emitCaret(Loc, Level, MutableRanges, FixItHints, *SM);
170
171 // If this location is within a macro, walk from UnexpandedLoc up to Loc
172 // and produce a macro backtrace.
173 if (UnexpandedLoc.isValid() && UnexpandedLoc.isMacroID()) {
Richard Smithac31c832012-12-05 06:20:58 +0000174 unsigned MacroDepth = 0;
Richard Smith98cffc62012-12-05 09:47:49 +0000175 emitMacroExpansions(UnexpandedLoc, Level, MutableRanges, FixItHints, *SM,
Richard Smithac31c832012-12-05 06:20:58 +0000176 MacroDepth);
177 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000178 }
Richard Smith98cffc62012-12-05 09:47:49 +0000179
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000180 LastLoc = Loc;
181 LastLevel = Level;
Richard Smith98cffc62012-12-05 09:47:49 +0000182
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000183 endDiagnostic(D, Level);
184}
185
186
187void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) {
188 emitDiagnostic(Diag.getLocation(), Diag.getLevel(), Diag.getMessage(),
189 Diag.getRanges(), Diag.getFixIts(),
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000190 Diag.getLocation().isValid() ? &Diag.getLocation().getManager()
191 : 0,
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000192 &Diag);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000193}
194
195/// \brief Prints an include stack when appropriate for a particular
196/// diagnostic level and location.
197///
198/// This routine handles all the logic of suppressing particular include
199/// stacks (such as those for notes) and duplicate include stacks when
200/// repeated warnings occur within the same file. It also handles the logic
201/// of customizing the formatting and display of the include stack.
202///
Douglas Gregor6c325432012-11-30 21:58:49 +0000203/// \param Loc The diagnostic location.
204/// \param PLoc The presumed location of the diagnostic location.
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000205/// \param Level The diagnostic level of the message this stack pertains to.
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000206void DiagnosticRenderer::emitIncludeStack(SourceLocation Loc,
Douglas Gregor6c325432012-11-30 21:58:49 +0000207 PresumedLoc PLoc,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000208 DiagnosticsEngine::Level Level,
209 const SourceManager &SM) {
Douglas Gregor6c325432012-11-30 21:58:49 +0000210 SourceLocation IncludeLoc = PLoc.getIncludeLoc();
211
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000212 // Skip redundant include stacks altogether.
Douglas Gregor6c325432012-11-30 21:58:49 +0000213 if (LastIncludeLoc == IncludeLoc)
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000214 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000215
216 LastIncludeLoc = IncludeLoc;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000217
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000218 if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000219 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000220
221 if (IncludeLoc.isValid())
222 emitIncludeStackRecursively(IncludeLoc, SM);
223 else {
Douglas Gregor4565e482012-11-30 22:11:57 +0000224 emitModuleBuildStack(SM);
Douglas Gregor6c325432012-11-30 21:58:49 +0000225 emitImportStack(Loc, SM);
226 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000227}
228
229/// \brief Helper to recursivly walk up the include stack and print each layer
230/// on the way back down.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000231void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc,
232 const SourceManager &SM) {
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000233 if (Loc.isInvalid()) {
Douglas Gregor4565e482012-11-30 22:11:57 +0000234 emitModuleBuildStack(SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000235 return;
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000236 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000237
Richard Smith62221b12012-11-14 23:55:25 +0000238 PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000239 if (PLoc.isInvalid())
240 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000241
242 // If this source location was imported from a module, print the module
243 // import stack rather than the
244 // FIXME: We want submodule granularity here.
245 std::pair<SourceLocation, StringRef> Imported = SM.getModuleImportLoc(Loc);
246 if (Imported.first.isValid()) {
247 // This location was imported by a module. Emit the module import stack.
248 emitImportStackRecursively(Imported.first, Imported.second, SM);
249 return;
250 }
251
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000252 // Emit the other include frames first.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000253 emitIncludeStackRecursively(PLoc.getIncludeLoc(), SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000254
255 // Emit the inclusion text/note.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000256 emitIncludeLocation(Loc, PLoc, SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000257}
258
Douglas Gregor6c325432012-11-30 21:58:49 +0000259/// \brief Emit the module import stack associated with the current location.
260void DiagnosticRenderer::emitImportStack(SourceLocation Loc,
261 const SourceManager &SM) {
262 if (Loc.isInvalid()) {
Douglas Gregor4565e482012-11-30 22:11:57 +0000263 emitModuleBuildStack(SM);
Douglas Gregor6c325432012-11-30 21:58:49 +0000264 return;
265 }
266
267 std::pair<SourceLocation, StringRef> NextImportLoc
268 = SM.getModuleImportLoc(Loc);
269 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
270}
271
272/// \brief Helper to recursivly walk up the import stack and print each layer
273/// on the way back down.
274void DiagnosticRenderer::emitImportStackRecursively(SourceLocation Loc,
275 StringRef ModuleName,
276 const SourceManager &SM) {
277 if (Loc.isInvalid()) {
278 return;
279 }
280
281 PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
282 if (PLoc.isInvalid())
283 return;
284
285 // Emit the other import frames first.
286 std::pair<SourceLocation, StringRef> NextImportLoc
287 = SM.getModuleImportLoc(Loc);
288 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
289
290 // Emit the inclusion text/note.
291 emitImportLocation(Loc, PLoc, ModuleName, SM);
292}
293
Douglas Gregor4565e482012-11-30 22:11:57 +0000294/// \brief Emit the module build stack, for cases where a module is (re-)built
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000295/// on demand.
Douglas Gregor4565e482012-11-30 22:11:57 +0000296void DiagnosticRenderer::emitModuleBuildStack(const SourceManager &SM) {
297 ModuleBuildStack Stack = SM.getModuleBuildStack();
298 for (unsigned I = 0, N = Stack.size(); I != N; ++I) {
299 const SourceManager &CurSM = Stack[I].second.getManager();
300 SourceLocation CurLoc = Stack[I].second;
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000301 emitBuildingModuleLocation(CurLoc,
302 CurSM.getPresumedLoc(CurLoc,
303 DiagOpts->ShowPresumedLoc),
Douglas Gregor4565e482012-11-30 22:11:57 +0000304 Stack[I].first,
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000305 CurSM);
306 }
307}
308
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000309// Helper function to fix up source ranges. It takes in an array of ranges,
310// and outputs an array of ranges where we want to draw the range highlighting
311// around the location specified by CaretLoc.
312//
313// To find locations which correspond to the caret, we crawl the macro caller
314// chain for the beginning and end of each range. If the caret location
315// is in a macro expansion, we search each chain for a location
316// in the same expansion as the caret; otherwise, we crawl to the top of
317// each chain. Two locations are part of the same macro expansion
318// iff the FileID is the same.
319static void mapDiagnosticRanges(
320 SourceLocation CaretLoc,
Richard Smithac31c832012-12-05 06:20:58 +0000321 ArrayRef<CharSourceRange> Ranges,
Richard Smith98cffc62012-12-05 09:47:49 +0000322 SmallVectorImpl<CharSourceRange> &SpellingRanges,
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000323 const SourceManager *SM) {
324 FileID CaretLocFileID = SM->getFileID(CaretLoc);
325
Richard Smithac31c832012-12-05 06:20:58 +0000326 for (ArrayRef<CharSourceRange>::const_iterator I = Ranges.begin(),
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000327 E = Ranges.end();
328 I != E; ++I) {
329 SourceLocation Begin = I->getBegin(), End = I->getEnd();
330 bool IsTokenRange = I->isTokenRange();
331
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000332 FileID BeginFileID = SM->getFileID(Begin);
333 FileID EndFileID = SM->getFileID(End);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000334
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000335 // Find the common parent for the beginning and end of the range.
336
337 // First, crawl the expansion chain for the beginning of the range.
338 llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap;
339 while (Begin.isMacroID() && BeginFileID != EndFileID) {
340 BeginLocsMap[BeginFileID] = Begin;
341 Begin = SM->getImmediateExpansionRange(Begin).first;
342 BeginFileID = SM->getFileID(Begin);
343 }
344
345 // Then, crawl the expansion chain for the end of the range.
346 if (BeginFileID != EndFileID) {
347 while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) {
348 End = SM->getImmediateExpansionRange(End).second;
349 EndFileID = SM->getFileID(End);
350 }
351 if (End.isMacroID()) {
352 Begin = BeginLocsMap[EndFileID];
353 BeginFileID = EndFileID;
354 }
355 }
356
357 while (Begin.isMacroID() && BeginFileID != CaretLocFileID) {
358 if (SM->isMacroArgExpansion(Begin)) {
359 Begin = SM->getImmediateSpellingLoc(Begin);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000360 End = SM->getImmediateSpellingLoc(End);
361 } else {
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000362 Begin = SM->getImmediateExpansionRange(Begin).first;
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000363 End = SM->getImmediateExpansionRange(End).second;
364 }
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000365 BeginFileID = SM->getFileID(Begin);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000366 }
367
368 // Return the spelling location of the beginning and end of the range.
369 Begin = SM->getSpellingLoc(Begin);
370 End = SM->getSpellingLoc(End);
371 SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End),
372 IsTokenRange));
373 }
374}
375
Richard Smithac31c832012-12-05 06:20:58 +0000376void DiagnosticRenderer::emitCaret(SourceLocation Loc,
377 DiagnosticsEngine::Level Level,
378 ArrayRef<CharSourceRange> Ranges,
379 ArrayRef<FixItHint> Hints,
380 const SourceManager &SM) {
381 SmallVector<CharSourceRange, 4> SpellingRanges;
382 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
383 emitCodeContext(Loc, Level, SpellingRanges, Hints, SM);
384}
385
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000386/// \brief Recursively emit notes for each macro expansion and caret
387/// diagnostics where appropriate.
388///
389/// Walks up the macro expansion stack printing expansion notes, the code
390/// snippet, caret, underlines and FixItHint display as appropriate at each
391/// level.
392///
393/// \param Loc The location for this caret.
394/// \param Level The diagnostic level currently being emitted.
395/// \param Ranges The underlined ranges for this code snippet.
396/// \param Hints The FixIt hints active for this diagnostic.
397/// \param MacroSkipEnd The depth to stop skipping macro expansions.
398/// \param OnMacroInst The current depth of the macro expansion stack.
Richard Smithac31c832012-12-05 06:20:58 +0000399void DiagnosticRenderer::emitMacroExpansions(SourceLocation Loc,
400 DiagnosticsEngine::Level Level,
401 ArrayRef<CharSourceRange> Ranges,
402 ArrayRef<FixItHint> Hints,
403 const SourceManager &SM,
404 unsigned &MacroDepth,
405 unsigned OnMacroInst) {
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000406 assert(!Loc.isInvalid() && "must have a valid source location here");
Richard Smith67883922012-12-05 03:18:16 +0000407
Richard Smith67883922012-12-05 03:18:16 +0000408 // Walk up to the caller of this macro, and produce a backtrace down to there.
409 SourceLocation OneLevelUp = SM.getImmediateMacroCallerLoc(Loc);
Richard Smithac31c832012-12-05 06:20:58 +0000410 if (OneLevelUp.isMacroID())
411 emitMacroExpansions(OneLevelUp, Level, Ranges, Hints, SM,
412 MacroDepth, OnMacroInst + 1);
413 else
414 MacroDepth = OnMacroInst + 1;
Richard Smith67883922012-12-05 03:18:16 +0000415
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000416 unsigned MacroSkipStart = 0, MacroSkipEnd = 0;
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000417 if (MacroDepth > DiagOpts->MacroBacktraceLimit &&
418 DiagOpts->MacroBacktraceLimit != 0) {
419 MacroSkipStart = DiagOpts->MacroBacktraceLimit / 2 +
420 DiagOpts->MacroBacktraceLimit % 2;
421 MacroSkipEnd = MacroDepth - DiagOpts->MacroBacktraceLimit / 2;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000422 }
Richard Smith67883922012-12-05 03:18:16 +0000423
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000424 // Whether to suppress printing this macro expansion.
425 bool Suppressed = (OnMacroInst >= MacroSkipStart &&
426 OnMacroInst < MacroSkipEnd);
Richard Smith67883922012-12-05 03:18:16 +0000427
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000428 if (Suppressed) {
429 // Tell the user that we've skipped contexts.
430 if (OnMacroInst == MacroSkipStart) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000431 SmallString<200> MessageStorage;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000432 llvm::raw_svector_ostream Message(MessageStorage);
433 Message << "(skipping " << (MacroSkipEnd - MacroSkipStart)
434 << " expansions in backtrace; use -fmacro-backtrace-limit=0 to "
435 "see all)";
436 emitBasicNote(Message.str());
437 }
438 return;
439 }
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000440
Richard Smith67883922012-12-05 03:18:16 +0000441 // Find the spelling location for the macro definition. We must use the
442 // spelling location here to avoid emitting a macro bactrace for the note.
443 SourceLocation SpellingLoc = Loc;
444 // If this is the expansion of a macro argument, point the caret at the
445 // use of the argument in the definition of the macro, not the expansion.
446 if (SM.isMacroArgExpansion(Loc))
447 SpellingLoc = SM.getImmediateExpansionRange(Loc).first;
448 SpellingLoc = SM.getSpellingLoc(SpellingLoc);
449
450 // Map the ranges into the FileID of the diagnostic location.
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000451 SmallVector<CharSourceRange, 4> SpellingRanges;
Richard Smith67883922012-12-05 03:18:16 +0000452 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000453
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000454 SmallString<100> MessageStorage;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000455 llvm::raw_svector_ostream Message(MessageStorage);
Richard Smith5b10af72012-12-05 11:04:55 +0000456 StringRef MacroName = getImmediateMacroName(Loc, SM, LangOpts);
457 if (MacroName.empty())
458 Message << "expanded from here";
459 else
460 Message << "expanded from macro '" << MacroName << "'";
Richard Smith67883922012-12-05 03:18:16 +0000461 emitDiagnostic(SpellingLoc, DiagnosticsEngine::Note,
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000462 Message.str(),
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000463 SpellingRanges, ArrayRef<FixItHint>(), &SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000464}
465
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000466DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {}
467
468void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000469 PresumedLoc PLoc,
470 const SourceManager &SM) {
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000471 // Generate a note indicating the include location.
472 SmallString<200> MessageStorage;
473 llvm::raw_svector_ostream Message(MessageStorage);
474 Message << "in file included from " << PLoc.getFilename() << ':'
475 << PLoc.getLine() << ":";
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000476 emitNote(Loc, Message.str(), &SM);
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000477}
478
Douglas Gregor6c325432012-11-30 21:58:49 +0000479void DiagnosticNoteRenderer::emitImportLocation(SourceLocation Loc,
480 PresumedLoc PLoc,
481 StringRef ModuleName,
482 const SourceManager &SM) {
483 // Generate a note indicating the include location.
484 SmallString<200> MessageStorage;
485 llvm::raw_svector_ostream Message(MessageStorage);
486 Message << "in module '" << ModuleName << "' imported from "
487 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
488 emitNote(Loc, Message.str(), &SM);
489}
490
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000491void
492DiagnosticNoteRenderer::emitBuildingModuleLocation(SourceLocation Loc,
493 PresumedLoc PLoc,
494 StringRef ModuleName,
495 const SourceManager &SM) {
496 // Generate a note indicating the include location.
497 SmallString<200> MessageStorage;
498 llvm::raw_svector_ostream Message(MessageStorage);
499 Message << "while building module '" << ModuleName << "' imported from "
500 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
501 emitNote(Loc, Message.str(), &SM);
502}
503
504
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000505void DiagnosticNoteRenderer::emitBasicNote(StringRef Message) {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000506 emitNote(SourceLocation(), Message, 0);
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000507}