Skip to content

Commit b08895b

Browse files
committed
Add a few more arguments to allow errors during indexing
I noticed that we were missing a bunch more arguments (in particular for clang index invocations), while diagnosing rdar://129071600, which is fixed by passing `-experimental-allow-module-with-compiler-errors` to the Swift index invocations. rdar://129071600
1 parent e011829 commit b08895b

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,6 @@ private func adjustSwiftCompilerArgumentsForIndexStoreUpdate(
430430
.option("output-file-map", [.singleDash], [.separatedBySpace, .separatedByEqualSign]),
431431
]
432432

433-
let removeFrontendFlags = [
434-
"-experimental-skip-non-inlinable-function-bodies",
435-
"-experimental-skip-all-function-bodies",
436-
]
437-
438433
var result: [String] = []
439434
result.reserveCapacity(compilerArguments.count)
440435
var iterator = compilerArguments.makeIterator()
@@ -448,18 +443,14 @@ private func adjustSwiftCompilerArgumentsForIndexStoreUpdate(
448443
case nil:
449444
break
450445
}
451-
if argument == "-Xfrontend" {
452-
if let nextArgument = iterator.next() {
453-
if removeFrontendFlags.contains(nextArgument) {
454-
continue
455-
}
456-
result += [argument, nextArgument]
457-
continue
458-
}
459-
}
460446
result.append(argument)
461447
}
448+
result += supplementalClangIndexingArgs.flatMap { ["-Xcc", $0] }
462449
result += [
450+
// Preparation produces modules with errors. We should allow reading them.
451+
"-Xfrontend", "-experimental-allow-module-with-compiler-errors",
452+
// Avoid emitting the ABI descriptor, we don't need it
453+
"-Xfrontend", "-empty-abi-descriptor",
463454
"-index-file",
464455
"-index-file-path", fileToIndex.pseudoPath,
465456
// batch mode is not compatible with -index-file
@@ -520,12 +511,40 @@ private func adjustClangCompilerArgumentsForIndexStoreUpdate(
520511
}
521512
result.append(argument)
522513
}
514+
result += supplementalClangIndexingArgs
523515
result.append(
524516
"-fsyntax-only"
525517
)
526518
return result
527519
}
528520

521+
#if compiler(>=6.1)
522+
#warning(
523+
"Remove -fmodules-validate-system-headers from supplementalClangIndexingArgs once all supported Swift compilers have https://github.com/apple/swift/pull/74063"
524+
)
525+
#endif
526+
527+
fileprivate let supplementalClangIndexingArgs: [String] = [
528+
// Retain extra information for indexing
529+
"-fretain-comments-from-system-headers",
530+
// Pick up macro definitions during indexing
531+
"-Xclang", "-detailed-preprocessing-record",
532+
533+
// libclang uses 'raw' module-format. Match it so we can reuse the module cache and PCHs that libclang uses.
534+
"-Xclang", "-fmodule-format=raw",
535+
536+
// Be less strict - we want to continue and typecheck/index as much as possible
537+
"-Xclang", "-fallow-pch-with-compiler-errors",
538+
"-Xclang", "-fallow-pcm-with-compiler-errors",
539+
"-Wno-non-modular-include-in-framework-module",
540+
"-Wno-incomplete-umbrella",
541+
542+
// sourcekitd adds `-fno-modules-validate-system-headers` before https://github.com/apple/swift/pull/74063.
543+
// This completely disables system module validation and never re-builds pcm for system modules. The intended behavior
544+
// is to only re-build those PCMs once per sourcekitd session.
545+
"-fmodules-validate-system-headers",
546+
]
547+
529548
fileprivate extension Sequence {
530549
/// Returns `true` if this sequence contains an element that is equal to an element in `otherSequence` when
531550
/// considering two elements as equal if they satisfy `predicate`.

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,4 +887,47 @@ final class BackgroundIndexingTests: XCTestCase {
887887
_ = try project.openDocument("Lib.swift")
888888
_ = try await project.testClient.send(BarrierRequest())
889889
}
890+
891+
func testImportPreparedModuleWithFunctionBodiesSkipped() async throws {
892+
// This test case was crashing the indexing compiler invocation for Client if Lib was built for index preparation
893+
// (using `-enable-library-evolution -experimental-skip-all-function-bodies -experimental-lazy-typecheck`) but x
894+
// Client was not indexed with `-experimental-allow-module-with-compiler-errors`. rdar://129071600
895+
let project = try await SwiftPMTestProject(
896+
files: [
897+
"Lib/Lib.swift": """
898+
public class TerminalController {
899+
public var 1️⃣width: Int { 1 }
900+
}
901+
""",
902+
"Client/Client.swift": """
903+
import Lib
904+
905+
func test(terminal: TerminalController) {
906+
let width = terminal.width
907+
}
908+
""",
909+
],
910+
manifest: """
911+
// swift-tools-version: 5.7
912+
913+
import PackageDescription
914+
915+
let package = Package(
916+
name: "MyLibrary",
917+
targets: [
918+
.target(name: "Lib"),
919+
.target(name: "Client", dependencies: ["Lib"]),
920+
]
921+
)
922+
""",
923+
enableBackgroundIndexing: true
924+
)
925+
let (uri, positions) = try project.openDocument("Lib.swift")
926+
927+
// Check that we indexed `Client.swift` by checking that we return a rename location within it.
928+
let result = try await project.testClient.send(
929+
RenameRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"], newName: "height")
930+
)
931+
XCTAssertEqual((result?.changes?.keys).map(Set.init), [uri, try project.uri(for: "Client.swift")])
932+
}
890933
}

0 commit comments

Comments
 (0)