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