17330f729Sjoerg //===- Sanitizers.cpp - C Language Family Language Options ----------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file defines the classes from Sanitizers.h
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "clang/Basic/Sanitizers.h"
147330f729Sjoerg #include "llvm/ADT/Hashing.h"
15*e038c9c4Sjoerg #include "llvm/ADT/SmallVector.h"
167330f729Sjoerg #include "llvm/ADT/StringSwitch.h"
177330f729Sjoerg
187330f729Sjoerg using namespace clang;
197330f729Sjoerg
207330f729Sjoerg // Once LLVM switches to C++17, the constexpr variables can be inline and we
217330f729Sjoerg // won't need this.
227330f729Sjoerg #define SANITIZER(NAME, ID) constexpr SanitizerMask SanitizerKind::ID;
237330f729Sjoerg #define SANITIZER_GROUP(NAME, ID, ALIAS) \
247330f729Sjoerg constexpr SanitizerMask SanitizerKind::ID; \
257330f729Sjoerg constexpr SanitizerMask SanitizerKind::ID##Group;
267330f729Sjoerg #include "clang/Basic/Sanitizers.def"
277330f729Sjoerg
parseSanitizerValue(StringRef Value,bool AllowGroups)287330f729Sjoerg SanitizerMask clang::parseSanitizerValue(StringRef Value, bool AllowGroups) {
297330f729Sjoerg SanitizerMask ParsedKind = llvm::StringSwitch<SanitizerMask>(Value)
307330f729Sjoerg #define SANITIZER(NAME, ID) .Case(NAME, SanitizerKind::ID)
317330f729Sjoerg #define SANITIZER_GROUP(NAME, ID, ALIAS) \
327330f729Sjoerg .Case(NAME, AllowGroups ? SanitizerKind::ID##Group : SanitizerMask())
337330f729Sjoerg #include "clang/Basic/Sanitizers.def"
347330f729Sjoerg .Default(SanitizerMask());
357330f729Sjoerg return ParsedKind;
367330f729Sjoerg }
377330f729Sjoerg
serializeSanitizerSet(SanitizerSet Set,SmallVectorImpl<StringRef> & Values)38*e038c9c4Sjoerg void clang::serializeSanitizerSet(SanitizerSet Set,
39*e038c9c4Sjoerg SmallVectorImpl<StringRef> &Values) {
40*e038c9c4Sjoerg #define SANITIZER(NAME, ID) \
41*e038c9c4Sjoerg if (Set.has(SanitizerKind::ID)) \
42*e038c9c4Sjoerg Values.push_back(NAME);
43*e038c9c4Sjoerg #include "clang/Basic/Sanitizers.def"
44*e038c9c4Sjoerg }
45*e038c9c4Sjoerg
expandSanitizerGroups(SanitizerMask Kinds)467330f729Sjoerg SanitizerMask clang::expandSanitizerGroups(SanitizerMask Kinds) {
477330f729Sjoerg #define SANITIZER(NAME, ID)
487330f729Sjoerg #define SANITIZER_GROUP(NAME, ID, ALIAS) \
497330f729Sjoerg if (Kinds & SanitizerKind::ID##Group) \
507330f729Sjoerg Kinds |= SanitizerKind::ID;
517330f729Sjoerg #include "clang/Basic/Sanitizers.def"
527330f729Sjoerg return Kinds;
537330f729Sjoerg }
547330f729Sjoerg
hash_value() const557330f729Sjoerg llvm::hash_code SanitizerMask::hash_value() const {
567330f729Sjoerg return llvm::hash_combine_range(&maskLoToHigh[0], &maskLoToHigh[kNumElem]);
577330f729Sjoerg }
587330f729Sjoerg
597330f729Sjoerg namespace clang {
hash_value(const clang::SanitizerMask & Arg)607330f729Sjoerg llvm::hash_code hash_value(const clang::SanitizerMask &Arg) {
617330f729Sjoerg return Arg.hash_value();
627330f729Sjoerg }
63*e038c9c4Sjoerg
AsanDtorKindToString(llvm::AsanDtorKind kind)64*e038c9c4Sjoerg StringRef AsanDtorKindToString(llvm::AsanDtorKind kind) {
65*e038c9c4Sjoerg switch (kind) {
66*e038c9c4Sjoerg case llvm::AsanDtorKind::None:
67*e038c9c4Sjoerg return "none";
68*e038c9c4Sjoerg case llvm::AsanDtorKind::Global:
69*e038c9c4Sjoerg return "global";
70*e038c9c4Sjoerg case llvm::AsanDtorKind::Invalid:
71*e038c9c4Sjoerg return "invalid";
72*e038c9c4Sjoerg }
73*e038c9c4Sjoerg return "invalid";
74*e038c9c4Sjoerg }
75*e038c9c4Sjoerg
AsanDtorKindFromString(StringRef kindStr)76*e038c9c4Sjoerg llvm::AsanDtorKind AsanDtorKindFromString(StringRef kindStr) {
77*e038c9c4Sjoerg return llvm::StringSwitch<llvm::AsanDtorKind>(kindStr)
78*e038c9c4Sjoerg .Case("none", llvm::AsanDtorKind::None)
79*e038c9c4Sjoerg .Case("global", llvm::AsanDtorKind::Global)
80*e038c9c4Sjoerg .Default(llvm::AsanDtorKind::Invalid);
81*e038c9c4Sjoerg }
82*e038c9c4Sjoerg
837330f729Sjoerg } // namespace clang
84