Use an enum value instead of a string.

The old StringSwitch use was also broken. It assumed that a
StringSwitch returns Optional<T> instead of T and was missing a
.Default.

llvm-svn: 322792
This commit is contained in:
Rafael Espindola 2018-01-18 00:20:03 +00:00
parent e769fb73b5
commit e1d7053032
4 changed files with 18 additions and 24 deletions

View File

@ -16,6 +16,7 @@
#include "clang/Basic/DebugInfoOptions.h"
#include "clang/Basic/Sanitizers.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/Regex.h"
#include "llvm/Target/TargetOptions.h"
#include <map>
@ -167,7 +168,7 @@ public:
std::string SplitDwarfFile;
/// The name of the relocation model to use.
std::string RelocationModel;
llvm::Reloc::Model RelocationModel;
/// The thread model to use
std::string ThreadModel;

View File

@ -362,21 +362,6 @@ getCodeModel(const CodeGenOptions &CodeGenOpts) {
return static_cast<llvm::CodeModel::Model>(CodeModel);
}
static llvm::Reloc::Model getRelocModel(const CodeGenOptions &CodeGenOpts) {
// Keep this synced with the equivalent code in
// lib/Frontend/CompilerInvocation.cpp
llvm::Optional<llvm::Reloc::Model> RM;
RM = llvm::StringSwitch<llvm::Reloc::Model>(CodeGenOpts.RelocationModel)
.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);
assert(RM.hasValue() && "invalid PIC model!");
return *RM;
}
static TargetMachine::CodeGenFileType getCodeGenFileType(BackendAction Action) {
if (Action == Backend_EmitObj)
return TargetMachine::CGFT_ObjectFile;
@ -692,7 +677,7 @@ void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
Optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
std::string FeaturesStr =
llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
llvm::Reloc::Model RM = getRelocModel(CodeGenOpts);
llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
llvm::TargetOptions Options;
@ -1113,7 +1098,7 @@ static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M,
Conf.CPU = TOpts.CPU;
Conf.CodeModel = getCodeModel(CGOpts);
Conf.MAttrs = TOpts.Features;
Conf.RelocModel = getRelocModel(CGOpts);
Conf.RelocModel = CGOpts.RelocationModel;
Conf.CGOptLevel = getCGOptLevel(CGOpts);
initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
Conf.SampleProfile = std::move(SampleProfile);

View File

@ -17,7 +17,7 @@ CodeGenOptions::CodeGenOptions() {
#define ENUM_CODEGENOPT(Name, Type, Bits, Default) set##Name(Default);
#include "clang/Frontend/CodeGenOptions.def"
RelocationModel = "pic";
RelocationModel = llvm::Reloc::PIC_;
memcpy(CoverageVersion, "402*", 4);
}

View File

@ -330,15 +330,23 @@ static StringRef getCodeModel(ArgList &Args, DiagnosticsEngine &Diags) {
return "default";
}
static StringRef getRelocModel(ArgList &Args, DiagnosticsEngine &Diags) {
static llvm::Reloc::Model getRelocModel(ArgList &Args,
DiagnosticsEngine &Diags) {
if (Arg *A = Args.getLastArg(OPT_mrelocation_model)) {
StringRef Value = A->getValue();
if (Value == "static" || Value == "pic" || Value == "ropi" ||
Value == "rwpi" || Value == "ropi-rwpi" || Value == "dynamic-no-pic")
return Value;
auto RM = llvm::StringSwitch<llvm::Optional<llvm::Reloc::Model>>(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 "pic";
return llvm::Reloc::PIC_;
}
/// \brief Create a new Regex instance out of the string value in \p RpassArg.