1//===- DiagnosticRenderer.cpp - Diagnostic Pretty-Printing ----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Frontend/DiagnosticRenderer.h"
10#include "clang/Basic/Diagnostic.h"
11#include "clang/Basic/DiagnosticOptions.h"
12#include "clang/Basic/LLVM.h"
13#include "clang/Basic/SourceLocation.h"
14#include "clang/Basic/SourceManager.h"
15#include "clang/Edit/Commit.h"
16#include "clang/Edit/EditedSource.h"
17#include "clang/Edit/EditsReceiver.h"
18#include "clang/Lex/Lexer.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/DenseMap.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/raw_ostream.h"
25#include <algorithm>
26#include <cassert>
27#include <iterator>
28#include <utility>
29
30using namespace clang;
31
32DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts,
33 DiagnosticOptions *DiagOpts)
34 : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {}
35
36DiagnosticRenderer::~DiagnosticRenderer() = default;
37
38namespace {
39
40class FixitReceiver : public edit::EditsReceiver {
41 SmallVectorImpl<FixItHint> &MergedFixits;
42
43public:
44 FixitReceiver(SmallVectorImpl<FixItHint> &MergedFixits)
45 : MergedFixits(MergedFixits) {}
46
47 void insert(SourceLocation loc, StringRef text) override {
48 MergedFixits.push_back(Elt: FixItHint::CreateInsertion(InsertionLoc: loc, Code: text));
49 }
50
51 void replace(CharSourceRange range, StringRef text) override {
52 MergedFixits.push_back(Elt: FixItHint::CreateReplacement(RemoveRange: range, Code: text));
53 }
54};
55
56} // namespace
57
58static void mergeFixits(ArrayRef<FixItHint> FixItHints,
59 const SourceManager &SM, const LangOptions &LangOpts,
60 SmallVectorImpl<FixItHint> &MergedFixits) {
61 edit::Commit commit(SM, LangOpts);
62 for (const auto &Hint : FixItHints)
63 if (Hint.CodeToInsert.empty()) {
64 if (Hint.InsertFromRange.isValid())
65 commit.insertFromRange(loc: Hint.RemoveRange.getBegin(),
66 range: Hint.InsertFromRange, /*afterToken=*/false,
67 beforePreviousInsertions: Hint.BeforePreviousInsertions);
68 else
69 commit.remove(range: Hint.RemoveRange);
70 } else {
71 if (Hint.RemoveRange.isTokenRange() ||
72 Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
73 commit.replace(range: Hint.RemoveRange, text: Hint.CodeToInsert);
74 else
75 commit.insert(loc: Hint.RemoveRange.getBegin(), text: Hint.CodeToInsert,
76 /*afterToken=*/false, beforePreviousInsertions: Hint.BeforePreviousInsertions);
77 }
78
79 edit::EditedSource Editor(SM, LangOpts);
80 if (Editor.commit(commit)) {
81 FixitReceiver Rec(MergedFixits);
82 Editor.applyRewrites(receiver&: Rec);
83 }
84}
85
86void DiagnosticRenderer::emitDiagnostic(FullSourceLoc Loc,
87 DiagnosticsEngine::Level Level,
88 StringRef Message,
89 ArrayRef<CharSourceRange> Ranges,
90 ArrayRef<FixItHint> FixItHints,
91 DiagOrStoredDiag D) {
92 assert(Loc.hasManager() || Loc.isInvalid());
93
94 beginDiagnostic(D, Level);
95
96 if (!Loc.isValid())
97 // If we have no source location, just emit the diagnostic message.
98 emitDiagnosticMessage(Loc, PLoc: PresumedLoc(), Level, Message, Ranges, Info: D);
99 else {
100 // Get the ranges into a local array we can hack on.
101 SmallVector<CharSourceRange, 20> MutableRanges(Ranges.begin(),
102 Ranges.end());
103
104 SmallVector<FixItHint, 8> MergedFixits;
105 if (!FixItHints.empty()) {
106 mergeFixits(FixItHints, SM: Loc.getManager(), LangOpts, MergedFixits);
107 FixItHints = MergedFixits;
108 }
109
110 for (const auto &Hint : FixItHints)
111 if (Hint.RemoveRange.isValid())
112 MutableRanges.push_back(Elt: Hint.RemoveRange);
113
114 FullSourceLoc UnexpandedLoc = Loc;
115
116 // Find the ultimate expansion location for the diagnostic.
117 Loc = Loc.getFileLoc();
118
119 PresumedLoc PLoc = Loc.getPresumedLoc(UseLineDirectives: DiagOpts->ShowPresumedLoc);
120
121 // First, if this diagnostic is not in the main file, print out the
122 // "included from" lines.
123 emitIncludeStack(Loc, PLoc, Level);
124
125 // Next, emit the actual diagnostic message and caret.
126 emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, Info: D);
127 emitCaret(Loc, Level, Ranges: MutableRanges, Hints: FixItHints);
128
129 // If this location is within a macro, walk from UnexpandedLoc up to Loc
130 // and produce a macro backtrace.
131 if (UnexpandedLoc.isValid() && UnexpandedLoc.isMacroID()) {
132 emitMacroExpansions(Loc: UnexpandedLoc, Level, Ranges: MutableRanges, Hints: FixItHints);
133 }
134 }
135
136 LastLoc = Loc;
137 LastLevel = Level;
138
139 endDiagnostic(D, Level);
140}
141
142void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) {
143 emitDiagnostic(Loc: Diag.getLocation(), Level: Diag.getLevel(), Message: Diag.getMessage(),
144 Ranges: Diag.getRanges(), FixItHints: Diag.getFixIts(),
145 D: &Diag);
146}
147
148void DiagnosticRenderer::emitBasicNote(StringRef Message) {
149 emitDiagnosticMessage(Loc: FullSourceLoc(), PLoc: PresumedLoc(), Level: DiagnosticsEngine::Note,
150 Message, Ranges: std::nullopt, Info: DiagOrStoredDiag());
151}
152
153/// Prints an include stack when appropriate for a particular
154/// diagnostic level and location.
155///
156/// This routine handles all the logic of suppressing particular include
157/// stacks (such as those for notes) and duplicate include stacks when
158/// repeated warnings occur within the same file. It also handles the logic
159/// of customizing the formatting and display of the include stack.
160///
161/// \param Loc The diagnostic location.
162/// \param PLoc The presumed location of the diagnostic location.
163/// \param Level The diagnostic level of the message this stack pertains to.
164void DiagnosticRenderer::emitIncludeStack(FullSourceLoc Loc, PresumedLoc PLoc,
165 DiagnosticsEngine::Level Level) {
166 FullSourceLoc IncludeLoc =
167 PLoc.isInvalid() ? FullSourceLoc()
168 : FullSourceLoc(PLoc.getIncludeLoc(), Loc.getManager());
169
170 // Skip redundant include stacks altogether.
171 if (LastIncludeLoc == IncludeLoc)
172 return;
173
174 LastIncludeLoc = IncludeLoc;
175
176 if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
177 return;
178
179 if (IncludeLoc.isValid())
180 emitIncludeStackRecursively(Loc: IncludeLoc);
181 else {
182 emitModuleBuildStack(SM: Loc.getManager());
183 emitImportStack(Loc);
184 }
185}
186
187/// Helper to recursively walk up the include stack and print each layer
188/// on the way back down.
189void DiagnosticRenderer::emitIncludeStackRecursively(FullSourceLoc Loc) {
190 if (Loc.isInvalid()) {
191 emitModuleBuildStack(SM: Loc.getManager());
192 return;
193 }
194
195 PresumedLoc PLoc = Loc.getPresumedLoc(UseLineDirectives: DiagOpts->ShowPresumedLoc);
196 if (PLoc.isInvalid())
197 return;
198
199 // If this source location was imported from a module, print the module
200 // import stack rather than the
201 // FIXME: We want submodule granularity here.
202 std::pair<FullSourceLoc, StringRef> Imported = Loc.getModuleImportLoc();
203 if (!Imported.second.empty()) {
204 // This location was imported by a module. Emit the module import stack.
205 emitImportStackRecursively(Loc: Imported.first, ModuleName: Imported.second);
206 return;
207 }
208
209 // Emit the other include frames first.
210 emitIncludeStackRecursively(
211 Loc: FullSourceLoc(PLoc.getIncludeLoc(), Loc.getManager()));
212
213 // Emit the inclusion text/note.
214 emitIncludeLocation(Loc, PLoc);
215}
216
217/// Emit the module import stack associated with the current location.
218void DiagnosticRenderer::emitImportStack(FullSourceLoc Loc) {
219 if (Loc.isInvalid()) {
220 emitModuleBuildStack(SM: Loc.getManager());
221 return;
222 }
223
224 std::pair<FullSourceLoc, StringRef> NextImportLoc = Loc.getModuleImportLoc();
225 emitImportStackRecursively(Loc: NextImportLoc.first, ModuleName: NextImportLoc.second);
226}
227
228/// Helper to recursively walk up the import stack and print each layer
229/// on the way back down.
230void DiagnosticRenderer::emitImportStackRecursively(FullSourceLoc Loc,
231 StringRef ModuleName) {
232 if (ModuleName.empty()) {
233 return;
234 }
235
236 PresumedLoc PLoc = Loc.getPresumedLoc(UseLineDirectives: DiagOpts->ShowPresumedLoc);
237
238 // Emit the other import frames first.
239 std::pair<FullSourceLoc, StringRef> NextImportLoc = Loc.getModuleImportLoc();
240 emitImportStackRecursively(Loc: NextImportLoc.first, ModuleName: NextImportLoc.second);
241
242 // Emit the inclusion text/note.
243 emitImportLocation(Loc, PLoc, ModuleName);
244}
245
246/// Emit the module build stack, for cases where a module is (re-)built
247/// on demand.
248void DiagnosticRenderer::emitModuleBuildStack(const SourceManager &SM) {
249 ModuleBuildStack Stack = SM.getModuleBuildStack();
250 for (const auto &I : Stack) {
251 emitBuildingModuleLocation(Loc: I.second, PLoc: I.second.getPresumedLoc(
252 UseLineDirectives: DiagOpts->ShowPresumedLoc),
253 ModuleName: I.first);
254 }
255}
256
257/// A recursive function to trace all possible backtrace locations
258/// to match the \p CaretLocFileID.
259static SourceLocation
260retrieveMacroLocation(SourceLocation Loc, FileID MacroFileID,
261 FileID CaretFileID,
262 const SmallVectorImpl<FileID> &CommonArgExpansions,
263 bool IsBegin, const SourceManager *SM,
264 bool &IsTokenRange) {
265 assert(SM->getFileID(Loc) == MacroFileID);
266 if (MacroFileID == CaretFileID)
267 return Loc;
268 if (!Loc.isMacroID())
269 return {};
270
271 CharSourceRange MacroRange, MacroArgRange;
272
273 if (SM->isMacroArgExpansion(Loc)) {
274 // Only look at the immediate spelling location of this macro argument if
275 // the other location in the source range is also present in that expansion.
276 if (std::binary_search(first: CommonArgExpansions.begin(),
277 last: CommonArgExpansions.end(), val: MacroFileID))
278 MacroRange =
279 CharSourceRange(SM->getImmediateSpellingLoc(Loc), IsTokenRange);
280 MacroArgRange = SM->getImmediateExpansionRange(Loc);
281 } else {
282 MacroRange = SM->getImmediateExpansionRange(Loc);
283 MacroArgRange =
284 CharSourceRange(SM->getImmediateSpellingLoc(Loc), IsTokenRange);
285 }
286
287 SourceLocation MacroLocation =
288 IsBegin ? MacroRange.getBegin() : MacroRange.getEnd();
289 if (MacroLocation.isValid()) {
290 MacroFileID = SM->getFileID(SpellingLoc: MacroLocation);
291 bool TokenRange = IsBegin ? IsTokenRange : MacroRange.isTokenRange();
292 MacroLocation =
293 retrieveMacroLocation(Loc: MacroLocation, MacroFileID, CaretFileID,
294 CommonArgExpansions, IsBegin, SM, IsTokenRange&: TokenRange);
295 if (MacroLocation.isValid()) {
296 IsTokenRange = TokenRange;
297 return MacroLocation;
298 }
299 }
300
301 // If we moved the end of the range to an expansion location, we now have
302 // a range of the same kind as the expansion range.
303 if (!IsBegin)
304 IsTokenRange = MacroArgRange.isTokenRange();
305
306 SourceLocation MacroArgLocation =
307 IsBegin ? MacroArgRange.getBegin() : MacroArgRange.getEnd();
308 MacroFileID = SM->getFileID(SpellingLoc: MacroArgLocation);
309 return retrieveMacroLocation(Loc: MacroArgLocation, MacroFileID, CaretFileID,
310 CommonArgExpansions, IsBegin, SM, IsTokenRange);
311}
312
313/// Walk up the chain of macro expansions and collect the FileIDs identifying the
314/// expansions.
315static void getMacroArgExpansionFileIDs(SourceLocation Loc,
316 SmallVectorImpl<FileID> &IDs,
317 bool IsBegin, const SourceManager *SM) {
318 while (Loc.isMacroID()) {
319 if (SM->isMacroArgExpansion(Loc)) {
320 IDs.push_back(Elt: SM->getFileID(SpellingLoc: Loc));
321 Loc = SM->getImmediateSpellingLoc(Loc);
322 } else {
323 auto ExpRange = SM->getImmediateExpansionRange(Loc);
324 Loc = IsBegin ? ExpRange.getBegin() : ExpRange.getEnd();
325 }
326 }
327}
328
329/// Collect the expansions of the begin and end locations and compute the set
330/// intersection. Produces a sorted vector of FileIDs in CommonArgExpansions.
331static void computeCommonMacroArgExpansionFileIDs(
332 SourceLocation Begin, SourceLocation End, const SourceManager *SM,
333 SmallVectorImpl<FileID> &CommonArgExpansions) {
334 SmallVector<FileID, 4> BeginArgExpansions;
335 SmallVector<FileID, 4> EndArgExpansions;
336 getMacroArgExpansionFileIDs(Loc: Begin, IDs&: BeginArgExpansions, /*IsBegin=*/true, SM);
337 getMacroArgExpansionFileIDs(Loc: End, IDs&: EndArgExpansions, /*IsBegin=*/false, SM);
338 llvm::sort(C&: BeginArgExpansions);
339 llvm::sort(C&: EndArgExpansions);
340 std::set_intersection(first1: BeginArgExpansions.begin(), last1: BeginArgExpansions.end(),
341 first2: EndArgExpansions.begin(), last2: EndArgExpansions.end(),
342 result: std::back_inserter(x&: CommonArgExpansions));
343}
344
345// Helper function to fix up source ranges. It takes in an array of ranges,
346// and outputs an array of ranges where we want to draw the range highlighting
347// around the location specified by CaretLoc.
348//
349// To find locations which correspond to the caret, we crawl the macro caller
350// chain for the beginning and end of each range. If the caret location
351// is in a macro expansion, we search each chain for a location
352// in the same expansion as the caret; otherwise, we crawl to the top of
353// each chain. Two locations are part of the same macro expansion
354// iff the FileID is the same.
355static void
356mapDiagnosticRanges(FullSourceLoc CaretLoc, ArrayRef<CharSourceRange> Ranges,
357 SmallVectorImpl<CharSourceRange> &SpellingRanges) {
358 FileID CaretLocFileID = CaretLoc.getFileID();
359
360 const SourceManager *SM = &CaretLoc.getManager();
361
362 for (const auto &Range : Ranges) {
363 if (Range.isInvalid())
364 continue;
365
366 SourceLocation Begin = Range.getBegin(), End = Range.getEnd();
367 bool IsTokenRange = Range.isTokenRange();
368
369 FileID BeginFileID = SM->getFileID(SpellingLoc: Begin);
370 FileID EndFileID = SM->getFileID(SpellingLoc: End);
371
372 // Find the common parent for the beginning and end of the range.
373
374 // First, crawl the expansion chain for the beginning of the range.
375 llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap;
376 while (Begin.isMacroID() && BeginFileID != EndFileID) {
377 BeginLocsMap[BeginFileID] = Begin;
378 Begin = SM->getImmediateExpansionRange(Loc: Begin).getBegin();
379 BeginFileID = SM->getFileID(SpellingLoc: Begin);
380 }
381
382 // Then, crawl the expansion chain for the end of the range.
383 if (BeginFileID != EndFileID) {
384 while (End.isMacroID() && !BeginLocsMap.count(Val: EndFileID)) {
385 auto Exp = SM->getImmediateExpansionRange(Loc: End);
386 IsTokenRange = Exp.isTokenRange();
387 End = Exp.getEnd();
388 EndFileID = SM->getFileID(SpellingLoc: End);
389 }
390 if (End.isMacroID()) {
391 Begin = BeginLocsMap[EndFileID];
392 BeginFileID = EndFileID;
393 }
394 }
395
396 // There is a chance that begin or end is invalid here, for example if
397 // specific compile error is reported.
398 // It is possible that the FileID's do not match, if one comes from an
399 // included file. In this case we can not produce a meaningful source range.
400 if (Begin.isInvalid() || End.isInvalid() || BeginFileID != EndFileID)
401 continue;
402
403 // Do the backtracking.
404 SmallVector<FileID, 4> CommonArgExpansions;
405 computeCommonMacroArgExpansionFileIDs(Begin, End, SM, CommonArgExpansions);
406 Begin = retrieveMacroLocation(Loc: Begin, MacroFileID: BeginFileID, CaretFileID: CaretLocFileID,
407 CommonArgExpansions, /*IsBegin=*/true, SM,
408 IsTokenRange);
409 End = retrieveMacroLocation(Loc: End, MacroFileID: BeginFileID, CaretFileID: CaretLocFileID,
410 CommonArgExpansions, /*IsBegin=*/false, SM,
411 IsTokenRange);
412 if (Begin.isInvalid() || End.isInvalid()) continue;
413
414 // Return the spelling location of the beginning and end of the range.
415 Begin = SM->getSpellingLoc(Loc: Begin);
416 End = SM->getSpellingLoc(Loc: End);
417
418 SpellingRanges.push_back(Elt: CharSourceRange(SourceRange(Begin, End),
419 IsTokenRange));
420 }
421}
422
423void DiagnosticRenderer::emitCaret(FullSourceLoc Loc,
424 DiagnosticsEngine::Level Level,
425 ArrayRef<CharSourceRange> Ranges,
426 ArrayRef<FixItHint> Hints) {
427 SmallVector<CharSourceRange, 4> SpellingRanges;
428 mapDiagnosticRanges(CaretLoc: Loc, Ranges, SpellingRanges);
429 emitCodeContext(Loc, Level, Ranges&: SpellingRanges, Hints);
430}
431
432/// A helper function for emitMacroExpansion to print the
433/// macro expansion message
434void DiagnosticRenderer::emitSingleMacroExpansion(
435 FullSourceLoc Loc, DiagnosticsEngine::Level Level,
436 ArrayRef<CharSourceRange> Ranges) {
437 // Find the spelling location for the macro definition. We must use the
438 // spelling location here to avoid emitting a macro backtrace for the note.
439 FullSourceLoc SpellingLoc = Loc.getSpellingLoc();
440
441 // Map the ranges into the FileID of the diagnostic location.
442 SmallVector<CharSourceRange, 4> SpellingRanges;
443 mapDiagnosticRanges(CaretLoc: Loc, Ranges, SpellingRanges);
444
445 SmallString<100> MessageStorage;
446 llvm::raw_svector_ostream Message(MessageStorage);
447 StringRef MacroName = Lexer::getImmediateMacroNameForDiagnostics(
448 Loc, SM: Loc.getManager(), LangOpts);
449 if (MacroName.empty())
450 Message << "expanded from here";
451 else
452 Message << "expanded from macro '" << MacroName << "'";
453
454 emitDiagnostic(Loc: SpellingLoc, Level: DiagnosticsEngine::Note, Message: Message.str(),
455 Ranges: SpellingRanges, FixItHints: std::nullopt);
456}
457
458/// Check that the macro argument location of Loc starts with ArgumentLoc.
459/// The starting location of the macro expansions is used to differeniate
460/// different macro expansions.
461static bool checkLocForMacroArgExpansion(SourceLocation Loc,
462 const SourceManager &SM,
463 SourceLocation ArgumentLoc) {
464 SourceLocation MacroLoc;
465 if (SM.isMacroArgExpansion(Loc, StartLoc: &MacroLoc)) {
466 if (ArgumentLoc == MacroLoc) return true;
467 }
468
469 return false;
470}
471
472/// Check if all the locations in the range have the same macro argument
473/// expansion, and that the expansion starts with ArgumentLoc.
474static bool checkRangeForMacroArgExpansion(CharSourceRange Range,
475 const SourceManager &SM,
476 SourceLocation ArgumentLoc) {
477 SourceLocation BegLoc = Range.getBegin(), EndLoc = Range.getEnd();
478 while (BegLoc != EndLoc) {
479 if (!checkLocForMacroArgExpansion(Loc: BegLoc, SM, ArgumentLoc))
480 return false;
481 BegLoc.getLocWithOffset(Offset: 1);
482 }
483
484 return checkLocForMacroArgExpansion(Loc: BegLoc, SM, ArgumentLoc);
485}
486
487/// A helper function to check if the current ranges are all inside the same
488/// macro argument expansion as Loc.
489static bool checkRangesForMacroArgExpansion(FullSourceLoc Loc,
490 ArrayRef<CharSourceRange> Ranges) {
491 assert(Loc.isMacroID() && "Must be a macro expansion!");
492
493 SmallVector<CharSourceRange, 4> SpellingRanges;
494 mapDiagnosticRanges(CaretLoc: Loc, Ranges, SpellingRanges);
495
496 // Count all valid ranges.
497 unsigned ValidCount =
498 llvm::count_if(Range&: Ranges, P: [](const auto &R) { return R.isValid(); });
499
500 if (ValidCount > SpellingRanges.size())
501 return false;
502
503 // To store the source location of the argument location.
504 FullSourceLoc ArgumentLoc;
505
506 // Set the ArgumentLoc to the beginning location of the expansion of Loc
507 // so to check if the ranges expands to the same beginning location.
508 if (!Loc.isMacroArgExpansion(StartLoc: &ArgumentLoc))
509 return false;
510
511 for (const auto &Range : SpellingRanges)
512 if (!checkRangeForMacroArgExpansion(Range, SM: Loc.getManager(), ArgumentLoc))
513 return false;
514
515 return true;
516}
517
518/// Recursively emit notes for each macro expansion and caret
519/// diagnostics where appropriate.
520///
521/// Walks up the macro expansion stack printing expansion notes, the code
522/// snippet, caret, underlines and FixItHint display as appropriate at each
523/// level.
524///
525/// \param Loc The location for this caret.
526/// \param Level The diagnostic level currently being emitted.
527/// \param Ranges The underlined ranges for this code snippet.
528/// \param Hints The FixIt hints active for this diagnostic.
529void DiagnosticRenderer::emitMacroExpansions(FullSourceLoc Loc,
530 DiagnosticsEngine::Level Level,
531 ArrayRef<CharSourceRange> Ranges,
532 ArrayRef<FixItHint> Hints) {
533 assert(Loc.isValid() && "must have a valid source location here");
534 const SourceManager &SM = Loc.getManager();
535 SourceLocation L = Loc;
536
537 // Produce a stack of macro backtraces.
538 SmallVector<SourceLocation, 8> LocationStack;
539 unsigned IgnoredEnd = 0;
540 while (L.isMacroID()) {
541 // If this is the expansion of a macro argument, point the caret at the
542 // use of the argument in the definition of the macro, not the expansion.
543 if (SM.isMacroArgExpansion(Loc: L))
544 LocationStack.push_back(Elt: SM.getImmediateExpansionRange(Loc: L).getBegin());
545 else
546 LocationStack.push_back(Elt: L);
547
548 if (checkRangesForMacroArgExpansion(Loc: FullSourceLoc(L, SM), Ranges))
549 IgnoredEnd = LocationStack.size();
550
551 L = SM.getImmediateMacroCallerLoc(Loc: L);
552
553 // Once the location no longer points into a macro, try stepping through
554 // the last found location. This sometimes produces additional useful
555 // backtraces.
556 if (L.isFileID())
557 L = SM.getImmediateMacroCallerLoc(Loc: LocationStack.back());
558 assert(L.isValid() && "must have a valid source location here");
559 }
560
561 LocationStack.erase(CS: LocationStack.begin(),
562 CE: LocationStack.begin() + IgnoredEnd);
563
564 unsigned MacroDepth = LocationStack.size();
565 unsigned MacroLimit = DiagOpts->MacroBacktraceLimit;
566 if (MacroDepth <= MacroLimit || MacroLimit == 0) {
567 for (auto I = LocationStack.rbegin(), E = LocationStack.rend();
568 I != E; ++I)
569 emitSingleMacroExpansion(Loc: FullSourceLoc(*I, SM), Level, Ranges);
570 return;
571 }
572
573 unsigned MacroStartMessages = MacroLimit / 2;
574 unsigned MacroEndMessages = MacroLimit / 2 + MacroLimit % 2;
575
576 for (auto I = LocationStack.rbegin(),
577 E = LocationStack.rbegin() + MacroStartMessages;
578 I != E; ++I)
579 emitSingleMacroExpansion(Loc: FullSourceLoc(*I, SM), Level, Ranges);
580
581 SmallString<200> MessageStorage;
582 llvm::raw_svector_ostream Message(MessageStorage);
583 Message << "(skipping " << (MacroDepth - MacroLimit)
584 << " expansions in backtrace; use -fmacro-backtrace-limit=0 to "
585 "see all)";
586 emitBasicNote(Message: Message.str());
587
588 for (auto I = LocationStack.rend() - MacroEndMessages,
589 E = LocationStack.rend();
590 I != E; ++I)
591 emitSingleMacroExpansion(Loc: FullSourceLoc(*I, SM), Level, Ranges);
592}
593
594DiagnosticNoteRenderer::~DiagnosticNoteRenderer() = default;
595
596void DiagnosticNoteRenderer::emitIncludeLocation(FullSourceLoc Loc,
597 PresumedLoc PLoc) {
598 // Generate a note indicating the include location.
599 SmallString<200> MessageStorage;
600 llvm::raw_svector_ostream Message(MessageStorage);
601 Message << "in file included from " << PLoc.getFilename() << ':'
602 << PLoc.getLine() << ":";
603 emitNote(Loc, Message: Message.str());
604}
605
606void DiagnosticNoteRenderer::emitImportLocation(FullSourceLoc Loc,
607 PresumedLoc PLoc,
608 StringRef ModuleName) {
609 // Generate a note indicating the include location.
610 SmallString<200> MessageStorage;
611 llvm::raw_svector_ostream Message(MessageStorage);
612 Message << "in module '" << ModuleName;
613 if (PLoc.isValid())
614 Message << "' imported from " << PLoc.getFilename() << ':'
615 << PLoc.getLine();
616 Message << ":";
617 emitNote(Loc, Message: Message.str());
618}
619
620void DiagnosticNoteRenderer::emitBuildingModuleLocation(FullSourceLoc Loc,
621 PresumedLoc PLoc,
622 StringRef ModuleName) {
623 // Generate a note indicating the include location.
624 SmallString<200> MessageStorage;
625 llvm::raw_svector_ostream Message(MessageStorage);
626 if (PLoc.isValid())
627 Message << "while building module '" << ModuleName << "' imported from "
628 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
629 else
630 Message << "while building module '" << ModuleName << "':";
631 emitNote(Loc, Message: Message.str());
632}
633

source code of clang/lib/Frontend/DiagnosticRenderer.cpp