1f4a2713aSLionel Sambuc //===--- DiagnosticRenderer.cpp - Diagnostic Pretty-Printing --------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "clang/Frontend/DiagnosticRenderer.h"
11f4a2713aSLionel Sambuc #include "clang/Basic/DiagnosticOptions.h"
12f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
13f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
14f4a2713aSLionel Sambuc #include "clang/Edit/Commit.h"
15f4a2713aSLionel Sambuc #include "clang/Edit/EditedSource.h"
16f4a2713aSLionel Sambuc #include "clang/Edit/EditsReceiver.h"
17f4a2713aSLionel Sambuc #include "clang/Lex/Lexer.h"
18f4a2713aSLionel Sambuc #include "llvm/ADT/SmallSet.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
23f4a2713aSLionel Sambuc #include <algorithm>
24f4a2713aSLionel Sambuc using namespace clang;
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc /// \brief Retrieve the name of the immediate macro expansion.
27f4a2713aSLionel Sambuc ///
28f4a2713aSLionel Sambuc /// This routine starts from a source location, and finds the name of the macro
29f4a2713aSLionel Sambuc /// responsible for its immediate expansion. It looks through any intervening
30f4a2713aSLionel Sambuc /// macro argument expansions to compute this. It returns a StringRef which
31f4a2713aSLionel Sambuc /// refers to the SourceManager-owned buffer of the source where that macro
32f4a2713aSLionel Sambuc /// name is spelled. Thus, the result shouldn't out-live that SourceManager.
33f4a2713aSLionel Sambuc ///
34f4a2713aSLionel Sambuc /// This differs from Lexer::getImmediateMacroName in that any macro argument
35f4a2713aSLionel Sambuc /// location will result in the topmost function macro that accepted it.
36f4a2713aSLionel Sambuc /// e.g.
37f4a2713aSLionel Sambuc /// \code
38f4a2713aSLionel Sambuc ///   MAC1( MAC2(foo) )
39f4a2713aSLionel Sambuc /// \endcode
40f4a2713aSLionel Sambuc /// for location of 'foo' token, this function will return "MAC1" while
41f4a2713aSLionel Sambuc /// Lexer::getImmediateMacroName will return "MAC2".
getImmediateMacroName(SourceLocation Loc,const SourceManager & SM,const LangOptions & LangOpts)42f4a2713aSLionel Sambuc static StringRef getImmediateMacroName(SourceLocation Loc,
43f4a2713aSLionel Sambuc                                        const SourceManager &SM,
44f4a2713aSLionel Sambuc                                        const LangOptions &LangOpts) {
45f4a2713aSLionel Sambuc    assert(Loc.isMacroID() && "Only reasonble to call this on macros");
46f4a2713aSLionel Sambuc    // Walk past macro argument expanions.
47f4a2713aSLionel Sambuc    while (SM.isMacroArgExpansion(Loc))
48f4a2713aSLionel Sambuc      Loc = SM.getImmediateExpansionRange(Loc).first;
49f4a2713aSLionel Sambuc 
50f4a2713aSLionel Sambuc    // If the macro's spelling has no FileID, then it's actually a token paste
51f4a2713aSLionel Sambuc    // or stringization (or similar) and not a macro at all.
52f4a2713aSLionel Sambuc    if (!SM.getFileEntryForID(SM.getFileID(SM.getSpellingLoc(Loc))))
53f4a2713aSLionel Sambuc      return StringRef();
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc    // Find the spelling location of the start of the non-argument expansion
56f4a2713aSLionel Sambuc    // range. This is where the macro name was spelled in order to begin
57f4a2713aSLionel Sambuc    // expanding this macro.
58f4a2713aSLionel Sambuc    Loc = SM.getSpellingLoc(SM.getImmediateExpansionRange(Loc).first);
59f4a2713aSLionel Sambuc 
60f4a2713aSLionel Sambuc    // Dig out the buffer where the macro name was spelled and the extents of the
61f4a2713aSLionel Sambuc    // name so that we can render it into the expansion note.
62f4a2713aSLionel Sambuc    std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc);
63f4a2713aSLionel Sambuc    unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts);
64f4a2713aSLionel Sambuc    StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first);
65f4a2713aSLionel Sambuc    return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength);
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc 
DiagnosticRenderer(const LangOptions & LangOpts,DiagnosticOptions * DiagOpts)68f4a2713aSLionel Sambuc DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts,
69f4a2713aSLionel Sambuc                                        DiagnosticOptions *DiagOpts)
70f4a2713aSLionel Sambuc   : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {}
71f4a2713aSLionel Sambuc 
~DiagnosticRenderer()72f4a2713aSLionel Sambuc DiagnosticRenderer::~DiagnosticRenderer() {}
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc namespace {
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc class FixitReceiver : public edit::EditsReceiver {
77f4a2713aSLionel Sambuc   SmallVectorImpl<FixItHint> &MergedFixits;
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc public:
FixitReceiver(SmallVectorImpl<FixItHint> & MergedFixits)80f4a2713aSLionel Sambuc   FixitReceiver(SmallVectorImpl<FixItHint> &MergedFixits)
81f4a2713aSLionel Sambuc     : MergedFixits(MergedFixits) { }
insert(SourceLocation loc,StringRef text)82*0a6a1f1dSLionel Sambuc   void insert(SourceLocation loc, StringRef text) override {
83f4a2713aSLionel Sambuc     MergedFixits.push_back(FixItHint::CreateInsertion(loc, text));
84f4a2713aSLionel Sambuc   }
replace(CharSourceRange range,StringRef text)85*0a6a1f1dSLionel Sambuc   void replace(CharSourceRange range, StringRef text) override {
86f4a2713aSLionel Sambuc     MergedFixits.push_back(FixItHint::CreateReplacement(range, text));
87f4a2713aSLionel Sambuc   }
88f4a2713aSLionel Sambuc };
89f4a2713aSLionel Sambuc 
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc 
mergeFixits(ArrayRef<FixItHint> FixItHints,const SourceManager & SM,const LangOptions & LangOpts,SmallVectorImpl<FixItHint> & MergedFixits)92f4a2713aSLionel Sambuc static void mergeFixits(ArrayRef<FixItHint> FixItHints,
93f4a2713aSLionel Sambuc                         const SourceManager &SM, const LangOptions &LangOpts,
94f4a2713aSLionel Sambuc                         SmallVectorImpl<FixItHint> &MergedFixits) {
95f4a2713aSLionel Sambuc   edit::Commit commit(SM, LangOpts);
96f4a2713aSLionel Sambuc   for (ArrayRef<FixItHint>::const_iterator
97f4a2713aSLionel Sambuc          I = FixItHints.begin(), E = FixItHints.end(); I != E; ++I) {
98f4a2713aSLionel Sambuc     const FixItHint &Hint = *I;
99f4a2713aSLionel Sambuc     if (Hint.CodeToInsert.empty()) {
100f4a2713aSLionel Sambuc       if (Hint.InsertFromRange.isValid())
101f4a2713aSLionel Sambuc         commit.insertFromRange(Hint.RemoveRange.getBegin(),
102f4a2713aSLionel Sambuc                            Hint.InsertFromRange, /*afterToken=*/false,
103f4a2713aSLionel Sambuc                            Hint.BeforePreviousInsertions);
104f4a2713aSLionel Sambuc       else
105f4a2713aSLionel Sambuc         commit.remove(Hint.RemoveRange);
106f4a2713aSLionel Sambuc     } else {
107f4a2713aSLionel Sambuc       if (Hint.RemoveRange.isTokenRange() ||
108f4a2713aSLionel Sambuc           Hint.RemoveRange.getBegin() != Hint.RemoveRange.getEnd())
109f4a2713aSLionel Sambuc         commit.replace(Hint.RemoveRange, Hint.CodeToInsert);
110f4a2713aSLionel Sambuc       else
111f4a2713aSLionel Sambuc         commit.insert(Hint.RemoveRange.getBegin(), Hint.CodeToInsert,
112f4a2713aSLionel Sambuc                     /*afterToken=*/false, Hint.BeforePreviousInsertions);
113f4a2713aSLionel Sambuc     }
114f4a2713aSLionel Sambuc   }
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc   edit::EditedSource Editor(SM, LangOpts);
117f4a2713aSLionel Sambuc   if (Editor.commit(commit)) {
118f4a2713aSLionel Sambuc     FixitReceiver Rec(MergedFixits);
119f4a2713aSLionel Sambuc     Editor.applyRewrites(Rec);
120f4a2713aSLionel Sambuc   }
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc 
emitDiagnostic(SourceLocation Loc,DiagnosticsEngine::Level Level,StringRef Message,ArrayRef<CharSourceRange> Ranges,ArrayRef<FixItHint> FixItHints,const SourceManager * SM,DiagOrStoredDiag D)123f4a2713aSLionel Sambuc void DiagnosticRenderer::emitDiagnostic(SourceLocation Loc,
124f4a2713aSLionel Sambuc                                         DiagnosticsEngine::Level Level,
125f4a2713aSLionel Sambuc                                         StringRef Message,
126f4a2713aSLionel Sambuc                                         ArrayRef<CharSourceRange> Ranges,
127f4a2713aSLionel Sambuc                                         ArrayRef<FixItHint> FixItHints,
128f4a2713aSLionel Sambuc                                         const SourceManager *SM,
129f4a2713aSLionel Sambuc                                         DiagOrStoredDiag D) {
130f4a2713aSLionel Sambuc   assert(SM || Loc.isInvalid());
131f4a2713aSLionel Sambuc 
132f4a2713aSLionel Sambuc   beginDiagnostic(D, Level);
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc   if (!Loc.isValid())
135f4a2713aSLionel Sambuc     // If we have no source location, just emit the diagnostic message.
136f4a2713aSLionel Sambuc     emitDiagnosticMessage(Loc, PresumedLoc(), Level, Message, Ranges, SM, D);
137f4a2713aSLionel Sambuc   else {
138f4a2713aSLionel Sambuc     // Get the ranges into a local array we can hack on.
139f4a2713aSLionel Sambuc     SmallVector<CharSourceRange, 20> MutableRanges(Ranges.begin(),
140f4a2713aSLionel Sambuc                                                    Ranges.end());
141f4a2713aSLionel Sambuc 
142f4a2713aSLionel Sambuc     SmallVector<FixItHint, 8> MergedFixits;
143f4a2713aSLionel Sambuc     if (!FixItHints.empty()) {
144f4a2713aSLionel Sambuc       mergeFixits(FixItHints, *SM, LangOpts, MergedFixits);
145f4a2713aSLionel Sambuc       FixItHints = MergedFixits;
146f4a2713aSLionel Sambuc     }
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc     for (ArrayRef<FixItHint>::const_iterator I = FixItHints.begin(),
149f4a2713aSLionel Sambuc          E = FixItHints.end();
150f4a2713aSLionel Sambuc          I != E; ++I)
151f4a2713aSLionel Sambuc       if (I->RemoveRange.isValid())
152f4a2713aSLionel Sambuc         MutableRanges.push_back(I->RemoveRange);
153f4a2713aSLionel Sambuc 
154f4a2713aSLionel Sambuc     SourceLocation UnexpandedLoc = Loc;
155f4a2713aSLionel Sambuc 
156f4a2713aSLionel Sambuc     // Find the ultimate expansion location for the diagnostic.
157f4a2713aSLionel Sambuc     Loc = SM->getFileLoc(Loc);
158f4a2713aSLionel Sambuc 
159f4a2713aSLionel Sambuc     PresumedLoc PLoc = SM->getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
160f4a2713aSLionel Sambuc 
161f4a2713aSLionel Sambuc     // First, if this diagnostic is not in the main file, print out the
162f4a2713aSLionel Sambuc     // "included from" lines.
163f4a2713aSLionel Sambuc     emitIncludeStack(Loc, PLoc, Level, *SM);
164f4a2713aSLionel Sambuc 
165f4a2713aSLionel Sambuc     // Next, emit the actual diagnostic message and caret.
166f4a2713aSLionel Sambuc     emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, SM, D);
167f4a2713aSLionel Sambuc     emitCaret(Loc, Level, MutableRanges, FixItHints, *SM);
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc     // If this location is within a macro, walk from UnexpandedLoc up to Loc
170f4a2713aSLionel Sambuc     // and produce a macro backtrace.
171f4a2713aSLionel Sambuc     if (UnexpandedLoc.isValid() && UnexpandedLoc.isMacroID()) {
172f4a2713aSLionel Sambuc       unsigned MacroDepth = 0;
173f4a2713aSLionel Sambuc       emitMacroExpansions(UnexpandedLoc, Level, MutableRanges, FixItHints, *SM,
174f4a2713aSLionel Sambuc                           MacroDepth);
175f4a2713aSLionel Sambuc     }
176f4a2713aSLionel Sambuc   }
177f4a2713aSLionel Sambuc 
178f4a2713aSLionel Sambuc   LastLoc = Loc;
179f4a2713aSLionel Sambuc   LastLevel = Level;
180f4a2713aSLionel Sambuc 
181f4a2713aSLionel Sambuc   endDiagnostic(D, Level);
182f4a2713aSLionel Sambuc }
183f4a2713aSLionel Sambuc 
184f4a2713aSLionel Sambuc 
emitStoredDiagnostic(StoredDiagnostic & Diag)185f4a2713aSLionel Sambuc void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) {
186f4a2713aSLionel Sambuc   emitDiagnostic(Diag.getLocation(), Diag.getLevel(), Diag.getMessage(),
187f4a2713aSLionel Sambuc                  Diag.getRanges(), Diag.getFixIts(),
188f4a2713aSLionel Sambuc                  Diag.getLocation().isValid() ? &Diag.getLocation().getManager()
189*0a6a1f1dSLionel Sambuc                                               : nullptr,
190f4a2713aSLionel Sambuc                  &Diag);
191f4a2713aSLionel Sambuc }
192f4a2713aSLionel Sambuc 
emitBasicNote(StringRef Message)193*0a6a1f1dSLionel Sambuc void DiagnosticRenderer::emitBasicNote(StringRef Message) {
194*0a6a1f1dSLionel Sambuc   emitDiagnosticMessage(
195*0a6a1f1dSLionel Sambuc       SourceLocation(), PresumedLoc(), DiagnosticsEngine::Note, Message,
196*0a6a1f1dSLionel Sambuc       None, nullptr, DiagOrStoredDiag());
197*0a6a1f1dSLionel Sambuc }
198*0a6a1f1dSLionel Sambuc 
199f4a2713aSLionel Sambuc /// \brief Prints an include stack when appropriate for a particular
200f4a2713aSLionel Sambuc /// diagnostic level and location.
201f4a2713aSLionel Sambuc ///
202f4a2713aSLionel Sambuc /// This routine handles all the logic of suppressing particular include
203f4a2713aSLionel Sambuc /// stacks (such as those for notes) and duplicate include stacks when
204f4a2713aSLionel Sambuc /// repeated warnings occur within the same file. It also handles the logic
205f4a2713aSLionel Sambuc /// of customizing the formatting and display of the include stack.
206f4a2713aSLionel Sambuc ///
207f4a2713aSLionel Sambuc /// \param Loc   The diagnostic location.
208f4a2713aSLionel Sambuc /// \param PLoc  The presumed location of the diagnostic location.
209f4a2713aSLionel Sambuc /// \param Level The diagnostic level of the message this stack pertains to.
emitIncludeStack(SourceLocation Loc,PresumedLoc PLoc,DiagnosticsEngine::Level Level,const SourceManager & SM)210f4a2713aSLionel Sambuc void DiagnosticRenderer::emitIncludeStack(SourceLocation Loc,
211f4a2713aSLionel Sambuc                                           PresumedLoc PLoc,
212f4a2713aSLionel Sambuc                                           DiagnosticsEngine::Level Level,
213f4a2713aSLionel Sambuc                                           const SourceManager &SM) {
214f4a2713aSLionel Sambuc   SourceLocation IncludeLoc = PLoc.getIncludeLoc();
215f4a2713aSLionel Sambuc 
216f4a2713aSLionel Sambuc   // Skip redundant include stacks altogether.
217f4a2713aSLionel Sambuc   if (LastIncludeLoc == IncludeLoc)
218f4a2713aSLionel Sambuc     return;
219f4a2713aSLionel Sambuc 
220f4a2713aSLionel Sambuc   LastIncludeLoc = IncludeLoc;
221f4a2713aSLionel Sambuc 
222f4a2713aSLionel Sambuc   if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
223f4a2713aSLionel Sambuc     return;
224f4a2713aSLionel Sambuc 
225f4a2713aSLionel Sambuc   if (IncludeLoc.isValid())
226f4a2713aSLionel Sambuc     emitIncludeStackRecursively(IncludeLoc, SM);
227f4a2713aSLionel Sambuc   else {
228f4a2713aSLionel Sambuc     emitModuleBuildStack(SM);
229f4a2713aSLionel Sambuc     emitImportStack(Loc, SM);
230f4a2713aSLionel Sambuc   }
231f4a2713aSLionel Sambuc }
232f4a2713aSLionel Sambuc 
233f4a2713aSLionel Sambuc /// \brief Helper to recursivly walk up the include stack and print each layer
234f4a2713aSLionel Sambuc /// on the way back down.
emitIncludeStackRecursively(SourceLocation Loc,const SourceManager & SM)235f4a2713aSLionel Sambuc void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc,
236f4a2713aSLionel Sambuc                                                      const SourceManager &SM) {
237f4a2713aSLionel Sambuc   if (Loc.isInvalid()) {
238f4a2713aSLionel Sambuc     emitModuleBuildStack(SM);
239f4a2713aSLionel Sambuc     return;
240f4a2713aSLionel Sambuc   }
241f4a2713aSLionel Sambuc 
242f4a2713aSLionel Sambuc   PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
243f4a2713aSLionel Sambuc   if (PLoc.isInvalid())
244f4a2713aSLionel Sambuc     return;
245f4a2713aSLionel Sambuc 
246f4a2713aSLionel Sambuc   // If this source location was imported from a module, print the module
247f4a2713aSLionel Sambuc   // import stack rather than the
248f4a2713aSLionel Sambuc   // FIXME: We want submodule granularity here.
249f4a2713aSLionel Sambuc   std::pair<SourceLocation, StringRef> Imported = SM.getModuleImportLoc(Loc);
250f4a2713aSLionel Sambuc   if (Imported.first.isValid()) {
251f4a2713aSLionel Sambuc     // This location was imported by a module. Emit the module import stack.
252f4a2713aSLionel Sambuc     emitImportStackRecursively(Imported.first, Imported.second, SM);
253f4a2713aSLionel Sambuc     return;
254f4a2713aSLionel Sambuc   }
255f4a2713aSLionel Sambuc 
256f4a2713aSLionel Sambuc   // Emit the other include frames first.
257f4a2713aSLionel Sambuc   emitIncludeStackRecursively(PLoc.getIncludeLoc(), SM);
258f4a2713aSLionel Sambuc 
259f4a2713aSLionel Sambuc   // Emit the inclusion text/note.
260f4a2713aSLionel Sambuc   emitIncludeLocation(Loc, PLoc, SM);
261f4a2713aSLionel Sambuc }
262f4a2713aSLionel Sambuc 
263f4a2713aSLionel Sambuc /// \brief Emit the module import stack associated with the current location.
emitImportStack(SourceLocation Loc,const SourceManager & SM)264f4a2713aSLionel Sambuc void DiagnosticRenderer::emitImportStack(SourceLocation Loc,
265f4a2713aSLionel Sambuc                                          const SourceManager &SM) {
266f4a2713aSLionel Sambuc   if (Loc.isInvalid()) {
267f4a2713aSLionel Sambuc     emitModuleBuildStack(SM);
268f4a2713aSLionel Sambuc     return;
269f4a2713aSLionel Sambuc   }
270f4a2713aSLionel Sambuc 
271f4a2713aSLionel Sambuc   std::pair<SourceLocation, StringRef> NextImportLoc
272f4a2713aSLionel Sambuc     = SM.getModuleImportLoc(Loc);
273f4a2713aSLionel Sambuc   emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
274f4a2713aSLionel Sambuc }
275f4a2713aSLionel Sambuc 
276f4a2713aSLionel Sambuc /// \brief Helper to recursivly walk up the import stack and print each layer
277f4a2713aSLionel Sambuc /// on the way back down.
emitImportStackRecursively(SourceLocation Loc,StringRef ModuleName,const SourceManager & SM)278f4a2713aSLionel Sambuc void DiagnosticRenderer::emitImportStackRecursively(SourceLocation Loc,
279f4a2713aSLionel Sambuc                                                     StringRef ModuleName,
280f4a2713aSLionel Sambuc                                                     const SourceManager &SM) {
281f4a2713aSLionel Sambuc   if (Loc.isInvalid()) {
282f4a2713aSLionel Sambuc     return;
283f4a2713aSLionel Sambuc   }
284f4a2713aSLionel Sambuc 
285f4a2713aSLionel Sambuc   PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc);
286f4a2713aSLionel Sambuc   if (PLoc.isInvalid())
287f4a2713aSLionel Sambuc     return;
288f4a2713aSLionel Sambuc 
289f4a2713aSLionel Sambuc   // Emit the other import frames first.
290f4a2713aSLionel Sambuc   std::pair<SourceLocation, StringRef> NextImportLoc
291f4a2713aSLionel Sambuc     = SM.getModuleImportLoc(Loc);
292f4a2713aSLionel Sambuc   emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM);
293f4a2713aSLionel Sambuc 
294f4a2713aSLionel Sambuc   // Emit the inclusion text/note.
295f4a2713aSLionel Sambuc   emitImportLocation(Loc, PLoc, ModuleName, SM);
296f4a2713aSLionel Sambuc }
297f4a2713aSLionel Sambuc 
298f4a2713aSLionel Sambuc /// \brief Emit the module build stack, for cases where a module is (re-)built
299f4a2713aSLionel Sambuc /// on demand.
emitModuleBuildStack(const SourceManager & SM)300f4a2713aSLionel Sambuc void DiagnosticRenderer::emitModuleBuildStack(const SourceManager &SM) {
301f4a2713aSLionel Sambuc   ModuleBuildStack Stack = SM.getModuleBuildStack();
302f4a2713aSLionel Sambuc   for (unsigned I = 0, N = Stack.size(); I != N; ++I) {
303f4a2713aSLionel Sambuc     const SourceManager &CurSM = Stack[I].second.getManager();
304f4a2713aSLionel Sambuc     SourceLocation CurLoc = Stack[I].second;
305f4a2713aSLionel Sambuc     emitBuildingModuleLocation(CurLoc,
306f4a2713aSLionel Sambuc                                CurSM.getPresumedLoc(CurLoc,
307f4a2713aSLionel Sambuc                                                     DiagOpts->ShowPresumedLoc),
308f4a2713aSLionel Sambuc                                Stack[I].first,
309f4a2713aSLionel Sambuc                                CurSM);
310f4a2713aSLionel Sambuc   }
311f4a2713aSLionel Sambuc }
312f4a2713aSLionel Sambuc 
313f4a2713aSLionel Sambuc // Helper function to fix up source ranges.  It takes in an array of ranges,
314f4a2713aSLionel Sambuc // and outputs an array of ranges where we want to draw the range highlighting
315f4a2713aSLionel Sambuc // around the location specified by CaretLoc.
316f4a2713aSLionel Sambuc //
317f4a2713aSLionel Sambuc // To find locations which correspond to the caret, we crawl the macro caller
318f4a2713aSLionel Sambuc // chain for the beginning and end of each range.  If the caret location
319f4a2713aSLionel Sambuc // is in a macro expansion, we search each chain for a location
320f4a2713aSLionel Sambuc // in the same expansion as the caret; otherwise, we crawl to the top of
321f4a2713aSLionel Sambuc // each chain. Two locations are part of the same macro expansion
322f4a2713aSLionel Sambuc // iff the FileID is the same.
mapDiagnosticRanges(SourceLocation CaretLoc,ArrayRef<CharSourceRange> Ranges,SmallVectorImpl<CharSourceRange> & SpellingRanges,const SourceManager * SM)323f4a2713aSLionel Sambuc static void mapDiagnosticRanges(
324f4a2713aSLionel Sambuc     SourceLocation CaretLoc,
325f4a2713aSLionel Sambuc     ArrayRef<CharSourceRange> Ranges,
326f4a2713aSLionel Sambuc     SmallVectorImpl<CharSourceRange> &SpellingRanges,
327f4a2713aSLionel Sambuc     const SourceManager *SM) {
328f4a2713aSLionel Sambuc   FileID CaretLocFileID = SM->getFileID(CaretLoc);
329f4a2713aSLionel Sambuc 
330f4a2713aSLionel Sambuc   for (ArrayRef<CharSourceRange>::const_iterator I = Ranges.begin(),
331f4a2713aSLionel Sambuc        E = Ranges.end();
332f4a2713aSLionel Sambuc        I != E; ++I) {
333f4a2713aSLionel Sambuc     SourceLocation Begin = I->getBegin(), End = I->getEnd();
334f4a2713aSLionel Sambuc     bool IsTokenRange = I->isTokenRange();
335f4a2713aSLionel Sambuc 
336f4a2713aSLionel Sambuc     FileID BeginFileID = SM->getFileID(Begin);
337f4a2713aSLionel Sambuc     FileID EndFileID = SM->getFileID(End);
338f4a2713aSLionel Sambuc 
339f4a2713aSLionel Sambuc     // Find the common parent for the beginning and end of the range.
340f4a2713aSLionel Sambuc 
341f4a2713aSLionel Sambuc     // First, crawl the expansion chain for the beginning of the range.
342f4a2713aSLionel Sambuc     llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap;
343f4a2713aSLionel Sambuc     while (Begin.isMacroID() && BeginFileID != EndFileID) {
344f4a2713aSLionel Sambuc       BeginLocsMap[BeginFileID] = Begin;
345f4a2713aSLionel Sambuc       Begin = SM->getImmediateExpansionRange(Begin).first;
346f4a2713aSLionel Sambuc       BeginFileID = SM->getFileID(Begin);
347f4a2713aSLionel Sambuc     }
348f4a2713aSLionel Sambuc 
349f4a2713aSLionel Sambuc     // Then, crawl the expansion chain for the end of the range.
350f4a2713aSLionel Sambuc     if (BeginFileID != EndFileID) {
351f4a2713aSLionel Sambuc       while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) {
352f4a2713aSLionel Sambuc         End = SM->getImmediateExpansionRange(End).second;
353f4a2713aSLionel Sambuc         EndFileID = SM->getFileID(End);
354f4a2713aSLionel Sambuc       }
355f4a2713aSLionel Sambuc       if (End.isMacroID()) {
356f4a2713aSLionel Sambuc         Begin = BeginLocsMap[EndFileID];
357f4a2713aSLionel Sambuc         BeginFileID = EndFileID;
358f4a2713aSLionel Sambuc       }
359f4a2713aSLionel Sambuc     }
360f4a2713aSLionel Sambuc 
361f4a2713aSLionel Sambuc     while (Begin.isMacroID() && BeginFileID != CaretLocFileID) {
362f4a2713aSLionel Sambuc       if (SM->isMacroArgExpansion(Begin)) {
363f4a2713aSLionel Sambuc         Begin = SM->getImmediateSpellingLoc(Begin);
364f4a2713aSLionel Sambuc         End = SM->getImmediateSpellingLoc(End);
365f4a2713aSLionel Sambuc       } else {
366f4a2713aSLionel Sambuc         Begin = SM->getImmediateExpansionRange(Begin).first;
367f4a2713aSLionel Sambuc         End = SM->getImmediateExpansionRange(End).second;
368f4a2713aSLionel Sambuc       }
369f4a2713aSLionel Sambuc       BeginFileID = SM->getFileID(Begin);
370f4a2713aSLionel Sambuc       if (BeginFileID != SM->getFileID(End)) {
371f4a2713aSLionel Sambuc         // FIXME: Ugly hack to stop a crash; this code is making bad
372f4a2713aSLionel Sambuc         // assumptions and it's too complicated for me to reason
373f4a2713aSLionel Sambuc         // about.
374f4a2713aSLionel Sambuc         Begin = End = SourceLocation();
375f4a2713aSLionel Sambuc         break;
376f4a2713aSLionel Sambuc       }
377f4a2713aSLionel Sambuc     }
378f4a2713aSLionel Sambuc 
379f4a2713aSLionel Sambuc     // Return the spelling location of the beginning and end of the range.
380f4a2713aSLionel Sambuc     Begin = SM->getSpellingLoc(Begin);
381f4a2713aSLionel Sambuc     End = SM->getSpellingLoc(End);
382f4a2713aSLionel Sambuc     SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End),
383f4a2713aSLionel Sambuc                                              IsTokenRange));
384f4a2713aSLionel Sambuc   }
385f4a2713aSLionel Sambuc }
386f4a2713aSLionel Sambuc 
emitCaret(SourceLocation Loc,DiagnosticsEngine::Level Level,ArrayRef<CharSourceRange> Ranges,ArrayRef<FixItHint> Hints,const SourceManager & SM)387f4a2713aSLionel Sambuc void DiagnosticRenderer::emitCaret(SourceLocation Loc,
388f4a2713aSLionel Sambuc                                    DiagnosticsEngine::Level Level,
389f4a2713aSLionel Sambuc                                    ArrayRef<CharSourceRange> Ranges,
390f4a2713aSLionel Sambuc                                    ArrayRef<FixItHint> Hints,
391f4a2713aSLionel Sambuc                                    const SourceManager &SM) {
392f4a2713aSLionel Sambuc   SmallVector<CharSourceRange, 4> SpellingRanges;
393f4a2713aSLionel Sambuc   mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
394f4a2713aSLionel Sambuc   emitCodeContext(Loc, Level, SpellingRanges, Hints, SM);
395f4a2713aSLionel Sambuc }
396f4a2713aSLionel Sambuc 
397f4a2713aSLionel Sambuc /// \brief Recursively emit notes for each macro expansion and caret
398f4a2713aSLionel Sambuc /// diagnostics where appropriate.
399f4a2713aSLionel Sambuc ///
400f4a2713aSLionel Sambuc /// Walks up the macro expansion stack printing expansion notes, the code
401f4a2713aSLionel Sambuc /// snippet, caret, underlines and FixItHint display as appropriate at each
402f4a2713aSLionel Sambuc /// level.
403f4a2713aSLionel Sambuc ///
404f4a2713aSLionel Sambuc /// \param Loc The location for this caret.
405f4a2713aSLionel Sambuc /// \param Level The diagnostic level currently being emitted.
406f4a2713aSLionel Sambuc /// \param Ranges The underlined ranges for this code snippet.
407f4a2713aSLionel Sambuc /// \param Hints The FixIt hints active for this diagnostic.
408f4a2713aSLionel Sambuc /// \param OnMacroInst The current depth of the macro expansion stack.
emitMacroExpansions(SourceLocation Loc,DiagnosticsEngine::Level Level,ArrayRef<CharSourceRange> Ranges,ArrayRef<FixItHint> Hints,const SourceManager & SM,unsigned & MacroDepth,unsigned OnMacroInst)409f4a2713aSLionel Sambuc void DiagnosticRenderer::emitMacroExpansions(SourceLocation Loc,
410f4a2713aSLionel Sambuc                                              DiagnosticsEngine::Level Level,
411f4a2713aSLionel Sambuc                                              ArrayRef<CharSourceRange> Ranges,
412f4a2713aSLionel Sambuc                                              ArrayRef<FixItHint> Hints,
413f4a2713aSLionel Sambuc                                              const SourceManager &SM,
414f4a2713aSLionel Sambuc                                              unsigned &MacroDepth,
415f4a2713aSLionel Sambuc                                              unsigned OnMacroInst) {
416f4a2713aSLionel Sambuc   assert(!Loc.isInvalid() && "must have a valid source location here");
417f4a2713aSLionel Sambuc 
418f4a2713aSLionel Sambuc   // Walk up to the caller of this macro, and produce a backtrace down to there.
419f4a2713aSLionel Sambuc   SourceLocation OneLevelUp = SM.getImmediateMacroCallerLoc(Loc);
420f4a2713aSLionel Sambuc   if (OneLevelUp.isMacroID())
421f4a2713aSLionel Sambuc     emitMacroExpansions(OneLevelUp, Level, Ranges, Hints, SM,
422f4a2713aSLionel Sambuc                         MacroDepth, OnMacroInst + 1);
423f4a2713aSLionel Sambuc   else
424f4a2713aSLionel Sambuc     MacroDepth = OnMacroInst + 1;
425f4a2713aSLionel Sambuc 
426f4a2713aSLionel Sambuc   unsigned MacroSkipStart = 0, MacroSkipEnd = 0;
427f4a2713aSLionel Sambuc   if (MacroDepth > DiagOpts->MacroBacktraceLimit &&
428f4a2713aSLionel Sambuc       DiagOpts->MacroBacktraceLimit != 0) {
429f4a2713aSLionel Sambuc     MacroSkipStart = DiagOpts->MacroBacktraceLimit / 2 +
430f4a2713aSLionel Sambuc     DiagOpts->MacroBacktraceLimit % 2;
431f4a2713aSLionel Sambuc     MacroSkipEnd = MacroDepth - DiagOpts->MacroBacktraceLimit / 2;
432f4a2713aSLionel Sambuc   }
433f4a2713aSLionel Sambuc 
434f4a2713aSLionel Sambuc   // Whether to suppress printing this macro expansion.
435f4a2713aSLionel Sambuc   bool Suppressed = (OnMacroInst >= MacroSkipStart &&
436f4a2713aSLionel Sambuc                      OnMacroInst < MacroSkipEnd);
437f4a2713aSLionel Sambuc 
438f4a2713aSLionel Sambuc   if (Suppressed) {
439f4a2713aSLionel Sambuc     // Tell the user that we've skipped contexts.
440f4a2713aSLionel Sambuc     if (OnMacroInst == MacroSkipStart) {
441f4a2713aSLionel Sambuc       SmallString<200> MessageStorage;
442f4a2713aSLionel Sambuc       llvm::raw_svector_ostream Message(MessageStorage);
443f4a2713aSLionel Sambuc       Message << "(skipping " << (MacroSkipEnd - MacroSkipStart)
444f4a2713aSLionel Sambuc               << " expansions in backtrace; use -fmacro-backtrace-limit=0 to "
445f4a2713aSLionel Sambuc                  "see all)";
446f4a2713aSLionel Sambuc       emitBasicNote(Message.str());
447f4a2713aSLionel Sambuc     }
448f4a2713aSLionel Sambuc     return;
449f4a2713aSLionel Sambuc   }
450f4a2713aSLionel Sambuc 
451f4a2713aSLionel Sambuc   // Find the spelling location for the macro definition. We must use the
452f4a2713aSLionel Sambuc   // spelling location here to avoid emitting a macro bactrace for the note.
453f4a2713aSLionel Sambuc   SourceLocation SpellingLoc = Loc;
454f4a2713aSLionel Sambuc   // If this is the expansion of a macro argument, point the caret at the
455f4a2713aSLionel Sambuc   // use of the argument in the definition of the macro, not the expansion.
456f4a2713aSLionel Sambuc   if (SM.isMacroArgExpansion(Loc))
457f4a2713aSLionel Sambuc     SpellingLoc = SM.getImmediateExpansionRange(Loc).first;
458f4a2713aSLionel Sambuc   SpellingLoc = SM.getSpellingLoc(SpellingLoc);
459f4a2713aSLionel Sambuc 
460f4a2713aSLionel Sambuc   // Map the ranges into the FileID of the diagnostic location.
461f4a2713aSLionel Sambuc   SmallVector<CharSourceRange, 4> SpellingRanges;
462f4a2713aSLionel Sambuc   mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM);
463f4a2713aSLionel Sambuc 
464f4a2713aSLionel Sambuc   SmallString<100> MessageStorage;
465f4a2713aSLionel Sambuc   llvm::raw_svector_ostream Message(MessageStorage);
466f4a2713aSLionel Sambuc   StringRef MacroName = getImmediateMacroName(Loc, SM, LangOpts);
467f4a2713aSLionel Sambuc   if (MacroName.empty())
468f4a2713aSLionel Sambuc     Message << "expanded from here";
469f4a2713aSLionel Sambuc   else
470f4a2713aSLionel Sambuc     Message << "expanded from macro '" << MacroName << "'";
471f4a2713aSLionel Sambuc   emitDiagnostic(SpellingLoc, DiagnosticsEngine::Note, Message.str(),
472f4a2713aSLionel Sambuc                  SpellingRanges, None, &SM);
473f4a2713aSLionel Sambuc }
474f4a2713aSLionel Sambuc 
~DiagnosticNoteRenderer()475f4a2713aSLionel Sambuc DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {}
476f4a2713aSLionel Sambuc 
emitIncludeLocation(SourceLocation Loc,PresumedLoc PLoc,const SourceManager & SM)477f4a2713aSLionel Sambuc void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc,
478f4a2713aSLionel Sambuc                                                  PresumedLoc PLoc,
479f4a2713aSLionel Sambuc                                                  const SourceManager &SM) {
480f4a2713aSLionel Sambuc   // Generate a note indicating the include location.
481f4a2713aSLionel Sambuc   SmallString<200> MessageStorage;
482f4a2713aSLionel Sambuc   llvm::raw_svector_ostream Message(MessageStorage);
483f4a2713aSLionel Sambuc   Message << "in file included from " << PLoc.getFilename() << ':'
484f4a2713aSLionel Sambuc           << PLoc.getLine() << ":";
485f4a2713aSLionel Sambuc   emitNote(Loc, Message.str(), &SM);
486f4a2713aSLionel Sambuc }
487f4a2713aSLionel Sambuc 
emitImportLocation(SourceLocation Loc,PresumedLoc PLoc,StringRef ModuleName,const SourceManager & SM)488f4a2713aSLionel Sambuc void DiagnosticNoteRenderer::emitImportLocation(SourceLocation Loc,
489f4a2713aSLionel Sambuc                                                 PresumedLoc PLoc,
490f4a2713aSLionel Sambuc                                                 StringRef ModuleName,
491f4a2713aSLionel Sambuc                                                 const SourceManager &SM) {
492f4a2713aSLionel Sambuc   // Generate a note indicating the include location.
493f4a2713aSLionel Sambuc   SmallString<200> MessageStorage;
494f4a2713aSLionel Sambuc   llvm::raw_svector_ostream Message(MessageStorage);
495f4a2713aSLionel Sambuc   Message << "in module '" << ModuleName << "' imported from "
496f4a2713aSLionel Sambuc           << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
497f4a2713aSLionel Sambuc   emitNote(Loc, Message.str(), &SM);
498f4a2713aSLionel Sambuc }
499f4a2713aSLionel Sambuc 
500f4a2713aSLionel Sambuc void
emitBuildingModuleLocation(SourceLocation Loc,PresumedLoc PLoc,StringRef ModuleName,const SourceManager & SM)501f4a2713aSLionel Sambuc DiagnosticNoteRenderer::emitBuildingModuleLocation(SourceLocation Loc,
502f4a2713aSLionel Sambuc                                                    PresumedLoc PLoc,
503f4a2713aSLionel Sambuc                                                    StringRef ModuleName,
504f4a2713aSLionel Sambuc                                                    const SourceManager &SM) {
505f4a2713aSLionel Sambuc   // Generate a note indicating the include location.
506f4a2713aSLionel Sambuc   SmallString<200> MessageStorage;
507f4a2713aSLionel Sambuc   llvm::raw_svector_ostream Message(MessageStorage);
508*0a6a1f1dSLionel Sambuc   if (PLoc.getFilename())
509f4a2713aSLionel Sambuc     Message << "while building module '" << ModuleName << "' imported from "
510f4a2713aSLionel Sambuc             << PLoc.getFilename() << ':' << PLoc.getLine() << ":";
511*0a6a1f1dSLionel Sambuc   else
512*0a6a1f1dSLionel Sambuc     Message << "while building module '" << ModuleName << "':";
513f4a2713aSLionel Sambuc   emitNote(Loc, Message.str(), &SM);
514f4a2713aSLionel Sambuc }
515