[clang] 7314aea - [clang] Move branch-protection from CodeGenOptions to LangOptions
Daniel Kiss via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 2 01:32:00 PDT 2020
Author: Daniel Kiss
Date: 2020-04-02T10:31:52+02:00
New Revision: 7314aea5a42d33f9f5af5d158d8892e88072764e
URL: https://github.com/llvm/llvm-project/commit/7314aea5a42d33f9f5af5d158d8892e88072764e
DIFF: https://github.com/llvm/llvm-project/commit/7314aea5a42d33f9f5af5d158d8892e88072764e.diff
LOG: [clang] Move branch-protection from CodeGenOptions to LangOptions
Summary:
Reason: the option has an effect on preprocessing.
Also see thread: http://lists.llvm.org/pipermail/cfe-dev/2020-March/065014.html
Reviewers: chill, efriedma
Reviewed By: efriedma
Subscribers: efriedma, danielkiss, cfe-commits, kristof.beyls
Tags: #clang
Differential Revision: https://reviews.llvm.org/D77131
Added:
Modified:
clang/include/clang/Basic/CodeGenOptions.def
clang/include/clang/Basic/CodeGenOptions.h
clang/include/clang/Basic/LangOptions.def
clang/include/clang/Basic/LangOptions.h
clang/include/clang/Basic/TargetInfo.h
clang/lib/AST/PrintfFormatString.cpp
clang/lib/Basic/Targets/AArch64.cpp
clang/lib/CodeGen/CGDeclCXX.cpp
clang/lib/CodeGen/TargetInfo.cpp
clang/lib/Frontend/CompilerInvocation.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index a62cf5e729db..0faa013ac497 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -387,10 +387,6 @@ CODEGENOPT(ForceEmitVTables, 1, 0)
/// Whether to emit an address-significance table into the object file.
CODEGENOPT(Addrsig, 1, 0)
-ENUM_CODEGENOPT(SignReturnAddress, SignReturnAddressScope, 2, SignReturnAddressScope::None)
-ENUM_CODEGENOPT(SignReturnAddressKey, SignReturnAddressKeyValue, 1, SignReturnAddressKeyValue::AKey)
-CODEGENOPT(BranchTargetEnforcement, 1, 0)
-
/// Whether to emit unused static constants.
CODEGENOPT(KeepStaticConsts, 1, 0)
@@ -400,4 +396,3 @@ CODEGENOPT(ForceAAPCSBitfieldLoad, 1, 0)
#undef CODEGENOPT
#undef ENUM_CODEGENOPT
#undef VALUE_CODEGENOPT
-
diff --git a/clang/include/clang/Basic/CodeGenOptions.h b/clang/include/clang/Basic/CodeGenOptions.h
index d435bebcdcf9..60d418688710 100644
--- a/clang/include/clang/Basic/CodeGenOptions.h
+++ b/clang/include/clang/Basic/CodeGenOptions.h
@@ -110,14 +110,6 @@ class CodeGenOptions : public CodeGenOptionsBase {
Embed_Marker // Embed a marker as a placeholder for bitcode.
};
- enum class SignReturnAddressScope {
- None, // No signing for any function
- NonLeaf, // Sign the return address of functions that spill LR
- All // Sign the return address of all functions
- };
-
- enum class SignReturnAddressKeyValue { AKey, BKey };
-
enum class FramePointerKind {
None, // Omit all frame pointers.
NonLeaf, // Keep non-leaf frame pointers.
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index 6152e227d599..51dc87b0b671 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -353,6 +353,12 @@ LANGOPT(RegisterStaticDestructors, 1, 1, "Register C++ static destructors")
COMPATIBLE_VALUE_LANGOPT(MaxTokens, 32, 0, "Max number of tokens per TU or 0")
+ENUM_LANGOPT(SignReturnAddressScope, SignReturnAddressScopeKind, 2, SignReturnAddressScopeKind::None,
+ "Scope of return address signing")
+ENUM_LANGOPT(SignReturnAddressKey, SignReturnAddressKeyKind, 1, SignReturnAddressKeyKind::AKey,
+ "Key used for return address signing")
+LANGOPT(BranchTargetEnforcement, 1, 0, "Branch-target enforcement enabled")
+
#undef LANGOPT
#undef COMPATIBLE_LANGOPT
#undef BENIGN_LANGOPT
diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h
index 524ae9a822f4..090263342401 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -229,6 +229,22 @@ class LangOptions : public LangOptionsBase {
All,
};
+ enum class SignReturnAddressScopeKind {
+ /// No signing for any function.
+ None,
+ /// Sign the return address of functions that spill LR.
+ NonLeaf,
+ /// Sign the return address of all functions,
+ All
+ };
+
+ enum class SignReturnAddressKeyKind {
+ /// Return address signing uses APIA key.
+ AKey,
+ /// Return address signing uses APIB key.
+ BKey
+ };
+
public:
/// Set of enabled sanitizers.
SanitizerSet Sanitize;
diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h
index 5edfa0e4e0c7..ab4795404071 100644
--- a/clang/include/clang/Basic/TargetInfo.h
+++ b/clang/include/clang/Basic/TargetInfo.h
@@ -16,7 +16,7 @@
#include "clang/Basic/AddressSpaces.h"
#include "clang/Basic/LLVM.h"
-#include "clang/Basic/CodeGenOptions.h"
+#include "clang/Basic/LangOptions.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Basic/TargetCXXABI.h"
#include "clang/Basic/TargetOptions.h"
@@ -1138,10 +1138,10 @@ class TargetInfo : public virtual TransferrableTargetInfo,
}
struct BranchProtectionInfo {
- CodeGenOptions::SignReturnAddressScope SignReturnAddr =
- CodeGenOptions::SignReturnAddressScope::None;
- CodeGenOptions::SignReturnAddressKeyValue SignKey =
- CodeGenOptions::SignReturnAddressKeyValue::AKey;
+ LangOptions::SignReturnAddressScopeKind SignReturnAddr =
+ LangOptions::SignReturnAddressScopeKind::None;
+ LangOptions::SignReturnAddressKeyKind SignKey =
+ LangOptions::SignReturnAddressKeyKind::AKey;
bool BranchTargetEnforcement = false;
};
diff --git a/clang/lib/AST/PrintfFormatString.cpp b/clang/lib/AST/PrintfFormatString.cpp
index bae60d464407..d13ff0a02d17 100644
--- a/clang/lib/AST/PrintfFormatString.cpp
+++ b/clang/lib/AST/PrintfFormatString.cpp
@@ -11,10 +11,11 @@
//
//===----------------------------------------------------------------------===//
+#include "FormatStringParsing.h"
#include "clang/AST/FormatString.h"
#include "clang/AST/OSLog.h"
-#include "FormatStringParsing.h"
#include "clang/Basic/TargetInfo.h"
+#include "llvm/Support/Regex.h"
using clang::analyze_format_string::ArgType;
using clang::analyze_format_string::FormatStringHandler;
diff --git a/clang/lib/Basic/Targets/AArch64.cpp b/clang/lib/Basic/Targets/AArch64.cpp
index e7bfff504a7c..f70a53457613 100644
--- a/clang/lib/Basic/Targets/AArch64.cpp
+++ b/clang/lib/Basic/Targets/AArch64.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "AArch64.h"
+#include "clang/Basic/LangOptions.h"
#include "clang/Basic/TargetBuiltins.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/ADT/ArrayRef.h"
@@ -121,15 +122,15 @@ bool AArch64TargetInfo::validateBranchProtection(StringRef Spec,
return false;
BPI.SignReturnAddr =
- llvm::StringSwitch<CodeGenOptions::SignReturnAddressScope>(PBP.Scope)
- .Case("non-leaf", CodeGenOptions::SignReturnAddressScope::NonLeaf)
- .Case("all", CodeGenOptions::SignReturnAddressScope::All)
- .Default(CodeGenOptions::SignReturnAddressScope::None);
+ llvm::StringSwitch<LangOptions::SignReturnAddressScopeKind>(PBP.Scope)
+ .Case("non-leaf", LangOptions::SignReturnAddressScopeKind::NonLeaf)
+ .Case("all", LangOptions::SignReturnAddressScopeKind::All)
+ .Default(LangOptions::SignReturnAddressScopeKind::None);
if (PBP.Key == "a_key")
- BPI.SignKey = CodeGenOptions::SignReturnAddressKeyValue::AKey;
+ BPI.SignKey = LangOptions::SignReturnAddressKeyKind::AKey;
else
- BPI.SignKey = CodeGenOptions::SignReturnAddressKeyValue::BKey;
+ BPI.SignKey = LangOptions::SignReturnAddressKeyKind::BKey;
BPI.BranchTargetEnforcement = PBP.BranchTargetEnforcement;
return true;
diff --git a/clang/lib/CodeGen/CGDeclCXX.cpp b/clang/lib/CodeGen/CGDeclCXX.cpp
index 3baa0a080f5d..2a01aff9f0f2 100644
--- a/clang/lib/CodeGen/CGDeclCXX.cpp
+++ b/clang/lib/CodeGen/CGDeclCXX.cpp
@@ -16,7 +16,7 @@
#include "CodeGenFunction.h"
#include "TargetInfo.h"
#include "clang/AST/Attr.h"
-#include "clang/Basic/CodeGenOptions.h"
+#include "clang/Basic/LangOptions.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/MDBuilder.h"
@@ -392,20 +392,20 @@ llvm::Function *CodeGenModule::CreateGlobalInitOrDestructFunction(
!isInSanitizerBlacklist(SanitizerKind::ShadowCallStack, Fn, Loc))
Fn->addFnAttr(llvm::Attribute::ShadowCallStack);
- auto RASignKind = getCodeGenOpts().getSignReturnAddress();
- if (RASignKind != CodeGenOptions::SignReturnAddressScope::None) {
+ auto RASignKind = getLangOpts().getSignReturnAddressScope();
+ if (RASignKind != LangOptions::SignReturnAddressScopeKind::None) {
Fn->addFnAttr("sign-return-address",
- RASignKind == CodeGenOptions::SignReturnAddressScope::All
+ RASignKind == LangOptions::SignReturnAddressScopeKind::All
? "all"
: "non-leaf");
- auto RASignKey = getCodeGenOpts().getSignReturnAddressKey();
+ auto RASignKey = getLangOpts().getSignReturnAddressKey();
Fn->addFnAttr("sign-return-address-key",
- RASignKey == CodeGenOptions::SignReturnAddressKeyValue::AKey
+ RASignKey == LangOptions::SignReturnAddressKeyKind::AKey
? "a_key"
: "b_key");
}
- if (getCodeGenOpts().BranchTargetEnforcement)
+ if (getLangOpts().BranchTargetEnforcement)
Fn->addFnAttr("branch-target-enforcement");
return Fn;
diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp
index e64fe4f3943d..e2825a3752a1 100644
--- a/clang/lib/CodeGen/TargetInfo.cpp
+++ b/clang/lib/CodeGen/TargetInfo.cpp
@@ -5107,9 +5107,11 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
if (!FD)
return;
- CodeGenOptions::SignReturnAddressScope Scope = CGM.getCodeGenOpts().getSignReturnAddress();
- CodeGenOptions::SignReturnAddressKeyValue Key = CGM.getCodeGenOpts().getSignReturnAddressKey();
- bool BranchTargetEnforcement = CGM.getCodeGenOpts().BranchTargetEnforcement;
+ LangOptions::SignReturnAddressScopeKind Scope =
+ CGM.getLangOpts().getSignReturnAddressScope();
+ LangOptions::SignReturnAddressKeyKind Key =
+ CGM.getLangOpts().getSignReturnAddressKey();
+ bool BranchTargetEnforcement = CGM.getLangOpts().BranchTargetEnforcement;
if (const auto *TA = FD->getAttr<TargetAttr>()) {
ParsedTargetAttr Attr = TA->parse();
if (!Attr.BranchProtection.empty()) {
@@ -5125,14 +5127,14 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
}
auto *Fn = cast<llvm::Function>(GV);
- if (Scope != CodeGenOptions::SignReturnAddressScope::None) {
+ if (Scope != LangOptions::SignReturnAddressScopeKind::None) {
Fn->addFnAttr("sign-return-address",
- Scope == CodeGenOptions::SignReturnAddressScope::All
+ Scope == LangOptions::SignReturnAddressScopeKind::All
? "all"
: "non-leaf");
Fn->addFnAttr("sign-return-address-key",
- Key == CodeGenOptions::SignReturnAddressKeyValue::AKey
+ Key == LangOptions::SignReturnAddressKeyKind::AKey
? "a_key"
: "b_key");
}
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 5d0be3c1f1b0..23638e16a5b7 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -1387,38 +1387,6 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
Opts.Addrsig = Args.hasArg(OPT_faddrsig);
- if (Arg *A = Args.getLastArg(OPT_msign_return_address_EQ)) {
- StringRef SignScope = A->getValue();
-
- if (SignScope.equals_lower("none"))
- Opts.setSignReturnAddress(CodeGenOptions::SignReturnAddressScope::None);
- else if (SignScope.equals_lower("all"))
- Opts.setSignReturnAddress(CodeGenOptions::SignReturnAddressScope::All);
- else if (SignScope.equals_lower("non-leaf"))
- Opts.setSignReturnAddress(
- CodeGenOptions::SignReturnAddressScope::NonLeaf);
- else
- Diags.Report(diag::err_drv_invalid_value)
- << A->getAsString(Args) << SignScope;
-
- if (Arg *A = Args.getLastArg(OPT_msign_return_address_key_EQ)) {
- StringRef SignKey = A->getValue();
- if (!SignScope.empty() && !SignKey.empty()) {
- if (SignKey.equals_lower("a_key"))
- Opts.setSignReturnAddressKey(
- CodeGenOptions::SignReturnAddressKeyValue::AKey);
- else if (SignKey.equals_lower("b_key"))
- Opts.setSignReturnAddressKey(
- CodeGenOptions::SignReturnAddressKeyValue::BKey);
- else
- Diags.Report(diag::err_drv_invalid_value)
- << A->getAsString(Args) << SignKey;
- }
- }
- }
-
- Opts.BranchTargetEnforcement = Args.hasArg(OPT_mbranch_target_enforce);
-
Opts.KeepStaticConsts = Args.hasArg(OPT_fkeep_static_consts);
Opts.SpeculativeLoadHardening = Args.hasArg(OPT_mspeculative_load_hardening);
@@ -3348,6 +3316,40 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
Opts.BuildingPCHWithObjectFile = Args.hasArg(OPT_building_pch_with_obj);
Opts.MaxTokens = getLastArgIntValue(Args, OPT_fmax_tokens_EQ, 0, Diags);
+
+ if (Arg *A = Args.getLastArg(OPT_msign_return_address_EQ)) {
+ StringRef SignScope = A->getValue();
+
+ if (SignScope.equals_lower("none"))
+ Opts.setSignReturnAddressScope(
+ LangOptions::SignReturnAddressScopeKind::None);
+ else if (SignScope.equals_lower("all"))
+ Opts.setSignReturnAddressScope(
+ LangOptions::SignReturnAddressScopeKind::All);
+ else if (SignScope.equals_lower("non-leaf"))
+ Opts.setSignReturnAddressScope(
+ LangOptions::SignReturnAddressScopeKind::NonLeaf);
+ else
+ Diags.Report(diag::err_drv_invalid_value)
+ << A->getAsString(Args) << SignScope;
+
+ if (Arg *A = Args.getLastArg(OPT_msign_return_address_key_EQ)) {
+ StringRef SignKey = A->getValue();
+ if (!SignScope.empty() && !SignKey.empty()) {
+ if (SignKey.equals_lower("a_key"))
+ Opts.setSignReturnAddressKey(
+ LangOptions::SignReturnAddressKeyKind::AKey);
+ else if (SignKey.equals_lower("b_key"))
+ Opts.setSignReturnAddressKey(
+ LangOptions::SignReturnAddressKeyKind::BKey);
+ else
+ Diags.Report(diag::err_drv_invalid_value)
+ << A->getAsString(Args) << SignKey;
+ }
+ }
+ }
+
+ Opts.BranchTargetEnforcement = Args.hasArg(OPT_mbranch_target_enforce);
}
static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) {
More information about the cfe-commits
mailing list