diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -818,6 +818,8 @@ def cl_uniform_work_group_size : Flag<["-"], "cl-uniform-work-group-size">, Group, Flags<[CC1Option]>, HelpText<"OpenCL only. Defines that the global work-size be a multiple of the work-group size specified to clEnqueueNDRangeKernel">, MarshallingInfoFlag>; +def cl_no_stdinc : Flag<["-"], "cl-no-stdinc">, Group, + HelpText<"OpenCL only. Disables all standard includes containing non-native compiler types and functions.">; def client__name : JoinedOrSeparate<["-"], "client_name">; def combine : Flag<["-", "--"], "combine">, Flags<[NoXarchOption, Unsupported]>; def compatibility__version : JoinedOrSeparate<["-"], "compatibility_version">; diff --git a/clang/include/clang/Driver/Types.h b/clang/include/clang/Driver/Types.h --- a/clang/include/clang/Driver/Types.h +++ b/clang/include/clang/Driver/Types.h @@ -81,6 +81,9 @@ /// isObjC - Is this an "ObjC" input (Obj-C and Obj-C++ sources and headers). bool isObjC(ID Id); + /// isOpenCL - Is this an "OpenCL" input. + bool isOpenCL(ID Id); + /// isFortran - Is this a Fortran input. bool isFortran(ID Id); diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -3193,7 +3193,8 @@ } } -static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs) { +static void RenderOpenCLOptions(const ArgList &Args, ArgStringList &CmdArgs, + types::ID InputType) { // cl-denorms-are-zero is not forwarded. It is translated into a generic flag // for denormal flushing handling based on the target. const unsigned ForwardedArguments[] = { @@ -3218,6 +3219,13 @@ for (const auto &Arg : ForwardedArguments) if (const auto *A = Args.getLastArg(Arg)) CmdArgs.push_back(Args.MakeArgString(A->getOption().getPrefixedName())); + + // Only add the default headers if we are compiling OpenCL sources. + if ((types::isOpenCL(InputType) || Args.hasArg(options::OPT_cl_std_EQ)) && + !Args.hasArg(options::OPT_cl_no_stdinc)) { + CmdArgs.push_back("-finclude-default-header"); + CmdArgs.push_back("-fdeclare-opencl-builtins"); + } } static void RenderARCMigrateToolOptions(const Driver &D, const ArgList &Args, @@ -5726,7 +5734,7 @@ } // Forward -cl options to -cc1 - RenderOpenCLOptions(Args, CmdArgs); + RenderOpenCLOptions(Args, CmdArgs, InputType); if (IsHIP) { if (Args.hasFlag(options::OPT_fhip_new_launch_api, diff --git a/clang/lib/Driver/Types.cpp b/clang/lib/Driver/Types.cpp --- a/clang/lib/Driver/Types.cpp +++ b/clang/lib/Driver/Types.cpp @@ -160,6 +160,8 @@ } } +bool types::isOpenCL(ID Id) { return Id == TY_CL; } + bool types::isCXX(ID Id) { switch (Id) { default: diff --git a/clang/test/Driver/default-includes.cl b/clang/test/Driver/default-includes.cl new file mode 100644 --- /dev/null +++ b/clang/test/Driver/default-includes.cl @@ -0,0 +1,13 @@ +// RUN: %clang %s -Xclang -verify -fsyntax-only +// RUN: %clang %s -cl-no-stdinc -Xclang -verify -DNOINC -fsyntax-only + +#ifndef NOINC +//expected-no-diagnostics +#endif + +void test() { +int i = get_global_id(0); +#ifdef NOINC +//expected-error@-2{{implicit declaration of function 'get_global_id' is invalid in OpenCL}} +#endif +} diff --git a/clang/unittests/AST/MatchVerifier.h b/clang/unittests/AST/MatchVerifier.h --- a/clang/unittests/AST/MatchVerifier.h +++ b/clang/unittests/AST/MatchVerifier.h @@ -117,6 +117,7 @@ FileName = "input.cc"; break; case Lang_OpenCL: + Args.push_back("-cl-no-stdinc"); FileName = "input.cl"; break; case Lang_OBJCXX: