LLVM  14.0.0git
StringTableBuilder.h
Go to the documentation of this file.
1 //===- StringTableBuilder.h - String table building utility -----*- 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 
9 #ifndef LLVM_MC_STRINGTABLEBUILDER_H
10 #define LLVM_MC_STRINGTABLEBUILDER_H
11 
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/StringRef.h"
15 #include <cstddef>
16 #include <cstdint>
17 
18 namespace llvm {
19 
20 class raw_ostream;
21 
22 /// Utility for building string tables with deduplicated suffixes.
24 public:
25  enum Kind {
26  ELF,
32  RAW,
35  };
36 
37 private:
39  size_t Size = 0;
40  Kind K;
41  unsigned Alignment;
42  bool Finalized = false;
43 
44  void finalizeStringTable(bool Optimize);
45  void initSize();
46 
47 public:
48  StringTableBuilder(Kind K, unsigned Alignment = 1);
50 
51  /// Add a string to the builder. Returns the position of S in the
52  /// table. The position will be changed if finalize is used.
53  /// Can only be used before the table is finalized.
54  size_t add(CachedHashStringRef S);
55  size_t add(StringRef S) { return add(CachedHashStringRef(S)); }
56 
57  /// Analyze the strings and build the final table. No more strings can
58  /// be added after this point.
59  void finalize();
60 
61  /// Finalize the string table without reording it. In this mode, offsets
62  /// returned by add will still be valid.
63  void finalizeInOrder();
64 
65  /// Get the offest of a string in the string table. Can only be used
66  /// after the table is finalized.
67  size_t getOffset(CachedHashStringRef S) const;
68  size_t getOffset(StringRef S) const {
70  }
71 
72  /// Check if a string is contained in the string table. Since this class
73  /// doesn't store the string values, this function can be used to check if
74  /// storage needs to be done prior to adding the string.
75  bool contains(StringRef S) const {
77  }
79  return StringIndexMap.count(S);
80  }
81 
82  size_t getSize() const { return Size; }
83  void clear();
84 
85  void write(raw_ostream &OS) const;
86  void write(uint8_t *Buf) const;
87 
88 private:
89  bool isFinalized() const { return Finalized; }
90 };
91 
92 } // end namespace llvm
93 
94 #endif // LLVM_MC_STRINGTABLEBUILDER_H
llvm::StringTableBuilder::XCOFF
@ XCOFF
Definition: StringTableBuilder.h:34
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::StringTableBuilder::contains
bool contains(StringRef S) const
Check if a string is contained in the string table.
Definition: StringTableBuilder.h:75
StringRef.h
llvm::StringTableBuilder::finalizeInOrder
void finalizeInOrder()
Finalize the string table without reording it.
Definition: StringTableBuilder.cpp:133
DenseMap.h
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::count
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:145
llvm::StringTableBuilder::finalize
void finalize()
Analyze the strings and build the final table.
Definition: StringTableBuilder.cpp:128
llvm::StringTableBuilder::getOffset
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
Definition: StringTableBuilder.cpp:194
CachedHashString.h
llvm::StringTableBuilder::MachO64
@ MachO64
Definition: StringTableBuilder.h:29
llvm::StringTableBuilder::WinCOFF
@ WinCOFF
Definition: StringTableBuilder.h:27
llvm::StringTableBuilder::getSize
size_t getSize() const
Definition: StringTableBuilder.h:82
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::StringTableBuilder::MachO64Linked
@ MachO64Linked
Definition: StringTableBuilder.h:31
llvm::StringTableBuilder::RAW
@ RAW
Definition: StringTableBuilder.h:32
llvm::CachedHashStringRef
A container which contains a StringRef plus a precomputed hash.
Definition: CachedHashString.h:28
llvm::StringTableBuilder::clear
void clear()
Definition: StringTableBuilder.cpp:189
llvm::StringTableBuilder::MachO
@ MachO
Definition: StringTableBuilder.h:28
llvm::DenseMap
Definition: DenseMap.h:714
llvm::StringTableBuilder::~StringTableBuilder
~StringTableBuilder()
llvm::StringTableBuilder::contains
bool contains(CachedHashStringRef S) const
Definition: StringTableBuilder.h:78
llvm::StringTableBuilder::write
void write(raw_ostream &OS) const
Definition: StringTableBuilder.cpp:60
llvm::StringTableBuilder::MachOLinked
@ MachOLinked
Definition: StringTableBuilder.h:30
llvm::StringTableBuilder::add
size_t add(StringRef S)
Definition: StringTableBuilder.h:55
llvm::StringTableBuilder::DWARF
@ DWARF
Definition: StringTableBuilder.h:33
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::StringTableBuilder::getOffset
size_t getOffset(StringRef S) const
Definition: StringTableBuilder.h:68
llvm::StringTableBuilder::add
size_t add(CachedHashStringRef S)
Add a string to the builder.
Definition: StringTableBuilder.cpp:201
llvm::StringTableBuilder
Utility for building string tables with deduplicated suffixes.
Definition: StringTableBuilder.h:23
llvm::StringTableBuilder::ELF
@ ELF
Definition: StringTableBuilder.h:26
llvm::StringTableBuilder::Kind
Kind
Definition: StringTableBuilder.h:25
llvm::StringTableBuilder::StringTableBuilder
StringTableBuilder(Kind K, unsigned Alignment=1)
Definition: StringTableBuilder.cpp:55