[clang][WebAssembly] Loosen restriction on `main` symbol mangling
authorSam Clegg <sbc@chromium.org>
Wed, 15 Jun 2022 18:30:13 +0000 (11:30 -0700)
committerSam Clegg <sbc@chromium.org>
Wed, 15 Jun 2022 20:56:05 +0000 (13:56 -0700)
Remove the `hasPrototype()` restriction so that old style K&R
declarations of main work too.

For example the following has 2 params but no prototype.

```
int main(argc, argv)
    int argc;
    char *argv[];
{
  return 0;
}
```

Also, use `getNumParams()` over `param_size()` which seems to be a more
direct way to get at the same information.

Also, add missing tests for this mangling.

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

clang/lib/AST/Mangle.cpp
clang/test/CodeGen/mangle-wasm-main-noproto.c [new file with mode: 0644]
clang/test/CodeGen/mangle-wasm-main-void.c [new file with mode: 0644]
clang/test/CodeGen/mangle-wasm-main.c [new file with mode: 0644]

index c1a345390091bfcc5bb3ce53fcaf78b7ec463818..7ea569c63d9e4a4230bcc2055ae3a7e13e67dae2 100644 (file)
@@ -72,7 +72,7 @@ static CCMangling getCallingConvMangling(const ASTContext &Context,
   // can call it with the correct function signature.
   if (Triple.isWasm())
     if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
-      if (FD->isMain() && FD->hasPrototype() && FD->param_size() == 2)
+      if (FD->isMain() && FD->getNumParams() == 2)
         return CCM_WasmMainArgcArgv;
 
   if (!Triple.isOSWindows() || !Triple.isX86())
diff --git a/clang/test/CodeGen/mangle-wasm-main-noproto.c b/clang/test/CodeGen/mangle-wasm-main-noproto.c
new file mode 100644 (file)
index 0000000..313ce36
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -emit-llvm %s -o - -triple=wasm32-unknown-unknown -Wno-deprecated-non-prototype | FileCheck %s
+
+int main(argc, argv)
+  int argc;
+  char* argv[];
+{
+  return 0;
+}
+// CHECK-NOT: __main_void
+// CHECK: define i32 @__main_argc_argv(i32 noundef %argc, ptr noundef %argv) #0 { 
diff --git a/clang/test/CodeGen/mangle-wasm-main-void.c b/clang/test/CodeGen/mangle-wasm-main-void.c
new file mode 100644 (file)
index 0000000..f541e55
--- /dev/null
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -emit-llvm %s -o - -triple=wasm32-unknown-unknown | FileCheck %s
+
+int main(void) {
+  return 0;
+}
+// CHECK: @__main_void = hidden alias i32 (), ptr @main
+// CHECK: define i32 @main() #0 {
diff --git a/clang/test/CodeGen/mangle-wasm-main.c b/clang/test/CodeGen/mangle-wasm-main.c
new file mode 100644 (file)
index 0000000..3a16d06
--- /dev/null
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -emit-llvm %s -o - -triple=wasm32-unknown-unknown | FileCheck %s
+
+int main(int argc, char* argv[]) {
+  return 0;
+}
+// CHECK-NOT: __main_void
+// CHECK: define i32 @__main_argc_argv(i32 noundef %argc, ptr noundef %argv) #0 {