blob: 4eee59548c82130262d86fd28885047264c16a4e [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
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000142 SmallVector<FixItHint, 8> MergedFixits;
Ted Kremenek30660a82012-03-06 20:06:33 +0000143 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
Ted Kremenek6ee225c2012-12-19 01:16:49 +0000156 // Find the ultimate expansion location for the diagnostic.
157 Loc = SM->getFileLoc(Loc);
Richard Smith98cffc62012-12-05 09:47:49 +0000158
159 PresumedLoc PLoc = SM->getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
160
161 // First, if this diagnostic is not in the main file, print out the
162 // "included from" lines.
163 emitIncludeStack(Loc, PLoc, Level, *SM);
164
165 // Next, emit the actual diagnostic message and caret.
166 emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, SM, D);
167 emitCaret(Loc, Level, MutableRanges, FixItHints, *SM);
168
169 // If this location is within a macro, walk from UnexpandedLoc up to Loc
170 // and produce a macro backtrace.
171 if (UnexpandedLoc.isValid() && UnexpandedLoc.isMacroID()) {
Richard Smithac31c832012-12-05 06:20:58 +0000172 unsigned MacroDepth = 0;
Richard Smith98cffc62012-12-05 09:47:49 +0000173 emitMacroExpansions(UnexpandedLoc, Level, MutableRanges, FixItHints, *SM,
Richard Smithac31c832012-12-05 06:20:58 +0000174 MacroDepth);
175 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000176 }
Richard Smith98cffc62012-12-05 09:47:49 +0000177
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000178 LastLoc = Loc;
179 LastLevel = Level;
Richard Smith98cffc62012-12-05 09:47:49 +0000180
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000181 endDiagnostic(D, Level);
182}
183
184
185void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) {
186 emitDiagnostic(Diag.getLocation(), Diag.getLevel(), Diag.getMessage(),
187 Diag.getRanges(), Diag.getFixIts(),
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000188 Diag.getLocation().isValid() ? &Diag.getLocation().getManager()
189 : 0,
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000190 &Diag);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000191}
192
193/// \brief Prints an include stack when appropriate for a particular
194/// diagnostic level and location.
195///
196/// This routine handles all the logic of suppressing particular include
197/// stacks (such as those for notes) and duplicate include stacks when
198/// repeated warnings occur within the same file. It also handles the logic
199/// of customizing the formatting and display of the include stack.
200///
Douglas Gregor6c325432012-11-30 21:58:49 +0000201/// \param Loc The diagnostic location.
202/// \param PLoc The presumed location of the diagnostic location.
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000203/// \param Level The diagnostic level of the message this stack pertains to.
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000204void DiagnosticRenderer::emitIncludeStack(SourceLocation Loc,
Douglas Gregor6c325432012-11-30 21:58:49 +0000205 PresumedLoc PLoc,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000206 DiagnosticsEngine::Level Level,
207 const SourceManager &SM) {
Douglas Gregor6c325432012-11-30 21:58:49 +0000208 SourceLocation IncludeLoc = PLoc.getIncludeLoc();
209
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000210 // Skip redundant include stacks altogether.
Douglas Gregor6c325432012-11-30 21:58:49 +0000211 if (LastIncludeLoc == IncludeLoc)
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000212 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000213
214 LastIncludeLoc = IncludeLoc;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000215
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000216 if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000217 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000218
219 if (IncludeLoc.isValid())
220 emitIncludeStackRecursively(IncludeLoc, SM);
221 else {
Douglas Gregor4565e482012-11-30 22:11:57 +0000222 emitModuleBuildStack(SM);
Douglas Gregor6c325432012-11-30 21:58:49 +0000223 emitImportStack(Loc, SM);
224 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000225}
226
227/// \brief Helper to recursivly walk up the include stack and print each layer
228/// on the way back down.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000229void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc,
230 const SourceManager &SM) {
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000231 if (Loc.isInvalid()) {
Douglas Gregor4565e482012-11-30 22:11:57 +0000232 emitModuleBuildStack(SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000233 return;
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000234 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000235
Richard Smith62221b12012-11-14 23:55:25 +0000236 PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000237 if (PLoc.isInvalid())
238 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000239
240 // If this source location was imported from a module, print the module
241 // import stack rather than the
242 // FIXME: We want submodule granularity here.
243 std::pair<SourceLocation, StringRef> Imported = SM.getModuleImportLoc(Loc);
244 if (Imported.first.isValid()) {
245 // This location was imported by a module. Emit the module import stack.
246 emitImportStackRecursively(Imported.first, Imported.second, SM);
247 return;
248 }
249
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000250 // Emit the other include frames first.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000251 emitIncludeStackRecursively(PLoc.getIncludeLoc(), SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000252
253 // Emit the inclusion text/note.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000254 emitIncludeLocation(Loc, PLoc, SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000255}
256
Douglas Gregor6c325432012-11-30 21:58:49 +0000257/// \brief Emit the module import stack associated with the current location.
258void DiagnosticRenderer::emitImportStack(SourceLocation Loc,
259 const SourceManager &SM) {
260 if (Loc.isInvalid()) {
Douglas Gregor4565e482012-11-30 22:11:57 +0000261 emitModuleBuildStack(SM);
Douglas Gregor6c325432012-11-30 21:58:49 +0000262 return;
263 }
264
265 std::pair<SourceLocation, StringRef> NextImportLoc
266 = SM.getModuleImportLoc(Loc);
267 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
268}
269
270/// \brief Helper to recursivly walk up the import stack and print each layer
271/// on the way back down.
272void DiagnosticRenderer::emitImportStackRecursively(SourceLocation Loc,
273 StringRef ModuleName,
274 const SourceManager &SM) {
275 if (Loc.isInvalid()) {
276 return;
277 }
278
279 PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
280 if (PLoc.isInvalid())
281 return;
282
283 // Emit the other import frames first.
284 std::pair<SourceLocation, StringRef> NextImportLoc
285 = SM.getModuleImportLoc(Loc);
286 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
287
288 // Emit the inclusion text/note.
289 emitImportLocation(Loc, PLoc, ModuleName, SM);
290}
291
Douglas Gregor4565e482012-11-30 22:11:57 +0000292/// \brief Emit the module build stack, for cases where a module is (re-)built
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000293/// on demand.
Douglas Gregor4565e482012-11-30 22:11:57 +0000294void DiagnosticRenderer::emitModuleBuildStack(const SourceManager &SM) {
295 ModuleBuildStack Stack = SM.getModuleBuildStack();
296 for (unsigned I = 0, N = Stack.size(); I != N; ++I) {
297 const SourceManager &CurSM = Stack[I].second.getManager();
298 SourceLocation CurLoc = Stack[I].second;
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000299 emitBuildingModuleLocation(CurLoc,
300 CurSM.getPresumedLoc(CurLoc,
301 DiagOpts->ShowPresumedLoc),
Douglas Gregor4565e482012-11-30 22:11:57 +0000302 Stack[I].first,
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000303 CurSM);
304 }
305}
306
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000307// Helper function to fix up source ranges. It takes in an array of ranges,
308// and outputs an array of ranges where we want to draw the range highlighting
309// around the location specified by CaretLoc.
310//
311// To find locations which correspond to the caret, we crawl the macro caller
312// chain for the beginning and end of each range. If the caret location
313// is in a macro expansion, we search each chain for a location
314// in the same expansion as the caret; otherwise, we crawl to the top of
315// each chain. Two locations are part of the same macro expansion
316// iff the FileID is the same.
317static void mapDiagnosticRanges(
318 SourceLocation CaretLoc,
Richard Smithac31c832012-12-05 06:20:58 +0000319 ArrayRef<CharSourceRange> Ranges,
Richard Smith98cffc62012-12-05 09:47:49 +0000320 SmallVectorImpl<CharSourceRange> &SpellingRanges,
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000321 const SourceManager *SM) {
322 FileID CaretLocFileID = SM->getFileID(CaretLoc);
323
Richard Smithac31c832012-12-05 06:20:58 +0000324 for (ArrayRef<CharSourceRange>::const_iterator I = Ranges.begin(),
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000325 E = Ranges.end();
326 I != E; ++I) {
327 SourceLocation Begin = I->getBegin(), End = I->getEnd();
328 bool IsTokenRange = I->isTokenRange();
329
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000330 FileID BeginFileID = SM->getFileID(Begin);
Nadav Rotem5bdbc112012-12-13 19:58:10 +0000331 FileID EndFileID = SM->getFileID(End);
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000332
Nadav Rotem5bdbc112012-12-13 19:58:10 +0000333 // Find the common parent for the beginning and end of the range.
334
335 // First, crawl the expansion chain for the beginning of the range.
336 llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap;
337 while (Begin.isMacroID() && BeginFileID != EndFileID) {
338 BeginLocsMap[BeginFileID] = Begin;
339 Begin = SM->getImmediateExpansionRange(Begin).first;
340 BeginFileID = SM->getFileID(Begin);
341 }
342
343 // Then, crawl the expansion chain for the end of the range.
344 if (BeginFileID != EndFileID) {
345 while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) {
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000346 End = SM->getImmediateExpansionRange(End).second;
Nadav Rotem5bdbc112012-12-13 19:58:10 +0000347 EndFileID = SM->getFileID(End);
348 }
349 if (End.isMacroID()) {
350 Begin = BeginLocsMap[EndFileID];
351 BeginFileID = EndFileID;
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000352 }
Eli Friedman0fdcd1e2012-12-13 00:14:59 +0000353 }
354
Nadav Rotem5bdbc112012-12-13 19:58:10 +0000355 while (Begin.isMacroID() && BeginFileID != CaretLocFileID) {
356 if (SM->isMacroArgExpansion(Begin)) {
357 Begin = SM->getImmediateSpellingLoc(Begin);
358 End = SM->getImmediateSpellingLoc(End);
359 } else {
360 Begin = SM->getImmediateExpansionRange(Begin).first;
361 End = SM->getImmediateExpansionRange(End).second;
362 }
363 BeginFileID = SM->getFileID(Begin);
Eli Friedmanadff43a2012-12-18 00:52:36 +0000364 if (BeginFileID != SM->getFileID(End)) {
365 // FIXME: Ugly hack to stop a crash; this code is making bad
366 // assumptions and it's too complicated for me to reason
367 // about.
368 Begin = End = SourceLocation();
369 break;
370 }
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000371 }
372
373 // Return the spelling location of the beginning and end of the range.
374 Begin = SM->getSpellingLoc(Begin);
375 End = SM->getSpellingLoc(End);
376 SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End),
377 IsTokenRange));
378 }
379}
380
Richard Smithac31c832012-12-05 06:20:58 +0000381void DiagnosticRenderer::emitCaret(SourceLocation Loc,
382 DiagnosticsEngine::Level Level,
383 ArrayRef<CharSourceRange> Ranges,
384 ArrayRef<FixItHint> Hints,
385 const SourceManager &SM) {
386 SmallVector<CharSourceRange, 4> SpellingRanges;
387 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
388 emitCodeContext(Loc, Level, SpellingRanges, Hints, SM);
389}
390
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000391/// \brief Recursively emit notes for each macro expansion and caret
392/// diagnostics where appropriate.
393///
394/// Walks up the macro expansion stack printing expansion notes, the code
395/// snippet, caret, underlines and FixItHint display as appropriate at each
396/// level.
397///
398/// \param Loc The location for this caret.
399/// \param Level The diagnostic level currently being emitted.
400/// \param Ranges The underlined ranges for this code snippet.
401/// \param Hints The FixIt hints active for this diagnostic.
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000402/// \param OnMacroInst The current depth of the macro expansion stack.
Richard Smithac31c832012-12-05 06:20:58 +0000403void DiagnosticRenderer::emitMacroExpansions(SourceLocation Loc,
404 DiagnosticsEngine::Level Level,
405 ArrayRef<CharSourceRange> Ranges,
406 ArrayRef<FixItHint> Hints,
407 const SourceManager &SM,
408 unsigned &MacroDepth,
409 unsigned OnMacroInst) {
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000410 assert(!Loc.isInvalid() && "must have a valid source location here");
Richard Smith67883922012-12-05 03:18:16 +0000411
Richard Smith67883922012-12-05 03:18:16 +0000412 // Walk up to the caller of this macro, and produce a backtrace down to there.
413 SourceLocation OneLevelUp = SM.getImmediateMacroCallerLoc(Loc);
Richard Smithac31c832012-12-05 06:20:58 +0000414 if (OneLevelUp.isMacroID())
415 emitMacroExpansions(OneLevelUp, Level, Ranges, Hints, SM,
416 MacroDepth, OnMacroInst + 1);
417 else
418 MacroDepth = OnMacroInst + 1;
Richard Smith67883922012-12-05 03:18:16 +0000419
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000420 unsigned MacroSkipStart = 0, MacroSkipEnd = 0;
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000421 if (MacroDepth > DiagOpts->MacroBacktraceLimit &&
422 DiagOpts->MacroBacktraceLimit != 0) {
423 MacroSkipStart = DiagOpts->MacroBacktraceLimit / 2 +
424 DiagOpts->MacroBacktraceLimit % 2;
425 MacroSkipEnd = MacroDepth - DiagOpts->MacroBacktraceLimit / 2;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000426 }
Richard Smith67883922012-12-05 03:18:16 +0000427
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000428 // Whether to suppress printing this macro expansion.
429 bool Suppressed = (OnMacroInst >= MacroSkipStart &&
430 OnMacroInst < MacroSkipEnd);
Richard Smith67883922012-12-05 03:18:16 +0000431
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000432 if (Suppressed) {
433 // Tell the user that we've skipped contexts.
434 if (OnMacroInst == MacroSkipStart) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000435 SmallString<200> MessageStorage;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000436 llvm::raw_svector_ostream Message(MessageStorage);
437 Message << "(skipping " << (MacroSkipEnd - MacroSkipStart)
438 << " expansions in backtrace; use -fmacro-backtrace-limit=0 to "
439 "see all)";
440 emitBasicNote(Message.str());
441 }
442 return;
443 }
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000444
Richard Smith67883922012-12-05 03:18:16 +0000445 // Find the spelling location for the macro definition. We must use the
446 // spelling location here to avoid emitting a macro bactrace for the note.
447 SourceLocation SpellingLoc = Loc;
448 // If this is the expansion of a macro argument, point the caret at the
449 // use of the argument in the definition of the macro, not the expansion.
450 if (SM.isMacroArgExpansion(Loc))
451 SpellingLoc = SM.getImmediateExpansionRange(Loc).first;
452 SpellingLoc = SM.getSpellingLoc(SpellingLoc);
453
454 // Map the ranges into the FileID of the diagnostic location.
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000455 SmallVector<CharSourceRange, 4> SpellingRanges;
Richard Smith67883922012-12-05 03:18:16 +0000456 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000457
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000458 SmallString<100> MessageStorage;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000459 llvm::raw_svector_ostream Message(MessageStorage);
Richard Smith5b10af72012-12-05 11:04:55 +0000460 StringRef MacroName = getImmediateMacroName(Loc, SM, LangOpts);
461 if (MacroName.empty())
462 Message << "expanded from here";
463 else
464 Message << "expanded from macro '" << MacroName << "'";
Dmitri Gribenko55431692013-05-05 00:41:58 +0000465 emitDiagnostic(SpellingLoc, DiagnosticsEngine::Note, Message.str(),
466 SpellingRanges, None, &SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000467}
468
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000469DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {}
470
471void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000472 PresumedLoc PLoc,
473 const SourceManager &SM) {
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000474 // Generate a note indicating the include location.
475 SmallString<200> MessageStorage;
476 llvm::raw_svector_ostream Message(MessageStorage);
477 Message << "in file included from " << PLoc.getFilename() << ':'
478 << PLoc.getLine() << ":";
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000479 emitNote(Loc, Message.str(), &SM);
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000480}
481
Douglas Gregor6c325432012-11-30 21:58:49 +0000482void DiagnosticNoteRenderer::emitImportLocation(SourceLocation Loc,
483 PresumedLoc PLoc,
484 StringRef ModuleName,
485 const SourceManager &SM) {
486 // Generate a note indicating the include location.
487 SmallString<200> MessageStorage;
488 llvm::raw_svector_ostream Message(MessageStorage);
489 Message << "in module '" << ModuleName << "' imported from "
490 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
491 emitNote(Loc, Message.str(), &SM);
492}
493
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000494void
495DiagnosticNoteRenderer::emitBuildingModuleLocation(SourceLocation Loc,
496 PresumedLoc PLoc,
497 StringRef ModuleName,
498 const SourceManager &SM) {
499 // Generate a note indicating the include location.
500 SmallString<200> MessageStorage;
501 llvm::raw_svector_ostream Message(MessageStorage);
502 Message << "while building module '" << ModuleName << "' imported from "
503 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
504 emitNote(Loc, Message.str(), &SM);
505}
506
507
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000508void DiagnosticNoteRenderer::emitBasicNote(StringRef Message) {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000509 emitNote(SourceLocation(), Message, 0);
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000510}