]> granicus.if.org Git - clang/commitdiff
[CUDA] Disallow variadic functions other than printf in device code.
authorJustin Lebar <jlebar@google.com>
Sat, 23 Jan 2016 21:28:17 +0000 (21:28 +0000)
committerJustin Lebar <jlebar@google.com>
Sat, 23 Jan 2016 21:28:17 +0000 (21:28 +0000)
Reviewers: tra

Subscribers: cfe-commits, echristo, jhen

Differential Revision: http://reviews.llvm.org/D16484

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
test/SemaCUDA/vararg.cu [moved from test/SemaCUDA/va-arg.cu with 55% similarity]

index e9e877eacf204c64af28c9ae76e178632e61ae14..180d3d18db3dec14dc4fe06d18ff5aa3ef2fd5cc 100644 (file)
@@ -6423,6 +6423,8 @@ def warn_kern_is_method : Extension<
 def warn_kern_is_inline : Warning<
   "ignored 'inline' attribute on kernel function %0">,
   InGroup<CudaCompat>;
+def err_variadic_device_fn : Error<
+  "CUDA device code does not support variadic functions">;
 def err_va_arg_in_device : Error<
   "CUDA device code does not support va_arg">;
 def err_alias_not_supported_on_nvptx : Error<"CUDA does not support aliases">;
index 16ad13db90d491559a86aeacd5559d1ae5edffb0..6adbc3fab6f6a79e59294ab28293ec327163df52 100644 (file)
@@ -8276,18 +8276,26 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
 
   MarkUnusedFileScopedDecl(NewFD);
 
-  if (getLangOpts().CUDA)
-    if (IdentifierInfo *II = NewFD->getIdentifier())
-      if (!NewFD->isInvalidDecl() &&
-          NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
-        if (II->isStr("cudaConfigureCall")) {
-          if (!R->getAs<FunctionType>()->getReturnType()->isScalarType())
-            Diag(NewFD->getLocation(), diag::err_config_scalar_return);
-
-          Context.setcudaConfigureCallDecl(NewFD);
-        }
-      }
-  
+  if (getLangOpts().CUDA) {
+    IdentifierInfo *II = NewFD->getIdentifier();
+    if (II && II->isStr("cudaConfigureCall") && !NewFD->isInvalidDecl() &&
+        NewFD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
+      if (!R->getAs<FunctionType>()->getReturnType()->isScalarType())
+        Diag(NewFD->getLocation(), diag::err_config_scalar_return);
+
+      Context.setcudaConfigureCallDecl(NewFD);
+    }
+
+    // Variadic functions, other than a *declaration* of printf, are not allowed
+    // in device-side CUDA code.
+    if (NewFD->isVariadic() && (NewFD->hasAttr<CUDADeviceAttr>() ||
+                                NewFD->hasAttr<CUDAGlobalAttr>()) &&
+        !(II && II->isStr("printf") && NewFD->isExternC() &&
+          !D.isFunctionDefinition())) {
+      Diag(NewFD->getLocation(), diag::err_variadic_device_fn);
+    }
+  }
+
   // Here we have an function template explicit specialization at class scope.
   // The actually specialization will be postponed to template instatiation
   // time via the ClassScopeFunctionSpecializationDecl node.
similarity index 55%
rename from test/SemaCUDA/va-arg.cu
rename to test/SemaCUDA/vararg.cu
index aad9b74c2d80af291d353d44935d3dbc10cc9a97..f3fe26376076062eca11ee44c1ce486e1d6540b4 100644 (file)
@@ -2,7 +2,7 @@
 // REQUIRES: nvptx-registered-target
 // RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fcuda-is-device -fsyntax-only \
 // RUN:   -verify -DEXPECT_ERR %s
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsyntax-only %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsyntax-only -verify %s
 
 #include <stdarg.h>
 #include "Inputs/cuda.h"
@@ -26,3 +26,17 @@ __device__ void baz() {
   va_arg(list, int);  // OK: only seen when compiling for host
 #endif
 }
+
+__device__ void vararg(const char* x, ...) {}
+// expected-error@-1 {{CUDA device code does not support variadic functions}}
+
+extern "C" __device__ int printf(const char* fmt, ...);  // OK, special case.
+
+// Definition of printf not allowed.
+extern "C" __device__ int printf(const char* fmt, ...) { return 0; }
+// expected-error@-1 {{CUDA device code does not support variadic functions}}
+
+namespace ns {
+__device__ int printf(const char* fmt, ...);
+// expected-error@-1 {{CUDA device code does not support variadic functions}}
+}