From 84c475437267e7fffedc40029ce274b099d8f8f3 Mon Sep 17 00:00:00 2001 From: Leonard Chan Date: Wed, 21 Apr 2021 15:09:12 -0700 Subject: [clang] Add -fc++-abi= flag for specifying which C++ ABI to use This implements the flag proposed in RFC http://lists.llvm.org/pipermail/cfe-dev/2020-August/066437.html. The goal is to add a way to override the default target C++ ABI through a compiler flag. This makes it easier to test and transition between different C++ ABIs through compile flags rather than build flags. In this patch: - Store -fc++-abi= in a LangOpt. This isn't stored in a CodeGenOpt because there are instances outside of codegen where Clang needs to know what the ABI is (particularly through ASTContext::createCXXABI), and we should be able to override the target default if the flag is provided at that point. - Expose the existing ABIs in TargetCXXABI as values that can be passed through this flag. - Create a .def file for these ABIs to make it easier to check flag values. - Add an error for diagnosing bad ABI flag values. Differential Revision: https://reviews.llvm.org/D85802 --- clang/lib/Frontend/CompilerInvocation.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'clang/lib/Frontend/CompilerInvocation.cpp') diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index b2fa21e..4b0bd30 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -3512,6 +3512,10 @@ void CompilerInvocation::GenerateLangArgs(const LangOptions &Opts, if (Opts.getSignReturnAddressKey() == LangOptions::SignReturnAddressKeyKind::BKey) GenerateArg(Args, OPT_msign_return_address_key_EQ, "b_key", SA); + + if (Opts.CXXABI) + GenerateArg(Args, OPT_fcxx_abi_EQ, TargetCXXABI::getSpelling(*Opts.CXXABI), + SA); } bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args, @@ -3996,6 +4000,20 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args, } } + // The value can be empty, which indicates the system default should be used. + StringRef CXXABI = Args.getLastArgValue(OPT_fcxx_abi_EQ); + if (!CXXABI.empty()) { + if (!TargetCXXABI::isABI(CXXABI)) { + Diags.Report(diag::err_invalid_cxx_abi) << CXXABI; + } else { + auto Kind = TargetCXXABI::getKind(CXXABI); + if (!TargetCXXABI::isSupportedCXXABI(T, Kind)) + Diags.Report(diag::err_unsupported_cxx_abi) << CXXABI << T.str(); + else + Opts.CXXABI = Kind; + } + } + return Diags.getNumErrors() == NumErrorsBefore; } -- cgit v1.1