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