Skip to content

Revert "Use SwiftIfConfig to determine where to emit new parser diagnostics #76040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion include/swift/Bridging/ASTGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
28 changes: 15 additions & 13 deletions lib/ASTGen/Sources/ASTGen/SourceFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import ASTBridging
import SwiftDiagnostics
import SwiftIfConfig
@_spi(ExperimentalLanguageFeatures) import SwiftParser
import SwiftParserDiagnostics
import SwiftSyntax
Expand Down Expand Up @@ -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<UInt8>,
emitOnlyErrors: CInt,
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void Parser::parseTopLevelItems(SmallVectorImpl<ASTNode> &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 ||
Expand Down Expand Up @@ -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() &&
Expand Down
4 changes: 2 additions & 2 deletions test/ASTGen/if_config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down