[OpenACC] Enable 'attach' clause for combined constructs
[llvm-project.git] / compiler-rt / lib / fuzzer / FuzzerDictionary.h
blob64eb35c57a5610419629d3a4b7234c787aa03ff0
1 //===- FuzzerDictionary.h - Internal header for the Fuzzer ------*- 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 // fuzzer::Dictionary
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_FUZZER_DICTIONARY_H
12 #define LLVM_FUZZER_DICTIONARY_H
14 #include "FuzzerDefs.h"
15 #include "FuzzerIO.h"
16 #include "FuzzerUtil.h"
17 #include <algorithm>
18 #include <limits>
20 namespace fuzzer {
21 // A simple POD sized array of bytes.
22 template <size_t kMaxSizeT> class FixedWord {
23 public:
24 static const size_t kMaxSize = kMaxSizeT;
25 FixedWord() {}
26 FixedWord(const uint8_t *B, size_t S) { Set(B, S); }
28 void Set(const uint8_t *B, size_t S) {
29 static_assert(kMaxSizeT <= std::numeric_limits<uint8_t>::max(),
30 "FixedWord::kMaxSizeT cannot fit in a uint8_t.");
31 assert(S <= kMaxSize);
32 // memcpy cannot take null pointer arguments even if Size is 0.
33 if (S)
34 memcpy(Data, B, S);
35 Size = static_cast<uint8_t>(S);
38 bool operator==(const FixedWord<kMaxSize> &w) const {
39 return Size == w.Size && 0 == memcmp(Data, w.Data, Size);
42 static size_t GetMaxSize() { return kMaxSize; }
43 const uint8_t *data() const { return Data; }
44 uint8_t size() const { return Size; }
46 private:
47 uint8_t Size = 0;
48 uint8_t Data[kMaxSize];
51 typedef FixedWord<64> Word;
53 class DictionaryEntry {
54 public:
55 DictionaryEntry() {}
56 DictionaryEntry(Word W) : W(W) {}
57 DictionaryEntry(Word W, size_t PositionHint)
58 : W(W), PositionHint(PositionHint) {}
59 const Word &GetW() const { return W; }
61 bool HasPositionHint() const {
62 return PositionHint != std::numeric_limits<size_t>::max();
64 size_t GetPositionHint() const {
65 assert(HasPositionHint());
66 return PositionHint;
68 void IncUseCount() { UseCount++; }
69 void IncSuccessCount() { SuccessCount++; }
70 size_t GetUseCount() const { return UseCount; }
71 size_t GetSuccessCount() const {return SuccessCount; }
73 void Print(const char *PrintAfter = "\n") {
74 PrintASCII(W.data(), W.size());
75 if (HasPositionHint())
76 Printf("@%zd", GetPositionHint());
77 Printf("%s", PrintAfter);
80 private:
81 Word W;
82 size_t PositionHint = std::numeric_limits<size_t>::max();
83 size_t UseCount = 0;
84 size_t SuccessCount = 0;
87 class Dictionary {
88 public:
89 static const size_t kMaxDictSize = 1 << 14;
91 bool ContainsWord(const Word &W) const {
92 return std::any_of(begin(), end(), [&](const DictionaryEntry &DE) {
93 return DE.GetW() == W;
94 });
96 const DictionaryEntry *begin() const { return &DE[0]; }
97 const DictionaryEntry *end() const { return begin() + Size; }
98 DictionaryEntry & operator[] (size_t Idx) {
99 assert(Idx < Size);
100 return DE[Idx];
102 void push_back(DictionaryEntry DE) {
103 if (Size < kMaxDictSize)
104 this->DE[Size++] = DE;
106 void clear() { Size = 0; }
107 bool empty() const { return Size == 0; }
108 size_t size() const { return Size; }
110 private:
111 DictionaryEntry DE[kMaxDictSize];
112 size_t Size = 0;
115 // Parses one dictionary entry.
116 // If successful, writes the entry to Unit and returns true,
117 // otherwise returns false.
118 bool ParseOneDictionaryEntry(const std::string &Str, Unit *U);
119 // Parses the dictionary file, fills Units, returns true iff all lines
120 // were parsed successfully.
121 bool ParseDictionaryFile(const std::string &Text, std::vector<Unit> *Units);
123 } // namespace fuzzer
125 #endif // LLVM_FUZZER_DICTIONARY_H