Sanitizers.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===- Sanitizers.cpp - C Language Family Language Options ----------------===//
  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. // This file defines the classes from Sanitizers.h
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Basic/Sanitizers.h"
  13. #include "llvm/ADT/Hashing.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/StringSwitch.h"
  16. #include "llvm/Support/MathExtras.h"
  17. using namespace clang;
  18. // Once LLVM switches to C++17, the constexpr variables can be inline and we
  19. // won't need this.
  20. #define SANITIZER(NAME, ID) constexpr SanitizerMask SanitizerKind::ID;
  21. #define SANITIZER_GROUP(NAME, ID, ALIAS) \
  22. constexpr SanitizerMask SanitizerKind::ID; \
  23. constexpr SanitizerMask SanitizerKind::ID##Group;
  24. #include "clang/Basic/Sanitizers.def"
  25. SanitizerMask clang::parseSanitizerValue(StringRef Value, bool AllowGroups) {
  26. SanitizerMask ParsedKind = llvm::StringSwitch<SanitizerMask>(Value)
  27. #define SANITIZER(NAME, ID) .Case(NAME, SanitizerKind::ID)
  28. #define SANITIZER_GROUP(NAME, ID, ALIAS) \
  29. .Case(NAME, AllowGroups ? SanitizerKind::ID##Group : SanitizerMask())
  30. #include "clang/Basic/Sanitizers.def"
  31. .Default(SanitizerMask());
  32. return ParsedKind;
  33. }
  34. void clang::serializeSanitizerSet(SanitizerSet Set,
  35. SmallVectorImpl<StringRef> &Values) {
  36. #define SANITIZER(NAME, ID) \
  37. if (Set.has(SanitizerKind::ID)) \
  38. Values.push_back(NAME);
  39. #include "clang/Basic/Sanitizers.def"
  40. }
  41. SanitizerMask clang::expandSanitizerGroups(SanitizerMask Kinds) {
  42. #define SANITIZER(NAME, ID)
  43. #define SANITIZER_GROUP(NAME, ID, ALIAS) \
  44. if (Kinds & SanitizerKind::ID##Group) \
  45. Kinds |= SanitizerKind::ID;
  46. #include "clang/Basic/Sanitizers.def"
  47. return Kinds;
  48. }
  49. llvm::hash_code SanitizerMask::hash_value() const {
  50. return llvm::hash_combine_range(&maskLoToHigh[0], &maskLoToHigh[kNumElem]);
  51. }
  52. namespace clang {
  53. unsigned SanitizerMask::countPopulation() const {
  54. unsigned total = 0;
  55. for (const auto &Val : maskLoToHigh)
  56. total += llvm::countPopulation(Val);
  57. return total;
  58. }
  59. llvm::hash_code hash_value(const clang::SanitizerMask &Arg) {
  60. return Arg.hash_value();
  61. }
  62. StringRef AsanDtorKindToString(llvm::AsanDtorKind kind) {
  63. switch (kind) {
  64. case llvm::AsanDtorKind::None:
  65. return "none";
  66. case llvm::AsanDtorKind::Global:
  67. return "global";
  68. case llvm::AsanDtorKind::Invalid:
  69. return "invalid";
  70. }
  71. return "invalid";
  72. }
  73. llvm::AsanDtorKind AsanDtorKindFromString(StringRef kindStr) {
  74. return llvm::StringSwitch<llvm::AsanDtorKind>(kindStr)
  75. .Case("none", llvm::AsanDtorKind::None)
  76. .Case("global", llvm::AsanDtorKind::Global)
  77. .Default(llvm::AsanDtorKind::Invalid);
  78. }
  79. StringRef AsanDetectStackUseAfterReturnModeToString(
  80. llvm::AsanDetectStackUseAfterReturnMode mode) {
  81. switch (mode) {
  82. case llvm::AsanDetectStackUseAfterReturnMode::Always:
  83. return "always";
  84. case llvm::AsanDetectStackUseAfterReturnMode::Runtime:
  85. return "runtime";
  86. case llvm::AsanDetectStackUseAfterReturnMode::Never:
  87. return "never";
  88. case llvm::AsanDetectStackUseAfterReturnMode::Invalid:
  89. return "invalid";
  90. }
  91. return "invalid";
  92. }
  93. llvm::AsanDetectStackUseAfterReturnMode
  94. AsanDetectStackUseAfterReturnModeFromString(StringRef modeStr) {
  95. return llvm::StringSwitch<llvm::AsanDetectStackUseAfterReturnMode>(modeStr)
  96. .Case("always", llvm::AsanDetectStackUseAfterReturnMode::Always)
  97. .Case("runtime", llvm::AsanDetectStackUseAfterReturnMode::Runtime)
  98. .Case("never", llvm::AsanDetectStackUseAfterReturnMode::Never)
  99. .Default(llvm::AsanDetectStackUseAfterReturnMode::Invalid);
  100. }
  101. } // namespace clang