]> granicus.if.org Git - clang/commitdiff
[WebAssembly] Add no-prototype attribute to prototype-less C functions
authorSam Clegg <sbc@chromium.org>
Mon, 25 Jun 2018 18:47:32 +0000 (18:47 +0000)
committerSam Clegg <sbc@chromium.org>
Mon, 25 Jun 2018 18:47:32 +0000 (18:47 +0000)
The WebAssembly backend in particular benefits from being
able to distinguish between varargs functions (...) and prototype-less
C functions.

Differential Revision: https://reviews.llvm.org/D48443

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@335510 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/TargetInfo.cpp
test/CodeGen/no-prototype.c [new file with mode: 0644]

index b29bcce237cffd232f75ba300aa900ae398939eb..3fcafbbfd371356c9928980a3d1fa7bcd8d11c57 100644 (file)
@@ -747,6 +747,15 @@ class WebAssemblyTargetCodeGenInfo final : public TargetCodeGenInfo {
 public:
   explicit WebAssemblyTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
       : TargetCodeGenInfo(new WebAssemblyABIInfo(CGT)) {}
+
+  void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
+                           CodeGen::CodeGenModule &CGM) const override {
+    if (auto *FD = dyn_cast_or_null<FunctionDecl>(D)) {
+      llvm::Function *Fn = cast<llvm::Function>(GV);
+      if (!FD->doesThisDeclarationHaveABody() && !FD->hasPrototype())
+        Fn->addFnAttr("no-prototype");
+    }
+  }
 };
 
 /// Classify argument of given type \p Ty.
diff --git a/test/CodeGen/no-prototype.c b/test/CodeGen/no-prototype.c
new file mode 100644 (file)
index 0000000..6030357
--- /dev/null
@@ -0,0 +1,20 @@
+// REQUIRES: webassembly-registered-target
+// RUN: %clang_cc1 -triple wasm32 -emit-llvm %s -o - | FileCheck %s
+
+int foo();
+
+int bar(int a) {
+  return foo();
+}
+
+int baz() {
+  return foo();
+}
+
+// CHECK: define i32 @bar(i32 %a) [[BAR_ATTR:#[0-9]+]] {
+// CHECK: declare i32 @foo(...) [[FOO_ATTR:#[0-9]+]]
+// CHECK: define i32 @baz() [[BAZ_ATTR:#[0-9]+]] {
+
+// CHECK: attributes [[FOO_ATTR]] = {  {{.*}}"no-prototype"{{.*}} }
+// CHECK-NOT: attributes [[BAR_ATTR]] = {  {{.*}}"no-prototype"{{.*}} }
+// CHECK-NOT: attributes [[BAZ_ATTR]] = {  {{.*}}"no-prototype"{{.*}} }