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