Skip to content

Commit 88c5e2a

Browse files
committed
translate-c: don't export inline functions
1 parent 5ba143e commit 88c5e2a

File tree

5 files changed

+25
-1
lines changed

5 files changed

+25
-1
lines changed

src-self-hosted/clang.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,8 @@ pub extern fn ZigClangFunctionDecl_hasBody(self: *const ZigClangFunctionDecl) bo
832832
pub extern fn ZigClangFunctionDecl_getStorageClass(self: *const ZigClangFunctionDecl) ZigClangStorageClass;
833833
pub extern fn ZigClangFunctionDecl_getParamDecl(self: *const ZigClangFunctionDecl, i: c_uint) *const struct_ZigClangParmVarDecl;
834834
pub extern fn ZigClangFunctionDecl_getBody(self: *const ZigClangFunctionDecl) *const struct_ZigClangStmt;
835+
pub extern fn ZigClangFunctionDecl_doesDeclarationForceExternallyVisibleDefinition(self: *const ZigClangFunctionDecl) bool;
836+
pub extern fn ZigClangFunctionDecl_isInlineSpecified(self: *const ZigClangFunctionDecl) bool;
835837

836838
pub extern fn ZigClangBuiltinType_getKind(self: *const struct_ZigClangBuiltinType) ZigClangBuiltinTypeKind;
837839

src-self-hosted/translate_c.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
423423
.has_body = has_body,
424424
.storage_class = storage_class,
425425
.is_export = switch (storage_class) {
426-
.None => has_body,
426+
.None => has_body and !ZigClangFunctionDecl_isInlineSpecified(fn_decl),
427427
.Extern, .Static => false,
428428
.PrivateExtern => return failDecl(c, fn_decl_loc, fn_name, "unsupported storage class: private extern", .{}),
429429
.Auto => unreachable, // Not legal on functions

src/zig_clang.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,16 @@ const struct ZigClangStmt *ZigClangFunctionDecl_getBody(const struct ZigClangFun
16861686
return reinterpret_cast<const ZigClangStmt *>(stmt);
16871687
}
16881688

1689+
bool ZigClangFunctionDecl_doesDeclarationForceExternallyVisibleDefinition(const struct ZigClangFunctionDecl *self) {
1690+
auto casted = reinterpret_cast<const clang::FunctionDecl *>(self);
1691+
return casted->doesDeclarationForceExternallyVisibleDefinition();
1692+
}
1693+
1694+
bool ZigClangFunctionDecl_isInlineSpecified(const struct ZigClangFunctionDecl *self) {
1695+
auto casted = reinterpret_cast<const clang::FunctionDecl *>(self);
1696+
return casted->isInlineSpecified();
1697+
}
1698+
16891699
const ZigClangTypedefNameDecl *ZigClangTypedefType_getDecl(const ZigClangTypedefType *self) {
16901700
auto casted = reinterpret_cast<const clang::TypedefType *>(self);
16911701
const clang::TypedefNameDecl *name_decl = casted->getDecl();

src/zig_clang.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,8 @@ ZIG_EXTERN_C bool ZigClangFunctionDecl_hasBody(const struct ZigClangFunctionDecl
873873
ZIG_EXTERN_C enum ZigClangStorageClass ZigClangFunctionDecl_getStorageClass(const struct ZigClangFunctionDecl *);
874874
ZIG_EXTERN_C const struct ZigClangParmVarDecl *ZigClangFunctionDecl_getParamDecl(const struct ZigClangFunctionDecl *, unsigned i);
875875
ZIG_EXTERN_C const struct ZigClangStmt *ZigClangFunctionDecl_getBody(const struct ZigClangFunctionDecl *);
876+
ZIG_EXTERN_C bool ZigClangFunctionDecl_doesDeclarationForceExternallyVisibleDefinition(const struct ZigClangFunctionDecl *);
877+
ZIG_EXTERN_C bool ZigClangFunctionDecl_isInlineSpecified(const struct ZigClangFunctionDecl *);
876878

877879
ZIG_EXTERN_C bool ZigClangRecordDecl_isUnion(const struct ZigClangRecordDecl *record_decl);
878880
ZIG_EXTERN_C bool ZigClangRecordDecl_isStruct(const struct ZigClangRecordDecl *record_decl);

test/translate_c.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,4 +2302,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
23022302
,
23032303
\\pub const bar = 4;
23042304
});
2305+
2306+
cases.add("don't export inline functions",
2307+
\\inline void a(void) {}
2308+
\\static void b(void) {}
2309+
\\void c(void) {}
2310+
, &[_][]const u8{
2311+
\\pub fn a() void {}
2312+
\\pub fn b() void {}
2313+
\\pub export fn c() void {}
2314+
});
23052315
}

0 commit comments

Comments
 (0)