blob: caf1f0dce99ffce4a719c537f2e1d185e476cd6f [file] [log] [blame]
Ted Kremenekc4bbd852011-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 Gregor811db4e2012-10-23 22:26:28 +000011#include "clang/Basic/DiagnosticOptions.h"
Ted Kremenekc4bbd852011-12-17 05:26:04 +000012#include "clang/Basic/FileManager.h"
13#include "clang/Basic/SourceManager.h"
Ted Kremenekf7639e12012-03-06 20:06:33 +000014#include "clang/Edit/Commit.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/Edit/EditedSource.h"
Ted Kremenekf7639e12012-03-06 20:06:33 +000016#include "clang/Edit/EditsReceiver.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Lex/Lexer.h"
Eli Friedman34ff0ea2012-11-03 03:36:51 +000018#include "llvm/ADT/SmallSet.h"
Ted Kremenekc4bbd852011-12-17 05:26:04 +000019#include "llvm/ADT/SmallString.h"
Chandler Carruth3a022472012-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 Kremenekc4bbd852011-12-17 05:26:04 +000023#include <algorithm>
24using namespace clang;
25
Argyrios Kyrtzidisabff5f12012-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 Smithf89e2e22012-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 Kyrtzidisabff5f12012-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 Kyrtzidisb16ff5d2012-05-10 05:03:45 +000068DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts,
Douglas Gregor811db4e2012-10-23 22:26:28 +000069 DiagnosticOptions *DiagOpts)
70 : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {}
Ted Kremenekc4bbd852011-12-17 05:26:04 +000071
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000072DiagnosticRenderer::~DiagnosticRenderer() {}
Ted Kremenekc4bbd852011-12-17 05:26:04 +000073
Ted Kremenekf7639e12012-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) { }
Craig Topperafa7cb32014-03-13 06:07:04 +000082 void insert(SourceLocation loc, StringRef text) override {
Ted Kremenekf7639e12012-03-06 20:06:33 +000083 MergedFixits.push_back(FixItHint::CreateInsertion(loc, text));
84 }
Craig Topperafa7cb32014-03-13 06:07:04 +000085 void replace(CharSourceRange range, StringRef text) override {
Ted Kremenekf7639e12012-03-06 20:06:33 +000086 MergedFixits.push_back(FixItHint::CreateReplacement(range, text));
87 }
88};
89
Alexander Kornienkoab9db512015-06-22 23:07:51 +000090}
Ted Kremenekf7639e12012-03-06 20:06:33 +000091
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 Kremenekc4bbd852011-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 Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000128 const SourceManager *SM,
Ted Kremenek0964cca2012-02-14 02:46:00 +0000129 DiagOrStoredDiag D) {
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000130 assert(SM || Loc.isInvalid());
Richard Smithc01cca22012-12-05 09:47:49 +0000131
Ted Kremenek0964cca2012-02-14 02:46:00 +0000132 beginDiagnostic(D, Level);
Richard Smithc01cca22012-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 Kremenekc4bbd852011-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 Smithc01cca22012-12-05 09:47:49 +0000141
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000142 SmallVector<FixItHint, 8> MergedFixits;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000143 if (!FixItHints.empty()) {
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000144 mergeFixits(FixItHints, *SM, LangOpts, MergedFixits);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000145 FixItHints = MergedFixits;
146 }
147
Ted Kremenekc4bbd852011-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 Smithaebee682012-12-05 06:20:58 +0000153
Richard Smithc01cca22012-12-05 09:47:49 +0000154 SourceLocation UnexpandedLoc = Loc;
Richard Smithaebee682012-12-05 06:20:58 +0000155
Ted Kremenek372735f2012-12-19 01:16:49 +0000156 // Find the ultimate expansion location for the diagnostic.
157 Loc = SM->getFileLoc(Loc);
Richard Smithc01cca22012-12-05 09:47:49 +0000158
159 PresumedLoc PLoc = SM->getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
160
161 // First, if this diagnostic is not in the main file, print out the
162 // "included from" lines.
163 emitIncludeStack(Loc, PLoc, Level, *SM);
164
165 // Next, emit the actual diagnostic message and caret.
166 emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, SM, D);
167 emitCaret(Loc, Level, MutableRanges, FixItHints, *SM);
168
169 // If this location is within a macro, walk from UnexpandedLoc up to Loc
170 // and produce a macro backtrace.
171 if (UnexpandedLoc.isValid() && UnexpandedLoc.isMacroID()) {
Richard Trieu97c45b62015-07-28 20:53:46 +0000172 emitMacroExpansions(UnexpandedLoc, Level, MutableRanges, FixItHints, *SM);
Richard Smithaebee682012-12-05 06:20:58 +0000173 }
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000174 }
Richard Smithc01cca22012-12-05 09:47:49 +0000175
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000176 LastLoc = Loc;
177 LastLevel = Level;
Richard Smithc01cca22012-12-05 09:47:49 +0000178
Ted Kremenek0964cca2012-02-14 02:46:00 +0000179 endDiagnostic(D, Level);
180}
181
182
183void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) {
184 emitDiagnostic(Diag.getLocation(), Diag.getLevel(), Diag.getMessage(),
185 Diag.getRanges(), Diag.getFixIts(),
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000186 Diag.getLocation().isValid() ? &Diag.getLocation().getManager()
Craig Topper49a27902014-05-22 04:46:25 +0000187 : nullptr,
Ted Kremenek0964cca2012-02-14 02:46:00 +0000188 &Diag);
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000189}
190
Alp Toker4db87ab2014-06-21 23:31:59 +0000191void DiagnosticRenderer::emitBasicNote(StringRef Message) {
192 emitDiagnosticMessage(
193 SourceLocation(), PresumedLoc(), DiagnosticsEngine::Note, Message,
Craig Topper5fc8fc22014-08-27 06:28:36 +0000194 None, nullptr, DiagOrStoredDiag());
Alp Toker4db87ab2014-06-21 23:31:59 +0000195}
196
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000197/// \brief Prints an include stack when appropriate for a particular
198/// diagnostic level and location.
199///
200/// This routine handles all the logic of suppressing particular include
201/// stacks (such as those for notes) and duplicate include stacks when
202/// repeated warnings occur within the same file. It also handles the logic
203/// of customizing the formatting and display of the include stack.
204///
Douglas Gregor22103e32012-11-30 21:58:49 +0000205/// \param Loc The diagnostic location.
206/// \param PLoc The presumed location of the diagnostic location.
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000207/// \param Level The diagnostic level of the message this stack pertains to.
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000208void DiagnosticRenderer::emitIncludeStack(SourceLocation Loc,
Douglas Gregor22103e32012-11-30 21:58:49 +0000209 PresumedLoc PLoc,
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000210 DiagnosticsEngine::Level Level,
211 const SourceManager &SM) {
Douglas Gregor22103e32012-11-30 21:58:49 +0000212 SourceLocation IncludeLoc = PLoc.getIncludeLoc();
213
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000214 // Skip redundant include stacks altogether.
Douglas Gregor22103e32012-11-30 21:58:49 +0000215 if (LastIncludeLoc == IncludeLoc)
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000216 return;
Douglas Gregor22103e32012-11-30 21:58:49 +0000217
218 LastIncludeLoc = IncludeLoc;
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000219
Douglas Gregor811db4e2012-10-23 22:26:28 +0000220 if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000221 return;
Douglas Gregor22103e32012-11-30 21:58:49 +0000222
223 if (IncludeLoc.isValid())
224 emitIncludeStackRecursively(IncludeLoc, SM);
225 else {
Douglas Gregor63365432012-11-30 22:11:57 +0000226 emitModuleBuildStack(SM);
Douglas Gregor22103e32012-11-30 21:58:49 +0000227 emitImportStack(Loc, SM);
228 }
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000229}
230
231/// \brief Helper to recursivly walk up the include stack and print each layer
232/// on the way back down.
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000233void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc,
234 const SourceManager &SM) {
Douglas Gregoraf8f0262012-11-30 18:38:50 +0000235 if (Loc.isInvalid()) {
Douglas Gregor63365432012-11-30 22:11:57 +0000236 emitModuleBuildStack(SM);
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000237 return;
Douglas Gregoraf8f0262012-11-30 18:38:50 +0000238 }
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000239
Richard Smith0b50cb72012-11-14 23:55:25 +0000240 PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000241 if (PLoc.isInvalid())
242 return;
Douglas Gregor22103e32012-11-30 21:58:49 +0000243
244 // If this source location was imported from a module, print the module
245 // import stack rather than the
246 // FIXME: We want submodule granularity here.
247 std::pair<SourceLocation, StringRef> Imported = SM.getModuleImportLoc(Loc);
Richard Smitha24ff552015-08-11 00:05:21 +0000248 if (!Imported.second.empty()) {
Douglas Gregor22103e32012-11-30 21:58:49 +0000249 // This location was imported by a module. Emit the module import stack.
250 emitImportStackRecursively(Imported.first, Imported.second, SM);
251 return;
252 }
253
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000254 // Emit the other include frames first.
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000255 emitIncludeStackRecursively(PLoc.getIncludeLoc(), SM);
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000256
257 // Emit the inclusion text/note.
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000258 emitIncludeLocation(Loc, PLoc, SM);
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000259}
260
Douglas Gregor22103e32012-11-30 21:58:49 +0000261/// \brief Emit the module import stack associated with the current location.
262void DiagnosticRenderer::emitImportStack(SourceLocation Loc,
263 const SourceManager &SM) {
264 if (Loc.isInvalid()) {
Douglas Gregor63365432012-11-30 22:11:57 +0000265 emitModuleBuildStack(SM);
Douglas Gregor22103e32012-11-30 21:58:49 +0000266 return;
267 }
268
269 std::pair<SourceLocation, StringRef> NextImportLoc
270 = SM.getModuleImportLoc(Loc);
271 emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
272}
273
274/// \brief Helper to recursivly walk up the import stack and print each layer
275/// on the way back down.
276void DiagnosticRenderer::emitImportStackRecursively(SourceLocation Loc,
277 StringRef ModuleName,
278 const SourceManager &SM) {
Richard Smitha24ff552015-08-11 00:05:21 +0000279 if (ModuleName.empty()) {
Douglas Gregor22103e32012-11-30 21:58:49 +0000280 return;
281 }
282
283 PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
Douglas Gregor22103e32012-11-30 21:58:49 +0000284
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 Gregor63365432012-11-30 22:11:57 +0000294/// \brief Emit the module build stack, for cases where a module is (re-)built
Douglas Gregoraf8f0262012-11-30 18:38:50 +0000295/// on demand.
Douglas Gregor63365432012-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 Gregoraf8f0262012-11-30 18:38:50 +0000301 emitBuildingModuleLocation(CurLoc,
302 CurSM.getPresumedLoc(CurLoc,
303 DiagOpts->ShowPresumedLoc),
Douglas Gregor63365432012-11-30 22:11:57 +0000304 Stack[I].first,
Douglas Gregoraf8f0262012-11-30 18:38:50 +0000305 CurSM);
306 }
307}
308
Richard Trieuc3096242015-09-24 01:21:01 +0000309/// A recursive function to trace all possible backtrace locations
310/// to match the \p CaretLocFileID.
Reid Klecknerda30cff2015-12-08 01:08:09 +0000311static SourceLocation
312retrieveMacroLocation(SourceLocation Loc, FileID MacroFileID,
313 FileID CaretFileID,
314 const SmallVectorImpl<FileID> &CommonArgExpansions,
315 bool IsBegin, const SourceManager *SM) {
316 assert(SM->getFileID(Loc) == MacroFileID);
317 if (MacroFileID == CaretFileID)
318 return Loc;
319 if (!Loc.isMacroID())
320 return SourceLocation();
Richard Trieuc3096242015-09-24 01:21:01 +0000321
322 SourceLocation MacroLocation, MacroArgLocation;
323
324 if (SM->isMacroArgExpansion(Loc)) {
Reid Klecknerda30cff2015-12-08 01:08:09 +0000325 // Only look at the immediate spelling location of this macro argument if
326 // the other location in the source range is also present in that expansion.
327 if (std::binary_search(CommonArgExpansions.begin(),
328 CommonArgExpansions.end(), MacroFileID))
329 MacroLocation = SM->getImmediateSpellingLoc(Loc);
330 MacroArgLocation = IsBegin ? SM->getImmediateExpansionRange(Loc).first
331 : SM->getImmediateExpansionRange(Loc).second;
Richard Trieuc3096242015-09-24 01:21:01 +0000332 } else {
Reid Klecknerda30cff2015-12-08 01:08:09 +0000333 MacroLocation = IsBegin ? SM->getImmediateExpansionRange(Loc).first
334 : SM->getImmediateExpansionRange(Loc).second;
Richard Trieuc3096242015-09-24 01:21:01 +0000335 MacroArgLocation = SM->getImmediateSpellingLoc(Loc);
336 }
337
Reid Klecknerda30cff2015-12-08 01:08:09 +0000338 if (MacroLocation.isValid()) {
339 MacroFileID = SM->getFileID(MacroLocation);
340 MacroLocation =
341 retrieveMacroLocation(MacroLocation, MacroFileID, CaretFileID,
342 CommonArgExpansions, IsBegin, SM);
343 if (MacroLocation.isValid())
344 return MacroLocation;
345 }
Richard Trieuc3096242015-09-24 01:21:01 +0000346
347 MacroFileID = SM->getFileID(MacroArgLocation);
348 return retrieveMacroLocation(MacroArgLocation, MacroFileID, CaretFileID,
Reid Klecknerda30cff2015-12-08 01:08:09 +0000349 CommonArgExpansions, IsBegin, SM);
350}
351
352/// Walk up the chain of macro expansions and collect the FileIDs identifying the
353/// expansions.
354static void getMacroArgExpansionFileIDs(SourceLocation Loc,
355 SmallVectorImpl<FileID> &IDs,
356 bool IsBegin, const SourceManager *SM) {
357 while (Loc.isMacroID()) {
358 if (SM->isMacroArgExpansion(Loc)) {
359 IDs.push_back(SM->getFileID(Loc));
360 Loc = SM->getImmediateSpellingLoc(Loc);
361 } else {
362 auto ExpRange = SM->getImmediateExpansionRange(Loc);
363 Loc = IsBegin ? ExpRange.first : ExpRange.second;
364 }
365 }
366}
367
368/// Collect the expansions of the begin and end locations and compute the set
369/// intersection. Produces a sorted vector of FileIDs in CommonArgExpansions.
370static void computeCommonMacroArgExpansionFileIDs(
371 SourceLocation Begin, SourceLocation End, const SourceManager *SM,
372 SmallVectorImpl<FileID> &CommonArgExpansions) {
373 SmallVector<FileID, 4> BeginArgExpansions;
374 SmallVector<FileID, 4> EndArgExpansions;
375 getMacroArgExpansionFileIDs(Begin, BeginArgExpansions, /*IsBegin=*/true, SM);
376 getMacroArgExpansionFileIDs(End, EndArgExpansions, /*IsBegin=*/false, SM);
377 std::sort(BeginArgExpansions.begin(), BeginArgExpansions.end());
378 std::sort(EndArgExpansions.begin(), EndArgExpansions.end());
379 std::set_intersection(BeginArgExpansions.begin(), BeginArgExpansions.end(),
380 EndArgExpansions.begin(), EndArgExpansions.end(),
381 std::back_inserter(CommonArgExpansions));
Richard Trieuc3096242015-09-24 01:21:01 +0000382}
383
Eli Friedman34ff0ea2012-11-03 03:36:51 +0000384// Helper function to fix up source ranges. It takes in an array of ranges,
385// and outputs an array of ranges where we want to draw the range highlighting
386// around the location specified by CaretLoc.
387//
388// To find locations which correspond to the caret, we crawl the macro caller
389// chain for the beginning and end of each range. If the caret location
390// is in a macro expansion, we search each chain for a location
391// in the same expansion as the caret; otherwise, we crawl to the top of
392// each chain. Two locations are part of the same macro expansion
393// iff the FileID is the same.
394static void mapDiagnosticRanges(
395 SourceLocation CaretLoc,
Richard Smithaebee682012-12-05 06:20:58 +0000396 ArrayRef<CharSourceRange> Ranges,
Richard Smithc01cca22012-12-05 09:47:49 +0000397 SmallVectorImpl<CharSourceRange> &SpellingRanges,
Eli Friedman34ff0ea2012-11-03 03:36:51 +0000398 const SourceManager *SM) {
399 FileID CaretLocFileID = SM->getFileID(CaretLoc);
400
Richard Trieuc3096242015-09-24 01:21:01 +0000401 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
402 if (I->isInvalid()) continue;
403
Eli Friedman34ff0ea2012-11-03 03:36:51 +0000404 SourceLocation Begin = I->getBegin(), End = I->getEnd();
405 bool IsTokenRange = I->isTokenRange();
406
Eli Friedmandea98de2012-11-30 06:19:40 +0000407 FileID BeginFileID = SM->getFileID(Begin);
Nadav Rotemb8937342012-12-13 19:58:10 +0000408 FileID EndFileID = SM->getFileID(End);
Eli Friedmandea98de2012-11-30 06:19:40 +0000409
Nadav Rotemb8937342012-12-13 19:58:10 +0000410 // Find the common parent for the beginning and end of the range.
411
412 // First, crawl the expansion chain for the beginning of the range.
413 llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap;
414 while (Begin.isMacroID() && BeginFileID != EndFileID) {
415 BeginLocsMap[BeginFileID] = Begin;
416 Begin = SM->getImmediateExpansionRange(Begin).first;
417 BeginFileID = SM->getFileID(Begin);
418 }
419
420 // Then, crawl the expansion chain for the end of the range.
421 if (BeginFileID != EndFileID) {
422 while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) {
Eli Friedman34ff0ea2012-11-03 03:36:51 +0000423 End = SM->getImmediateExpansionRange(End).second;
Nadav Rotemb8937342012-12-13 19:58:10 +0000424 EndFileID = SM->getFileID(End);
425 }
426 if (End.isMacroID()) {
427 Begin = BeginLocsMap[EndFileID];
428 BeginFileID = EndFileID;
Eli Friedman34ff0ea2012-11-03 03:36:51 +0000429 }
Eli Friedmancdb135a2012-12-13 00:14:59 +0000430 }
431
Richard Trieuc3096242015-09-24 01:21:01 +0000432 // Do the backtracking.
Reid Klecknerda30cff2015-12-08 01:08:09 +0000433 SmallVector<FileID, 4> CommonArgExpansions;
434 computeCommonMacroArgExpansionFileIDs(Begin, End, SM, CommonArgExpansions);
Richard Trieuc3096242015-09-24 01:21:01 +0000435 Begin = retrieveMacroLocation(Begin, BeginFileID, CaretLocFileID,
Reid Klecknerda30cff2015-12-08 01:08:09 +0000436 CommonArgExpansions, /*IsBegin=*/true, SM);
Richard Trieuc3096242015-09-24 01:21:01 +0000437 End = retrieveMacroLocation(End, BeginFileID, CaretLocFileID,
Reid Klecknerda30cff2015-12-08 01:08:09 +0000438 CommonArgExpansions, /*IsBegin=*/false, SM);
Richard Trieuc3096242015-09-24 01:21:01 +0000439 if (Begin.isInvalid() || End.isInvalid()) continue;
Eli Friedman34ff0ea2012-11-03 03:36:51 +0000440
441 // Return the spelling location of the beginning and end of the range.
442 Begin = SM->getSpellingLoc(Begin);
443 End = SM->getSpellingLoc(End);
Richard Trieuc3096242015-09-24 01:21:01 +0000444
Eli Friedman34ff0ea2012-11-03 03:36:51 +0000445 SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End),
446 IsTokenRange));
447 }
448}
449
Richard Smithaebee682012-12-05 06:20:58 +0000450void DiagnosticRenderer::emitCaret(SourceLocation Loc,
451 DiagnosticsEngine::Level Level,
452 ArrayRef<CharSourceRange> Ranges,
453 ArrayRef<FixItHint> Hints,
454 const SourceManager &SM) {
455 SmallVector<CharSourceRange, 4> SpellingRanges;
456 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
457 emitCodeContext(Loc, Level, SpellingRanges, Hints, SM);
458}
459
Richard Trieu97c45b62015-07-28 20:53:46 +0000460/// \brief A helper function for emitMacroExpansion to print the
461/// macro expansion message
462void DiagnosticRenderer::emitSingleMacroExpansion(
463 SourceLocation Loc,
464 DiagnosticsEngine::Level Level,
465 ArrayRef<CharSourceRange> Ranges,
466 const SourceManager &SM) {
Richard Smith7a2d40d2012-12-05 03:18:16 +0000467 // Find the spelling location for the macro definition. We must use the
Richard Trieu97c45b62015-07-28 20:53:46 +0000468 // spelling location here to avoid emitting a macro backtrace for the note.
Richard Trieua1d7ece2015-08-27 23:38:45 +0000469 SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
Richard Smith7a2d40d2012-12-05 03:18:16 +0000470
471 // Map the ranges into the FileID of the diagnostic location.
Eli Friedman34ff0ea2012-11-03 03:36:51 +0000472 SmallVector<CharSourceRange, 4> SpellingRanges;
Richard Smith7a2d40d2012-12-05 03:18:16 +0000473 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
Eli Friedman34ff0ea2012-11-03 03:36:51 +0000474
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000475 SmallString<100> MessageStorage;
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000476 llvm::raw_svector_ostream Message(MessageStorage);
Richard Smithf89e2e22012-12-05 11:04:55 +0000477 StringRef MacroName = getImmediateMacroName(Loc, SM, LangOpts);
478 if (MacroName.empty())
479 Message << "expanded from here";
480 else
481 Message << "expanded from macro '" << MacroName << "'";
Richard Trieu97c45b62015-07-28 20:53:46 +0000482
Dmitri Gribenko44ebbd52013-05-05 00:41:58 +0000483 emitDiagnostic(SpellingLoc, DiagnosticsEngine::Note, Message.str(),
484 SpellingRanges, None, &SM);
Ted Kremenekc4bbd852011-12-17 05:26:04 +0000485}
486
Richard Trieuc3096242015-09-24 01:21:01 +0000487/// Check that the macro argument location of Loc starts with ArgumentLoc.
488/// The starting location of the macro expansions is used to differeniate
489/// different macro expansions.
490static bool checkLocForMacroArgExpansion(SourceLocation Loc,
491 const SourceManager &SM,
492 SourceLocation ArgumentLoc) {
493 SourceLocation MacroLoc;
494 if (SM.isMacroArgExpansion(Loc, &MacroLoc)) {
495 if (ArgumentLoc == MacroLoc) return true;
496 }
497
498 return false;
499}
500
501/// Check if all the locations in the range have the same macro argument
502/// expansion, and that that expansion starts with ArgumentLoc.
Richard Trieuecd36ee2015-08-12 18:24:59 +0000503static bool checkRangeForMacroArgExpansion(CharSourceRange Range,
Richard Trieuc3096242015-09-24 01:21:01 +0000504 const SourceManager &SM,
505 SourceLocation ArgumentLoc) {
Richard Trieuecd36ee2015-08-12 18:24:59 +0000506 SourceLocation BegLoc = Range.getBegin(), EndLoc = Range.getEnd();
507 while (BegLoc != EndLoc) {
Richard Trieuc3096242015-09-24 01:21:01 +0000508 if (!checkLocForMacroArgExpansion(BegLoc, SM, ArgumentLoc))
Richard Trieuecd36ee2015-08-12 18:24:59 +0000509 return false;
510 BegLoc.getLocWithOffset(1);
511 }
512
Richard Trieuc3096242015-09-24 01:21:01 +0000513 return checkLocForMacroArgExpansion(BegLoc, SM, ArgumentLoc);
Richard Trieuecd36ee2015-08-12 18:24:59 +0000514}
515
Richard Trieuc3096242015-09-24 01:21:01 +0000516/// A helper function to check if the current ranges are all inside the same
517/// macro argument expansion as Loc.
Richard Trieuecd36ee2015-08-12 18:24:59 +0000518static bool checkRangesForMacroArgExpansion(SourceLocation Loc,
519 ArrayRef<CharSourceRange> Ranges,
520 const SourceManager &SM) {
521 assert(Loc.isMacroID() && "Must be a macro expansion!");
522
523 SmallVector<CharSourceRange, 4> SpellingRanges;
524 mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
525
Richard Trieuc3096242015-09-24 01:21:01 +0000526 /// Count all valid ranges.
527 unsigned ValidCount = 0;
528 for (auto I : Ranges)
529 if (I.isValid()) ValidCount++;
530
531 if (ValidCount > SpellingRanges.size())
Richard Trieuecd36ee2015-08-12 18:24:59 +0000532 return false;
533
Richard Trieuc3096242015-09-24 01:21:01 +0000534 /// To store the source location of the argument location.
535 SourceLocation ArgumentLoc;
536
537 /// Set the ArgumentLoc to the beginning location of the expansion of Loc
538 /// so to check if the ranges expands to the same beginning location.
539 if (!SM.isMacroArgExpansion(Loc,&ArgumentLoc))
540 return false;
541
542 for (auto I = SpellingRanges.begin(), E = SpellingRanges.end(); I != E; ++I) {
543 if (!checkRangeForMacroArgExpansion(*I, SM, ArgumentLoc))
Richard Trieuecd36ee2015-08-12 18:24:59 +0000544 return false;
Richard Trieuc3096242015-09-24 01:21:01 +0000545 }
Richard Trieuecd36ee2015-08-12 18:24:59 +0000546
547 return true;
548}
549
Richard Trieu97c45b62015-07-28 20:53:46 +0000550/// \brief Recursively emit notes for each macro expansion and caret
551/// diagnostics where appropriate.
552///
553/// Walks up the macro expansion stack printing expansion notes, the code
554/// snippet, caret, underlines and FixItHint display as appropriate at each
555/// level.
556///
557/// \param Loc The location for this caret.
558/// \param Level The diagnostic level currently being emitted.
559/// \param Ranges The underlined ranges for this code snippet.
560/// \param Hints The FixIt hints active for this diagnostic.
561void DiagnosticRenderer::emitMacroExpansions(SourceLocation Loc,
562 DiagnosticsEngine::Level Level,
563 ArrayRef<CharSourceRange> Ranges,
564 ArrayRef<FixItHint> Hints,
565 const SourceManager &SM) {
Yaron Kerened1fe5d2015-10-03 05:15:57 +0000566 assert(Loc.isValid() && "must have a valid source location here");
Richard Trieu97c45b62015-07-28 20:53:46 +0000567
568 // Produce a stack of macro backtraces.
569 SmallVector<SourceLocation, 8> LocationStack;
Richard Trieuecd36ee2015-08-12 18:24:59 +0000570 unsigned IgnoredEnd = 0;
Richard Trieu97c45b62015-07-28 20:53:46 +0000571 while (Loc.isMacroID()) {
Richard Trieua1d7ece2015-08-27 23:38:45 +0000572 // If this is the expansion of a macro argument, point the caret at the
573 // use of the argument in the definition of the macro, not the expansion.
574 if (SM.isMacroArgExpansion(Loc))
575 LocationStack.push_back(SM.getImmediateExpansionRange(Loc).first);
576 else
577 LocationStack.push_back(Loc);
578
Richard Trieuecd36ee2015-08-12 18:24:59 +0000579 if (checkRangesForMacroArgExpansion(Loc, Ranges, SM))
580 IgnoredEnd = LocationStack.size();
581
Richard Trieu97c45b62015-07-28 20:53:46 +0000582 Loc = SM.getImmediateMacroCallerLoc(Loc);
Richard Trieua1d7ece2015-08-27 23:38:45 +0000583
584 // Once the location no longer points into a macro, try stepping through
585 // the last found location. This sometimes produces additional useful
586 // backtraces.
587 if (Loc.isFileID())
588 Loc = SM.getImmediateMacroCallerLoc(LocationStack.back());
Yaron Kerened1fe5d2015-10-03 05:15:57 +0000589 assert(Loc.isValid() && "must have a valid source location here");
Richard Trieu97c45b62015-07-28 20:53:46 +0000590 }
591
Richard Trieuecd36ee2015-08-12 18:24:59 +0000592 LocationStack.erase(LocationStack.begin(),
593 LocationStack.begin() + IgnoredEnd);
594
Richard Trieu97c45b62015-07-28 20:53:46 +0000595 unsigned MacroDepth = LocationStack.size();
596 unsigned MacroLimit = DiagOpts->MacroBacktraceLimit;
597 if (MacroDepth <= MacroLimit || MacroLimit == 0) {
598 for (auto I = LocationStack.rbegin(), E = LocationStack.rend();
599 I != E; ++I)
600 emitSingleMacroExpansion(*I, Level, Ranges, SM);
601 return;
602 }
603
604 unsigned MacroStartMessages = MacroLimit / 2;
605 unsigned MacroEndMessages = MacroLimit / 2 + MacroLimit % 2;
606
607 for (auto I = LocationStack.rbegin(),
608 E = LocationStack.rbegin() + MacroStartMessages;
609 I != E; ++I)
610 emitSingleMacroExpansion(*I, Level, Ranges, SM);
611
612 SmallString<200> MessageStorage;
613 llvm::raw_svector_ostream Message(MessageStorage);
614 Message << "(skipping " << (MacroDepth - MacroLimit)
615 << " expansions in backtrace; use -fmacro-backtrace-limit=0 to "
616 "see all)";
617 emitBasicNote(Message.str());
618
619 for (auto I = LocationStack.rend() - MacroEndMessages,
620 E = LocationStack.rend();
621 I != E; ++I)
622 emitSingleMacroExpansion(*I, Level, Ranges, SM);
623}
624
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000625DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {}
Ted Kremenek0964cca2012-02-14 02:46:00 +0000626
627void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc,
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000628 PresumedLoc PLoc,
629 const SourceManager &SM) {
Ted Kremenek0964cca2012-02-14 02:46:00 +0000630 // Generate a note indicating the include location.
631 SmallString<200> MessageStorage;
632 llvm::raw_svector_ostream Message(MessageStorage);
633 Message << "in file included from " << PLoc.getFilename() << ':'
634 << PLoc.getLine() << ":";
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000635 emitNote(Loc, Message.str(), &SM);
Ted Kremenek0964cca2012-02-14 02:46:00 +0000636}
637
Douglas Gregor22103e32012-11-30 21:58:49 +0000638void DiagnosticNoteRenderer::emitImportLocation(SourceLocation Loc,
639 PresumedLoc PLoc,
640 StringRef ModuleName,
641 const SourceManager &SM) {
642 // Generate a note indicating the include location.
643 SmallString<200> MessageStorage;
644 llvm::raw_svector_ostream Message(MessageStorage);
Richard Smitha24ff552015-08-11 00:05:21 +0000645 Message << "in module '" << ModuleName;
Yaron Kerened1fe5d2015-10-03 05:15:57 +0000646 if (PLoc.isValid())
Richard Smitha24ff552015-08-11 00:05:21 +0000647 Message << "' imported from " << PLoc.getFilename() << ':'
648 << PLoc.getLine();
649 Message << ":";
Douglas Gregor22103e32012-11-30 21:58:49 +0000650 emitNote(Loc, Message.str(), &SM);
651}
652
Douglas Gregoraf8f0262012-11-30 18:38:50 +0000653void
654DiagnosticNoteRenderer::emitBuildingModuleLocation(SourceLocation Loc,
655 PresumedLoc PLoc,
656 StringRef ModuleName,
657 const SourceManager &SM) {
658 // Generate a note indicating the include location.
659 SmallString<200> MessageStorage;
660 llvm::raw_svector_ostream Message(MessageStorage);
Richard Smith7bea1d42014-03-05 20:55:36 +0000661 if (PLoc.getFilename())
662 Message << "while building module '" << ModuleName << "' imported from "
663 << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
664 else
Jordan Rose6dcdaa62014-07-26 01:22:02 +0000665 Message << "while building module '" << ModuleName << "':";
Douglas Gregoraf8f0262012-11-30 18:38:50 +0000666 emitNote(Loc, Message.str(), &SM);
667}