r221653 - [Sanitizer] Refactor sanitizer options in LangOptions.
Alexey Samsonov
vonosmas at gmail.com
Mon Nov 10 17:26:14 PST 2014
Author: samsonov
Date: Mon Nov 10 19:26:14 2014
New Revision: 221653
URL: http://llvm.org/viewvc/llvm-project?rev=221653&view=rev
Log:
[Sanitizer] Refactor sanitizer options in LangOptions.
Get rid of ugly SanitizerOptions class thrust into LangOptions:
* Make SanitizeAddressFieldPadding a regular language option,
and rely on default behavior to initialize/reset it.
* Make SanitizerBlacklistFile a regular member LangOptions.
* Introduce the helper class "SanitizerSet" to represent the
set of enabled sanitizers and make it a member of LangOptions.
It is exactly the entity we want to cache and modify in CodeGenFunction,
for instance. We'd also be able to reuse SanitizerSet in
CodeGenOptions for storing the set of recoverable sanitizers,
and in the Driver to represent the set of sanitizers
turned on/off by the commandline flags.
No functionality change.
Added:
cfe/trunk/lib/Basic/Sanitizers.cpp
Modified:
cfe/trunk/include/clang/Basic/LangOptions.def
cfe/trunk/include/clang/Basic/LangOptions.h
cfe/trunk/include/clang/Basic/Sanitizers.h
cfe/trunk/lib/AST/ASTContext.cpp
cfe/trunk/lib/AST/Decl.cpp
cfe/trunk/lib/Basic/CMakeLists.txt
cfe/trunk/lib/Basic/LangOptions.cpp
cfe/trunk/lib/CodeGen/BackendUtil.cpp
cfe/trunk/lib/CodeGen/CGClass.cpp
cfe/trunk/lib/CodeGen/CodeGenFunction.h
cfe/trunk/lib/Frontend/CompilerInvocation.cpp
Modified: cfe/trunk/include/clang/Basic/LangOptions.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.def?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/LangOptions.def (original)
+++ cfe/trunk/include/clang/Basic/LangOptions.def Mon Nov 10 19:26:14 2014
@@ -216,6 +216,10 @@ LANGOPT(ApplePragmaPack, 1, 0, "Apple gc
LANGOPT(RetainCommentsFromSystemHeaders, 1, 0, "retain documentation comments from system headers in the AST")
+LANGOPT(SanitizeAddressFieldPadding, 2, 0, "controls how aggressive is ASan "
+ "field padding (0: none, 1:least "
+ "aggressive, 2: more aggressive)")
+
#undef LANGOPT
#undef COMPATIBLE_LANGOPT
#undef BENIGN_LANGOPT
Modified: cfe/trunk/include/clang/Basic/LangOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.h?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/LangOptions.h (original)
+++ cfe/trunk/include/clang/Basic/LangOptions.h Mon Nov 10 19:26:14 2014
@@ -24,29 +24,6 @@
namespace clang {
-class SanitizerOptions {
- /// \brief Bitmask of enabled sanitizers.
- unsigned Kind;
-public:
- SanitizerOptions();
-
- /// \brief Check if a certain sanitizer is enabled.
- bool has(SanitizerKind K) const;
- /// \brief Enable or disable a certain sanitizer.
- void set(SanitizerKind K, bool Value);
-
- /// \brief Controls how agressive is asan field padding (0: none, 1: least
- /// aggressive, 2: more aggressive).
- unsigned SanitizeAddressFieldPadding : 2;
-
- /// \brief Path to blacklist file specifying which objects
- /// (files, functions, variables) should not be instrumented.
- std::string BlacklistFile;
-
- /// \brief Disable all sanitizers.
- void clear();
-};
-
/// Bitfields of LangOptions, split out from LangOptions in order to ensure that
/// this large collection of bitfields is a trivial class type.
class LangOptionsBase {
@@ -56,7 +33,6 @@ public:
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description)
#include "clang/Basic/LangOptions.def"
- SanitizerOptions Sanitize;
protected:
// Define language options of enumeration type. These are private, and will
// have accessors (below).
@@ -91,6 +67,13 @@ public:
enum AddrSpaceMapMangling { ASMM_Target, ASMM_On, ASMM_Off };
public:
+ /// \brief Set of enabled sanitizers.
+ SanitizerSet Sanitize;
+
+ /// \brief Path to blacklist file specifying which objects
+ /// (files, functions, variables) should not be instrumented.
+ std::string SanitizerBlacklistFile;
+
clang::ObjCRuntime ObjCRuntime;
std::string ObjCConstantStringClass;
Modified: cfe/trunk/include/clang/Basic/Sanitizers.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Sanitizers.h?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Sanitizers.h (original)
+++ cfe/trunk/include/clang/Basic/Sanitizers.h Mon Nov 10 19:26:14 2014
@@ -23,6 +23,22 @@ enum class SanitizerKind {
Unknown
};
+class SanitizerSet {
+ /// \brief Bitmask of enabled sanitizers.
+ unsigned Kinds;
+public:
+ SanitizerSet();
+
+ /// \brief Check if a certain sanitizer is enabled.
+ bool has(SanitizerKind K) const;
+
+ /// \brief Enable or disable a certain sanitizer.
+ void set(SanitizerKind K, bool Value);
+
+ /// \brief Disable all sanitizers.
+ void clear();
+};
+
} // end namespace clang
#endif
Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Mon Nov 10 19:26:14 2014
@@ -738,7 +738,7 @@ ASTContext::ASTContext(LangOptions &LOpt
BlockDescriptorExtendedType(nullptr), cudaConfigureCallDecl(nullptr),
NullTypeSourceInfo(QualType()), FirstLocalImport(), LastLocalImport(),
SourceMgr(SM), LangOpts(LOpts),
- SanitizerBL(new SanitizerBlacklist(LangOpts.Sanitize.BlacklistFile, SM)),
+ SanitizerBL(new SanitizerBlacklist(LangOpts.SanitizerBlacklistFile, SM)),
AddrSpaceMap(nullptr), Target(nullptr), PrintingPolicy(LOpts),
Idents(idents), Selectors(sels), BuiltinInfo(builtins),
DeclarationNames(*this), ExternalSource(nullptr), Listener(nullptr),
Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Mon Nov 10 19:26:14 2014
@@ -3628,7 +3628,7 @@ void RecordDecl::LoadFieldsFromExternalS
bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const {
ASTContext &Context = getASTContext();
if (!Context.getLangOpts().Sanitize.has(SanitizerKind::Address) ||
- !Context.getLangOpts().Sanitize.SanitizeAddressFieldPadding)
+ !Context.getLangOpts().SanitizeAddressFieldPadding)
return false;
const auto &Blacklist = Context.getSanitizerBlacklist();
const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this);
Modified: cfe/trunk/lib/Basic/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/CMakeLists.txt?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/CMakeLists.txt (original)
+++ cfe/trunk/lib/Basic/CMakeLists.txt Mon Nov 10 19:26:14 2014
@@ -19,6 +19,7 @@ add_clang_library(clangBasic
OpenMPKinds.cpp
OperatorPrecedence.cpp
SanitizerBlacklist.cpp
+ Sanitizers.cpp
SourceLocation.cpp
SourceManager.cpp
TargetInfo.cpp
Modified: cfe/trunk/lib/Basic/LangOptions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/LangOptions.cpp?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/LangOptions.cpp (original)
+++ cfe/trunk/lib/Basic/LangOptions.cpp Mon Nov 10 19:26:14 2014
@@ -14,24 +14,6 @@
using namespace clang;
-SanitizerOptions::SanitizerOptions()
- : Kind(0), SanitizeAddressFieldPadding(0) {}
-
-bool SanitizerOptions::has(SanitizerKind K) const {
- unsigned Bit = static_cast<unsigned>(K);
- return Kind & (1 << Bit);
-}
-
-void SanitizerOptions::set(SanitizerKind K, bool Value) {
- unsigned Bit = static_cast<unsigned>(K);
- Kind = Value ? (Kind | (1 << Bit)) : (Kind & ~(1 << Bit));
-}
-
-void SanitizerOptions::clear() {
- SanitizerOptions Default;
- *this = std::move(Default);
-}
-
LangOptions::LangOptions() {
#define LANGOPT(Name, Bits, Default, Description) Name = Default;
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) set##Name(Default);
@@ -48,6 +30,7 @@ void LangOptions::resetNonModularOptions
// FIXME: This should not be reset; modules can be different with different
// sanitizer options (this affects __has_feature(address_sanitizer) etc).
Sanitize.clear();
+ SanitizerBlacklistFile.clear();
CurrentModule.clear();
ImplementationOfModule.clear();
Added: cfe/trunk/lib/Basic/Sanitizers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Sanitizers.cpp?rev=221653&view=auto
==============================================================================
--- cfe/trunk/lib/Basic/Sanitizers.cpp (added)
+++ cfe/trunk/lib/Basic/Sanitizers.cpp Mon Nov 10 19:26:14 2014
@@ -0,0 +1,31 @@
+//===--- Sanitizers.cpp - C Language Family Language Options ----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the classes from Sanitizers.h
+//
+//===----------------------------------------------------------------------===//
+#include "clang/Basic/Sanitizers.h"
+
+using namespace clang;
+
+SanitizerSet::SanitizerSet() : Kinds(0) {}
+
+bool SanitizerSet::has(SanitizerKind K) const {
+ unsigned Bit = static_cast<unsigned>(K);
+ return Kinds & (1 << Bit);
+}
+
+void SanitizerSet::set(SanitizerKind K, bool Value) {
+ unsigned Bit = static_cast<unsigned>(K);
+ Kinds = Value ? (Kinds | (1 << Bit)) : (Kinds & ~(1 << Bit));
+}
+
+void SanitizerSet::clear() {
+ Kinds = 0;
+}
Modified: cfe/trunk/lib/CodeGen/BackendUtil.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/BackendUtil.cpp?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/BackendUtil.cpp (original)
+++ cfe/trunk/lib/CodeGen/BackendUtil.cpp Mon Nov 10 19:26:14 2014
@@ -215,7 +215,7 @@ static void addDataFlowSanitizerPass(con
const PassManagerBuilderWrapper &BuilderWrapper =
static_cast<const PassManagerBuilderWrapper&>(Builder);
const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
- PM.add(createDataFlowSanitizerPass(LangOpts.Sanitize.BlacklistFile));
+ PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFile));
}
static TargetLibraryInfo *createTLI(llvm::Triple &TargetTriple,
Modified: cfe/trunk/lib/CodeGen/CGClass.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGClass.cpp?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGClass.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGClass.cpp Mon Nov 10 19:26:14 2014
@@ -842,7 +842,7 @@ namespace {
}
private:
CodeGenFunction &CGF;
- SanitizerOptions OldSanOpts;
+ SanitizerSet OldSanOpts;
};
}
@@ -858,7 +858,7 @@ namespace {
bool isMemcpyableField(FieldDecl *F) const {
// Never memcpy fields when we are adding poisoned paddings.
- if (CGF.getContext().getLangOpts().Sanitize.SanitizeAddressFieldPadding)
+ if (CGF.getContext().getLangOpts().SanitizeAddressFieldPadding)
return false;
Qualifiers Qual = F->getType().getQualifiers();
if (Qual.hasVolatile() || Qual.hasObjCLifetime())
Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Mon Nov 10 19:26:14 2014
@@ -248,8 +248,8 @@ public:
/// potentially higher performance penalties.
unsigned char BoundsChecking;
- /// \brief Sanitizer options to use for this function.
- SanitizerOptions SanOpts;
+ /// \brief Sanitizers enabled for this function.
+ SanitizerSet SanOpts;
/// \brief True if CodeGen currently emits code implementing sanitizer checks.
bool IsSanitizerScope;
Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=221653&r1=221652&r2=221653&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Mon Nov 10 19:26:14 2014
@@ -1622,9 +1622,9 @@ static void ParseLangArgs(LangOptions &O
Opts.Sanitize.set(K, true);
}
// -fsanitize-address-field-padding=N has to be a LangOpt, parse it here.
- Opts.Sanitize.SanitizeAddressFieldPadding =
+ Opts.SanitizeAddressFieldPadding =
getLastArgIntValue(Args, OPT_fsanitize_address_field_padding, 0, Diags);
- Opts.Sanitize.BlacklistFile = Args.getLastArgValue(OPT_fsanitize_blacklist);
+ Opts.SanitizerBlacklistFile = Args.getLastArgValue(OPT_fsanitize_blacklist);
}
static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args,
More information about the cfe-commits
mailing list