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