From 88c5e2a96e09270a2ec3045639e7cab3712f5291 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 1 Jan 2020 20:54:17 -0500 Subject: [PATCH] translate-c: don't export inline functions --- src-self-hosted/clang.zig | 2 ++ src-self-hosted/translate_c.zig | 2 +- src/zig_clang.cpp | 10 ++++++++++ src/zig_clang.h | 2 ++ test/translate_c.zig | 10 ++++++++++ 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src-self-hosted/clang.zig b/src-self-hosted/clang.zig index d461a8e786..3eb2d64005 100644 --- a/src-self-hosted/clang.zig +++ b/src-self-hosted/clang.zig @@ -832,6 +832,8 @@ pub extern fn ZigClangFunctionDecl_hasBody(self: *const ZigClangFunctionDecl) bo pub extern fn ZigClangFunctionDecl_getStorageClass(self: *const ZigClangFunctionDecl) ZigClangStorageClass; pub extern fn ZigClangFunctionDecl_getParamDecl(self: *const ZigClangFunctionDecl, i: c_uint) *const struct_ZigClangParmVarDecl; pub extern fn ZigClangFunctionDecl_getBody(self: *const ZigClangFunctionDecl) *const struct_ZigClangStmt; +pub extern fn ZigClangFunctionDecl_doesDeclarationForceExternallyVisibleDefinition(self: *const ZigClangFunctionDecl) bool; +pub extern fn ZigClangFunctionDecl_isInlineSpecified(self: *const ZigClangFunctionDecl) bool; pub extern fn ZigClangBuiltinType_getKind(self: *const struct_ZigClangBuiltinType) ZigClangBuiltinTypeKind; diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 71563ca950..d0cb778bfc 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -423,7 +423,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { .has_body = has_body, .storage_class = storage_class, .is_export = switch (storage_class) { - .None => has_body, + .None => has_body and !ZigClangFunctionDecl_isInlineSpecified(fn_decl), .Extern, .Static => false, .PrivateExtern => return failDecl(c, fn_decl_loc, fn_name, "unsupported storage class: private extern", .{}), .Auto => unreachable, // Not legal on functions diff --git a/src/zig_clang.cpp b/src/zig_clang.cpp index f1e79ab34f..ba25d365f7 100644 --- a/src/zig_clang.cpp +++ b/src/zig_clang.cpp @@ -1686,6 +1686,16 @@ const struct ZigClangStmt *ZigClangFunctionDecl_getBody(const struct ZigClangFun return reinterpret_cast(stmt); } +bool ZigClangFunctionDecl_doesDeclarationForceExternallyVisibleDefinition(const struct ZigClangFunctionDecl *self) { + auto casted = reinterpret_cast(self); + return casted->doesDeclarationForceExternallyVisibleDefinition(); +} + +bool ZigClangFunctionDecl_isInlineSpecified(const struct ZigClangFunctionDecl *self) { + auto casted = reinterpret_cast(self); + return casted->isInlineSpecified(); +} + const ZigClangTypedefNameDecl *ZigClangTypedefType_getDecl(const ZigClangTypedefType *self) { auto casted = reinterpret_cast(self); const clang::TypedefNameDecl *name_decl = casted->getDecl(); diff --git a/src/zig_clang.h b/src/zig_clang.h index ce71612468..734eea48b5 100644 --- a/src/zig_clang.h +++ b/src/zig_clang.h @@ -873,6 +873,8 @@ ZIG_EXTERN_C bool ZigClangFunctionDecl_hasBody(const struct ZigClangFunctionDecl ZIG_EXTERN_C enum ZigClangStorageClass ZigClangFunctionDecl_getStorageClass(const struct ZigClangFunctionDecl *); ZIG_EXTERN_C const struct ZigClangParmVarDecl *ZigClangFunctionDecl_getParamDecl(const struct ZigClangFunctionDecl *, unsigned i); ZIG_EXTERN_C const struct ZigClangStmt *ZigClangFunctionDecl_getBody(const struct ZigClangFunctionDecl *); +ZIG_EXTERN_C bool ZigClangFunctionDecl_doesDeclarationForceExternallyVisibleDefinition(const struct ZigClangFunctionDecl *); +ZIG_EXTERN_C bool ZigClangFunctionDecl_isInlineSpecified(const struct ZigClangFunctionDecl *); ZIG_EXTERN_C bool ZigClangRecordDecl_isUnion(const struct ZigClangRecordDecl *record_decl); ZIG_EXTERN_C bool ZigClangRecordDecl_isStruct(const struct ZigClangRecordDecl *record_decl); diff --git a/test/translate_c.zig b/test/translate_c.zig index c10a9012b1..5de9abcbe7 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -2302,4 +2302,14 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , \\pub const bar = 4; }); + + cases.add("don't export inline functions", + \\inline void a(void) {} + \\static void b(void) {} + \\void c(void) {} + , &[_][]const u8{ + \\pub fn a() void {} + \\pub fn b() void {} + \\pub export fn c() void {} + }); }