blob: 1bc940c94096ab7831afc5c9b7f14a5d428e953f [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
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());
Richard Smith98cffc62012-12-05 09:47:49 +0000126
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000127 beginDiagnostic(D, Level);
Richard Smith98cffc62012-12-05 09:47:49 +0000128
129 if (!Loc.isValid())
130 // If we have no source location, just emit the diagnostic message.
131 emitDiagnosticMessage(Loc, PresumedLoc(), Level, Message, Ranges, SM, D);
132 else {
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000133 // Get the ranges into a local array we can hack on.
134 SmallVector<CharSourceRange, 20> MutableRanges(Ranges.begin(),
135 Ranges.end());
Richard Smith98cffc62012-12-05 09:47:49 +0000136
Ted Kremenek30660a82012-03-06 20:06:33 +0000137 llvm::SmallVector<FixItHint, 8> MergedFixits;
138 if (!FixItHints.empty()) {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000139 mergeFixits(FixItHints, *SM, LangOpts, MergedFixits);
Ted Kremenek30660a82012-03-06 20:06:33 +0000140 FixItHints = MergedFixits;
141 }
142
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000143 for (ArrayRef<FixItHint>::const_iterator I = FixItHints.begin(),
144 E = FixItHints.end();
145 I != E; ++I)
146 if (I->RemoveRange.isValid())
147 MutableRanges.push_back(I->RemoveRange);
Richard Smithac31c832012-12-05 06:20:58 +0000148
Richard Smith98cffc62012-12-05 09:47:49 +0000149 SourceLocation UnexpandedLoc = Loc;
Richard Smithac31c832012-12-05 06:20:58 +0000150
Richard Smith98cffc62012-12-05 09:47:49 +0000151 // Perform the same walk as emitMacroExpansions, to find the ultimate
152 // expansion location for the diagnostic.
153 while (Loc.isMacroID())
154 Loc = SM->getImmediateMacroCallerLoc(Loc);
155
156 PresumedLoc PLoc = SM->getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
157
158 // First, if this diagnostic is not in the main file, print out the
159 // "included from" lines.
160 emitIncludeStack(Loc, PLoc, Level, *SM);
161
162 // Next, emit the actual diagnostic message and caret.
163 emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, SM, D);
164 emitCaret(Loc, Level, MutableRanges, FixItHints, *SM);
165
166 // If this location is within a macro, walk from UnexpandedLoc up to Loc
167 // and produce a macro backtrace.
168 if (UnexpandedLoc.isValid() && UnexpandedLoc.isMacroID()) {
Richard Smithac31c832012-12-05 06:20:58 +0000169 unsigned MacroDepth = 0;
Richard Smith98cffc62012-12-05 09:47:49 +0000170 emitMacroExpansions(UnexpandedLoc, Level, MutableRanges, FixItHints, *SM,
Richard Smithac31c832012-12-05 06:20:58 +0000171 MacroDepth);
172 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000173 }
Richard Smith98cffc62012-12-05 09:47:49 +0000174
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000175 LastLoc = Loc;
176 LastLevel = Level;
Richard Smith98cffc62012-12-05 09:47:49 +0000177
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000178 endDiagnostic(D, Level);
179}
180
181
182void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) {
183 emitDiagnostic(Diag.getLocation(), Diag.getLevel(), Diag.getMessage(),
184 Diag.getRanges(), Diag.getFixIts(),
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000185 Diag.getLocation().isValid() ? &Diag.getLocation().getManager()
186 : 0,
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000187 &Diag);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000188}
189
190/// \brief Prints an include stack when appropriate for a particular
191/// diagnostic level and location.
192///
193/// This routine handles all the logic of suppressing particular include
194/// stacks (such as those for notes) and duplicate include stacks when
195/// repeated warnings occur within the same file. It also handles the logic
196/// of customizing the formatting and display of the include stack.
197///
Douglas Gregor6c325432012-11-30 21:58:49 +0000198/// \param Loc The diagnostic location.
199/// \param PLoc The presumed location of the diagnostic location.
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000200/// \param Level The diagnostic level of the message this stack pertains to.
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000201void DiagnosticRenderer::emitIncludeStack(SourceLocation Loc,
Douglas Gregor6c325432012-11-30 21:58:49 +0000202 PresumedLoc PLoc,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000203 DiagnosticsEngine::Level Level,
204 const SourceManager &SM) {
Douglas Gregor6c325432012-11-30 21:58:49 +0000205 SourceLocation IncludeLoc = PLoc.getIncludeLoc();
206
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000207 // Skip redundant include stacks altogether.
Douglas Gregor6c325432012-11-30 21:58:49 +0000208 if (LastIncludeLoc == IncludeLoc)
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000209 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000210
211 LastIncludeLoc = IncludeLoc;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000212
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000213 if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000214 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000215
216 if (IncludeLoc.isValid())
217 emitIncludeStackRecursively(IncludeLoc, SM);
218 else {
Douglas Gregor4565e482012-11-30 22:11:57 +0000219 emitModuleBuildStack(SM);
Douglas Gregor6c325432012-11-30 21:58:49 +0000220 emitImportStack(Loc, SM);
221 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000222}
223
224/// \brief Helper to recursivly walk up the include stack and print each layer
225/// on the way back down.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000226void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc,
227 const SourceManager &SM) {
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000228 if (Loc.isInvalid()) {
Douglas Gregor4565e482012-11-30 22:11:57 +0000229 emitModuleBuildStack(SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000230 return;
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000231 }
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000232
Richard Smith62221b12012-11-14 23:55:25 +0000233 PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000234 if (PLoc.isInvalid())
235 return;
Douglas Gregor6c325432012-11-30 21:58:49 +0000236
237 // If this source location was imported from a module, print the module
238 // import stack rather than the
239 // FIXME: We want submodule granularity here.
240 std::pair<SourceLocation, StringRef> Imported = SM.getModuleImportLoc(Loc);
241 if (Imported.first.isValid()) {
242 // This location was imported by a module. Emit the module import stack.
243 emitImportStackRecursively(Imported.first, Imported.second, SM);
244 return;
245 }
246
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000247 // Emit the other include frames first.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000248 emitIncludeStackRecursively(PLoc.getIncludeLoc(), SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000249
250 // Emit the inclusion text/note.
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000251 emitIncludeLocation(Loc, PLoc, SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000252}
253
Douglas Gregor6c325432012-11-30 21:58:49 +0000254/// \brief Emit the module import stack associated with the current location.
255void DiagnosticRenderer::emitImportStack(SourceLocation Loc,
256 const SourceManager &SM) {
257 if (Loc.isInvalid()) {
Douglas Gregor4565e482012-11-30 22:11:57 +0000258 emitModuleBuildStack(SM);
Douglas Gregor6c325432012-11-30 21:58:49 +0000259 return;
260 }
261
262 std::pair<SourceLocation, StringRef> NextImportLoc
263 = SM.getModuleImportLoc(Loc);
264 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
265}
266
267/// \brief Helper to recursivly walk up the import stack and print each layer
268/// on the way back down.
269void DiagnosticRenderer::emitImportStackRecursively(SourceLocation Loc,
270 StringRef ModuleName,
271 const SourceManager &SM) {
272 if (Loc.isInvalid()) {
273 return;
274 }
275
276 PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
277 if (PLoc.isInvalid())
278 return;
279
280 // Emit the other import frames first.
281 std::pair<SourceLocation, StringRef> NextImportLoc
282 = SM.getModuleImportLoc(Loc);
283 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
284
285 // Emit the inclusion text/note.
286 emitImportLocation(Loc, PLoc, ModuleName, SM);
287}
288
Douglas Gregor4565e482012-11-30 22:11:57 +0000289/// \brief Emit the module build stack, for cases where a module is (re-)built
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000290/// on demand.
Douglas Gregor4565e482012-11-30 22:11:57 +0000291void DiagnosticRenderer::emitModuleBuildStack(const SourceManager &SM) {
292 ModuleBuildStack Stack = SM.getModuleBuildStack();
293 for (unsigned I = 0, N = Stack.size(); I != N; ++I) {
294 const SourceManager &CurSM = Stack[I].second.getManager();
295 SourceLocation CurLoc = Stack[I].second;
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000296 emitBuildingModuleLocation(CurLoc,
297 CurSM.getPresumedLoc(CurLoc,
298 DiagOpts->ShowPresumedLoc),
Douglas Gregor4565e482012-11-30 22:11:57 +0000299 Stack[I].first,
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000300 CurSM);
301 }
302}
303
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000304// Helper function to fix up source ranges. It takes in an array of ranges,
305// and outputs an array of ranges where we want to draw the range highlighting
306// around the location specified by CaretLoc.
307//
308// To find locations which correspond to the caret, we crawl the macro caller
309// chain for the beginning and end of each range. If the caret location
310// is in a macro expansion, we search each chain for a location
311// in the same expansion as the caret; otherwise, we crawl to the top of
312// each chain. Two locations are part of the same macro expansion
313// iff the FileID is the same.
314static void mapDiagnosticRanges(
315 SourceLocation CaretLoc,
Richard Smithac31c832012-12-05 06:20:58 +0000316 ArrayRef<CharSourceRange> Ranges,
Richard Smith98cffc62012-12-05 09:47:49 +0000317 SmallVectorImpl<CharSourceRange> &SpellingRanges,
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000318 const SourceManager *SM) {
319 FileID CaretLocFileID = SM->getFileID(CaretLoc);
320
Richard Smithac31c832012-12-05 06:20:58 +0000321 for (ArrayRef<CharSourceRange>::const_iterator I = Ranges.begin(),
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000322 E = Ranges.end();
323 I != E; ++I) {
324 SourceLocation Begin = I->getBegin(), End = I->getEnd();
325 bool IsTokenRange = I->isTokenRange();
326
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000327 FileID BeginFileID = SM->getFileID(Begin);
328 FileID EndFileID = SM->getFileID(End);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000329
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000330 // Find the common parent for the beginning and end of the range.
331
332 // First, crawl the expansion chain for the beginning of the range.
333 llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap;
334 while (Begin.isMacroID() && BeginFileID != EndFileID) {
335 BeginLocsMap[BeginFileID] = Begin;
336 Begin = SM->getImmediateExpansionRange(Begin).first;
337 BeginFileID = SM->getFileID(Begin);
338 }
339
340 // Then, crawl the expansion chain for the end of the range.
341 if (BeginFileID != EndFileID) {
342 while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) {
343 End = SM->getImmediateExpansionRange(End).second;
344 EndFileID = SM->getFileID(End);
345 }
346 if (End.isMacroID()) {
347 Begin = BeginLocsMap[EndFileID];
348 BeginFileID = EndFileID;
349 }
350 }
351
352 while (Begin.isMacroID() && BeginFileID != CaretLocFileID) {
353 if (SM->isMacroArgExpansion(Begin)) {
354 Begin = SM->getImmediateSpellingLoc(Begin);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000355 End = SM->getImmediateSpellingLoc(End);
356 } else {
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000357 Begin = SM->getImmediateExpansionRange(Begin).first;
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000358 End = SM->getImmediateExpansionRange(End).second;
359 }
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000360 BeginFileID = SM->getFileID(Begin);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000361 }
362
363 // Return the spelling location of the beginning and end of the range.
364 Begin = SM->getSpellingLoc(Begin);
365 End = SM->getSpellingLoc(End);
366 SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End),
367 IsTokenRange));
368 }
369}
370
Richard Smithac31c832012-12-05 06:20:58 +0000371void DiagnosticRenderer::emitCaret(SourceLocation Loc,
372 DiagnosticsEngine::Level Level,
373 ArrayRef<CharSourceRange> Ranges,
374 ArrayRef<FixItHint> Hints,
375 const SourceManager &SM) {
376 SmallVector<CharSourceRange, 4> SpellingRanges;
377 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
378 emitCodeContext(Loc, Level, SpellingRanges, Hints, SM);
379}
380
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000381/// \brief Recursively emit notes for each macro expansion and caret
382/// diagnostics where appropriate.
383///
384/// Walks up the macro expansion stack printing expansion notes, the code
385/// snippet, caret, underlines and FixItHint display as appropriate at each
386/// level.
387///
388/// \param Loc The location for this caret.
389/// \param Level The diagnostic level currently being emitted.
390/// \param Ranges The underlined ranges for this code snippet.
391/// \param Hints The FixIt hints active for this diagnostic.
392/// \param MacroSkipEnd The depth to stop skipping macro expansions.
393/// \param OnMacroInst The current depth of the macro expansion stack.
Richard Smithac31c832012-12-05 06:20:58 +0000394void DiagnosticRenderer::emitMacroExpansions(SourceLocation Loc,
395 DiagnosticsEngine::Level Level,
396 ArrayRef<CharSourceRange> Ranges,
397 ArrayRef<FixItHint> Hints,
398 const SourceManager &SM,
399 unsigned &MacroDepth,
400 unsigned OnMacroInst) {
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000401 assert(!Loc.isInvalid() && "must have a valid source location here");
Richard Smith67883922012-12-05 03:18:16 +0000402
Richard Smith67883922012-12-05 03:18:16 +0000403 // Walk up to the caller of this macro, and produce a backtrace down to there.
404 SourceLocation OneLevelUp = SM.getImmediateMacroCallerLoc(Loc);
Richard Smithac31c832012-12-05 06:20:58 +0000405 if (OneLevelUp.isMacroID())
406 emitMacroExpansions(OneLevelUp, Level, Ranges, Hints, SM,
407 MacroDepth, OnMacroInst + 1);
408 else
409 MacroDepth = OnMacroInst + 1;
Richard Smith67883922012-12-05 03:18:16 +0000410
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000411 unsigned MacroSkipStart = 0, MacroSkipEnd = 0;
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000412 if (MacroDepth > DiagOpts->MacroBacktraceLimit &&
413 DiagOpts->MacroBacktraceLimit != 0) {
414 MacroSkipStart = DiagOpts->MacroBacktraceLimit / 2 +
415 DiagOpts->MacroBacktraceLimit % 2;
416 MacroSkipEnd = MacroDepth - DiagOpts->MacroBacktraceLimit / 2;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000417 }
Richard Smith67883922012-12-05 03:18:16 +0000418
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000419 // Whether to suppress printing this macro expansion.
420 bool Suppressed = (OnMacroInst >= MacroSkipStart &&
421 OnMacroInst < MacroSkipEnd);
Richard Smith67883922012-12-05 03:18:16 +0000422
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000423 if (Suppressed) {
424 // Tell the user that we've skipped contexts.
425 if (OnMacroInst == MacroSkipStart) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000426 SmallString<200> MessageStorage;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000427 llvm::raw_svector_ostream Message(MessageStorage);
428 Message << "(skipping " << (MacroSkipEnd - MacroSkipStart)
429 << " expansions in backtrace; use -fmacro-backtrace-limit=0 to "
430 "see all)";
431 emitBasicNote(Message.str());
432 }
433 return;
434 }
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000435
Richard Smith67883922012-12-05 03:18:16 +0000436 // Find the spelling location for the macro definition. We must use the
437 // spelling location here to avoid emitting a macro bactrace for the note.
438 SourceLocation SpellingLoc = Loc;
439 // If this is the expansion of a macro argument, point the caret at the
440 // use of the argument in the definition of the macro, not the expansion.
441 if (SM.isMacroArgExpansion(Loc))
442 SpellingLoc = SM.getImmediateExpansionRange(Loc).first;
443 SpellingLoc = SM.getSpellingLoc(SpellingLoc);
444
445 // Map the ranges into the FileID of the diagnostic location.
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000446 SmallVector<CharSourceRange, 4> SpellingRanges;
Richard Smith67883922012-12-05 03:18:16 +0000447 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000448
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000449 SmallString<100> MessageStorage;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000450 llvm::raw_svector_ostream Message(MessageStorage);
451 Message << "expanded from macro '"
Richard Smith67883922012-12-05 03:18:16 +0000452 << getImmediateMacroName(Loc, SM, LangOpts) << "'";
453 emitDiagnostic(SpellingLoc, DiagnosticsEngine::Note,
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000454 Message.str(),
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000455 SpellingRanges, ArrayRef<FixItHint>(), &SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000456}
457
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000458DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {}
459
460void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000461 PresumedLoc PLoc,
462 const SourceManager &SM) {
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000463 // Generate a note indicating the include location.
464 SmallString<200> MessageStorage;
465 llvm::raw_svector_ostream Message(MessageStorage);
466 Message << "in file included from " << PLoc.getFilename() << ':'
467 << PLoc.getLine() << ":";
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000468 emitNote(Loc, Message.str(), &SM);
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000469}
470
Douglas Gregor6c325432012-11-30 21:58:49 +0000471void DiagnosticNoteRenderer::emitImportLocation(SourceLocation Loc,
472 PresumedLoc PLoc,
473 StringRef ModuleName,
474 const SourceManager &SM) {
475 // Generate a note indicating the include location.
476 SmallString<200> MessageStorage;
477 llvm::raw_svector_ostream Message(MessageStorage);
478 Message << "in module '" << ModuleName << "' imported from "
479 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
480 emitNote(Loc, Message.str(), &SM);
481}
482
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000483void
484DiagnosticNoteRenderer::emitBuildingModuleLocation(SourceLocation Loc,
485 PresumedLoc PLoc,
486 StringRef ModuleName,
487 const SourceManager &SM) {
488 // Generate a note indicating the include location.
489 SmallString<200> MessageStorage;
490 llvm::raw_svector_ostream Message(MessageStorage);
491 Message << "while building module '" << ModuleName << "' imported from "
492 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
493 emitNote(Loc, Message.str(), &SM);
494}
495
496
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000497void DiagnosticNoteRenderer::emitBasicNote(StringRef Message) {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000498 emitNote(SourceLocation(), Message, 0);
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000499}