Skip to content

Commit 1da2631

Browse files
committed
Use SwiftIfConfig to determine where to emit new parser diagnostics
1 parent 0f2cedb commit 1da2631

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

include/swift/Bridging/ASTGen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ int swift_ASTGen_roundTripCheck(void *_Nonnull sourceFile);
4848
/// Emit parser diagnostics for given source file.. Returns non-zero if any
4949
/// diagnostics were emitted.
5050
int swift_ASTGen_emitParserDiagnostics(
51+
BridgedASTContext astContext,
5152
void *_Nonnull diagEngine, void *_Nonnull sourceFile, int emitOnlyErrors,
5253
int downgradePlaceholderErrorsToWarnings);
5354

lib/ASTGen/Sources/ASTGen/SourceFile.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import ASTBridging
1414
import SwiftDiagnostics
15+
import SwiftIfConfig
1516
@_spi(ExperimentalLanguageFeatures) import SwiftParser
1617
import SwiftParserDiagnostics
1718
import SwiftSyntax
@@ -142,20 +143,10 @@ public func roundTripCheck(
142143
}
143144
}
144145

145-
extension Syntax {
146-
/// Whether this syntax node is or is enclosed within a #if.
147-
fileprivate var isInIfConfig: Bool {
148-
if self.is(IfConfigDeclSyntax.self) {
149-
return true
150-
}
151-
152-
return parent?.isInIfConfig ?? false
153-
}
154-
}
155-
156146
/// Emit diagnostics within the given source file.
157147
@_cdecl("swift_ASTGen_emitParserDiagnostics")
158148
public func emitParserDiagnostics(
149+
ctx: BridgedASTContext,
159150
diagEnginePtr: UnsafeMutableRawPointer,
160151
sourceFilePtr: UnsafeMutablePointer<UInt8>,
161152
emitOnlyErrors: CInt,
@@ -172,11 +163,18 @@ public func emitParserDiagnostics(
172163
)
173164

174165
let diagnosticEngine = BridgedDiagnosticEngine(raw: diagEnginePtr)
166+
let buildConfiguration = CompilerBuildConfiguration(
167+
ctx: ctx,
168+
conditionLoc:
169+
BridgedSourceLoc(
170+
at: AbsolutePosition(utf8Offset: 0),
171+
in: sourceFile.pointee.buffer
172+
)
173+
)
174+
175175
for diag in diags {
176-
// Skip over diagnostics within #if, because we don't know whether
177-
// we are in an active region or not.
178-
// FIXME: This heuristic could be improved.
179-
if diag.node.isInIfConfig {
176+
// If the diagnostic is in an unparsed #if region, don't emit it.
177+
if diag.node.isActive(in: buildConfiguration).state == .unparsed {
180178
continue
181179
}
182180

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ void Parser::parseTopLevelItems(SmallVectorImpl<ASTNode> &items) {
267267
if (parsingOpts.contains(ParsingFlags::ValidateNewParserDiagnostics) &&
268268
!Context.Diags.hadAnyError()) {
269269
auto hadSyntaxError = swift_ASTGen_emitParserDiagnostics(
270-
&Context.Diags, exportedSourceFile,
270+
Context, &Context.Diags, exportedSourceFile,
271271
/*emitOnlyErrors=*/true,
272272
/*downgradePlaceholderErrorsToWarnings=*/
273273
Context.LangOpts.Playground ||
@@ -346,7 +346,7 @@ void Parser::parseSourceFileViaASTGen(
346346
// If we're supposed to emit diagnostics from the parser, do so now.
347347
if (!suppressDiagnostics) {
348348
auto hadSyntaxError = swift_ASTGen_emitParserDiagnostics(
349-
&Context.Diags, exportedSourceFile, /*emitOnlyErrors=*/false,
349+
Context, &Context.Diags, exportedSourceFile, /*emitOnlyErrors=*/false,
350350
/*downgradePlaceholderErrorsToWarnings=*/langOpts.Playground ||
351351
langOpts.WarnOnEditorPlaceholder);
352352
if (hadSyntaxError && Context.Diags.hadAnyError() &&

test/ASTGen/if_config.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// REQUIRES: asserts
44

55
#if NOT_SET
6-
func f { } // FIXME: Error once the parser diagnostics generator knows to
7-
// evaluate the active clause.
6+
func f { } // expected-error{{expected parameter clause in function signature}}
7+
// expected-note@-1{{insert parameter clause}}{{7-8=}}{{8-8=(}}{{8-8=) }}
88
#endif
99

1010
#if compiler(>=10.0)

0 commit comments

Comments
 (0)