Skip to content

Commit 791634b

Browse files
committed
[clang][cli] Parse & generate options necessary for LangOptions defaults manually
It turns out we need to handle `LangOptions` separately from the rest of the options. `LangOptions` used to be conditionally parsed only when `!(DashX.getFormat() == InputKind::Precompiled || DashX.getLanguage() == Language::LLVM_IR)` and we need to restore this order (for more info, see D94682). D94682 moves the parsing of marshalled `LangOpts` from `parseSimpleArgs` back to `ParseLangArgs`. We need to parse marshalled `LangOpts` **after** `ParseLangArgs` calls `setLangDefaults`. This will enable future patches, where values of some `LangOpts` depend on the defaults. However, two language options (`-finclude-default-header` and `-fdeclare-opencl-builtins`) need to be parsed **before** `ParseLangArgs` calls `setLangDefaults`, because they are necessary for setting up OpenCL defaults correctly. This patch implements this by removing their marshalling info and manually parsing (and generating) them exactly where necessary. Reviewed By: Bigcheese Differential Revision: https://reviews.llvm.org/D94678
1 parent d1862a1 commit 791634b

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5207,12 +5207,13 @@ def fdefault_calling_conv_EQ : Joined<["-"], "fdefault-calling-conv=">,
52075207
NormalizedValuesScope<"LangOptions">,
52085208
NormalizedValues<["DCC_CDecl", "DCC_FastCall", "DCC_StdCall", "DCC_VectorCall", "DCC_RegCall"]>,
52095209
MarshallingInfoString<LangOpts<"DefaultCallingConv">, "DCC_None">, AutoNormalizeEnum;
5210+
5211+
// These options cannot be marshalled, because they are used to set up the LangOptions defaults.
52105212
def finclude_default_header : Flag<["-"], "finclude-default-header">,
5211-
HelpText<"Include default header file for OpenCL">,
5212-
MarshallingInfoFlag<LangOpts<"IncludeDefaultHeader">>;
5213+
HelpText<"Include default header file for OpenCL">;
52135214
def fdeclare_opencl_builtins : Flag<["-"], "fdeclare-opencl-builtins">,
5214-
HelpText<"Add OpenCL builtin function declarations (experimental)">,
5215-
MarshallingInfoFlag<LangOpts<"DeclareOpenCLBuiltins">>;
5215+
HelpText<"Add OpenCL builtin function declarations (experimental)">;
5216+
52165217
def fpreserve_vec3_type : Flag<["-"], "fpreserve-vec3-type">,
52175218
HelpText<"Preserve 3-component vector type">,
52185219
MarshallingInfoFlag<CodeGenOpts<"PreserveVec3Type">>;

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,11 @@ static unsigned getOptimizationLevelSize(ArgList &Args) {
491491
return 0;
492492
}
493493

494+
static std::string GetOptName(llvm::opt::OptSpecifier OptSpecifier) {
495+
static const OptTable &OptTable = getDriverOptTable();
496+
return OptTable.getOption(OptSpecifier).getPrefixedName();
497+
}
498+
494499
static void addDiagnosticArgs(ArgList &Args, OptSpecifier Group,
495500
OptSpecifier GroupWithValue,
496501
std::vector<std::string> &Diagnostics) {
@@ -2137,6 +2142,15 @@ static const StringRef GetInputKindName(InputKind IK) {
21372142
llvm_unreachable("unknown input language");
21382143
}
21392144

2145+
static void GenerateLangArgs(const LangOptions &Opts,
2146+
SmallVectorImpl<const char *> &Args,
2147+
CompilerInvocation::StringAllocator SA) {
2148+
if (Opts.IncludeDefaultHeader)
2149+
Args.push_back(SA(GetOptName(OPT_finclude_default_header)));
2150+
if (Opts.DeclareOpenCLBuiltins)
2151+
Args.push_back(SA(GetOptName(OPT_fdeclare_opencl_builtins)));
2152+
}
2153+
21402154
static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
21412155
const llvm::Triple &T,
21422156
std::vector<std::string> &Includes,
@@ -2212,6 +2226,10 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
22122226

22132227
Opts.SYCLIsDevice = Opts.SYCL && Args.hasArg(options::OPT_fsycl_is_device);
22142228

2229+
// These need to be parsed now. They are used to set OpenCL defaults.
2230+
Opts.IncludeDefaultHeader = Args.hasArg(OPT_finclude_default_header);
2231+
Opts.DeclareOpenCLBuiltins = Args.hasArg(OPT_fdeclare_opencl_builtins);
2232+
22152233
CompilerInvocation::setLangDefaults(Opts, IK, T, Includes, LangStd);
22162234

22172235
// -cl-strict-aliasing needs to emit diagnostic in the case where CL > 1.0.
@@ -3163,6 +3181,8 @@ void CompilerInvocation::generateCC1CommandLine(
31633181
#undef DIAG_OPTION_WITH_MARSHALLING
31643182
#undef OPTION_WITH_MARSHALLING
31653183
#undef GENERATE_OPTION_WITH_MARSHALLING
3184+
3185+
GenerateLangArgs(*LangOpts, Args, SA);
31663186
}
31673187

31683188
IntrusiveRefCntPtr<llvm::vfs::FileSystem>

0 commit comments

Comments
 (0)