diff --git a/include/swift/Bridging/ASTGen.h b/include/swift/Bridging/ASTGen.h index 19cef55aef530..bb09e8b0097c6 100644 --- a/include/swift/Bridging/ASTGen.h +++ b/include/swift/Bridging/ASTGen.h @@ -48,7 +48,6 @@ int swift_ASTGen_roundTripCheck(void *_Nonnull sourceFile); /// Emit parser diagnostics for given source file.. Returns non-zero if any /// diagnostics were emitted. int swift_ASTGen_emitParserDiagnostics( - BridgedASTContext astContext, void *_Nonnull diagEngine, void *_Nonnull sourceFile, int emitOnlyErrors, int downgradePlaceholderErrorsToWarnings); diff --git a/lib/ASTGen/Sources/ASTGen/SourceFile.swift b/lib/ASTGen/Sources/ASTGen/SourceFile.swift index 4c9dc3e2bbe8b..927153b571b8d 100644 --- a/lib/ASTGen/Sources/ASTGen/SourceFile.swift +++ b/lib/ASTGen/Sources/ASTGen/SourceFile.swift @@ -12,7 +12,6 @@ import ASTBridging import SwiftDiagnostics -import SwiftIfConfig @_spi(ExperimentalLanguageFeatures) import SwiftParser import SwiftParserDiagnostics import SwiftSyntax @@ -143,10 +142,20 @@ public func roundTripCheck( } } +extension Syntax { + /// Whether this syntax node is or is enclosed within a #if. + fileprivate var isInIfConfig: Bool { + if self.is(IfConfigDeclSyntax.self) { + return true + } + + return parent?.isInIfConfig ?? false + } +} + /// Emit diagnostics within the given source file. @_cdecl("swift_ASTGen_emitParserDiagnostics") public func emitParserDiagnostics( - ctx: BridgedASTContext, diagEnginePtr: UnsafeMutableRawPointer, sourceFilePtr: UnsafeMutablePointer, emitOnlyErrors: CInt, @@ -163,18 +172,11 @@ public func emitParserDiagnostics( ) let diagnosticEngine = BridgedDiagnosticEngine(raw: diagEnginePtr) - let buildConfiguration = CompilerBuildConfiguration( - ctx: ctx, - conditionLoc: - BridgedSourceLoc( - at: AbsolutePosition(utf8Offset: 0), - in: sourceFile.pointee.buffer - ) - ) - for diag in diags { - // If the diagnostic is in an unparsed #if region, don't emit it. - if diag.node.isActive(in: buildConfiguration).state == .unparsed { + // Skip over diagnostics within #if, because we don't know whether + // we are in an active region or not. + // FIXME: This heuristic could be improved. + if diag.node.isInIfConfig { continue } diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 3da1693e47a13..a3f764bccaf8b 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -267,7 +267,7 @@ void Parser::parseTopLevelItems(SmallVectorImpl &items) { if (parsingOpts.contains(ParsingFlags::ValidateNewParserDiagnostics) && !Context.Diags.hadAnyError()) { auto hadSyntaxError = swift_ASTGen_emitParserDiagnostics( - Context, &Context.Diags, exportedSourceFile, + &Context.Diags, exportedSourceFile, /*emitOnlyErrors=*/true, /*downgradePlaceholderErrorsToWarnings=*/ Context.LangOpts.Playground || @@ -346,7 +346,7 @@ void Parser::parseSourceFileViaASTGen( // If we're supposed to emit diagnostics from the parser, do so now. if (!suppressDiagnostics) { auto hadSyntaxError = swift_ASTGen_emitParserDiagnostics( - Context, &Context.Diags, exportedSourceFile, /*emitOnlyErrors=*/false, + &Context.Diags, exportedSourceFile, /*emitOnlyErrors=*/false, /*downgradePlaceholderErrorsToWarnings=*/langOpts.Playground || langOpts.WarnOnEditorPlaceholder); if (hadSyntaxError && Context.Diags.hadAnyError() && diff --git a/test/ASTGen/if_config.swift b/test/ASTGen/if_config.swift index 23232d1bd5ca0..1047a23fec9ac 100644 --- a/test/ASTGen/if_config.swift +++ b/test/ASTGen/if_config.swift @@ -3,8 +3,8 @@ // REQUIRES: asserts #if NOT_SET -func f { } // expected-error{{expected parameter clause in function signature}} - // expected-note@-1{{insert parameter clause}}{{7-8=}}{{8-8=(}}{{8-8=) }} +func f { } // FIXME: Error once the parser diagnostics generator knows to + // evaluate the active clause. #endif #if compiler(>=10.0)