Skip to content

Commit b80d141

Browse files
authored
Merge pull request #61631 from CodaFi/locavore
Bridge Parser Diagnostics into the C++ Diagnostic Engine
2 parents a13412e + fc2e10a commit b80d141

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

SwiftCompilerSources/Sources/Parse/Regex.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private func _RegexLiteralLexingFn(
6666
let diagEngine = DiagnosticEngine(bridged: bridged)
6767
let startLoc = SourceLoc(
6868
locationInFile: error.location.assumingMemoryBound(to: UInt8.self))!
69-
diagEngine.diagnose(startLoc, .regex_literal_parsing_error, error.message)
69+
diagEngine.diagnose(startLoc, .foreign_diagnostic, error.message)
7070
}
7171
return error.completelyErroneous
7272
}
@@ -113,7 +113,7 @@ public func _RegexLiteralParsingFn(
113113
let offset = str.utf8.distance(from: str.startIndex, to: errorLoc)
114114
diagLoc = _diagLoc.advanced(by: offset)
115115
}
116-
diagEngine.diagnose(diagLoc, .regex_literal_parsing_error, error.message)
116+
diagEngine.diagnose(diagLoc, .foreign_diagnostic, error.message)
117117
return true
118118
} catch {
119119
fatalError("Expected CompilerParseError")

include/swift/AST/DiagnosticsParse.def

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ ERROR(forbidden_interpolated_string,none,
9191
ERROR(forbidden_extended_escaping_string,none,
9292
"%0 cannot be an extended escaping string literal", (StringRef))
9393

94-
ERROR(regex_literal_parsing_error,none,
95-
"%0", (StringRef))
96-
9794
//------------------------------------------------------------------------------
9895
// MARK: Lexer diagnostics
9996
//------------------------------------------------------------------------------
@@ -1994,5 +1991,12 @@ ERROR(expected_closure_literal,none,
19941991
ERROR(expected_multiple_closures_block_rbrace,none,
19951992
"expected '}' at the end of a trailing closures block", ())
19961993

1994+
//------------------------------------------------------------------------------
1995+
// MARK: diagnostics emitted by Swift libraries
1996+
//------------------------------------------------------------------------------
1997+
1998+
ERROR(foreign_diagnostic,none,
1999+
"%0", (StringRef))
2000+
19972001
#define UNDEFINE_DIAGNOSTIC_MACROS
19982002
#include "DefineDiagnosticMacros.h"

lib/Parse/ParseRequests.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,21 @@ SourceFileParsingResult ParseSourceFileRequest::evaluate(Evaluator &evaluator,
215215
flags |= SCC_FoldSequences;
216216

217217
if (flags) {
218+
SourceLoc startLoc =
219+
parser.SourceMgr.getLocForBufferStart(parser.L->getBufferID());
220+
struct ParserContext {
221+
SourceLoc startLoc;
222+
DiagnosticEngine *engine;
223+
} context{startLoc, &parser.Diags};
218224
int roundTripResult = swift_parser_consistencyCheck(
219225
bufferRange.str().data(), bufferRange.getByteLength(),
220-
SF->getFilename().str().c_str(), flags);
226+
SF->getFilename().str().c_str(), flags, static_cast<void *>(&context),
227+
[](ptrdiff_t off, const char *text, void *ctx) {
228+
auto *context = static_cast<ParserContext *>(ctx);
229+
SourceLoc loc = context->startLoc.getAdvancedLoc(off);
230+
context->engine->diagnose(loc, diag::foreign_diagnostic,
231+
StringRef(text));
232+
});
221233

222234
if (roundTripResult)
223235
ctx.Diags.diagnose(SourceLoc(), diag::new_parser_failure);

0 commit comments

Comments
 (0)