From c879fe57330faa87ac22f39ec6f37d992db9790f Mon Sep 17 00:00:00 2001 From: Joey Gouly Date: Tue, 5 Nov 2013 12:30:39 +0000 Subject: [PATCH] Do not allow functions or kernels called 'main' in OpenCL. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194068 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 +- lib/Sema/SemaDecl.cpp | 7 +++++++ test/SemaOpenCL/invalid-kernel.cl | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index c1c701db08..ea0df85d72 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -6670,7 +6670,7 @@ def err_wrong_sampler_addressspace: Error< "sampler type cannot be used with the __local and __global address space qualifiers">; def err_opencl_global_invalid_addr_space : Error< "global variables must have a constant address space qualifier">; - +def err_opencl_no_main : Error<"%select{function|kernel}0 cannot be called 'main'">; } // end of sema category let CategoryName = "OpenMP Issue" in { diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index d9dd4507de..222b70cdab 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -7694,6 +7694,13 @@ void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) { FD->setConstexpr(false); } + if (getLangOpts().OpenCL) { + Diag(FD->getLocation(), diag::err_opencl_no_main) + << FD->hasAttr(); + FD->setInvalidDecl(); + return; + } + QualType T = FD->getType(); assert(T->isFunctionType() && "function decl is not of function type"); const FunctionType* FT = T->castAs(); diff --git a/test/SemaOpenCL/invalid-kernel.cl b/test/SemaOpenCL/invalid-kernel.cl index 3eb2f71aaf..c12bd8414e 100644 --- a/test/SemaOpenCL/invalid-kernel.cl +++ b/test/SemaOpenCL/invalid-kernel.cl @@ -5,3 +5,11 @@ kernel void no_ptrptr(global int **i) { } // expected-error{{kernel parameter ca kernel int bar() { // expected-error {{kernel must have void return type}} return 6; } + +kernel void main() { // expected-error {{kernel cannot be called 'main'}} + +} + +int main() { // expected-error {{function cannot be called 'main'}} + return 0; +} -- 2.40.0