diff --git a/clang/lib/Driver/ToolChains/Arch/AArch64.h b/clang/lib/Driver/ToolChains/Arch/AArch64.h --- a/clang/lib/Driver/ToolChains/Arch/AArch64.h +++ b/clang/lib/Driver/ToolChains/Arch/AArch64.h @@ -22,7 +22,6 @@ void getAArch64TargetFeatures(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, - llvm::opt::ArgStringList &CmdArgs, std::vector &Features, bool ForAS); diff --git a/clang/lib/Driver/ToolChains/Arch/AArch64.cpp b/clang/lib/Driver/ToolChains/Arch/AArch64.cpp --- a/clang/lib/Driver/ToolChains/Arch/AArch64.cpp +++ b/clang/lib/Driver/ToolChains/Arch/AArch64.cpp @@ -222,7 +222,6 @@ void aarch64::getAArch64TargetFeatures(const Driver &D, const llvm::Triple &Triple, const ArgList &Args, - llvm::opt::ArgStringList &CmdArgs, std::vector &Features, bool ForAS) { Arg *A; @@ -466,16 +465,10 @@ if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access, options::OPT_munaligned_access)) { - if (A->getOption().matches(options::OPT_mno_unaligned_access)) { + if (A->getOption().matches(options::OPT_mno_unaligned_access)) Features.push_back("+strict-align"); - if (!ForAS) - CmdArgs.push_back("-Wunaligned-access"); - } - } else if (Triple.isOSOpenBSD()) { + } else if (Triple.isOSOpenBSD()) Features.push_back("+strict-align"); - if (!ForAS) - CmdArgs.push_back("-Wunaligned-access"); - } if (Args.hasArg(options::OPT_ffixed_x1)) Features.push_back("+reserve-x1"); diff --git a/clang/lib/Driver/ToolChains/Arch/ARM.cpp b/clang/lib/Driver/ToolChains/Arch/ARM.cpp --- a/clang/lib/Driver/ToolChains/Arch/ARM.cpp +++ b/clang/lib/Driver/ToolChains/Arch/ARM.cpp @@ -772,8 +772,6 @@ // Kernel code has more strict alignment requirements. if (KernelOrKext) { Features.push_back("+strict-align"); - if (!ForAS) - CmdArgs.push_back("-Wunaligned-access"); } else if (Arg *A = Args.getLastArg(options::OPT_mno_unaligned_access, options::OPT_munaligned_access)) { if (A->getOption().matches(options::OPT_munaligned_access)) { @@ -784,11 +782,8 @@ // access either. else if (Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v8m_baseline) D.Diag(diag::err_target_unsupported_unaligned) << "v8m.base"; - } else { + } else Features.push_back("+strict-align"); - if (!ForAS) - CmdArgs.push_back("-Wunaligned-access"); - } } else { // Assume pre-ARMv6 doesn't support unaligned accesses. // @@ -807,23 +802,14 @@ int VersionNum = getARMSubArchVersionNumber(Triple); if (Triple.isOSDarwin() || Triple.isOSNetBSD()) { if (VersionNum < 6 || - Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m) { + Triple.getSubArch() == llvm::Triple::SubArchType::ARMSubArch_v6m) Features.push_back("+strict-align"); - if (!ForAS) - CmdArgs.push_back("-Wunaligned-access"); - } } else if (Triple.isOSLinux() || Triple.isOSNaCl() || Triple.isOSWindows()) { - if (VersionNum < 7) { + if (VersionNum < 7) Features.push_back("+strict-align"); - if (!ForAS) - CmdArgs.push_back("-Wunaligned-access"); - } - } else { + } else Features.push_back("+strict-align"); - if (!ForAS) - CmdArgs.push_back("-Wunaligned-access"); - } } // llvm does not support reserving registers in general. There is support 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 @@ -347,8 +347,7 @@ case llvm::Triple::aarch64: case llvm::Triple::aarch64_32: case llvm::Triple::aarch64_be: - aarch64::getAArch64TargetFeatures(D, Triple, Args, CmdArgs, Features, - ForAS); + aarch64::getAArch64TargetFeatures(D, Triple, Args, Features, ForAS); break; case llvm::Triple::x86: case llvm::Triple::x86_64: @@ -1625,6 +1624,16 @@ CmdArgs.push_back("-target-abi"); CmdArgs.push_back(ABIName); } + +void AddUnalignedAccessWarning(ArgStringList &CmdArgs) { + auto StrictAlignIter = + std::find_if(CmdArgs.rbegin(), CmdArgs.rend(), [](StringRef Arg) { + return Arg == "+strict-align" || Arg == "-strict-align"; + }); + if (StrictAlignIter != CmdArgs.rend() && + StringRef(*StrictAlignIter) == "+strict-align") + CmdArgs.push_back("-Wunaligned-access"); +} } static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args, @@ -1720,6 +1729,8 @@ // Enable/disable return address signing and indirect branch targets. CollectARMPACBTIOptions(getToolChain(), Args, CmdArgs, false /*isAArch64*/); + + AddUnalignedAccessWarning(CmdArgs); } void Clang::RenderTargetOptions(const llvm::Triple &EffectiveTriple, @@ -1893,6 +1904,8 @@ CmdArgs.push_back(Args.MakeArgString(TuneCPU)); } } + + AddUnalignedAccessWarning(CmdArgs); } void Clang::AddMIPSTargetArgs(const ArgList &Args, diff --git a/clang/test/Driver/arm-alignment.c b/clang/test/Driver/arm-alignment.c --- a/clang/test/Driver/arm-alignment.c +++ b/clang/test/Driver/arm-alignment.c @@ -86,8 +86,8 @@ // RUN: %clang -target aarch64-unknown-openbsd -### %s 2> %t // RUN: FileCheck --check-prefix=CHECK-ALIGNED-AARCH64 < %t %s -// CHECK-ALIGNED-ARM: "-target-feature" "+strict-align" -// CHECK-ALIGNED-AARCH64: "-target-feature" "+strict-align" +// CHECK-ALIGNED-ARM: "-target-feature" "+strict-align" {{.*}} "-Wunaligned-access" +// CHECK-ALIGNED-AARCH64: "-target-feature" "+strict-align" {{.*}} "-Wunaligned-access" // Make sure that v6M cores and v8M Baseline always trigger the unsupported // aligned accesses error for all supported architecture triples.