LLVM  3.7.0
StringTableBuilder.cpp
Go to the documentation of this file.
1 //===-- StringTableBuilder.cpp - String table building utility ------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/Support/COFF.h"
13 #include "llvm/Support/Endian.h"
14 
15 using namespace llvm;
16 
17 static bool compareBySuffix(StringRef a, StringRef b) {
18  size_t sizeA = a.size();
19  size_t sizeB = b.size();
20  size_t len = std::min(sizeA, sizeB);
21  for (size_t i = 0; i < len; ++i) {
22  char ca = a[sizeA - i - 1];
23  char cb = b[sizeB - i - 1];
24  if (ca != cb)
25  return ca > cb;
26  }
27  return sizeA > sizeB;
28 }
29 
32  Strings.reserve(StringIndexMap.size());
33 
34  for (auto i = StringIndexMap.begin(), e = StringIndexMap.end(); i != e; ++i)
35  Strings.push_back(i->getKey());
36 
37  std::sort(Strings.begin(), Strings.end(), compareBySuffix);
38 
39  switch (kind) {
40  case ELF:
41  case MachO:
42  // Start the table with a NUL byte.
43  StringTable += '\x00';
44  break;
45  case WinCOFF:
46  // Make room to write the table size later.
47  StringTable.append(4, '\x00');
48  break;
49  }
50 
51  StringRef Previous;
52  for (StringRef s : Strings) {
53  if (kind == WinCOFF)
54  assert(s.size() > COFF::NameSize && "Short string in COFF string table!");
55 
56  if (Previous.endswith(s)) {
57  StringIndexMap[s] = StringTable.size() - 1 - s.size();
58  continue;
59  }
60 
61  StringIndexMap[s] = StringTable.size();
62  StringTable += s;
63  StringTable += '\x00';
64  Previous = s;
65  }
66 
67  switch (kind) {
68  case ELF:
69  break;
70  case MachO:
71  // Pad to multiple of 4.
72  while (StringTable.size() % 4)
73  StringTable += '\x00';
74  break;
75  case WinCOFF:
76  // Write the table size in the first word.
77  assert(StringTable.size() <= std::numeric_limits<uint32_t>::max());
78  uint32_t size = static_cast<uint32_t>(StringTable.size());
79  support::endian::write<uint32_t, support::little, support::unaligned>(
80  StringTable.data(), size);
81  break;
82  }
83 }
84 
86  StringTable.clear();
87  StringIndexMap.clear();
88 }
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:224
void reserve(size_type N)
Definition: SmallVector.h:401
void finalize(Kind kind)
Analyze the strings and build the final table.
Number of individual test Apply this number of consecutive mutations to each input exit after the first new interesting input is found the minimized corpus is saved into the first input directory Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:74
unsigned size() const
Definition: StringMap.h:99
static bool compareBySuffix(StringRef a, StringRef b)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:134
iterator begin()
Definition: StringMap.h:252
void size_t size
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
iterator end()
Definition: StringMap.h:255