clang  7.0.0svn
Sanitizers.h
Go to the documentation of this file.
1 //===- Sanitizers.h - C Language Family Language Options --------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// Defines the clang::SanitizerKind enum.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_BASIC_SANITIZERS_H
16 #define LLVM_CLANG_BASIC_SANITIZERS_H
17 
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/MathExtras.h"
21 #include <cassert>
22 #include <cstdint>
23 
24 namespace clang {
25 
26 using SanitizerMask = uint64_t;
27 
28 namespace SanitizerKind {
29 
30 // Assign ordinals to possible values of -fsanitize= flag, which we will use as
31 // bit positions.
32 enum SanitizerOrdinal : uint64_t {
33 #define SANITIZER(NAME, ID) SO_##ID,
34 #define SANITIZER_GROUP(NAME, ID, ALIAS) SO_##ID##Group,
35 #include "clang/Basic/Sanitizers.def"
37 };
38 
39 // Define the set of sanitizer kinds, as well as the set of sanitizers each
40 // sanitizer group expands into.
41 #define SANITIZER(NAME, ID) \
42  const SanitizerMask ID = 1ULL << SO_##ID;
43 #define SANITIZER_GROUP(NAME, ID, ALIAS) \
44  const SanitizerMask ID = ALIAS; \
45  const SanitizerMask ID##Group = 1ULL << SO_##ID##Group;
46 #include "clang/Basic/Sanitizers.def"
47 
48 } // namespace SanitizerKind
49 
50 struct SanitizerSet {
51  /// Check if a certain (single) sanitizer is enabled.
52  bool has(SanitizerMask K) const {
53  assert(llvm::isPowerOf2_64(K));
54  return Mask & K;
55  }
56 
57  /// Check if one or more sanitizers are enabled.
58  bool hasOneOf(SanitizerMask K) const { return Mask & K; }
59 
60  /// Enable or disable a certain (single) sanitizer.
61  void set(SanitizerMask K, bool Value) {
62  assert(llvm::isPowerOf2_64(K));
63  Mask = Value ? (Mask | K) : (Mask & ~K);
64  }
65 
66  /// Disable the sanitizers specified in \p K.
67  void clear(SanitizerMask K = SanitizerKind::All) { Mask &= ~K; }
68 
69  /// Returns true if at least one sanitizer is enabled.
70  bool empty() const { return Mask == 0; }
71 
72  /// Bitmask of enabled sanitizers.
73  SanitizerMask Mask = 0;
74 };
75 
76 /// Parse a single value from a -fsanitize= or -fno-sanitize= value list.
77 /// Returns a non-zero SanitizerMask, or \c 0 if \p Value is not known.
78 SanitizerMask parseSanitizerValue(StringRef Value, bool AllowGroups);
79 
80 /// For each sanitizer group bit set in \p Kinds, set the bits for sanitizers
81 /// this group enables.
83 
84 /// Return the sanitizers which do not affect preprocessing.
86  return SanitizerKind::CFI | SanitizerKind::Integer |
87  SanitizerKind::Nullability | SanitizerKind::Undefined;
88 }
89 
90 } // namespace clang
91 
92 #endif // LLVM_CLANG_BASIC_SANITIZERS_H
bool hasOneOf(SanitizerMask K) const
Check if one or more sanitizers are enabled.
Definition: Sanitizers.h:58
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
void clear(SanitizerMask K=SanitizerKind::All)
Disable the sanitizers specified in K.
Definition: Sanitizers.h:67
SanitizerMask getPPTransparentSanitizers()
Return the sanitizers which do not affect preprocessing.
Definition: Sanitizers.h:85
Dataflow Directional Tag Classes.
bool empty() const
Returns true if at least one sanitizer is enabled.
Definition: Sanitizers.h:70
SanitizerMask parseSanitizerValue(StringRef Value, bool AllowGroups)
Parse a single value from a -fsanitize= or -fno-sanitize= value list.
Definition: Sanitizers.cpp:19
uint64_t SanitizerMask
Definition: Sanitizers.h:26
SanitizerMask expandSanitizerGroups(SanitizerMask Kinds)
For each sanitizer group bit set in Kinds, set the bits for sanitizers this group enables...
Definition: Sanitizers.cpp:29
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:52