Skip to content

Commit 6b6e9aa

Browse files
authored
Merge pull request #76074 from DougGregor/parse-diags-build-config
2 parents 8315120 + 9b1c938 commit 6b6e9aa

10 files changed

+42
-32
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/ASTGen+CompilerBuildConfiguration.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ extension ASTGenVisitor {
2525
/// produced due to the evaluation.
2626
func activeClause(in node: IfConfigDeclSyntax) -> IfConfigClauseSyntax? {
2727
// Determine the active clause.
28-
var buildConfiguration = self.buildConfiguration
29-
buildConfiguration.conditionLoc = generateSourceLoc(node)
3028
let (activeClause, diagnostics) = node.activeClause(in: buildConfiguration)
3129
diagnoseAll(diagnostics)
3230

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ struct ASTGenVisitor {
9898
self.legacyParse = legacyParser
9999
self.buildConfiguration = CompilerBuildConfiguration(
100100
ctx: ctx,
101-
conditionLoc: BridgedSourceLoc(at: AbsolutePosition(utf8Offset: 0), in: sourceBuffer)
101+
sourceBuffer: sourceBuffer
102102
)
103103
}
104104

lib/ASTGen/Sources/ASTGen/CompilerBuildConfiguration.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import SwiftSyntax
2020
/// queries.
2121
struct CompilerBuildConfiguration: BuildConfiguration {
2222
let ctx: BridgedASTContext
23-
var conditionLoc: BridgedSourceLoc
23+
let sourceBuffer: UnsafeBufferPointer<UInt8>
2424

25-
init(ctx: BridgedASTContext, conditionLoc: BridgedSourceLoc) {
25+
init(ctx: BridgedASTContext, sourceBuffer: UnsafeBufferPointer<UInt8>) {
2626
self.ctx = ctx
27-
self.conditionLoc = conditionLoc
27+
self.sourceBuffer = sourceBuffer
2828
}
2929

3030
func isCustomConditionSet(name: String) throws -> Bool {
@@ -48,8 +48,11 @@ struct CompilerBuildConfiguration: BuildConfiguration {
4848
}
4949
}
5050

51-
func canImport(importPath: [String], version: CanImportVersion) throws -> Bool {
52-
var importPathStr = importPath.joined(separator: ".")
51+
func canImport(
52+
importPath: [(TokenSyntax, String)],
53+
version: CanImportVersion
54+
) throws -> Bool {
55+
var importPathStr = importPath.map { $0.1 }.joined(separator: ".")
5356

5457
var versionComponents: [Int]
5558
let cVersionKind: BridgedCanImportVersion
@@ -71,7 +74,10 @@ struct CompilerBuildConfiguration: BuildConfiguration {
7174
versionComponents.withUnsafeBufferPointer { versionComponentsBuf in
7275
ctx.canImport(
7376
importPath: bridgedImportPathStr,
74-
location: conditionLoc,
77+
location: BridgedSourceLoc(
78+
at: importPath.first!.0.position,
79+
in: sourceBuffer
80+
),
7581
versionKind: cVersionKind,
7682
versionComponents: versionComponentsBuf.baseAddress,
7783
numVersionComponents: versionComponentsBuf.count
@@ -165,7 +171,7 @@ public func configuredRegions(
165171
let sourceFilePtr = sourceFilePtr.bindMemory(to: ExportedSourceFile.self, capacity: 1)
166172
let configuration = CompilerBuildConfiguration(
167173
ctx: astContext,
168-
conditionLoc: sourceFilePtr.pointee.sourceLoc(at: AbsolutePosition(utf8Offset: 0))
174+
sourceBuffer: sourceFilePtr.pointee.buffer
169175
)
170176
let regions = sourceFilePtr.pointee.syntax.configuredRegions(in: configuration)
171177

lib/ASTGen/Sources/ASTGen/SourceFile.swift

Lines changed: 12 additions & 18 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,
@@ -167,16 +158,19 @@ public func emitParserDiagnostics(
167158
) { sourceFile in
168159
var anyDiags = false
169160

170-
let diags = ParseDiagnosticsGenerator.diagnostics(
171-
for: sourceFile.pointee.syntax
172-
)
161+
let sourceFileSyntax = sourceFile.pointee.syntax
162+
let diags = ParseDiagnosticsGenerator.diagnostics(for: sourceFileSyntax)
173163

174164
let diagnosticEngine = BridgedDiagnosticEngine(raw: diagEnginePtr)
165+
let buildConfiguration = CompilerBuildConfiguration(
166+
ctx: ctx,
167+
sourceBuffer: sourceFile.pointee.buffer
168+
)
169+
170+
let configuredRegions = sourceFileSyntax.configuredRegions(in: buildConfiguration)
175171
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 {
172+
// If the diagnostic is in an unparsed #if region, don't emit it.
173+
if configuredRegions.isActive(diag.node) == .unparsed {
180174
continue
181175
}
182176

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)

test/ClangImporter/can_import_underlying_version_tbd_missing_version.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import Simple
1010

1111
func canImportUnderlyingVersion() {
1212
#if canImport(Simple, _underlyingVersion: 3.3) // expected-warning {{cannot find user version number for Clang module 'Simple'; version number ignored}}
13+
// TODO(ParserValidation): expected-warning@-1 *{{cannot find user version number for Clang module 'Simple'; version number ignored}}
1314
let a = 1 // expected-warning {{initialization of immutable value 'a' was never used; consider replacing with assignment to '_' or removing it}}
1415
#endif
1516
}
1617

1718
func canImportVersion() {
1819
#if canImport(Simple, _version: 3.3) // expected-warning {{cannot find user version number for Clang module 'Simple'; version number ignored}}
20+
// TODO(ParserValidation): expected-warning@-1 *{{cannot find user version number for Clang module 'Simple'; version number ignored}}
1921
let a = 1 // expected-warning {{initialization of immutable value 'a' was never used; consider replacing with assignment to '_' or removing it}}
2022
#endif
2123
}

test/ClangImporter/can_import_version_ignores_missing_tbd_version.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ import Simple
2323

2424
func canImportUnderlyingVersion() {
2525
#if canImport(Simple, _underlyingVersion: 2) // expected-warning {{cannot find user version number for Clang module 'Simple'; version number ignored}}
26+
// TODO(ParserValidation): expected-warning@-1 *{{cannot find user version number for Clang module 'Simple'; version number ignored}}
2627
let a = 1 // expected-warning {{initialization of immutable value 'a' was never used; consider replacing with assignment to '_' or removing it}}
2728
#endif
2829

2930
#if canImport(Simple, _underlyingVersion: 3) // expected-warning {{cannot find user version number for Clang module 'Simple'; version number ignored}}
31+
// TODO(ParserValidation): expected-warning@-1 *{{cannot find user version number for Clang module 'Simple'; version number ignored}}
3032
let b = 1 // expected-warning {{initialization of immutable value 'b' was never used; consider replacing with assignment to '_' or removing it}}
3133
#endif
3234

3335
#if canImport(Simple, _underlyingVersion: 4) // expected-warning {{cannot find user version number for Clang module 'Simple'; version number ignored}}
36+
// TODO(ParserValidation): expected-warning@-1 *{{cannot find user version number for Clang module 'Simple'; version number ignored}}
3437
let c = 1 // expected-warning {{initialization of immutable value 'c' was never used; consider replacing with assignment to '_' or removing it}}
3538
#endif
3639

test/Parse/ConditionalCompilation/can_import_version_missing_user_module_version.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ import NoUserModuleVersion
1414
func testCanImportNoUserModuleVersion() {
1515

1616
#if canImport(NoUserModuleVersion, _version: 113.331) // expected-warning {{cannot find user version number for Swift module 'NoUserModuleVersion'; version number ignored}}
17+
// NOTE: Duplicate warning because the canImport request happens twice when parser
18+
// validation is enabled.
19+
// TODO(ParserValidation): expected-warning@-3 *{{cannot find user version number for Swift module 'NoUserModuleVersion'; version number ignored}}
1720
let a = 1 // expected-warning {{initialization of immutable value 'a' was never used; consider replacing with assignment to '_' or removing it}}
1821
#endif
1922

2023
#if canImport(NoUserModuleVersion, _version: 2) // expected-warning {{cannot find user version number for Swift module 'NoUserModuleVersion'; version number ignored}}
2124
let b = 1 // expected-warning {{initialization of immutable value 'b' was never used; consider replacing with assignment to '_' or removing it}}
25+
// NOTE: Duplicate warning because the canImport request happens twice when parser
26+
// validation is enabled.
27+
// TODO(ParserValidation): expected-warning@-4 *{{cannot find user version number for Swift module 'NoUserModuleVersion'; version number ignored}}
2228
#endif
2329

2430
}

0 commit comments

Comments
 (0)