|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef FUZZTEST_FUZZTEST_INTERNAL_DOMAINS_ENUM_REFLECTION_H_ |
| 16 | +#define FUZZTEST_FUZZTEST_INTERNAL_DOMAINS_ENUM_REFLECTION_H_ |
| 17 | + |
| 18 | +#include <cstddef> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include "absl/strings/string_view.h" |
| 22 | +#include "absl/strings/strip.h" |
| 23 | +#include "./fuzztest/internal/meta.h" |
| 24 | + |
| 25 | +namespace fuzztest::internal::enum_reflection { |
| 26 | + |
| 27 | +constexpr bool IsValidCharacter(char c) { |
| 28 | + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || |
| 29 | + (c >= 'A' && c <= 'Z') || c == '_'; |
| 30 | +} |
| 31 | + |
| 32 | +constexpr bool IsDigit(char c) { return c >= '0' && c <= '9'; } |
| 33 | + |
| 34 | +// Checks if `name` ends with `compiler_suffix`. After removing it, checks if |
| 35 | +// the remaining string ends with a valid identifier (valid enum value) rather |
| 36 | +// than a numeric literal (invalid value). |
| 37 | +constexpr bool IsValidEnumValueSuffix(absl::string_view name, |
| 38 | + absl::string_view compiler_suffix) { |
| 39 | + if (!absl::ConsumeSuffix(&name, compiler_suffix)) return false; |
| 40 | + |
| 41 | + size_t i = name.size(); |
| 42 | + while (i > 0 && IsValidCharacter(name[i - 1])) { |
| 43 | + --i; |
| 44 | + } |
| 45 | + return i < name.size() && !IsDigit(name[i]); |
| 46 | +} |
| 47 | + |
| 48 | +// When the template parameter V is equal to a valid enum value, |
| 49 | +// compilers replace the signature macro with a string containing the enum name. |
| 50 | +// |
| 51 | +// For Clang/GCC, __PRETTY_FUNCTION__ ends in something like: |
| 52 | +// "[E = ns::MyEnum, V = ns::MyEnum::kRed]" |
| 53 | +// If V is not a valid value, the suffix looks like: |
| 54 | +// "[E = ns::MyEnum, V = (ns::MyEnum)5]". |
| 55 | +// |
| 56 | +// For MSVC, __FUNCSIG__ ends in something like: |
| 57 | +// "IsValidEnumValue<enum ns::MyEnum,ns::MyEnum::kRed>(void)" |
| 58 | +// If V is not a valid value, it looks like: |
| 59 | +// "IsValidEnumValue<enum ns::MyEnum,5>(void)" |
| 60 | +template <typename E, E V> |
| 61 | +constexpr bool IsValidEnumValue() { |
| 62 | +#if defined(__clang__) || defined(__GNUC__) |
| 63 | + constexpr absl::string_view kCompilerSuffix = "]"; |
| 64 | + return IsValidEnumValueSuffix( |
| 65 | + {__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 1}, kCompilerSuffix); |
| 66 | +#elif defined(_MSC_VER) |
| 67 | + constexpr absl::string_view kCompilerSuffix = ">(void)"; |
| 68 | + return IsValidEnumValueSuffix({__FUNCSIG__, sizeof(__FUNCSIG__) - 1}, |
| 69 | + kCompilerSuffix); |
| 70 | +#else |
| 71 | +#error "Enum reflection is only supported on Clang, GCC, and MSVC" |
| 72 | +#endif |
| 73 | +} |
| 74 | + |
| 75 | +template <typename E> |
| 76 | +constexpr bool HasEnumValuesInRange() { |
| 77 | + return ApplyIndex<256>([](auto... I) { |
| 78 | + return (IsValidEnumValue<E, static_cast<E>(static_cast<int>(I) - 128)>() || |
| 79 | + ...); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +// Currently only available with Clang, GCC and MSVC. |
| 84 | +// Assumes that the enums values are within the range [-128, 127]. |
| 85 | +template <typename E> |
| 86 | +std::vector<E> GetEnumValues() { |
| 87 | + return ApplyIndex<256>([](auto... I) { |
| 88 | + std::vector<E> values; |
| 89 | + auto add_if_valid = [&](auto index) { |
| 90 | + if constexpr (IsValidEnumValue<E, static_cast<E>(static_cast<int>(index) - |
| 91 | + 128)>()) { |
| 92 | + values.push_back(static_cast<E>(static_cast<int>(index) - 128)); |
| 93 | + } |
| 94 | + }; |
| 95 | + (add_if_valid(I), ...); |
| 96 | + return values; |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace fuzztest::internal::enum_reflection |
| 101 | + |
| 102 | +#endif // FUZZTEST_FUZZTEST_INTERNAL_DOMAINS_ENUM_REFLECTION_H_ |
0 commit comments