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/SourceManager.h"
13 #include "clang/Edit/Commit.h"
14 #include "clang/Edit/EditedSource.h"
15 #include "clang/Edit/EditsReceiver.h"
16 #include "clang/Lex/Lexer.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
22 using namespace clang;
24 DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts,
25 DiagnosticOptions *DiagOpts)
26 : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {}
28 DiagnosticRenderer::~DiagnosticRenderer() {}
32 class FixitReceiver : public edit::EditsReceiver {
33 SmallVectorImpl<FixItHint> &MergedFixits;
36 FixitReceiver(SmallVectorImpl<FixItHint> &MergedFixits)
37 : MergedFixits(MergedFixits) { }
38 void insert(SourceLocation loc, StringRef text) override {
39 MergedFixits.push_back(FixItHint::CreateInsertion(loc, text));
41 void replace(CharSourceRange range, StringRef text) override {
42 MergedFixits.push_back(FixItHint::CreateReplacement(range, text));
48 static void mergeFixits(ArrayRef<FixItHint> FixItHints,
49 const SourceManager &SM, const LangOptions &LangOpts,
50 SmallVectorImpl<FixItHint> &MergedFixits) {
51 edit::Commit commit(SM, LangOpts);
52 for (ArrayRef<FixItHint>::const_iterator
53 I = FixItHints.begin(), E = FixItHints.end(); I != E; ++I) {
54 const FixItHint &Hint = *I;
55 if (Hint.CodeToInsert.empty()) {
56 if (Hint.InsertFromRange.isValid())
57 commit.insertFromRange(Hint.RemoveRange.getBegin(),
58 Hint.InsertFromRange, /*afterToken=*/false,
59 Hint.BeforePreviousInsertions);
61 commit.remove(Hint.RemoveRange);
63 if (Hint.RemoveRange.isTokenRange() ||
64 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
65 commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
67 commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
68 /*afterToken=*/false, Hint.BeforePreviousInsertions);
72 edit::EditedSource Editor(SM, LangOpts);
73 if (Editor.commit(commit)) {
74 FixitReceiver Rec(MergedFixits);
75 Editor.applyRewrites(Rec);
79 void DiagnosticRenderer::emitDiagnostic(FullSourceLoc Loc,
80 DiagnosticsEngine::Level Level,
82 ArrayRef<CharSourceRange> Ranges,
83 ArrayRef<FixItHint> FixItHints,
85 assert(Loc.hasManager() || Loc.isInvalid());
87 beginDiagnostic(D, Level);
90 // If we have no source location, just emit the diagnostic message.
91 emitDiagnosticMessage(Loc, PresumedLoc(), Level, Message, Ranges, D);
93 // Get the ranges into a local array we can hack on.
94 SmallVector<CharSourceRange, 20> MutableRanges(Ranges.begin(),
97 SmallVector<FixItHint, 8> MergedFixits;
98 if (!FixItHints.empty()) {
99 mergeFixits(FixItHints, Loc.getManager(), LangOpts, MergedFixits);
100 FixItHints = MergedFixits;
103 for (ArrayRef<FixItHint>::const_iterator I = FixItHints.begin(),
104 E = FixItHints.end();
106 if (I->RemoveRange.isValid())
107 MutableRanges.push_back(I->RemoveRange);
109 FullSourceLoc UnexpandedLoc = Loc;
111 // Find the ultimate expansion location for the diagnostic.
112 Loc = Loc.getFileLoc();
114 PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts->ShowPresumedLoc);
116 // First, if this diagnostic is not in the main file, print out the
117 // "included from" lines.
118 emitIncludeStack(Loc, PLoc, Level);
120 // Next, emit the actual diagnostic message and caret.
121 emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, D);
122 emitCaret(Loc, Level, MutableRanges, FixItHints);
124 // If this location is within a macro, walk from UnexpandedLoc up to Loc
125 // and produce a macro backtrace.
126 if (UnexpandedLoc.isValid() && UnexpandedLoc.isMacroID()) {
127 emitMacroExpansions(UnexpandedLoc, Level, MutableRanges, FixItHints);
134 endDiagnostic(D, Level);
138 void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) {
139 emitDiagnostic(Diag.getLocation(), Diag.getLevel(), Diag.getMessage(),
140 Diag.getRanges(), Diag.getFixIts(),
144 void DiagnosticRenderer::emitBasicNote(StringRef Message) {
145 emitDiagnosticMessage(FullSourceLoc(), PresumedLoc(), DiagnosticsEngine::Note,
146 Message, None, DiagOrStoredDiag());
149 /// \brief Prints an include stack when appropriate for a particular
150 /// diagnostic level and location.
152 /// This routine handles all the logic of suppressing particular include
153 /// stacks (such as those for notes) and duplicate include stacks when
154 /// repeated warnings occur within the same file. It also handles the logic
155 /// of customizing the formatting and display of the include stack.
157 /// \param Loc The diagnostic location.
158 /// \param PLoc The presumed location of the diagnostic location.
159 /// \param Level The diagnostic level of the message this stack pertains to.
160 void DiagnosticRenderer::emitIncludeStack(FullSourceLoc Loc, PresumedLoc PLoc,
161 DiagnosticsEngine::Level Level) {
162 FullSourceLoc IncludeLoc =
163 PLoc.isInvalid() ? FullSourceLoc()
164 : FullSourceLoc(PLoc.getIncludeLoc(), Loc.getManager());
166 // Skip redundant include stacks altogether.
167 if (LastIncludeLoc == IncludeLoc)
170 LastIncludeLoc = IncludeLoc;
172 if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
175 if (IncludeLoc.isValid())
176 emitIncludeStackRecursively(IncludeLoc);
178 emitModuleBuildStack(Loc.getManager());
179 emitImportStack(Loc);
183 /// \brief Helper to recursivly walk up the include stack and print each layer
184 /// on the way back down.
185 void DiagnosticRenderer::emitIncludeStackRecursively(FullSourceLoc Loc) {
186 if (Loc.isInvalid()) {
187 emitModuleBuildStack(Loc.getManager());
191 PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts->ShowPresumedLoc);
192 if (PLoc.isInvalid())
195 // If this source location was imported from a module, print the module
196 // import stack rather than the
197 // FIXME: We want submodule granularity here.
198 std::pair<FullSourceLoc, StringRef> Imported = Loc.getModuleImportLoc();
199 if (!Imported.second.empty()) {
200 // This location was imported by a module. Emit the module import stack.
201 emitImportStackRecursively(Imported.first, Imported.second);
205 // Emit the other include frames first.
206 emitIncludeStackRecursively(
207 FullSourceLoc(PLoc.getIncludeLoc(), Loc.getManager()));
209 // Emit the inclusion text/note.
210 emitIncludeLocation(Loc, PLoc);
213 /// \brief Emit the module import stack associated with the current location.
214 void DiagnosticRenderer::emitImportStack(FullSourceLoc Loc) {
215 if (Loc.isInvalid()) {
216 emitModuleBuildStack(Loc.getManager());
220 std::pair<FullSourceLoc, StringRef> NextImportLoc = Loc.getModuleImportLoc();
221 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second);
224 /// \brief Helper to recursivly walk up the import stack and print each layer
225 /// on the way back down.
226 void DiagnosticRenderer::emitImportStackRecursively(FullSourceLoc Loc,
227 StringRef ModuleName) {
228 if (ModuleName.empty()) {
232 PresumedLoc PLoc = Loc.getPresumedLoc(DiagOpts->ShowPresumedLoc);
234 // Emit the other import frames first.
235 std::pair<FullSourceLoc, StringRef> NextImportLoc = Loc.getModuleImportLoc();
236 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second);
238 // Emit the inclusion text/note.
239 emitImportLocation(Loc, PLoc, ModuleName);
242 /// \brief Emit the module build stack, for cases where a module is (re-)built
244 void DiagnosticRenderer::emitModuleBuildStack(const SourceManager &SM) {
245 ModuleBuildStack Stack = SM.getModuleBuildStack();
246 for (unsigned I = 0, N = Stack.size(); I != N; ++I) {
247 emitBuildingModuleLocation(Stack[I].second, Stack[I].second.getPresumedLoc(
248 DiagOpts->ShowPresumedLoc),
253 /// A recursive function to trace all possible backtrace locations
254 /// to match the \p CaretLocFileID.
255 static SourceLocation
256 retrieveMacroLocation(SourceLocation Loc, FileID MacroFileID,
258 const SmallVectorImpl<FileID> &CommonArgExpansions,
259 bool IsBegin, const SourceManager *SM) {
260 assert(SM->getFileID(Loc) == MacroFileID);
261 if (MacroFileID == CaretFileID)
263 if (!Loc.isMacroID())
264 return SourceLocation();
266 SourceLocation MacroLocation, MacroArgLocation;
268 if (SM->isMacroArgExpansion(Loc)) {
269 // Only look at the immediate spelling location of this macro argument if
270 // the other location in the source range is also present in that expansion.
271 if (std::binary_search(CommonArgExpansions.begin(),
272 CommonArgExpansions.end(), MacroFileID))
273 MacroLocation = SM->getImmediateSpellingLoc(Loc);
274 MacroArgLocation = IsBegin ? SM->getImmediateExpansionRange(Loc).first
275 : SM->getImmediateExpansionRange(Loc).second;
277 MacroLocation = IsBegin ? SM->getImmediateExpansionRange(Loc).first
278 : SM->getImmediateExpansionRange(Loc).second;
279 MacroArgLocation = SM->getImmediateSpellingLoc(Loc);
282 if (MacroLocation.isValid()) {
283 MacroFileID = SM->getFileID(MacroLocation);
285 retrieveMacroLocation(MacroLocation, MacroFileID, CaretFileID,
286 CommonArgExpansions, IsBegin, SM);
287 if (MacroLocation.isValid())
288 return MacroLocation;
291 MacroFileID = SM->getFileID(MacroArgLocation);
292 return retrieveMacroLocation(MacroArgLocation, MacroFileID, CaretFileID,
293 CommonArgExpansions, IsBegin, SM);
296 /// Walk up the chain of macro expansions and collect the FileIDs identifying the
298 static void getMacroArgExpansionFileIDs(SourceLocation Loc,
299 SmallVectorImpl<FileID> &IDs,
300 bool IsBegin, const SourceManager *SM) {
301 while (Loc.isMacroID()) {
302 if (SM->isMacroArgExpansion(Loc)) {
303 IDs.push_back(SM->getFileID(Loc));
304 Loc = SM->getImmediateSpellingLoc(Loc);
306 auto ExpRange = SM->getImmediateExpansionRange(Loc);
307 Loc = IsBegin ? ExpRange.first : ExpRange.second;
312 /// Collect the expansions of the begin and end locations and compute the set
313 /// intersection. Produces a sorted vector of FileIDs in CommonArgExpansions.
314 static void computeCommonMacroArgExpansionFileIDs(
315 SourceLocation Begin, SourceLocation End, const SourceManager *SM,
316 SmallVectorImpl<FileID> &CommonArgExpansions) {
317 SmallVector<FileID, 4> BeginArgExpansions;
318 SmallVector<FileID, 4> EndArgExpansions;
319 getMacroArgExpansionFileIDs(Begin, BeginArgExpansions, /*IsBegin=*/true, SM);
320 getMacroArgExpansionFileIDs(End, EndArgExpansions, /*IsBegin=*/false, SM);
321 std::sort(BeginArgExpansions.begin(), BeginArgExpansions.end());
322 std::sort(EndArgExpansions.begin(), EndArgExpansions.end());
323 std::set_intersection(BeginArgExpansions.begin(), BeginArgExpansions.end(),
324 EndArgExpansions.begin(), EndArgExpansions.end(),
325 std::back_inserter(CommonArgExpansions));
328 // Helper function to fix up source ranges. It takes in an array of ranges,
329 // and outputs an array of ranges where we want to draw the range highlighting
330 // around the location specified by CaretLoc.
332 // To find locations which correspond to the caret, we crawl the macro caller
333 // chain for the beginning and end of each range. If the caret location
334 // is in a macro expansion, we search each chain for a location
335 // in the same expansion as the caret; otherwise, we crawl to the top of
336 // each chain. Two locations are part of the same macro expansion
337 // iff the FileID is the same.
339 mapDiagnosticRanges(FullSourceLoc CaretLoc, ArrayRef<CharSourceRange> Ranges,
340 SmallVectorImpl<CharSourceRange> &SpellingRanges) {
341 FileID CaretLocFileID = CaretLoc.getFileID();
343 const SourceManager *SM = &CaretLoc.getManager();
345 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
346 if (I->isInvalid()) continue;
348 SourceLocation Begin = I->getBegin(), End = I->getEnd();
349 bool IsTokenRange = I->isTokenRange();
351 FileID BeginFileID = SM->getFileID(Begin);
352 FileID EndFileID = SM->getFileID(End);
354 // Find the common parent for the beginning and end of the range.
356 // First, crawl the expansion chain for the beginning of the range.
357 llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap;
358 while (Begin.isMacroID() && BeginFileID != EndFileID) {
359 BeginLocsMap[BeginFileID] = Begin;
360 Begin = SM->getImmediateExpansionRange(Begin).first;
361 BeginFileID = SM->getFileID(Begin);
364 // Then, crawl the expansion chain for the end of the range.
365 if (BeginFileID != EndFileID) {
366 while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) {
367 End = SM->getImmediateExpansionRange(End).second;
368 EndFileID = SM->getFileID(End);
370 if (End.isMacroID()) {
371 Begin = BeginLocsMap[EndFileID];
372 BeginFileID = EndFileID;
376 // Do the backtracking.
377 SmallVector<FileID, 4> CommonArgExpansions;
378 computeCommonMacroArgExpansionFileIDs(Begin, End, SM, CommonArgExpansions);
379 Begin = retrieveMacroLocation(Begin, BeginFileID, CaretLocFileID,
380 CommonArgExpansions, /*IsBegin=*/true, SM);
381 End = retrieveMacroLocation(End, BeginFileID, CaretLocFileID,
382 CommonArgExpansions, /*IsBegin=*/false, SM);
383 if (Begin.isInvalid() || End.isInvalid()) continue;
385 // Return the spelling location of the beginning and end of the range.
386 Begin = SM->getSpellingLoc(Begin);
387 End = SM->getSpellingLoc(End);
389 SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End),
394 void DiagnosticRenderer::emitCaret(FullSourceLoc Loc,
395 DiagnosticsEngine::Level Level,
396 ArrayRef<CharSourceRange> Ranges,
397 ArrayRef<FixItHint> Hints) {
398 SmallVector<CharSourceRange, 4> SpellingRanges;
399 mapDiagnosticRanges(Loc, Ranges, SpellingRanges);
400 emitCodeContext(Loc, Level, SpellingRanges, Hints);
403 /// \brief A helper function for emitMacroExpansion to print the
404 /// macro expansion message
405 void DiagnosticRenderer::emitSingleMacroExpansion(
406 FullSourceLoc Loc, DiagnosticsEngine::Level Level,
407 ArrayRef<CharSourceRange> Ranges) {
408 // Find the spelling location for the macro definition. We must use the
409 // spelling location here to avoid emitting a macro backtrace for the note.
410 FullSourceLoc SpellingLoc = Loc.getSpellingLoc();
412 // Map the ranges into the FileID of the diagnostic location.
413 SmallVector<CharSourceRange, 4> SpellingRanges;
414 mapDiagnosticRanges(Loc, Ranges, SpellingRanges);
416 SmallString<100> MessageStorage;
417 llvm::raw_svector_ostream Message(MessageStorage);
418 StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics(
419 Loc, Loc.getManager(), LangOpts);
420 if (MacroName.empty())
421 Message << "expanded from here";
423 Message << "expanded from macro '" << MacroName << "'";
425 emitDiagnostic(SpellingLoc, DiagnosticsEngine::Note, Message.str(),
426 SpellingRanges, None);
429 /// Check that the macro argument location of Loc starts with ArgumentLoc.
430 /// The starting location of the macro expansions is used to differeniate
431 /// different macro expansions.
432 static bool checkLocForMacroArgExpansion(SourceLocation Loc,
433 const SourceManager &SM,
434 SourceLocation ArgumentLoc) {
435 SourceLocation MacroLoc;
436 if (SM.isMacroArgExpansion(Loc, &MacroLoc)) {
437 if (ArgumentLoc == MacroLoc) return true;
443 /// Check if all the locations in the range have the same macro argument
444 /// expansion, and that that expansion starts with ArgumentLoc.
445 static bool checkRangeForMacroArgExpansion(CharSourceRange Range,
446 const SourceManager &SM,
447 SourceLocation ArgumentLoc) {
448 SourceLocation BegLoc = Range.getBegin(), EndLoc = Range.getEnd();
449 while (BegLoc != EndLoc) {
450 if (!checkLocForMacroArgExpansion(BegLoc, SM, ArgumentLoc))
452 BegLoc.getLocWithOffset(1);
455 return checkLocForMacroArgExpansion(BegLoc, SM, ArgumentLoc);
458 /// A helper function to check if the current ranges are all inside the same
459 /// macro argument expansion as Loc.
460 static bool checkRangesForMacroArgExpansion(FullSourceLoc Loc,
461 ArrayRef<CharSourceRange> Ranges) {
462 assert(Loc.isMacroID() && "Must be a macro expansion!");
464 SmallVector<CharSourceRange, 4> SpellingRanges;
465 mapDiagnosticRanges(Loc, Ranges, SpellingRanges);
467 /// Count all valid ranges.
468 unsigned ValidCount = 0;
469 for (auto I : Ranges)
470 if (I.isValid()) ValidCount++;
472 if (ValidCount > SpellingRanges.size())
475 /// To store the source location of the argument location.
476 FullSourceLoc ArgumentLoc;
478 /// Set the ArgumentLoc to the beginning location of the expansion of Loc
479 /// so to check if the ranges expands to the same beginning location.
480 if (!Loc.isMacroArgExpansion(&ArgumentLoc))
483 for (auto I = SpellingRanges.begin(), E = SpellingRanges.end(); I != E; ++I) {
484 if (!checkRangeForMacroArgExpansion(*I, Loc.getManager(), ArgumentLoc))
491 /// \brief Recursively emit notes for each macro expansion and caret
492 /// diagnostics where appropriate.
494 /// Walks up the macro expansion stack printing expansion notes, the code
495 /// snippet, caret, underlines and FixItHint display as appropriate at each
498 /// \param Loc The location for this caret.
499 /// \param Level The diagnostic level currently being emitted.
500 /// \param Ranges The underlined ranges for this code snippet.
501 /// \param Hints The FixIt hints active for this diagnostic.
502 void DiagnosticRenderer::emitMacroExpansions(FullSourceLoc Loc,
503 DiagnosticsEngine::Level Level,
504 ArrayRef<CharSourceRange> Ranges,
505 ArrayRef<FixItHint> Hints) {
506 assert(Loc.isValid() && "must have a valid source location here");
508 // Produce a stack of macro backtraces.
509 SmallVector<FullSourceLoc, 8> LocationStack;
510 unsigned IgnoredEnd = 0;
511 while (Loc.isMacroID()) {
512 // If this is the expansion of a macro argument, point the caret at the
513 // use of the argument in the definition of the macro, not the expansion.
514 if (Loc.isMacroArgExpansion())
515 LocationStack.push_back(Loc.getImmediateExpansionRange().first);
517 LocationStack.push_back(Loc);
519 if (checkRangesForMacroArgExpansion(Loc, Ranges))
520 IgnoredEnd = LocationStack.size();
522 Loc = Loc.getImmediateMacroCallerLoc();
524 // Once the location no longer points into a macro, try stepping through
525 // the last found location. This sometimes produces additional useful
528 Loc = LocationStack.back().getImmediateMacroCallerLoc();
529 assert(Loc.isValid() && "must have a valid source location here");
532 LocationStack.erase(LocationStack.begin(),
533 LocationStack.begin() + IgnoredEnd);
535 unsigned MacroDepth = LocationStack.size();
536 unsigned MacroLimit = DiagOpts->MacroBacktraceLimit;
537 if (MacroDepth <= MacroLimit || MacroLimit == 0) {
538 for (auto I = LocationStack.rbegin(), E = LocationStack.rend();
540 emitSingleMacroExpansion(*I, Level, Ranges);
544 unsigned MacroStartMessages = MacroLimit / 2;
545 unsigned MacroEndMessages = MacroLimit / 2 + MacroLimit % 2;
547 for (auto I = LocationStack.rbegin(),
548 E = LocationStack.rbegin() + MacroStartMessages;
550 emitSingleMacroExpansion(*I, Level, Ranges);
552 SmallString<200> MessageStorage;
553 llvm::raw_svector_ostream Message(MessageStorage);
554 Message << "(skipping " << (MacroDepth - MacroLimit)
555 << " expansions in backtrace; use -fmacro-backtrace-limit=0 to "
557 emitBasicNote(Message.str());
559 for (auto I = LocationStack.rend() - MacroEndMessages,
560 E = LocationStack.rend();
562 emitSingleMacroExpansion(*I, Level, Ranges);
565 DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {}
567 void DiagnosticNoteRenderer::emitIncludeLocation(FullSourceLoc Loc,
569 // Generate a note indicating the include location.
570 SmallString<200> MessageStorage;
571 llvm::raw_svector_ostream Message(MessageStorage);
572 Message << "in file included from " << PLoc.getFilename() << ':'
573 << PLoc.getLine() << ":";
574 emitNote(Loc, Message.str());
577 void DiagnosticNoteRenderer::emitImportLocation(FullSourceLoc Loc,
579 StringRef ModuleName) {
580 // Generate a note indicating the include location.
581 SmallString<200> MessageStorage;
582 llvm::raw_svector_ostream Message(MessageStorage);
583 Message << "in module '" << ModuleName;
585 Message << "' imported from " << PLoc.getFilename() << ':'
588 emitNote(Loc, Message.str());
591 void DiagnosticNoteRenderer::emitBuildingModuleLocation(FullSourceLoc Loc,
593 StringRef ModuleName) {
594 // Generate a note indicating the include location.
595 SmallString<200> MessageStorage;
596 llvm::raw_svector_ostream Message(MessageStorage);
598 Message << "while building module '" << ModuleName << "' imported from "
599 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
601 Message << "while building module '" << ModuleName << "':";
602 emitNote(Loc, Message.str());