Skip to content

Commit 956d8e0

Browse files
committed
[clang][cli] Port GNU language options to marshalling system
Port some GNU-related language options to the marshalling system for automatic command line parsing and generation. Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D95343
1 parent 2154cff commit 956d8e0

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,11 @@ class MarshallingInfoVisibility<KeyPathAndMacro kpm, code default>
434434
AutoNormalizeEnum {}
435435

436436
// Key paths that are constant during parsing of options with the same key path prefix.
437+
defvar cplusplus = LangOpts<"CPlusPlus">;
438+
defvar c99 = LangOpts<"C99">;
437439
defvar lang_std = LangOpts<"LangStd">;
438440
defvar open_cl = LangOpts<"OpenCL">;
441+
defvar gnu_mode = LangOpts<"GNUMode">;
439442

440443
defvar std = !strconcat("LangStandard::getLangStandardForKind(", lang_std.KeyPath, ")");
441444

@@ -1619,9 +1622,19 @@ def ffreestanding : Flag<["-"], "ffreestanding">, Group<f_Group>, Flags<[CC1Opti
16191622
def fgnuc_version_EQ : Joined<["-"], "fgnuc-version=">, Group<f_Group>,
16201623
HelpText<"Sets various macros to claim compatibility with the given GCC version (default is 4.2.1)">,
16211624
Flags<[CC1Option, CoreOption]>;
1622-
def fgnu_keywords : Flag<["-"], "fgnu-keywords">, Group<f_Group>, Flags<[CC1Option]>,
1623-
HelpText<"Allow GNU-extension keywords regardless of language standard">;
1624-
defm gnu89_inline : OptInFFlag<"gnu89-inline", "Use the gnu89 inline semantics">;
1625+
// We abuse '-f[no-]gnu-keywords' to force overriding all GNU-extension
1626+
// keywords. This behavior is provided by GCC's poorly named '-fasm' flag,
1627+
// while a subset (the non-C++ GNU keywords) is provided by GCC's
1628+
// '-fgnu-keywords'. Clang conflates the two for simplicity under the single
1629+
// name, as it doesn't seem a useful distinction.
1630+
defm gnu_keywords : BoolFOption<"gnu-keywords",
1631+
LangOpts<"GNUKeywords">, Default<gnu_mode.KeyPath>,
1632+
PosFlag<SetTrue, [], "Allow GNU-extension keywords regardless of language standard">,
1633+
NegFlag<SetFalse>, BothFlags<[CC1Option]>>;
1634+
defm gnu89_inline : BoolFOption<"gnu89-inline",
1635+
LangOpts<"GNUInline">, Default<!strconcat("!", c99.KeyPath, " && !", cplusplus.KeyPath)>,
1636+
PosFlag<SetTrue, [CC1Option], "Use the gnu89 inline semantics">,
1637+
NegFlag<SetFalse>>, ShouldParseIf<!strconcat("!", cplusplus.KeyPath)>;
16251638
def fgnu_runtime : Flag<["-"], "fgnu-runtime">, Group<f_Group>,
16261639
HelpText<"Generate output compatible with the standard GNU Objective-C runtime">;
16271640
def fheinous_gnu_extensions : Flag<["-"], "fheinous-gnu-extensions">, Flags<[CC1Option]>,
@@ -1965,7 +1978,6 @@ def fno_declspec : Flag<["-"], "fno-declspec">, Group<f_clang_Group>,
19651978
def fno_dollars_in_identifiers : Flag<["-"], "fno-dollars-in-identifiers">, Group<f_Group>,
19661979
HelpText<"Disallow '$' in identifiers">, Flags<[CC1Option]>;
19671980
def fno_eliminate_unused_debug_symbols : Flag<["-"], "fno-eliminate-unused-debug-symbols">, Group<f_Group>;
1968-
def fno_gnu_keywords : Flag<["-"], "fno-gnu-keywords">, Group<f_Group>, Flags<[CC1Option]>;
19691981
def fno_inline_functions : Flag<["-"], "fno-inline-functions">, Group<f_clang_Group>, Flags<[CC1Option]>;
19701982
def fno_inline : Flag<["-"], "fno-inline">, Group<f_clang_Group>, Flags<[CC1Option]>;
19711983
def fno_global_isel : Flag<["-"], "fno-global-isel">, Group<f_clang_Group>,

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,11 @@ static T extractMaskValue(T KeyPath) {
400400
MERGER(KEYPATH, static_cast<decltype(KEYPATH)>(*MaybeValue)); \
401401
}
402402

403+
static const StringRef GetInputKindName(InputKind IK);
404+
403405
static void FixupInvocation(CompilerInvocation &Invocation,
404-
DiagnosticsEngine &Diags,
405-
const InputArgList &Args) {
406+
DiagnosticsEngine &Diags, const InputArgList &Args,
407+
InputKind IK) {
406408
LangOptions &LangOpts = *Invocation.getLangOpts();
407409
CodeGenOptions &CodeGenOpts = Invocation.getCodeGenOpts();
408410
TargetOptions &TargetOpts = Invocation.getTargetOpts();
@@ -438,6 +440,10 @@ static void FixupInvocation(CompilerInvocation &Invocation,
438440
LangOpts.NewAlignOverride = 0;
439441
}
440442

443+
if (Args.hasArg(OPT_fgnu89_inline) && LangOpts.CPlusPlus)
444+
Diags.Report(diag::err_drv_argument_not_allowed_with)
445+
<< "-fgnu89-inline" << GetInputKindName(IK);
446+
441447
if (Arg *A = Args.getLastArg(OPT_fdefault_calling_conv_EQ)) {
442448
auto DefaultCC = LangOpts.getDefaultCallingConv();
443449

@@ -1984,7 +1990,6 @@ void CompilerInvocation::setLangDefaults(LangOptions &Opts, InputKind IK,
19841990
Opts.CPlusPlus20 = Std.isCPlusPlus20();
19851991
Opts.CPlusPlus2b = Std.isCPlusPlus2b();
19861992
Opts.GNUMode = Std.isGNUMode();
1987-
Opts.GNUInline = !Opts.C99 && !Opts.CPlusPlus;
19881993
Opts.GNUCVersion = 0;
19891994
Opts.HexFloats = Std.hasHexFloats();
19901995
Opts.ImplicitInt = Std.hasImplicitInt();
@@ -2056,7 +2061,6 @@ void CompilerInvocation::setLangDefaults(LangOptions &Opts, InputKind IK,
20562061
// C++ has wchar_t keyword.
20572062
Opts.WChar = Opts.CPlusPlus;
20582063

2059-
Opts.GNUKeywords = Opts.GNUMode;
20602064
Opts.CXXOperatorNames = Opts.CPlusPlus;
20612065

20622066
Opts.AlignedAllocation = Opts.CPlusPlus17;
@@ -2254,14 +2258,6 @@ void CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
22542258
<< Args.getLastArg(OPT_cl_strict_aliasing)->getAsString(Args);
22552259
}
22562260

2257-
// We abuse '-f[no-]gnu-keywords' to force overriding all GNU-extension
2258-
// keywords. This behavior is provided by GCC's poorly named '-fasm' flag,
2259-
// while a subset (the non-C++ GNU keywords) is provided by GCC's
2260-
// '-fgnu-keywords'. Clang conflates the two for simplicity under the single
2261-
// name, as it doesn't seem a useful distinction.
2262-
Opts.GNUKeywords = Args.hasFlag(OPT_fgnu_keywords, OPT_fno_gnu_keywords,
2263-
Opts.GNUKeywords);
2264-
22652261
if (Args.hasArg(OPT_fno_operator_names))
22662262
Opts.CXXOperatorNames = 0;
22672263

@@ -2344,14 +2340,6 @@ void CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
23442340
Opts.GNUCVersion = Major * 100 * 100 + Minor * 100 + Patch;
23452341
}
23462342

2347-
if (Args.hasArg(OPT_fgnu89_inline)) {
2348-
if (Opts.CPlusPlus)
2349-
Diags.Report(diag::err_drv_argument_not_allowed_with)
2350-
<< "-fgnu89-inline" << GetInputKindName(IK);
2351-
else
2352-
Opts.GNUInline = 1;
2353-
}
2354-
23552343
if (Args.hasArg(OPT_ftrapv)) {
23562344
Opts.setSignedOverflowBehavior(LangOptions::SOB_Trapping);
23572345
// Set the handler, if one is specified.
@@ -2994,7 +2982,7 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
29942982
Res.getCodeGenOpts().Argv0 = Argv0;
29952983
Res.getCodeGenOpts().CommandLineArgs = CommandLineArgs;
29962984

2997-
FixupInvocation(Res, Diags, Args);
2985+
FixupInvocation(Res, Diags, Args, DashX);
29982986

29992987
return Success;
30002988
}

0 commit comments

Comments
 (0)