@@ -71,7 +71,8 @@ void FileSpecificDiagnosticConsumer::computeConsumersOrderedByRange(
7171 Optional<unsigned > bufferID = SM.getIDForBufferIdentifier (pair.first );
7272 assert (bufferID.hasValue () && " consumer registered for unknown file" );
7373 CharSourceRange range = SM.getRangeForBuffer (bufferID.getValue ());
74- ConsumersOrderedByRange.emplace_back (range, pair.second .get ());
74+ ConsumersOrderedByRange.emplace_back (range, pair.second ? pair.second .get ()
75+ : nullptr );
7576 }
7677
7778 // Sort the "map" by buffer /end/ location, for use with std::lower_bound
@@ -99,7 +100,7 @@ void FileSpecificDiagnosticConsumer::computeConsumersOrderedByRange(
99100 " overlapping ranges despite having distinct files" );
100101}
101102
102- DiagnosticConsumer *
103+ Optional< DiagnosticConsumer *>
103104FileSpecificDiagnosticConsumer::consumerForLocation (SourceManager &SM,
104105 SourceLoc loc) const {
105106 // If there's only one consumer, we'll use it no matter what, because...
@@ -111,7 +112,7 @@ FileSpecificDiagnosticConsumer::consumerForLocation(SourceManager &SM,
111112
112113 // Diagnostics with invalid locations always go to every consumer.
113114 if (loc.isInvalid ())
114- return nullptr ;
115+ return None ;
115116
116117 // This map is generated on first use and cached, to allow the
117118 // FileSpecificDiagnosticConsumer to be set up before the source files are
@@ -121,15 +122,15 @@ FileSpecificDiagnosticConsumer::consumerForLocation(SourceManager &SM,
121122 // It's possible to get here while a bridging header PCH is being
122123 // attached-to, if there's some sort of AST-reader warning or error, which
123124 // happens before CompilerInstance::setUpInputs(), at which point _no_
124- // source buffers are loaded in yet. In that case we return nullptr , rather
125+ // source buffers are loaded in yet. In that case we return None , rather
125126 // than trying to build a nonsensical map (and actually crashing since we
126127 // can't find buffers for the inputs).
127128 assert (!SubConsumers.empty ());
128129 if (!SM.getIDForBufferIdentifier (SubConsumers.begin ()->first ).hasValue ()) {
129130 assert (llvm::none_of (SubConsumers, [&](const ConsumerPair &pair) {
130131 return SM.getIDForBufferIdentifier (pair.first ).hasValue ();
131132 }));
132- return nullptr ;
133+ return None ;
133134 }
134135 auto *mutableThis = const_cast <FileSpecificDiagnosticConsumer*>(this );
135136 mutableThis->computeConsumersOrderedByRange (SM);
@@ -152,18 +153,21 @@ FileSpecificDiagnosticConsumer::consumerForLocation(SourceManager &SM,
152153
153154 if (possiblyContainingRangeIter != ConsumersOrderedByRange.end () &&
154155 possiblyContainingRangeIter->first .contains (loc)) {
155- return possiblyContainingRangeIter->second ;
156+ DiagnosticConsumer *consumerIfDiagnosticIsToBeEmitted =
157+ possiblyContainingRangeIter->second ;
158+ return consumerIfDiagnosticIsToBeEmitted ? consumerIfDiagnosticIsToBeEmitted
159+ : nullptr ;
156160 }
157161
158- return nullptr ;
162+ return None ;
159163}
160164
161165void FileSpecificDiagnosticConsumer::handleDiagnostic (
162166 SourceManager &SM, SourceLoc Loc, DiagnosticKind Kind,
163167 StringRef FormatString, ArrayRef<DiagnosticArgument> FormatArgs,
164168 const DiagnosticInfo &Info) {
165169
166- DiagnosticConsumer *specificConsumer;
170+ Optional< DiagnosticConsumer *> specificConsumer;
167171 switch (Kind) {
168172 case DiagnosticKind::Error:
169173 case DiagnosticKind::Warning:
@@ -176,14 +180,15 @@ void FileSpecificDiagnosticConsumer::handleDiagnostic(
176180 break ;
177181 }
178182
179- if (specificConsumer) {
180- specificConsumer->handleDiagnostic (SM, Loc, Kind, FormatString, FormatArgs,
181- Info);
182- } else {
183+ if (!specificConsumer.hasValue ()) {
183184 for (auto &subConsumer : SubConsumers) {
184185 subConsumer.second ->handleDiagnostic (SM, Loc, Kind, FormatString,
185186 FormatArgs, Info);
186187 }
188+ } else if (DiagnosticConsumer *c = specificConsumer.getValue ())
189+ c->handleDiagnostic (SM, Loc, Kind, FormatString, FormatArgs, Info);
190+ else {
191+ // / suppress non-primary diagnostic in batch mode
187192 }
188193}
189194
0 commit comments