Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 1 | //===--- 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 Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 11 | #include "clang/Basic/DiagnosticOptions.h" |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 12 | #include "clang/Basic/FileManager.h" |
| 13 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 14 | #include "clang/Edit/Commit.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/Edit/EditedSource.h" |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 16 | #include "clang/Edit/EditsReceiver.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "clang/Lex/Lexer.h" |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallSet.h" |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | using namespace clang; |
| 25 | |
Argyrios Kyrtzidis | 7f6cf97 | 2012-01-23 16:58:33 +0000 | [diff] [blame] | 26 | /// \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". |
| 42 | static StringRef getImmediateMacroName(SourceLocation Loc, |
| 43 | const SourceManager &SM, |
| 44 | const LangOptions &LangOpts) { |
| 45 | assert(Loc.isMacroID() && "Only reasonble to call this on macros"); |
| 46 | // Walk past macro argument expanions. |
| 47 | while (SM.isMacroArgExpansion(Loc)) |
| 48 | Loc = SM.getImmediateExpansionRange(Loc).first; |
| 49 | |
Richard Smith | 5b10af7 | 2012-12-05 11:04:55 +0000 | [diff] [blame^] | 50 | // 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 Kyrtzidis | 7f6cf97 | 2012-01-23 16:58:33 +0000 | [diff] [blame] | 55 | // 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 Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 68 | DiagnosticRenderer::DiagnosticRenderer(const LangOptions &LangOpts, |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 69 | DiagnosticOptions *DiagOpts) |
| 70 | : LangOpts(LangOpts), DiagOpts(DiagOpts), LastLevel() {} |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 71 | |
| 72 | DiagnosticRenderer::~DiagnosticRenderer() {} |
| 73 | |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 74 | namespace { |
| 75 | |
| 76 | class FixitReceiver : public edit::EditsReceiver { |
| 77 | SmallVectorImpl<FixItHint> &MergedFixits; |
| 78 | |
| 79 | public: |
| 80 | FixitReceiver(SmallVectorImpl<FixItHint> &MergedFixits) |
| 81 | : MergedFixits(MergedFixits) { } |
| 82 | virtual void insert(SourceLocation loc, StringRef text) { |
| 83 | MergedFixits.push_back(FixItHint::CreateInsertion(loc, text)); |
| 84 | } |
| 85 | virtual void replace(CharSourceRange range, StringRef text) { |
| 86 | MergedFixits.push_back(FixItHint::CreateReplacement(range, text)); |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | } |
| 91 | |
| 92 | static 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 Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 122 | |
| 123 | void DiagnosticRenderer::emitDiagnostic(SourceLocation Loc, |
| 124 | DiagnosticsEngine::Level Level, |
| 125 | StringRef Message, |
| 126 | ArrayRef<CharSourceRange> Ranges, |
| 127 | ArrayRef<FixItHint> FixItHints, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 128 | const SourceManager *SM, |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 129 | DiagOrStoredDiag D) { |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 130 | assert(SM || Loc.isInvalid()); |
Richard Smith | 98cffc6 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 132 | beginDiagnostic(D, Level); |
Richard Smith | 98cffc6 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 133 | |
| 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 Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 138 | // Get the ranges into a local array we can hack on. |
| 139 | SmallVector<CharSourceRange, 20> MutableRanges(Ranges.begin(), |
| 140 | Ranges.end()); |
Richard Smith | 98cffc6 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 142 | llvm::SmallVector<FixItHint, 8> MergedFixits; |
| 143 | if (!FixItHints.empty()) { |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 144 | mergeFixits(FixItHints, *SM, LangOpts, MergedFixits); |
Ted Kremenek | 30660a8 | 2012-03-06 20:06:33 +0000 | [diff] [blame] | 145 | FixItHints = MergedFixits; |
| 146 | } |
| 147 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 148 | 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 Smith | ac31c83 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 153 | |
Richard Smith | 98cffc6 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 154 | SourceLocation UnexpandedLoc = Loc; |
Richard Smith | ac31c83 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 155 | |
Richard Smith | 98cffc6 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 156 | // Perform the same walk as emitMacroExpansions, to find the ultimate |
| 157 | // expansion location for the diagnostic. |
| 158 | while (Loc.isMacroID()) |
| 159 | Loc = SM->getImmediateMacroCallerLoc(Loc); |
| 160 | |
| 161 | PresumedLoc PLoc = SM->getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc); |
| 162 | |
| 163 | // First, if this diagnostic is not in the main file, print out the |
| 164 | // "included from" lines. |
| 165 | emitIncludeStack(Loc, PLoc, Level, *SM); |
| 166 | |
| 167 | // Next, emit the actual diagnostic message and caret. |
| 168 | emitDiagnosticMessage(Loc, PLoc, Level, Message, Ranges, SM, D); |
| 169 | emitCaret(Loc, Level, MutableRanges, FixItHints, *SM); |
| 170 | |
| 171 | // If this location is within a macro, walk from UnexpandedLoc up to Loc |
| 172 | // and produce a macro backtrace. |
| 173 | if (UnexpandedLoc.isValid() && UnexpandedLoc.isMacroID()) { |
Richard Smith | ac31c83 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 174 | unsigned MacroDepth = 0; |
Richard Smith | 98cffc6 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 175 | emitMacroExpansions(UnexpandedLoc, Level, MutableRanges, FixItHints, *SM, |
Richard Smith | ac31c83 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 176 | MacroDepth); |
| 177 | } |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 178 | } |
Richard Smith | 98cffc6 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 179 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 180 | LastLoc = Loc; |
| 181 | LastLevel = Level; |
Richard Smith | 98cffc6 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 183 | endDiagnostic(D, Level); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void DiagnosticRenderer::emitStoredDiagnostic(StoredDiagnostic &Diag) { |
| 188 | emitDiagnostic(Diag.getLocation(), Diag.getLevel(), Diag.getMessage(), |
| 189 | Diag.getRanges(), Diag.getFixIts(), |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 190 | Diag.getLocation().isValid() ? &Diag.getLocation().getManager() |
| 191 | : 0, |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 192 | &Diag); |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /// \brief Prints an include stack when appropriate for a particular |
| 196 | /// diagnostic level and location. |
| 197 | /// |
| 198 | /// This routine handles all the logic of suppressing particular include |
| 199 | /// stacks (such as those for notes) and duplicate include stacks when |
| 200 | /// repeated warnings occur within the same file. It also handles the logic |
| 201 | /// of customizing the formatting and display of the include stack. |
| 202 | /// |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 203 | /// \param Loc The diagnostic location. |
| 204 | /// \param PLoc The presumed location of the diagnostic location. |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 205 | /// \param Level The diagnostic level of the message this stack pertains to. |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 206 | void DiagnosticRenderer::emitIncludeStack(SourceLocation Loc, |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 207 | PresumedLoc PLoc, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 208 | DiagnosticsEngine::Level Level, |
| 209 | const SourceManager &SM) { |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 210 | SourceLocation IncludeLoc = PLoc.getIncludeLoc(); |
| 211 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 212 | // Skip redundant include stacks altogether. |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 213 | if (LastIncludeLoc == IncludeLoc) |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 214 | return; |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 215 | |
| 216 | LastIncludeLoc = IncludeLoc; |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 217 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 218 | if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note) |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 219 | return; |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 220 | |
| 221 | if (IncludeLoc.isValid()) |
| 222 | emitIncludeStackRecursively(IncludeLoc, SM); |
| 223 | else { |
Douglas Gregor | 4565e48 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 224 | emitModuleBuildStack(SM); |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 225 | emitImportStack(Loc, SM); |
| 226 | } |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /// \brief Helper to recursivly walk up the include stack and print each layer |
| 230 | /// on the way back down. |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 231 | void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc, |
| 232 | const SourceManager &SM) { |
Douglas Gregor | 830ea5b | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 233 | if (Loc.isInvalid()) { |
Douglas Gregor | 4565e48 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 234 | emitModuleBuildStack(SM); |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 235 | return; |
Douglas Gregor | 830ea5b | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 236 | } |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 237 | |
Richard Smith | 62221b1 | 2012-11-14 23:55:25 +0000 | [diff] [blame] | 238 | PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc); |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 239 | if (PLoc.isInvalid()) |
| 240 | return; |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 241 | |
| 242 | // If this source location was imported from a module, print the module |
| 243 | // import stack rather than the |
| 244 | // FIXME: We want submodule granularity here. |
| 245 | std::pair<SourceLocation, StringRef> Imported = SM.getModuleImportLoc(Loc); |
| 246 | if (Imported.first.isValid()) { |
| 247 | // This location was imported by a module. Emit the module import stack. |
| 248 | emitImportStackRecursively(Imported.first, Imported.second, SM); |
| 249 | return; |
| 250 | } |
| 251 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 252 | // Emit the other include frames first. |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 253 | emitIncludeStackRecursively(PLoc.getIncludeLoc(), SM); |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 254 | |
| 255 | // Emit the inclusion text/note. |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 256 | emitIncludeLocation(Loc, PLoc, SM); |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 259 | /// \brief Emit the module import stack associated with the current location. |
| 260 | void DiagnosticRenderer::emitImportStack(SourceLocation Loc, |
| 261 | const SourceManager &SM) { |
| 262 | if (Loc.isInvalid()) { |
Douglas Gregor | 4565e48 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 263 | emitModuleBuildStack(SM); |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | |
| 267 | std::pair<SourceLocation, StringRef> NextImportLoc |
| 268 | = SM.getModuleImportLoc(Loc); |
| 269 | emitImportStackRecursively(NextImportLoc.first, NextImportLoc.second, SM); |
| 270 | } |
| 271 | |
| 272 | /// \brief Helper to recursivly walk up the import stack and print each layer |
| 273 | /// on the way back down. |
| 274 | void DiagnosticRenderer::emitImportStackRecursively(SourceLocation Loc, |
| 275 | StringRef ModuleName, |
| 276 | const SourceManager &SM) { |
| 277 | if (Loc.isInvalid()) { |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc); |
| 282 | if (PLoc.isInvalid()) |
| 283 | return; |
| 284 | |
| 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 Gregor | 4565e48 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 294 | /// \brief Emit the module build stack, for cases where a module is (re-)built |
Douglas Gregor | 830ea5b | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 295 | /// on demand. |
Douglas Gregor | 4565e48 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 296 | void 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 Gregor | 830ea5b | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 301 | emitBuildingModuleLocation(CurLoc, |
| 302 | CurSM.getPresumedLoc(CurLoc, |
| 303 | DiagOpts->ShowPresumedLoc), |
Douglas Gregor | 4565e48 | 2012-11-30 22:11:57 +0000 | [diff] [blame] | 304 | Stack[I].first, |
Douglas Gregor | 830ea5b | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 305 | CurSM); |
| 306 | } |
| 307 | } |
| 308 | |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 309 | // Helper function to fix up source ranges. It takes in an array of ranges, |
| 310 | // and outputs an array of ranges where we want to draw the range highlighting |
| 311 | // around the location specified by CaretLoc. |
| 312 | // |
| 313 | // To find locations which correspond to the caret, we crawl the macro caller |
| 314 | // chain for the beginning and end of each range. If the caret location |
| 315 | // is in a macro expansion, we search each chain for a location |
| 316 | // in the same expansion as the caret; otherwise, we crawl to the top of |
| 317 | // each chain. Two locations are part of the same macro expansion |
| 318 | // iff the FileID is the same. |
| 319 | static void mapDiagnosticRanges( |
| 320 | SourceLocation CaretLoc, |
Richard Smith | ac31c83 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 321 | ArrayRef<CharSourceRange> Ranges, |
Richard Smith | 98cffc6 | 2012-12-05 09:47:49 +0000 | [diff] [blame] | 322 | SmallVectorImpl<CharSourceRange> &SpellingRanges, |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 323 | const SourceManager *SM) { |
| 324 | FileID CaretLocFileID = SM->getFileID(CaretLoc); |
| 325 | |
Richard Smith | ac31c83 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 326 | for (ArrayRef<CharSourceRange>::const_iterator I = Ranges.begin(), |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 327 | E = Ranges.end(); |
| 328 | I != E; ++I) { |
| 329 | SourceLocation Begin = I->getBegin(), End = I->getEnd(); |
| 330 | bool IsTokenRange = I->isTokenRange(); |
| 331 | |
Eli Friedman | ecdc8d3 | 2012-11-30 06:19:40 +0000 | [diff] [blame] | 332 | FileID BeginFileID = SM->getFileID(Begin); |
| 333 | FileID EndFileID = SM->getFileID(End); |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 334 | |
Eli Friedman | ecdc8d3 | 2012-11-30 06:19:40 +0000 | [diff] [blame] | 335 | // Find the common parent for the beginning and end of the range. |
| 336 | |
| 337 | // First, crawl the expansion chain for the beginning of the range. |
| 338 | llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap; |
| 339 | while (Begin.isMacroID() && BeginFileID != EndFileID) { |
| 340 | BeginLocsMap[BeginFileID] = Begin; |
| 341 | Begin = SM->getImmediateExpansionRange(Begin).first; |
| 342 | BeginFileID = SM->getFileID(Begin); |
| 343 | } |
| 344 | |
| 345 | // Then, crawl the expansion chain for the end of the range. |
| 346 | if (BeginFileID != EndFileID) { |
| 347 | while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) { |
| 348 | End = SM->getImmediateExpansionRange(End).second; |
| 349 | EndFileID = SM->getFileID(End); |
| 350 | } |
| 351 | if (End.isMacroID()) { |
| 352 | Begin = BeginLocsMap[EndFileID]; |
| 353 | BeginFileID = EndFileID; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | while (Begin.isMacroID() && BeginFileID != CaretLocFileID) { |
| 358 | if (SM->isMacroArgExpansion(Begin)) { |
| 359 | Begin = SM->getImmediateSpellingLoc(Begin); |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 360 | End = SM->getImmediateSpellingLoc(End); |
| 361 | } else { |
Eli Friedman | ecdc8d3 | 2012-11-30 06:19:40 +0000 | [diff] [blame] | 362 | Begin = SM->getImmediateExpansionRange(Begin).first; |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 363 | End = SM->getImmediateExpansionRange(End).second; |
| 364 | } |
Eli Friedman | ecdc8d3 | 2012-11-30 06:19:40 +0000 | [diff] [blame] | 365 | BeginFileID = SM->getFileID(Begin); |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // Return the spelling location of the beginning and end of the range. |
| 369 | Begin = SM->getSpellingLoc(Begin); |
| 370 | End = SM->getSpellingLoc(End); |
| 371 | SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End), |
| 372 | IsTokenRange)); |
| 373 | } |
| 374 | } |
| 375 | |
Richard Smith | ac31c83 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 376 | void DiagnosticRenderer::emitCaret(SourceLocation Loc, |
| 377 | DiagnosticsEngine::Level Level, |
| 378 | ArrayRef<CharSourceRange> Ranges, |
| 379 | ArrayRef<FixItHint> Hints, |
| 380 | const SourceManager &SM) { |
| 381 | SmallVector<CharSourceRange, 4> SpellingRanges; |
| 382 | mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM); |
| 383 | emitCodeContext(Loc, Level, SpellingRanges, Hints, SM); |
| 384 | } |
| 385 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 386 | /// \brief Recursively emit notes for each macro expansion and caret |
| 387 | /// diagnostics where appropriate. |
| 388 | /// |
| 389 | /// Walks up the macro expansion stack printing expansion notes, the code |
| 390 | /// snippet, caret, underlines and FixItHint display as appropriate at each |
| 391 | /// level. |
| 392 | /// |
| 393 | /// \param Loc The location for this caret. |
| 394 | /// \param Level The diagnostic level currently being emitted. |
| 395 | /// \param Ranges The underlined ranges for this code snippet. |
| 396 | /// \param Hints The FixIt hints active for this diagnostic. |
| 397 | /// \param MacroSkipEnd The depth to stop skipping macro expansions. |
| 398 | /// \param OnMacroInst The current depth of the macro expansion stack. |
Richard Smith | ac31c83 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 399 | void DiagnosticRenderer::emitMacroExpansions(SourceLocation Loc, |
| 400 | DiagnosticsEngine::Level Level, |
| 401 | ArrayRef<CharSourceRange> Ranges, |
| 402 | ArrayRef<FixItHint> Hints, |
| 403 | const SourceManager &SM, |
| 404 | unsigned &MacroDepth, |
| 405 | unsigned OnMacroInst) { |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 406 | assert(!Loc.isInvalid() && "must have a valid source location here"); |
Richard Smith | 6788392 | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 407 | |
Richard Smith | 6788392 | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 408 | // Walk up to the caller of this macro, and produce a backtrace down to there. |
| 409 | SourceLocation OneLevelUp = SM.getImmediateMacroCallerLoc(Loc); |
Richard Smith | ac31c83 | 2012-12-05 06:20:58 +0000 | [diff] [blame] | 410 | if (OneLevelUp.isMacroID()) |
| 411 | emitMacroExpansions(OneLevelUp, Level, Ranges, Hints, SM, |
| 412 | MacroDepth, OnMacroInst + 1); |
| 413 | else |
| 414 | MacroDepth = OnMacroInst + 1; |
Richard Smith | 6788392 | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 416 | unsigned MacroSkipStart = 0, MacroSkipEnd = 0; |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 417 | if (MacroDepth > DiagOpts->MacroBacktraceLimit && |
| 418 | DiagOpts->MacroBacktraceLimit != 0) { |
| 419 | MacroSkipStart = DiagOpts->MacroBacktraceLimit / 2 + |
| 420 | DiagOpts->MacroBacktraceLimit % 2; |
| 421 | MacroSkipEnd = MacroDepth - DiagOpts->MacroBacktraceLimit / 2; |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 422 | } |
Richard Smith | 6788392 | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 423 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 424 | // Whether to suppress printing this macro expansion. |
| 425 | bool Suppressed = (OnMacroInst >= MacroSkipStart && |
| 426 | OnMacroInst < MacroSkipEnd); |
Richard Smith | 6788392 | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 427 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 428 | if (Suppressed) { |
| 429 | // Tell the user that we've skipped contexts. |
| 430 | if (OnMacroInst == MacroSkipStart) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 431 | SmallString<200> MessageStorage; |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 432 | llvm::raw_svector_ostream Message(MessageStorage); |
| 433 | Message << "(skipping " << (MacroSkipEnd - MacroSkipStart) |
| 434 | << " expansions in backtrace; use -fmacro-backtrace-limit=0 to " |
| 435 | "see all)"; |
| 436 | emitBasicNote(Message.str()); |
| 437 | } |
| 438 | return; |
| 439 | } |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 440 | |
Richard Smith | 6788392 | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 441 | // Find the spelling location for the macro definition. We must use the |
| 442 | // spelling location here to avoid emitting a macro bactrace for the note. |
| 443 | SourceLocation SpellingLoc = Loc; |
| 444 | // If this is the expansion of a macro argument, point the caret at the |
| 445 | // use of the argument in the definition of the macro, not the expansion. |
| 446 | if (SM.isMacroArgExpansion(Loc)) |
| 447 | SpellingLoc = SM.getImmediateExpansionRange(Loc).first; |
| 448 | SpellingLoc = SM.getSpellingLoc(SpellingLoc); |
| 449 | |
| 450 | // Map the ranges into the FileID of the diagnostic location. |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 451 | SmallVector<CharSourceRange, 4> SpellingRanges; |
Richard Smith | 6788392 | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 452 | mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM); |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 453 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 454 | SmallString<100> MessageStorage; |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 455 | llvm::raw_svector_ostream Message(MessageStorage); |
Richard Smith | 5b10af7 | 2012-12-05 11:04:55 +0000 | [diff] [blame^] | 456 | StringRef MacroName = getImmediateMacroName(Loc, SM, LangOpts); |
| 457 | if (MacroName.empty()) |
| 458 | Message << "expanded from here"; |
| 459 | else |
| 460 | Message << "expanded from macro '" << MacroName << "'"; |
Richard Smith | 6788392 | 2012-12-05 03:18:16 +0000 | [diff] [blame] | 461 | emitDiagnostic(SpellingLoc, DiagnosticsEngine::Note, |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 462 | Message.str(), |
Eli Friedman | 9cb1c3d | 2012-11-03 03:36:51 +0000 | [diff] [blame] | 463 | SpellingRanges, ArrayRef<FixItHint>(), &SM); |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 466 | DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {} |
| 467 | |
| 468 | void DiagnosticNoteRenderer::emitIncludeLocation(SourceLocation Loc, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 469 | PresumedLoc PLoc, |
| 470 | const SourceManager &SM) { |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 471 | // Generate a note indicating the include location. |
| 472 | SmallString<200> MessageStorage; |
| 473 | llvm::raw_svector_ostream Message(MessageStorage); |
| 474 | Message << "in file included from " << PLoc.getFilename() << ':' |
| 475 | << PLoc.getLine() << ":"; |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 476 | emitNote(Loc, Message.str(), &SM); |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 479 | void DiagnosticNoteRenderer::emitImportLocation(SourceLocation Loc, |
| 480 | PresumedLoc PLoc, |
| 481 | StringRef ModuleName, |
| 482 | const SourceManager &SM) { |
| 483 | // Generate a note indicating the include location. |
| 484 | SmallString<200> MessageStorage; |
| 485 | llvm::raw_svector_ostream Message(MessageStorage); |
| 486 | Message << "in module '" << ModuleName << "' imported from " |
| 487 | << PLoc.getFilename() << ':' << PLoc.getLine() << ":"; |
| 488 | emitNote(Loc, Message.str(), &SM); |
| 489 | } |
| 490 | |
Douglas Gregor | 830ea5b | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 491 | void |
| 492 | DiagnosticNoteRenderer::emitBuildingModuleLocation(SourceLocation Loc, |
| 493 | PresumedLoc PLoc, |
| 494 | StringRef ModuleName, |
| 495 | const SourceManager &SM) { |
| 496 | // Generate a note indicating the include location. |
| 497 | SmallString<200> MessageStorage; |
| 498 | llvm::raw_svector_ostream Message(MessageStorage); |
| 499 | Message << "while building module '" << ModuleName << "' imported from " |
| 500 | << PLoc.getFilename() << ':' << PLoc.getLine() << ":"; |
| 501 | emitNote(Loc, Message.str(), &SM); |
| 502 | } |
| 503 | |
| 504 | |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 505 | void DiagnosticNoteRenderer::emitBasicNote(StringRef Message) { |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 506 | emitNote(SourceLocation(), Message, 0); |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 507 | } |