1 //===--- DiagnosticRenderer.cpp - Diagnostic Pretty-Printing --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Frontend/DiagnosticRenderer.h"
11 #include "clang/Basic/DiagnosticOptions.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Basic/SourceManager.h"
14 #include "clang/Lex/Lexer.h"
15 #include "clang/Edit/EditedSource.h"
16 #include "clang/Edit/Commit.h"
17 #include "clang/Edit/EditsReceiver.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/ADT/SmallString.h"
24 using namespace clang;
26 /// \brief Retrieve the name of the immediate macro expansion.
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.
34 /// This differs from Lexer::getImmediateMacroName in that any macro argument
35 /// location will result in the topmost function macro that accepted it.
40 /// for location of 'foo' token, this function will return "MAC1" while
41 /// Lexer::getImmediateMacroName will return "MAC2".
42 static 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;
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);
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);
63 DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts,
64 DiagnosticOptions *DiagOpts)
65 : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {}
67 DiagnosticRenderer::~DiagnosticRenderer() {}
71 class FixitReceiver : public edit::EditsReceiver {
72 SmallVectorImpl<FixItHint> &MergedFixits;
75 FixitReceiver(SmallVectorImpl<FixItHint> &MergedFixits)
76 : MergedFixits(MergedFixits) { }
77 virtual void insert(SourceLocation loc, StringRef text) {
78 MergedFixits.push_back(FixItHint::CreateInsertion(loc, text));
80 virtual void replace(CharSourceRange range, StringRef text) {
81 MergedFixits.push_back(FixItHint::CreateReplacement(range, text));
87 static 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);
100 commit.remove(Hint.RemoveRange);
102 if (Hint.RemoveRange.isTokenRange() ||
103 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
104 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
106 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
107 /*afterToken=*/false, Hint.BeforePreviousInsertions);
111 edit::EditedSource Editor(SM, LangOpts);
112 if (Editor.commit(commit)) {
113 FixitReceiver Rec(MergedFixits);
114 Editor.applyRewrites(Rec);
118 void DiagnosticRenderer::emitDiagnostic(SourceLocation Loc,
119 DiagnosticsEngine::Level Level,
121 ArrayRef<CharSourceRange> Ranges,
122 ArrayRef<FixItHint> FixItHints,
123 const SourceManager *SM,
124 DiagOrStoredDiag D) {
125 assert(SM || Loc.isInvalid());
127 beginDiagnostic(D, Level);
131 PLoc = SM->getPresumedLocForDisplay(Loc);
133 // First, if this diagnostic is not in the main file, print out the
134 // "included from" lines.
135 emitIncludeStack(PLoc.getIncludeLoc(), Level, *SM);
138 // Next, emit the actual diagnostic message.
139 emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, SM, D);
141 // Only recurse if we have a valid location.
143 // Get the ranges into a local array we can hack on.
144 SmallVector<CharSourceRange, 20> MutableRanges(Ranges.begin(),
147 llvm::SmallVector<FixItHint, 8> MergedFixits;
148 if (!FixItHints.empty()) {
149 mergeFixits(FixItHints, *SM, LangOpts, MergedFixits);
150 FixItHints = MergedFixits;
153 for (ArrayRef<FixItHint>::const_iterator I = FixItHints.begin(),
154 E = FixItHints.end();
156 if (I->RemoveRange.isValid())
157 MutableRanges.push_back(I->RemoveRange);
159 unsigned MacroDepth = 0;
160 emitMacroExpansionsAndCarets(Loc, Level, MutableRanges, FixItHints, *SM,
167 endDiagnostic(D, Level);
171 void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) {
172 emitDiagnostic(Diag.getLocation(), Diag.getLevel(), Diag.getMessage(),
173 Diag.getRanges(), Diag.getFixIts(),
174 Diag.getLocation().isValid() ? &Diag.getLocation().getManager()
179 /// \brief Prints an include stack when appropriate for a particular
180 /// diagnostic level and location.
182 /// This routine handles all the logic of suppressing particular include
183 /// stacks (such as those for notes) and duplicate include stacks when
184 /// repeated warnings occur within the same file. It also handles the logic
185 /// of customizing the formatting and display of the include stack.
187 /// \param Level The diagnostic level of the message this stack pertains to.
188 /// \param Loc The include location of the current file (not the diagnostic
190 void DiagnosticRenderer::emitIncludeStack(SourceLocation Loc,
191 DiagnosticsEngine::Level Level,
192 const SourceManager &SM) {
193 // Skip redundant include stacks altogether.
194 if (LastIncludeLoc == Loc)
196 LastIncludeLoc = Loc;
198 if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
201 emitIncludeStackRecursively(Loc, SM);
204 /// \brief Helper to recursivly walk up the include stack and print each layer
205 /// on the way back down.
206 void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc,
207 const SourceManager &SM) {
211 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
212 if (PLoc.isInvalid())
215 // Emit the other include frames first.
216 emitIncludeStackRecursively(PLoc.getIncludeLoc(), SM);
218 // Emit the inclusion text/note.
219 emitIncludeLocation(Loc, PLoc, SM);
222 // Helper function to fix up source ranges. It takes in an array of ranges,
223 // and outputs an array of ranges where we want to draw the range highlighting
224 // around the location specified by CaretLoc.
226 // To find locations which correspond to the caret, we crawl the macro caller
227 // chain for the beginning and end of each range. If the caret location
228 // is in a macro expansion, we search each chain for a location
229 // in the same expansion as the caret; otherwise, we crawl to the top of
230 // each chain. Two locations are part of the same macro expansion
231 // iff the FileID is the same.
232 static void mapDiagnosticRanges(
233 SourceLocation CaretLoc,
234 const SmallVectorImpl<CharSourceRange>& Ranges,
235 SmallVectorImpl<CharSourceRange>& SpellingRanges,
236 const SourceManager *SM) {
237 FileID CaretLocFileID = SM->getFileID(CaretLoc);
239 for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(),
242 SourceLocation Begin = I->getBegin(), End = I->getEnd();
243 bool IsTokenRange = I->isTokenRange();
245 // Search the macro caller chain for the beginning of the range.
246 while (Begin.isMacroID() && SM->getFileID(Begin) != CaretLocFileID)
247 Begin = SM->getImmediateMacroCallerLoc(Begin);
249 // Search the macro caller chain for the beginning of the range.
250 while (End.isMacroID() && SM->getFileID(End) != CaretLocFileID) {
251 // The computation of the next End is an inlined version of
252 // getImmediateMacroCallerLoc, except it chooses the end of an
254 if (SM->isMacroArgExpansion(End)) {
255 End = SM->getImmediateSpellingLoc(End);
257 End = SM->getImmediateExpansionRange(End).second;
261 // Return the spelling location of the beginning and end of the range.
262 Begin = SM->getSpellingLoc(Begin);
263 End = SM->getSpellingLoc(End);
264 SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End),
269 /// \brief Recursively emit notes for each macro expansion and caret
270 /// diagnostics where appropriate.
272 /// Walks up the macro expansion stack printing expansion notes, the code
273 /// snippet, caret, underlines and FixItHint display as appropriate at each
276 /// \param Loc The location for this caret.
277 /// \param Level The diagnostic level currently being emitted.
278 /// \param Ranges The underlined ranges for this code snippet.
279 /// \param Hints The FixIt hints active for this diagnostic.
280 /// \param MacroSkipEnd The depth to stop skipping macro expansions.
281 /// \param OnMacroInst The current depth of the macro expansion stack.
282 void DiagnosticRenderer::emitMacroExpansionsAndCarets(
284 DiagnosticsEngine::Level Level,
285 SmallVectorImpl<CharSourceRange>& Ranges,
286 ArrayRef<FixItHint> Hints,
287 const SourceManager &SM,
288 unsigned &MacroDepth,
289 unsigned OnMacroInst)
291 assert(!Loc.isInvalid() && "must have a valid source location here");
293 // If this is a file source location, directly emit the source snippet and
294 // caret line. Also record the macro depth reached.
295 if (Loc.isFileID()) {
297 SmallVector<CharSourceRange, 4> SpellingRanges;
298 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
300 assert(MacroDepth == 0 && "We shouldn't hit a leaf node twice!");
301 MacroDepth = OnMacroInst;
302 emitCodeContext(Loc, Level, SpellingRanges, Hints, SM);
305 // Otherwise recurse through each macro expansion layer.
307 // When processing macros, skip over the expansions leading up to
308 // a macro argument, and trace the argument's expansion stack instead.
309 Loc = SM.skipToMacroArgExpansion(Loc);
311 SourceLocation OneLevelUp = SM.getImmediateMacroCallerLoc(Loc);
313 emitMacroExpansionsAndCarets(OneLevelUp, Level, Ranges, Hints, SM, MacroDepth,
316 // Save the original location so we can find the spelling of the macro call.
317 SourceLocation MacroLoc = Loc;
320 Loc = SM.getImmediateMacroCalleeLoc(Loc);
322 unsigned MacroSkipStart = 0, MacroSkipEnd = 0;
323 if (MacroDepth > DiagOpts->MacroBacktraceLimit &&
324 DiagOpts->MacroBacktraceLimit != 0) {
325 MacroSkipStart = DiagOpts->MacroBacktraceLimit / 2 +
326 DiagOpts->MacroBacktraceLimit % 2;
327 MacroSkipEnd = MacroDepth - DiagOpts->MacroBacktraceLimit / 2;
330 // Whether to suppress printing this macro expansion.
331 bool Suppressed = (OnMacroInst >= MacroSkipStart &&
332 OnMacroInst < MacroSkipEnd);
335 // Tell the user that we've skipped contexts.
336 if (OnMacroInst == MacroSkipStart) {
337 SmallString<200> MessageStorage;
338 llvm::raw_svector_ostream Message(MessageStorage);
339 Message << "(skipping " << (MacroSkipEnd - MacroSkipStart)
340 << " expansions in backtrace; use -fmacro-backtrace-limit=0 to "
342 emitBasicNote(Message.str());
348 SmallVector<CharSourceRange, 4> SpellingRanges;
349 mapDiagnosticRanges(MacroLoc, Ranges, SpellingRanges, &SM);
351 SmallString<100> MessageStorage;
352 llvm::raw_svector_ostream Message(MessageStorage);
353 Message << "expanded from macro '"
354 << getImmediateMacroName(MacroLoc, SM, LangOpts) << "'";
355 emitDiagnostic(SM.getSpellingLoc(Loc), DiagnosticsEngine::Note,
357 SpellingRanges, ArrayRef<FixItHint>(), &SM);
360 DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {}
362 void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc,
364 const SourceManager &SM) {
365 // Generate a note indicating the include location.
366 SmallString<200> MessageStorage;
367 llvm::raw_svector_ostream Message(MessageStorage);
368 Message << "in file included from " << PLoc.getFilename() << ':'
369 << PLoc.getLine() << ":";
370 emitNote(Loc, Message.str(), &SM);
373 void DiagnosticNoteRenderer::emitBasicNote(StringRef Message) {
374 emitNote(SourceLocation(), Message, 0);