Compilation: Override Clang's language type for header files.

Clang seems to treat them as linker input without this.
This commit is contained in:
Alex Rønne Petersen 2024-12-13 04:56:35 +01:00
parent b6ece854c9
commit 88f324ebe7
No known key found for this signature in database

View File

@ -4687,19 +4687,19 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
try argv.appendSlice(&[_][]const u8{ self_exe_path, "clang" });
// if "ext" is explicit, add "-x <lang>". Otherwise let clang do its thing.
if (c_object.src.ext != null) {
if (c_object.src.ext != null or ext.clangNeedsLanguageOverride()) {
try argv.appendSlice(&[_][]const u8{ "-x", switch (ext) {
.assembly => "assembler",
.assembly_with_cpp => "assembler-with-cpp",
.c => "c",
.cpp => "c++",
.h => "c-header",
.cpp => "c++",
.hpp => "c++-header",
.m => "objective-c",
.hm => "objective-c-header",
.mm => "objective-c++",
.hmm => "objective-c++-header",
.cu => "cuda",
.m => "objective-c",
.mm => "objective-c++",
else => fatal("language '{s}' is unsupported in this context", .{@tagName(ext)}),
} });
}
@ -5840,6 +5840,36 @@ pub const FileExt = enum {
manifest,
unknown,
pub fn clangNeedsLanguageOverride(ext: FileExt) bool {
return switch (ext) {
.h,
.hpp,
.hm,
.hmm,
=> true,
.c,
.cpp,
.cu,
.m,
.mm,
.ll,
.bc,
.assembly,
.assembly_with_cpp,
.shared_library,
.object,
.static_library,
.zig,
.def,
.rc,
.res,
.manifest,
.unknown,
=> false,
};
}
pub fn clangSupportsDiagnostics(ext: FileExt) bool {
return switch (ext) {
.c, .cpp, .h, .hpp, .hm, .hmm, .m, .mm, .cu, .ll, .bc => true,