diff --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h --- a/clang/include/clang/AST/ASTContext.h +++ b/clang/include/clang/AST/ASTContext.h @@ -39,6 +39,7 @@ #include "clang/Basic/SanitizerBlacklist.h" #include "clang/Basic/SourceLocation.h" #include "clang/Basic/Specifiers.h" +#include "clang/Basic/TargetCXXABI.h" #include "clang/Basic/XRayLists.h" #include "llvm/ADT/APSInt.h" #include "llvm/ADT/ArrayRef.h" @@ -697,6 +698,11 @@ return FullSourceLoc(Loc,SourceMgr); } + /// Return the C++ ABI kind that should be used. This is located here so the + /// ABI can be overriden by Clang-specific flags at compile-time. If there are + /// no overrides, we instead use the default ABI specified by the target. + TargetCXXABI::Kind getCXXABIKind(const TargetInfo *T = nullptr) const; + /// All comments in this translation unit. RawCommentList Comments; diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -327,6 +327,11 @@ /// input is a header file (i.e. -x c-header). bool IsHeaderFile = false; + /// Indicates that the Fuchsia C++ ABI is being used, either by default + /// through the target triple (ARCH-VENDOR-fuchsia) or explicitly through + /// the -ffuchsia-c++-abi flag. + bool UseFuchsiaCXXABI; + LangOptions(); // Define accessors/mutators for language options of enumeration type. diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -1370,6 +1370,11 @@ def fsymbol_partition_EQ : Joined<["-"], "fsymbol-partition=">, Group, Flags<[CC1Option]>, MarshallingInfoString<"CodeGenOpts.SymbolPartition">; +def ffuchsia_cxx_abi : Flag<["-"], "ffuchsia-c++-abi">, Flags<[CC1Option]>, + Group, HelpText<"Explicitly use the Fuchsia C++ ABI">; +def fno_fuchsia_cxx_abi : Flag<["-"], "fno-fuchsia-c++-abi">, Flags<[CC1Option]>, + Group, HelpText<"Do not explicitly use the Fuchsia C++ ABI. Instead use the default C++ ABI indicated by the target">; + defm memory_profile : OptInFFlag<"memory-profile", "Enable", "Disable", " heap memory profiling">; def fmemory_profile_EQ : Joined<["-"], "fmemory-profile=">, Group, Flags<[CC1Option]>, MetaVarName<"">, diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -879,10 +879,18 @@ return CanonTTP; } +TargetCXXABI::Kind ASTContext::getCXXABIKind(const TargetInfo *T) const { + if (getLangOpts().UseFuchsiaCXXABI) + return TargetCXXABI::Fuchsia; + if (T) + return T->getCXXABI().getKind(); + return getTargetInfo().getCXXABI().getKind(); +} + CXXABI *ASTContext::createCXXABI(const TargetInfo &T) { if (!LangOpts.CPlusPlus) return nullptr; - switch (T.getCXXABI().getKind()) { + switch (getCXXABIKind(&T)) { case TargetCXXABI::AppleARM64: case TargetCXXABI::Fuchsia: case TargetCXXABI::GenericARM: // Same as Itanium at this level @@ -10875,7 +10883,7 @@ MangleContext *ASTContext::createMangleContext(const TargetInfo *T) { if (!T) T = Target; - switch (T->getCXXABI().getKind()) { + switch (getCXXABIKind(T)) { case TargetCXXABI::AppleARM64: case TargetCXXABI::Fuchsia: case TargetCXXABI::GenericAArch64: diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -75,7 +75,7 @@ static const char AnnotationSection[] = "llvm.metadata"; static CGCXXABI *createCXXABI(CodeGenModule &CGM) { - switch (CGM.getTarget().getCXXABI().getKind()) { + switch (CGM.getContext().getCXXABIKind()) { case TargetCXXABI::AppleARM64: case TargetCXXABI::Fuchsia: case TargetCXXABI::GenericAArch64: diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp --- a/clang/lib/CodeGen/ItaniumCXXABI.cpp +++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp @@ -543,7 +543,7 @@ } CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) { - switch (CGM.getTarget().getCXXABI().getKind()) { + switch (CGM.getContext().getCXXABIKind()) { // For IR-generation purposes, there's no significant difference // between the ARM and iOS ABIs. case TargetCXXABI::GenericARM: diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5120,6 +5120,11 @@ /*Default=*/false)) Args.AddLastArg(CmdArgs, options::OPT_ffixed_point); + if (Args.hasFlag(options::OPT_ffuchsia_cxx_abi, + options::OPT_fno_fuchsia_cxx_abi, + /*Default=*/false)) + Args.AddLastArg(CmdArgs, options::OPT_ffuchsia_cxx_abi); + // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi} // (-ansi is equivalent to -std=c89 or -std=c++98). // diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2831,6 +2831,10 @@ } } + Opts.UseFuchsiaCXXABI = + Args.hasFlag(OPT_ffuchsia_cxx_abi, OPT_fno_fuchsia_cxx_abi, + /*default=*/T.isOSFuchsia()); + std::string ThreadModel = std::string(Args.getLastArgValue(OPT_mthread_model, "posix")); if (ThreadModel != "posix" && ThreadModel != "single") diff --git a/clang/test/CodeGenCXX/constructor-destructor-return-this.cpp b/clang/test/CodeGenCXX/constructor-destructor-return-this.cpp --- a/clang/test/CodeGenCXX/constructor-destructor-return-this.cpp +++ b/clang/test/CodeGenCXX/constructor-destructor-return-this.cpp @@ -5,6 +5,8 @@ //RUN: | FileCheck --check-prefix=CHECKARM %s //RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-unknown-fuchsia | FileCheck --check-prefix=CHECKFUCHSIA %s //RUN: %clang_cc1 %s -emit-llvm -o - -triple=aarch64-unknown-fuchsia | FileCheck --check-prefix=CHECKFUCHSIA %s +//RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-unknown-linux-gnu -ffuchsia-c++-abi | FileCheck --check-prefix=CHECKFUCHSIA %s +//RUN: %clang_cc1 %s -emit-llvm -o - -triple=x86_64-unknown-fuchsia -fno-fuchsia-c++-abi| FileCheck --check-prefix=CHECKFUCHSIA %s //RUN: %clang_cc1 %s -emit-llvm -o - -triple=i386-pc-win32 -fno-rtti | FileCheck --check-prefix=CHECKMS %s // FIXME: these tests crash on the bots when run with -triple=x86_64-pc-win32