Skip to content

Commit cfd0a3f

Browse files
committed
Thread a source location through canImport evaluation in CompilerBuildConfiguration
Unlike all of the other build configuration checks, `canImport` has side effects including the emission of various diagnostics. Thread a source location through here so the diagnostics end up on the right line.
1 parent d686a3e commit cfd0a3f

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,10 @@ enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedCanImportVersion : size_t {
252252
CanImportUnderlyingVersion,
253253
};
254254

255-
SWIFT_NAME("BridgedASTContext.canImport(self:importPath:versionKind:versionComponents:numVersionComponents:)")
255+
SWIFT_NAME("BridgedASTContext.canImport(self:importPath:location:versionKind:versionComponents:numVersionComponents:)")
256256
bool BridgedASTContext_canImport(BridgedASTContext cContext,
257257
BridgedStringRef importPath,
258+
BridgedSourceLoc canImportLoc,
258259
BridgedCanImportVersion versionKind,
259260
const SwiftInt * _Nullable versionComponents,
260261
SwiftInt numVersionComponents);

lib/AST/ASTBridging.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ SwiftInt BridgedASTContext_langOptsGetTargetAtomicBitWidths(BridgedASTContext cC
216216

217217
bool BridgedASTContext_canImport(BridgedASTContext cContext,
218218
BridgedStringRef importPath,
219+
BridgedSourceLoc canImportLoc,
219220
BridgedCanImportVersion versionKind,
220221
const SwiftInt * _Nullable versionComponents,
221222
SwiftInt numVersionComponents) {
@@ -240,15 +241,11 @@ bool BridgedASTContext_canImport(BridgedASTContext cContext,
240241
break;
241242
}
242243

243-
// FIXME: The source location here is empty because build configurations
244-
// are supposed to be completely separated from source code. We could re-plumb
245-
// things to have any errors reported up through the "canImportModule"
246-
// API.
247244
ImportPath::Module::Builder builder(
248245
cContext.unbridged(), importPath.unbridged(), /*separator=*/'.',
249-
SourceLoc());
246+
canImportLoc.unbridged());
250247
return cContext.unbridged().canImportModule(
251-
builder.get(), SourceLoc(), version,
248+
builder.get(), canImportLoc.unbridged(), version,
252249
versionKind == CanImportUnderlyingVersion);
253250
}
254251

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2598,7 +2598,10 @@ bool ASTContext::canImportModuleImpl(ImportPath::Module ModuleName,
25982598
// The module version could not be parsed from the preferred source for
25992599
// this query. Diagnose and treat the query as if it succeeded.
26002600
auto mID = ModuleName[0];
2601-
Diags.diagnose(mID.Loc, diag::cannot_find_project_version,
2601+
auto diagLoc = mID.Loc;
2602+
if (mID.Loc.isInvalid())
2603+
diagLoc = loc;
2604+
Diags.diagnose(diagLoc, diag::cannot_find_project_version,
26022605
getModuleVersionKindString(bestVersionInfo), mID.Item.str());
26032606
return true;
26042607
}

lib/ASTGen/Sources/ASTGen/CompilerBuildConfiguration.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import ASTBridging
14+
import SwiftDiagnostics
1415
import SwiftIfConfig
16+
import SwiftParser
1517
import SwiftSyntax
1618

1719
/// A build configuration that uses the compiler's ASTContext to answer
1820
/// queries.
1921
struct CompilerBuildConfiguration: BuildConfiguration {
2022
let ctx: BridgedASTContext
23+
let conditionLoc: BridgedSourceLoc
2124

2225
func isCustomConditionSet(name: String) throws -> Bool {
2326
var name = name
@@ -63,6 +66,7 @@ struct CompilerBuildConfiguration: BuildConfiguration {
6366
versionComponents.withUnsafeBufferPointer { versionComponentsBuf in
6467
ctx.canImport(
6568
importPath: bridgedImportPathStr,
69+
location: conditionLoc,
6670
versionKind: cVersionKind,
6771
versionComponents: versionComponentsBuf.baseAddress,
6872
numVersionComponents: versionComponentsBuf.count
@@ -154,12 +158,15 @@ public func configuredRegions(
154158
cRegionsOut: UnsafeMutablePointer<UnsafeMutablePointer<BridgedIfConfigClauseRangeInfo>?>
155159
) -> Int {
156160
let sourceFilePtr = sourceFilePtr.bindMemory(to: ExportedSourceFile.self, capacity: 1)
157-
let configuration = CompilerBuildConfiguration(ctx: astContext)
161+
let configuration = CompilerBuildConfiguration(
162+
ctx: astContext,
163+
conditionLoc: sourceFilePtr.pointee.sourceLoc(at: AbsolutePosition(utf8Offset: 0))
164+
)
158165
let regions = sourceFilePtr.pointee.syntax.configuredRegions(in: configuration)
159166

160167
var cRegions: [BridgedIfConfigClauseRangeInfo] = []
161168

162-
// Keep track of the enclosing #ifs so that we can emit and "#endif" directive
169+
// Keep track of the enclosing #ifs so that we can emit an "#endif" directive
163170
// right before moving on to the next #if (and at the end).
164171
var ifConfigStack: [IfConfigDeclSyntax] = []
165172

0 commit comments

Comments
 (0)