Index: clang/include/clang/Driver/CC1Options.td =================================================================== --- clang/include/clang/Driver/CC1Options.td +++ clang/include/clang/Driver/CC1Options.td @@ -23,7 +23,8 @@ def target_feature : Separate<["-"], "target-feature">, HelpText<"Target specific attributes">; def triple : Separate<["-"], "triple">, - HelpText<"Specify target triple (e.g. i686-apple-darwin9)">; + HelpText<"Specify target triple (e.g. i686-apple-darwin9)">, + MarshallingInfoTriple", "llvm::sys::getDefaultTargetTriple()", "normalizeTriple", "">>; def target_abi : Separate<["-"], "target-abi">, HelpText<"Target a particular ABI type">; def target_sdk_version_EQ : Joined<["-"], "target-sdk-version=">, @@ -212,7 +213,8 @@ "Note this may change .s semantics and shouldn't generally be used " "on compiler-generated code.">; def mrelocation_model : Separate<["-"], "mrelocation-model">, - HelpText<"The relocation model to use">, Values<"static,pic,ropi,rwpi,ropi-rwpi,dynamic-no-pic">; + HelpText<"The relocation model to use">, Values<"static,pic,ropi,rwpi,ropi-rwpi,dynamic-no-pic">, + MarshallingInfo>; def fno_math_builtin : Flag<["-"], "fno-math-builtin">, HelpText<"Disable implicit builtin knowledge of math functions">; } @@ -835,7 +837,8 @@ HelpText<"Enable hashing the content of a module file">; def fmodules_strict_context_hash : Flag<["-"], "fmodules-strict-context-hash">, HelpText<"Enable hashing of all compiler options that could impact the " - "semantics of a module in an implicit build">; + "semantics of a module in an implicit build">, + MarshallingInfoModulesStrictContextHash", 1, "false">>; def c_isystem : JoinedOrSeparate<["-"], "c-isystem">, MetaVarName<"">, HelpText<"Add directory to the C SYSTEM include search path">; def objc_isystem : JoinedOrSeparate<["-"], "objc-isystem">, Index: clang/include/clang/Frontend/CompilerInvocation.h =================================================================== --- clang/include/clang/Frontend/CompilerInvocation.h +++ clang/include/clang/Frontend/CompilerInvocation.h @@ -153,6 +153,8 @@ /// one of the vaild-to-access (albeit arbitrary) states. /// /// \param [out] Res - The resulting invocation. + /// \param [in] CommandLineArgs - Array of argument strings, this must not + /// contain "-cc1". static bool CreateFromArgs(CompilerInvocation &Res, ArrayRef CommandLineArgs, DiagnosticsEngine &Diags); @@ -183,6 +185,20 @@ /// identifying the conditions under which the module was built. std::string getModuleHash() const; + /// Generate a cc1-compatible command line arguments from this instance. + /// + /// \param [out] Args - The generated arguments. Note that the caller is + /// responsible for insersting the path to the clang executable and "-cc1" if + /// desired. + /// \param StringAllocator - A function that given a Twine can allocate + /// storage for a given command line argument and return a pointer to the + /// newly allocated string. The returned pointer is what gets appended to + /// Args. + void + generateCC1CommandLine(llvm::SmallVectorImpl &Args, + llvm::function_ref + StringAllocator) const; + /// @} /// @name Option Subgroups /// @{ @@ -221,6 +237,16 @@ } /// @} + +private: + /// Parse options for flags that expose marshalling information in their + /// table-gen definition + /// + /// \param Args - The argument list containing the arguments to parse + /// \param Diags - The DiagnosticsEngine associated with CreateFromArgs + /// \returns - True if parsing was successful, false otherwise + bool parseSimpleArgs(const llvm::opt::ArgList &Args, + DiagnosticsEngine &Diags); }; IntrusiveRefCntPtr Index: clang/lib/Frontend/CompilerInvocation.cpp =================================================================== --- clang/lib/Frontend/CompilerInvocation.cpp +++ clang/lib/Frontend/CompilerInvocation.cpp @@ -51,6 +51,7 @@ #include "llvm/ADT/Hashing.h" #include "llvm/ADT/None.h" #include "llvm/ADT/Optional.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" @@ -117,6 +118,55 @@ CompilerInvocationBase::~CompilerInvocationBase() = default; +//===----------------------------------------------------------------------===// +// Normalizers +//===----------------------------------------------------------------------===// + +static std::string normalizeTriple(const Arg *Arg, const ArgList &ArgList, + DiagnosticsEngine &Diags, + StringRef DefaultTriple) { + return llvm::Triple::normalize(Arg->getValue()); +} + +static llvm::Reloc::Model +normalizeRelocationModel(const Arg *Arg, const ArgList &ArgList, + DiagnosticsEngine &Diags, + llvm::Reloc::Model DefaultValue) { + StringRef ArgValue = Arg->getValue(); + auto RM = llvm::StringSwitch>(ArgValue) + .Case("static", llvm::Reloc::Static) + .Case("pic", llvm::Reloc::PIC_) + .Case("ropi", llvm::Reloc::ROPI) + .Case("rwpi", llvm::Reloc::RWPI) + .Case("ropi-rwpi", llvm::Reloc::ROPI_RWPI) + .Case("dynamic-no-pic", llvm::Reloc::DynamicNoPIC) + .Default(None); + + if (RM.hasValue()) + return *RM; + + Diags.Report(diag::err_drv_invalid_value) + << Arg->getAsString(ArgList) << ArgValue; + return DefaultValue; +} + +static const char *denormalizeRelocationModel(llvm::Reloc::Model RM) { + switch (RM) { + case llvm::Reloc::Static: + return "static"; + case llvm::Reloc::PIC_: + return "pic"; + case llvm::Reloc::DynamicNoPIC: + return "dynamic-no-pic"; + case llvm::Reloc::ROPI: + return "ropi"; + case llvm::Reloc::RWPI: + return "rwpi"; + case llvm::Reloc::ROPI_RWPI: + return "ropi-rwpi"; + } +} + //===----------------------------------------------------------------------===// // Deserialization (from args) //===----------------------------------------------------------------------===// @@ -528,25 +578,6 @@ Opts.ParseAllComments = Args.hasArg(OPT_fparse_all_comments); } -static llvm::Reloc::Model getRelocModel(ArgList &Args, - DiagnosticsEngine &Diags) { - if (Arg *A = Args.getLastArg(OPT_mrelocation_model)) { - StringRef Value = A->getValue(); - auto RM = llvm::StringSwitch>(Value) - .Case("static", llvm::Reloc::Static) - .Case("pic", llvm::Reloc::PIC_) - .Case("ropi", llvm::Reloc::ROPI) - .Case("rwpi", llvm::Reloc::RWPI) - .Case("ropi-rwpi", llvm::Reloc::ROPI_RWPI) - .Case("dynamic-no-pic", llvm::Reloc::DynamicNoPIC) - .Default(None); - if (RM.hasValue()) - return *RM; - Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Value; - } - return llvm::Reloc::PIC_; -} - /// Create a new Regex instance out of the string value in \p RpassArg. /// It returns a pointer to the newly generated Regex instance. static std::shared_ptr @@ -927,7 +958,6 @@ Opts.StrictVTablePointers = Args.hasArg(OPT_fstrict_vtable_pointers); Opts.ForceEmitVTables = Args.hasArg(OPT_fforce_emit_vtables); Opts.UnwindTables = Args.hasArg(OPT_munwind_tables); - Opts.RelocationModel = getRelocModel(Args, Diags); Opts.ThreadModel = std::string(Args.getLastArgValue(OPT_mthread_model, "posix")); if (Opts.ThreadModel != "posix" && Opts.ThreadModel != "single") @@ -2104,7 +2134,6 @@ Opts.AddPrebuiltModulePath(A->getValue()); Opts.DisableModuleHash = Args.hasArg(OPT_fdisable_module_hash); Opts.ModulesHashContent = Args.hasArg(OPT_fmodules_hash_content); - Opts.ModulesStrictContextHash = Args.hasArg(OPT_fmodules_strict_context_hash); Opts.ModulesValidateDiagnosticOptions = !Args.hasArg(OPT_fmodules_disable_diagnostic_validation); Opts.ImplicitModuleMaps = Args.hasArg(OPT_fimplicit_module_maps); @@ -3613,6 +3642,32 @@ } } +bool CompilerInvocation::parseSimpleArgs(const ArgList &Args, + DiagnosticsEngine &Diags) { +#define OPTION_WITH_MARSHALLING +#define OPTION_WITH_MARSHALLING_FLAG(PREFIX_TYPE, NAME, ID, KIND, GROUP, \ + ALIAS, ALIASARGS, FLAGS, PARAM, HELPTEXT, \ + METAVAR, VALUES, ALWAYS_EMIT, KEYPATH, \ + IS_POSITIVE, DEFAULT_VALUE) \ + this->KEYPATH = Args.hasArg(OPT_##ID) && IS_POSITIVE; + +#define OPTION_WITH_MARSHALLING_STRING( \ + PREFIX_TYPE, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ + HELPTEXT, METAVAR, VALUES, ALWAYS_EMIT, KEYPATH, DEFAULT_VALUE, \ + NORMALIZER, DENORMALIZER) \ + { \ + auto Arg = Args.getLastArg(OPT_##ID); \ + this->KEYPATH = \ + !Arg ? DEFAULT_VALUE : NORMALIZER(Arg, Args, Diags, DEFAULT_VALUE); \ + } + +#include "clang/Driver/Options.inc" +#undef OPTION_WITH_MARSHALLING_STRING +#undef OPTION_WITH_MARSHALLING_FLAG +#undef OPTION_WITH_MARSHALLING + return true; +} + bool CompilerInvocation::CreateFromArgs(CompilerInvocation &Res, ArrayRef CommandLineArgs, DiagnosticsEngine &Diags) { @@ -3645,6 +3700,7 @@ Success = false; } + Success &= Res.parseSimpleArgs(Args, Diags); Success &= ParseAnalyzerArgs(*Res.getAnalyzerOpts(), Args, Diags); Success &= ParseMigratorArgs(Res.getMigratorOpts(), Args); ParseDependencyOutputArgs(Res.getDependencyOutputOpts(), Args); @@ -3847,6 +3903,40 @@ return llvm::APInt(64, code).toString(36, /*Signed=*/false); } +void CompilerInvocation::generateCC1CommandLine( + SmallVectorImpl &Args, + llvm::function_ref StringAllocator) const { +#define PREFIX(PREFIX_TYPE, BRACED_INIT) \ + const char *PREFIX_TYPE[4] = BRACED_INIT; + +#define OPTION_WITH_MARSHALLING +#define OPTION_WITH_MARSHALLING_FLAG(PREFIX_TYPE, NAME, ID, KIND, GROUP, \ + ALIAS, ALIASARGS, FLAGS, PARAM, HELPTEXT, \ + METAVAR, VALUES, ALWAYS_EMIT, KEYPATH, \ + IS_POSITIVE, DEFAULT_VALUE) \ + if (FLAGS & options::CC1Option && IS_POSITIVE != DEFAULT_VALUE && \ + (this->KEYPATH != DEFAULT_VALUE || ALWAYS_EMIT)) \ + Args.push_back(StringAllocator(Twine(PREFIX_TYPE[0]) + NAME)); + +#define OPTION_WITH_MARSHALLING_STRING( \ + PREFIX_TYPE, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ + HELPTEXT, METAVAR, VALUES, ALWAYS_EMIT, KEYPATH, DEFAULT_VALUE, \ + NORMALIZER, DENORMALIZER) \ + if ((FLAGS & options::CC1Option) && \ + (this->KEYPATH != DEFAULT_VALUE || ALWAYS_EMIT)) { \ + if (Option::KIND##Class == Option::SeparateClass) { \ + Args.push_back(StringAllocator(Twine(PREFIX_TYPE[0]) + NAME)); \ + Args.push_back(StringAllocator(DENORMALIZER(this->KEYPATH))); \ + } \ + } + +#include "clang/Driver/Options.inc" +#undef OPTION_WITH_MARSHALLING_STRING +#undef OPTION_WITH_MARSHALLING_FLAG +#undef OPTION_WITH_MARSHALLING +#undef PREFIX +} + namespace clang { IntrusiveRefCntPtr Index: clang/unittests/Frontend/CMakeLists.txt =================================================================== --- clang/unittests/Frontend/CMakeLists.txt +++ clang/unittests/Frontend/CMakeLists.txt @@ -4,6 +4,7 @@ add_clang_unittest(FrontendTests ASTUnitTest.cpp + CompilerInvocationTest.cpp CompilerInstanceTest.cpp FixedPointString.cpp FrontendActionTest.cpp Index: clang/unittests/Frontend/CompilerInvocationTest.cpp =================================================================== --- /dev/null +++ clang/unittests/Frontend/CompilerInvocationTest.cpp @@ -0,0 +1,102 @@ +//===- unittests/Frontend/CompilerInvocationTest.cpp - CI tests //---------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/CompilerInvocation.h" +#include "clang/Basic/DiagnosticIDs.h" +#include "clang/Frontend/CompilerInstance.h" +#include "llvm/Support/Host.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace clang; + +using ::testing::Contains; +using ::testing::Each; +using ::testing::StrEq; +using ::testing::StrNe; + +namespace { + +class CC1CommandLineGenerationTest : public ::testing::Test { +public: + IntrusiveRefCntPtr Diags; + SmallVector GeneratedArgs; + SmallVector GeneratedArgsStorage; + + const char *operator()(const Twine &Arg) { + return GeneratedArgsStorage.emplace_back(Arg.str()).c_str(); + } + + CC1CommandLineGenerationTest() + : Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions())) {} +}; + +TEST_F(CC1CommandLineGenerationTest, CanGenerateCC1CommandLineFlag) { + const char *Args[] = {"clang", "-xc++", "-fmodules-strict-context-hash", "-"}; + + CompilerInvocation CInvok; + CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags); + + CInvok.generateCC1CommandLine(GeneratedArgs, *this); + + ASSERT_THAT(GeneratedArgs, Contains(StrEq("-fmodules-strict-context-hash"))); +} + +TEST_F(CC1CommandLineGenerationTest, CanGenerateCC1CommandLineSeparate) { + const char *TripleCStr = "i686-apple-darwin9"; + const char *Args[] = {"clang", "-xc++", "-triple", TripleCStr, "-"}; + + CompilerInvocation CInvok; + CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags); + + CInvok.generateCC1CommandLine(GeneratedArgs, *this); + + ASSERT_THAT(GeneratedArgs, Contains(StrEq(TripleCStr))); +} + +TEST_F(CC1CommandLineGenerationTest, + CanGenerateCC1CommandLineSeparateRequired) { + const char *TripleCStr = + llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple()).c_str(); + const char *Args[] = {"clang", "-xc++", "-triple", TripleCStr, "-"}; + + CompilerInvocation CInvok; + CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags); + + CInvok.generateCC1CommandLine(GeneratedArgs, *this); + + // Triple should always be emitted even if it is the default + ASSERT_THAT(GeneratedArgs, Contains(StrEq(TripleCStr))); +} + +TEST_F(CC1CommandLineGenerationTest, CanGenerateCC1CommandLineSeparateEnum) { + const char *RelocationModelCStr = "static"; + const char *Args[] = {"clang", "-xc++", "-mrelocation-model", + RelocationModelCStr, "-"}; + + CompilerInvocation CInvok; + CompilerInvocation::CreateFromArgs(CInvok, Args, *Diags); + + CInvok.generateCC1CommandLine(GeneratedArgs, *this); + + // Non default relocation model + ASSERT_THAT(GeneratedArgs, Contains(StrEq(RelocationModelCStr))); + GeneratedArgs.clear(); + + RelocationModelCStr = "pic"; + Args[3] = RelocationModelCStr; + + CompilerInvocation CInvok1; + CompilerInvocation::CreateFromArgs(CInvok1, Args, *Diags); + + CInvok1.generateCC1CommandLine(GeneratedArgs, *this); + ASSERT_THAT(GeneratedArgs, Each(StrNe(RelocationModelCStr))); +} + +} // anonymous namespace Index: llvm/include/llvm/Option/OptParser.td =================================================================== --- llvm/include/llvm/Option/OptParser.td +++ llvm/include/llvm/Option/OptParser.td @@ -80,6 +80,18 @@ list Flags = []; } +// Add support for generating marshalling code +class OptionMarshallingInfo { + string Kind = kind; + bit ShouldAlwaysEmit = 0; + code KeyPath = keypath; + // Used by the Flag option kind. + bit IsPositive = ?; + code DefaultValue = ?; + code Normalizer = ?; + code Denormalizer = ?; +} + // Define the option class. class Option prefixes, string name, OptionKind kind> { @@ -97,6 +109,7 @@ OptionGroup Group = ?; Option Alias = ?; list AliasArgs = []; + OptionMarshallingInfo MarshallingInfo = ?; } // Helpers for defining options. @@ -130,6 +143,26 @@ class Values { string Values = value; } class ValuesCode { code ValuesCode = valuecode; } +class MarshallingInfo { OptionMarshallingInfo MarshallingInfo = info; } +class MarshallingFlag + : OptionMarshallingInfo<"flag", keypath> { + bit IsPositive = ispositive; + code DefaultValue = defaultvalue; +} +class MarshallingFlagAlwaysEmit + : MarshallingFlag { + let ShouldAlwaysEmit = 1; +} +class MarshallingString + : OptionMarshallingInfo<"string", keypath> { + code DefaultValue = defaultvalue; + code Normalizer = normalizer; + code Denormalizer = denormalizer; +} +class MarshallingStringAlwaysEmit + : MarshallingString { + let ShouldAlwaysEmit = 1; +} // Predefined options. // FIXME: Have generator validate that these appear in correct position (and Index: llvm/utils/TableGen/OptParserEmitter.cpp =================================================================== --- llvm/utils/TableGen/OptParserEmitter.cpp +++ llvm/utils/TableGen/OptParserEmitter.cpp @@ -9,6 +9,7 @@ #include "OptEmitter.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Twine.h" #include "llvm/TableGen/Record.h" #include "llvm/TableGen/TableGenBackend.h" @@ -33,6 +34,20 @@ return OS; } +static void emitMarshallingInfoFlag(raw_ostream &OS, const Record *R) { + OS << R->getValueAsBit("IsPositive"); + OS << ","; + OS << R->getValueAsString("DefaultValue"); +} + +static void emitMarshallingInfoString(raw_ostream &OS, const Record *R) { + OS << R->getValueAsString("DefaultValue"); + OS << ", "; + OS << R->getValueAsString("Normalizer"); + OS << ", "; + OS << R->getValueAsString("Denormalizer"); +} + /// OptParserEmitter - This tablegen backend takes an input .td file /// describing a list of options and emits a data structure for parsing and /// working with those options when given an input command line. @@ -135,12 +150,8 @@ OS << "//////////\n"; OS << "// Options\n\n"; - for (unsigned i = 0, e = Opts.size(); i != e; ++i) { - const Record &R = *Opts[i]; - - // Start a single option entry. - OS << "OPTION("; + auto WriteOptRecordFields = [&](raw_ostream &OS, const Record &R) { // The option prefix; std::vector prf = R.getValueAsListOfStrings("Prefixes"); OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", "; @@ -223,11 +234,48 @@ write_cstring(OS, R.getValueAsString("Values")); else OS << "nullptr"; + }; + + for (unsigned i = 0, e = Opts.size(); i != e; ++i) { + const Record &R = *Opts[i]; + // Start a single option entry. + OS << "OPTION("; + WriteOptRecordFields(OS, R); OS << ")\n"; } OS << "#endif // OPTION\n"; + OS << "#ifdef OPTION_WITH_MARSHALLING\n"; + for (unsigned I = 0, E = Opts.size(); I != E; ++I) { + const Record &R = *Opts[I]; + + if (!isa(R.getValueInit("MarshallingInfo"))) { + Record *MarshallingInfoRecord = + cast(R.getValueInit("MarshallingInfo"))->getDef(); + StringRef KindStr = MarshallingInfoRecord->getValueAsString("Kind"); + auto KindInfoPair = + StringSwitch>>( + KindStr) + .Case("flag", std::make_pair("OPTION_WITH_MARSHALLING_FLAG", + &emitMarshallingInfoFlag)) + .Case("string", std::make_pair("OPTION_WITH_MARSHALLING_STRING", + &emitMarshallingInfoString)) + .Default(std::make_pair("", nullptr)); + OS << KindInfoPair.first << "("; + WriteOptRecordFields(OS, R); + OS << ", "; + OS << MarshallingInfoRecord->getValueAsBit("ShouldAlwaysEmit"); + OS << ", "; + OS << MarshallingInfoRecord->getValueAsString("KeyPath"); + OS << ", "; + KindInfoPair.second(OS, MarshallingInfoRecord); + OS << ")\n"; + } + } + OS << "#endif // OPTION_WITH_MARSHALLING\n"; + OS << "\n"; OS << "#ifdef OPTTABLE_ARG_INIT\n"; OS << "//////////\n";