]> granicus.if.org Git - clang/commitdiff
[Sanitizer] Refactor sanitizer options in LangOptions.
authorAlexey Samsonov <vonosmas@gmail.com>
Tue, 11 Nov 2014 01:26:14 +0000 (01:26 +0000)
committerAlexey Samsonov <vonosmas@gmail.com>
Tue, 11 Nov 2014 01:26:14 +0000 (01:26 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@221653 91177308-0d34-0410-b5e6-96231b3b80d8

12 files changed:
include/clang/Basic/LangOptions.def
include/clang/Basic/LangOptions.h
include/clang/Basic/Sanitizers.h
lib/AST/ASTContext.cpp
lib/AST/Decl.cpp
lib/Basic/CMakeLists.txt
lib/Basic/LangOptions.cpp
lib/Basic/Sanitizers.cpp [new file with mode: 0644]
lib/CodeGen/BackendUtil.cpp
lib/CodeGen/CGClass.cpp
lib/CodeGen/CodeGenFunction.h
lib/Frontend/CompilerInvocation.cpp

index e3adaec2f3a8e48905775c265c8fbc578107485f..9a77f9d00784bfa6bcb6b93e2ddd88b00a1d7d35 100644 (file)
@@ -216,6 +216,10 @@ LANGOPT(ApplePragmaPack, 1, 0, "Apple gcc-compatible #pragma pack handling")
 
 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
index 50958777b3d9737a17627c5b2cf05efa758649e9..5ac96c51985598895f35f454ced5fa40c665fedf 100644 (file)
 
 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;
index d3bea8b761b7b8b52c0e494a8d6912f96f96a959..423eaf6246d3b4eb43c4fa81a23f991df62e57cb 100644 (file)
@@ -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
index ba581396d9878f722b6e540166fdd4c44187a1db..dd67c9f73d3892b7eda3a31390259d3743e8f8a7 100644 (file)
@@ -738,7 +738,7 @@ ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
       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),
index 638ed8d9b086613cab02253f0de97ff2763df4f7..a937fdf09209400fa12f0232ccd0c8e6d964dc3c 100644 (file)
@@ -3628,7 +3628,7 @@ void RecordDecl::LoadFieldsFromExternalStorage() const {
 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);
index 2ecbf0aef6701d8f5353f7d1680b29e888d2c471..3bcbc89f8ca4c95df1b6f68e0c2cea6fcbbbe22b 100644 (file)
@@ -19,6 +19,7 @@ add_clang_library(clangBasic
   OpenMPKinds.cpp
   OperatorPrecedence.cpp
   SanitizerBlacklist.cpp
+  Sanitizers.cpp
   SourceLocation.cpp
   SourceManager.cpp
   TargetInfo.cpp
index 8992bfaff2b4457e81e562299a3ff510d3c20f82..dcbd22817111329cf6fb1ff96757654564beb750 100644 (file)
 
 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();
diff --git a/lib/Basic/Sanitizers.cpp b/lib/Basic/Sanitizers.cpp
new file mode 100644 (file)
index 0000000..7bc884d
--- /dev/null
@@ -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;
+}
index 3d4e6bbe1d76d6b81548ff915fa3a3af65543e53..57675b1fe192d409d557f2cdfe81b3c87eba80bf 100644 (file)
@@ -215,7 +215,7 @@ static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
   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,
index 1a4c1564270fbc900486874c5a1fe60a91f813ef..f64b8fef3faea450d5e4f7b5e4d0457da8f68d37 100644 (file)
@@ -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())
index de2f4daed9ba853d49c25f8c68628d298fd52784..013fdc0ed22669a79352de9e67b5ee18a25073bc 100644 (file)
@@ -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;
index f4665884cf5f91c5293572b83608fd04e77e7d48..add0c9bdb231983ee6956808abed2fbe8f1d9758 100644 (file)
@@ -1622,9 +1622,9 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
       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,