Skip to content

[clang] Workaround module format issue with -gmodules #285

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 2 commits into from
Jun 4, 2020
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
17 changes: 16 additions & 1 deletion Sources/SourceKit/Clang/ClangLanguageServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,22 @@ extension ClangLanguageServerShim {
return "settings for \(uri): \(settingsStr)"
}

if let settings = settings {
if var settings = settings {
if settings.compilerArguments.contains("-fmodules") == true {
// Clangd is not built with support for the 'obj' format.
settings.compilerArguments.append(contentsOf: [
"-Xclang", "-fmodule-format=raw"
])
}
if let workingDirectory = settings.workingDirectory {
// FIXME: this is a workaround for clangd not respecting the compilation
// database's "directory" field for relative -fmodules-cache-path.
// rdar://63984913
settings.compilerArguments.append(contentsOf: [
"-working-directory", workingDirectory
])
}

clangd.send(DidChangeConfigurationNotification(settings: .clangd(
ClangWorkspaceSettings(
compilationDatabaseChanges: [url.path: ClangCompileCommand(settings, clang: clang)]))))
Expand Down
6 changes: 6 additions & 0 deletions Tests/INPUTS/ClangModules/ClangModuleA.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef ClangModuleA_h
#define ClangModuleA_h

void func_ClangModuleA(void);

#endif /* ClangModuleA_h */
6 changes: 6 additions & 0 deletions Tests/INPUTS/ClangModules/ClangModules_main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*main_file*/
@import ClangModuleA;

void test(void) {
func_ClangModuleA();
}
3 changes: 3 additions & 0 deletions Tests/INPUTS/ClangModules/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ClangModuleA {
header "ClangModuleA.h"
}
4 changes: 4 additions & 0 deletions Tests/INPUTS/ClangModules/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"clang_flags": ["-gmodules"],
"sources": ["ClangModules_main.m"]
}
18 changes: 18 additions & 0 deletions Tests/SourceKitTests/LocalClangTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,22 @@ final class LocalClangTests: XCTestCase {
fatalError("error \(result) waiting for diagnostics notification")
}
}

func testClangModules() {
guard let ws = try! staticSourceKitTibsWorkspace(name: "ClangModules") else { return }
if ToolchainRegistry.shared.default?.clangd == nil { return }

let loc = ws.testLoc("main_file")

let expectation = self.expectation(description: "diagnostics")

ws.sk.handleNextNotification { (note: Notification<PublishDiagnosticsNotification>) in
XCTAssertEqual(note.params.diagnostics.count, 0)
expectation.fulfill()
}

try! ws.openDocument(loc.url, language: .objective_c)

waitForExpectations(timeout: 15)
}
}
1 change: 1 addition & 0 deletions Tests/SourceKitTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ extension LocalClangTests {
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__LocalClangTests = [
("testClangModules", testClangModules),
("testClangStdHeaderCanary", testClangStdHeaderCanary),
("testFoldingRange", testFoldingRange),
("testSymbolInfo", testSymbolInfo),
Expand Down