|
14 | 14 | //
|
15 | 15 | //===----------------------------------------------------------------------===//
|
16 | 16 |
|
17 |
| -#define DEBUG_TYPE "swift-basic" |
| 17 | +#define DEBUG_TYPE "swift-ast" |
18 | 18 | #include "swift/AST/DiagnosticConsumer.h"
|
19 | 19 | #include "swift/AST/DiagnosticEngine.h"
|
| 20 | +#include "swift/Basic/Defer.h" |
| 21 | +#include "swift/Basic/SourceManager.h" |
20 | 22 | #include "llvm/ADT/StringRef.h"
|
| 23 | +#include "llvm/ADT/StringSet.h" |
21 | 24 | #include "llvm/Support/Debug.h"
|
22 | 25 | #include "llvm/Support/raw_ostream.h"
|
23 | 26 | using namespace swift;
|
24 | 27 |
|
25 |
| -DiagnosticConsumer::~DiagnosticConsumer() { } |
| 28 | +DiagnosticConsumer::~DiagnosticConsumer() = default; |
| 29 | + |
| 30 | +llvm::SMLoc DiagnosticConsumer::getRawLoc(SourceLoc loc) { |
| 31 | + return loc.Value; |
| 32 | +} |
| 33 | + |
| 34 | +LLVM_ATTRIBUTE_UNUSED |
| 35 | +static bool hasDuplicateFileNames( |
| 36 | + ArrayRef<FileSpecificDiagnosticConsumer::ConsumerPair> consumers) { |
| 37 | + llvm::StringSet<> seenFiles; |
| 38 | + for (const auto &consumerPair : consumers) { |
| 39 | + if (consumerPair.first.empty()) { |
| 40 | + // We can handle multiple consumers that aren't associated with any file, |
| 41 | + // because they only collect diagnostics that aren't in any of the special |
| 42 | + // files. This isn't an important use case to support, but also SmallSet |
| 43 | + // doesn't handle empty strings anyway! |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + bool isUnique = seenFiles.insert(consumerPair.first).second; |
| 48 | + if (!isUnique) |
| 49 | + return true; |
| 50 | + } |
| 51 | + return false; |
| 52 | +} |
| 53 | + |
| 54 | +FileSpecificDiagnosticConsumer::FileSpecificDiagnosticConsumer( |
| 55 | + SmallVectorImpl<ConsumerPair> &consumers) |
| 56 | + : SubConsumers(std::move(consumers)) { |
| 57 | + assert(!SubConsumers.empty() && |
| 58 | + "don't waste time handling diagnostics that will never get emitted"); |
| 59 | + assert(!hasDuplicateFileNames(SubConsumers) && |
| 60 | + "having multiple consumers for the same file is not implemented"); |
| 61 | +} |
| 62 | + |
| 63 | +void FileSpecificDiagnosticConsumer::computeConsumersOrderedByRange( |
| 64 | + SourceManager &SM) { |
| 65 | + // Look up each file's source range and add it to the "map" (to be sorted). |
| 66 | + for (const ConsumerPair &pair : SubConsumers) { |
| 67 | + if (pair.first.empty()) |
| 68 | + continue; |
| 69 | + |
| 70 | + Optional<unsigned> bufferID = SM.getIDForBufferIdentifier(pair.first); |
| 71 | + assert(bufferID.hasValue() && "consumer registered for unknown file"); |
| 72 | + CharSourceRange range = SM.getRangeForBuffer(bufferID.getValue()); |
| 73 | + ConsumersOrderedByRange.emplace_back(range, pair.second.get()); |
| 74 | + } |
| 75 | + |
| 76 | + // Sort the "map" by buffer /end/ location, for use with std::lower_bound |
| 77 | + // later. (Sorting by start location would produce the same sort, since the |
| 78 | + // ranges must not be overlapping, but since we need to check end locations |
| 79 | + // later it's consistent to sort by that here.) |
| 80 | + std::sort(ConsumersOrderedByRange.begin(), ConsumersOrderedByRange.end(), |
| 81 | + [](const ConsumersOrderedByRangeEntry &left, |
| 82 | + const ConsumersOrderedByRangeEntry &right) -> bool { |
| 83 | + auto compare = std::less<const char *>(); |
| 84 | + return compare(getRawLoc(left.first.getEnd()).getPointer(), |
| 85 | + getRawLoc(right.first.getEnd()).getPointer()); |
| 86 | + }); |
| 87 | + |
| 88 | + // Check that the ranges are non-overlapping. If the files really are all |
| 89 | + // distinct, this should be trivially true, but if it's ever not we might end |
| 90 | + // up mis-filing diagnostics. |
| 91 | + assert(ConsumersOrderedByRange.end() == |
| 92 | + std::adjacent_find(ConsumersOrderedByRange.begin(), |
| 93 | + ConsumersOrderedByRange.end(), |
| 94 | + [](const ConsumersOrderedByRangeEntry &left, |
| 95 | + const ConsumersOrderedByRangeEntry &right) { |
| 96 | + return left.first.overlaps(right.first); |
| 97 | + }) && |
| 98 | + "overlapping ranges despite having distinct files"); |
| 99 | +} |
| 100 | + |
| 101 | +DiagnosticConsumer * |
| 102 | +FileSpecificDiagnosticConsumer::consumerForLocation(SourceManager &SM, |
| 103 | + SourceLoc loc) const { |
| 104 | + // If there's only one consumer, we'll use it no matter what, because... |
| 105 | + // - ...all diagnostics within the file will go to that consumer. |
| 106 | + // - ...all diagnostics not within the file will not be claimed by any |
| 107 | + // consumer, and so will go to all (one) consumers. |
| 108 | + if (SubConsumers.size() == 1) |
| 109 | + return SubConsumers.front().second.get(); |
| 110 | + |
| 111 | + // Diagnostics with invalid locations always go to every consumer. |
| 112 | + if (loc.isInvalid()) |
| 113 | + return nullptr; |
| 114 | + |
| 115 | + // This map is generated on first use and cached, to allow the |
| 116 | + // FileSpecificDiagnosticConsumer to be set up before the source files are |
| 117 | + // actually loaded. |
| 118 | + if (ConsumersOrderedByRange.empty()) { |
| 119 | + auto *mutableThis = const_cast<FileSpecificDiagnosticConsumer*>(this); |
| 120 | + mutableThis->computeConsumersOrderedByRange(SM); |
| 121 | + } |
| 122 | + |
| 123 | + // This std::lower_bound call is doing a binary search for the first range |
| 124 | + // that /might/ contain 'loc'. Specifically, since the ranges are sorted |
| 125 | + // by end location, it's looking for the first range where the end location |
| 126 | + // is greater than or equal to 'loc'. |
| 127 | + auto possiblyContainingRangeIter = |
| 128 | + std::lower_bound(ConsumersOrderedByRange.begin(), |
| 129 | + ConsumersOrderedByRange.end(), |
| 130 | + loc, |
| 131 | + [](const ConsumersOrderedByRangeEntry &entry, |
| 132 | + SourceLoc loc) -> bool { |
| 133 | + auto compare = std::less<const char *>(); |
| 134 | + return compare(getRawLoc(entry.first.getEnd()).getPointer(), |
| 135 | + getRawLoc(loc).getPointer()); |
| 136 | + }); |
| 137 | + |
| 138 | + if (possiblyContainingRangeIter != ConsumersOrderedByRange.end() && |
| 139 | + possiblyContainingRangeIter->first.contains(loc)) { |
| 140 | + return possiblyContainingRangeIter->second; |
| 141 | + } |
| 142 | + |
| 143 | + return nullptr; |
| 144 | +} |
| 145 | + |
| 146 | +void FileSpecificDiagnosticConsumer::handleDiagnostic( |
| 147 | + SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind, |
| 148 | + StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs, |
| 149 | + const DiagnosticInfo &Info) { |
| 150 | + |
| 151 | + DiagnosticConsumer *specificConsumer; |
| 152 | + switch (Kind) { |
| 153 | + case DiagnosticKind::Error: |
| 154 | + case DiagnosticKind::Warning: |
| 155 | + case DiagnosticKind::Remark: |
| 156 | + specificConsumer = consumerForLocation(SM, Loc); |
| 157 | + ConsumerForSubsequentNotes = specificConsumer; |
| 158 | + break; |
| 159 | + case DiagnosticKind::Note: |
| 160 | + specificConsumer = ConsumerForSubsequentNotes; |
| 161 | + break; |
| 162 | + } |
| 163 | + |
| 164 | + if (specificConsumer) { |
| 165 | + specificConsumer->handleDiagnostic(SM, Loc, Kind, FormatString, FormatArgs, |
| 166 | + Info); |
| 167 | + } else { |
| 168 | + for (auto &subConsumer : SubConsumers) { |
| 169 | + subConsumer.second->handleDiagnostic(SM, Loc, Kind, FormatString, |
| 170 | + FormatArgs, Info); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +bool FileSpecificDiagnosticConsumer::finishProcessing() { |
| 176 | + // Deliberately don't use std::any_of here because we don't want early-exit |
| 177 | + // behavior. |
| 178 | + bool hadError = false; |
| 179 | + for (auto &subConsumer : SubConsumers) |
| 180 | + hadError |= subConsumer.second->finishProcessing(); |
| 181 | + return hadError; |
| 182 | +} |
26 | 183 |
|
27 | 184 | void NullDiagnosticConsumer::handleDiagnostic(
|
28 | 185 | SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
|
|
0 commit comments