blob: aeab506b4100a8b135c470dee6725dc000f3edfb [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 Friedman0fdcd1e2012-12-13 00:14:59 +0000309// Find the common parent for the beginning and end of the range.
310static void findCommonParent(SourceLocation &Begin, SourceLocation &End,
311 const SourceManager *SM) {
312 if (Begin.isInvalid() || End.isInvalid()) {
313 Begin = End = SourceLocation();
314 return;
315 }
316
317 FileID BeginFileID = SM->getFileID(Begin);
318 FileID EndFileID = SM->getFileID(End);
319
320 // First, crawl the expansion chain for the beginning of the range.
321 llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap;
322 BeginLocsMap[BeginFileID] = Begin;
323 while (Begin.isMacroID() && BeginFileID != EndFileID) {
324 if (SM->isMacroArgExpansion(Begin)) {
325 Begin = SM->getImmediateSpellingLoc(Begin);
326 if (Begin.isMacroID())
327 continue;
328 Begin = SourceLocation();
329 BeginFileID = FileID();
330 break;
331 }
332 Begin = SM->getImmediateExpansionRange(Begin).first;
333 BeginFileID = SM->getFileID(Begin);
334 BeginLocsMap[BeginFileID] = Begin;
335 }
336
337 // Then, crawl the expansion chain for the end of the range.
338 if (BeginFileID != EndFileID) {
339 while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) {
340 if (SM->isMacroArgExpansion(End)) {
341 End = SM->getImmediateSpellingLoc(End);
342 if (End.isMacroID())
343 continue;
344 End = SourceLocation();
345 EndFileID = FileID();
346 break;
347 }
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 assert(Begin.isValid() == End.isValid());
357}
358
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000359// Helper function to fix up source ranges. It takes in an array of ranges,
360// and outputs an array of ranges where we want to draw the range highlighting
361// around the location specified by CaretLoc.
362//
363// To find locations which correspond to the caret, we crawl the macro caller
364// chain for the beginning and end of each range. If the caret location
365// is in a macro expansion, we search each chain for a location
366// in the same expansion as the caret; otherwise, we crawl to the top of
367// each chain. Two locations are part of the same macro expansion
368// iff the FileID is the same.
369static void mapDiagnosticRanges(
370 SourceLocation CaretLoc,
Richard Smithac31c832012-12-05 06:20:58 +0000371 ArrayRef<CharSourceRange> Ranges,
Richard Smith98cffc62012-12-05 09:47:49 +0000372 SmallVectorImpl<CharSourceRange> &SpellingRanges,
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000373 const SourceManager *SM) {
374 FileID CaretLocFileID = SM->getFileID(CaretLoc);
375
Richard Smithac31c832012-12-05 06:20:58 +0000376 for (ArrayRef<CharSourceRange>::const_iterator I = Ranges.begin(),
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000377 E = Ranges.end();
378 I != E; ++I) {
379 SourceLocation Begin = I->getBegin(), End = I->getEnd();
380 bool IsTokenRange = I->isTokenRange();
381
Eli Friedman0fdcd1e2012-12-13 00:14:59 +0000382 // Compute the common parent; we can't highlight a range where
383 // the begin and end have different FileIDs.
384 findCommonParent(Begin, End, SM);
385
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000386 FileID BeginFileID = SM->getFileID(Begin);
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000387
388 while (Begin.isMacroID() && BeginFileID != CaretLocFileID) {
389 if (SM->isMacroArgExpansion(Begin)) {
Eli Friedman0fdcd1e2012-12-13 00:14:59 +0000390 // We have a macro argument; take the spelling loc, which is
391 // a step closer to where the argument was written.
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000392 Begin = SM->getImmediateSpellingLoc(Begin);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000393 End = SM->getImmediateSpellingLoc(End);
Eli Friedman0fdcd1e2012-12-13 00:14:59 +0000394 BeginFileID = SM->getFileID(Begin);
395 assert(BeginFileID == SM->getFileID(End));
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000396 } else {
Eli Friedman0fdcd1e2012-12-13 00:14:59 +0000397 // Take the next expansion in the expansion chain.
Eli Friedmanecdc8d32012-11-30 06:19:40 +0000398 Begin = SM->getImmediateExpansionRange(Begin).first;
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000399 End = SM->getImmediateExpansionRange(End).second;
Eli Friedman0fdcd1e2012-12-13 00:14:59 +0000400
401 // Compute the common parent again; the beginning and end might
402 // come out of different macro expansions.
403 findCommonParent(Begin, End, SM);
404 BeginFileID = SM->getFileID(Begin);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000405 }
Eli Friedman0fdcd1e2012-12-13 00:14:59 +0000406 }
407
408 // If this is the expansion of a macro argument, point the range at the
409 // use of the argument in the definition of the macro, not the expansion.
410 if (SM->isMacroArgExpansion(Begin)) {
411 assert(SM->isMacroArgExpansion(End));
412 Begin = End = SM->getImmediateExpansionRange(Begin).first;
413 IsTokenRange = true;
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000414 }
415
416 // Return the spelling location of the beginning and end of the range.
417 Begin = SM->getSpellingLoc(Begin);
418 End = SM->getSpellingLoc(End);
419 SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End),
420 IsTokenRange));
421 }
422}
423
Richard Smithac31c832012-12-05 06:20:58 +0000424void DiagnosticRenderer::emitCaret(SourceLocation Loc,
425 DiagnosticsEngine::Level Level,
426 ArrayRef<CharSourceRange> Ranges,
427 ArrayRef<FixItHint> Hints,
428 const SourceManager &SM) {
429 SmallVector<CharSourceRange, 4> SpellingRanges;
430 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
431 emitCodeContext(Loc, Level, SpellingRanges, Hints, SM);
432}
433
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000434/// \brief Recursively emit notes for each macro expansion and caret
435/// diagnostics where appropriate.
436///
437/// Walks up the macro expansion stack printing expansion notes, the code
438/// snippet, caret, underlines and FixItHint display as appropriate at each
439/// level.
440///
441/// \param Loc The location for this caret.
442/// \param Level The diagnostic level currently being emitted.
443/// \param Ranges The underlined ranges for this code snippet.
444/// \param Hints The FixIt hints active for this diagnostic.
445/// \param MacroSkipEnd The depth to stop skipping macro expansions.
446/// \param OnMacroInst The current depth of the macro expansion stack.
Richard Smithac31c832012-12-05 06:20:58 +0000447void DiagnosticRenderer::emitMacroExpansions(SourceLocation Loc,
448 DiagnosticsEngine::Level Level,
449 ArrayRef<CharSourceRange> Ranges,
450 ArrayRef<FixItHint> Hints,
451 const SourceManager &SM,
452 unsigned &MacroDepth,
453 unsigned OnMacroInst) {
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000454 assert(!Loc.isInvalid() && "must have a valid source location here");
Richard Smith67883922012-12-05 03:18:16 +0000455
Richard Smith67883922012-12-05 03:18:16 +0000456 // Walk up to the caller of this macro, and produce a backtrace down to there.
457 SourceLocation OneLevelUp = SM.getImmediateMacroCallerLoc(Loc);
Richard Smithac31c832012-12-05 06:20:58 +0000458 if (OneLevelUp.isMacroID())
459 emitMacroExpansions(OneLevelUp, Level, Ranges, Hints, SM,
460 MacroDepth, OnMacroInst + 1);
461 else
462 MacroDepth = OnMacroInst + 1;
Richard Smith67883922012-12-05 03:18:16 +0000463
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000464 unsigned MacroSkipStart = 0, MacroSkipEnd = 0;
Douglas Gregor02c23eb2012-10-23 22:26:28 +0000465 if (MacroDepth > DiagOpts->MacroBacktraceLimit &&
466 DiagOpts->MacroBacktraceLimit != 0) {
467 MacroSkipStart = DiagOpts->MacroBacktraceLimit / 2 +
468 DiagOpts->MacroBacktraceLimit % 2;
469 MacroSkipEnd = MacroDepth - DiagOpts->MacroBacktraceLimit / 2;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000470 }
Richard Smith67883922012-12-05 03:18:16 +0000471
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000472 // Whether to suppress printing this macro expansion.
473 bool Suppressed = (OnMacroInst >= MacroSkipStart &&
474 OnMacroInst < MacroSkipEnd);
Richard Smith67883922012-12-05 03:18:16 +0000475
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000476 if (Suppressed) {
477 // Tell the user that we've skipped contexts.
478 if (OnMacroInst == MacroSkipStart) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000479 SmallString<200> MessageStorage;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000480 llvm::raw_svector_ostream Message(MessageStorage);
481 Message << "(skipping " << (MacroSkipEnd - MacroSkipStart)
482 << " expansions in backtrace; use -fmacro-backtrace-limit=0 to "
483 "see all)";
484 emitBasicNote(Message.str());
485 }
486 return;
487 }
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000488
Richard Smith67883922012-12-05 03:18:16 +0000489 // Find the spelling location for the macro definition. We must use the
490 // spelling location here to avoid emitting a macro bactrace for the note.
491 SourceLocation SpellingLoc = Loc;
492 // If this is the expansion of a macro argument, point the caret at the
493 // use of the argument in the definition of the macro, not the expansion.
494 if (SM.isMacroArgExpansion(Loc))
495 SpellingLoc = SM.getImmediateExpansionRange(Loc).first;
496 SpellingLoc = SM.getSpellingLoc(SpellingLoc);
497
498 // Map the ranges into the FileID of the diagnostic location.
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000499 SmallVector<CharSourceRange, 4> SpellingRanges;
Richard Smith67883922012-12-05 03:18:16 +0000500 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000501
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000502 SmallString<100> MessageStorage;
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000503 llvm::raw_svector_ostream Message(MessageStorage);
Richard Smith5b10af72012-12-05 11:04:55 +0000504 StringRef MacroName = getImmediateMacroName(Loc, SM, LangOpts);
505 if (MacroName.empty())
506 Message << "expanded from here";
507 else
508 Message << "expanded from macro '" << MacroName << "'";
Richard Smith67883922012-12-05 03:18:16 +0000509 emitDiagnostic(SpellingLoc, DiagnosticsEngine::Note,
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000510 Message.str(),
Eli Friedman9cb1c3d2012-11-03 03:36:51 +0000511 SpellingRanges, ArrayRef<FixItHint>(), &SM);
Ted Kremenek2898d4f2011-12-17 05:26:04 +0000512}
513
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000514DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {}
515
516void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc,
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000517 PresumedLoc PLoc,
518 const SourceManager &SM) {
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000519 // Generate a note indicating the include location.
520 SmallString<200> MessageStorage;
521 llvm::raw_svector_ostream Message(MessageStorage);
522 Message << "in file included from " << PLoc.getFilename() << ':'
523 << PLoc.getLine() << ":";
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000524 emitNote(Loc, Message.str(), &SM);
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000525}
526
Douglas Gregor6c325432012-11-30 21:58:49 +0000527void DiagnosticNoteRenderer::emitImportLocation(SourceLocation Loc,
528 PresumedLoc PLoc,
529 StringRef ModuleName,
530 const SourceManager &SM) {
531 // Generate a note indicating the include location.
532 SmallString<200> MessageStorage;
533 llvm::raw_svector_ostream Message(MessageStorage);
534 Message << "in module '" << ModuleName << "' imported from "
535 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
536 emitNote(Loc, Message.str(), &SM);
537}
538
Douglas Gregor830ea5b2012-11-30 18:38:50 +0000539void
540DiagnosticNoteRenderer::emitBuildingModuleLocation(SourceLocation Loc,
541 PresumedLoc PLoc,
542 StringRef ModuleName,
543 const SourceManager &SM) {
544 // Generate a note indicating the include location.
545 SmallString<200> MessageStorage;
546 llvm::raw_svector_ostream Message(MessageStorage);
547 Message << "while building module '" << ModuleName << "' imported from "
548 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
549 emitNote(Loc, Message.str(), &SM);
550}
551
552
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000553void DiagnosticNoteRenderer::emitBasicNote(StringRef Message) {
Argyrios Kyrtzidis16afdf72012-05-10 05:03:45 +0000554 emitNote(SourceLocation(), Message, 0);
Ted Kremenek8be51ea2012-02-14 02:46:00 +0000555}