xref: /netbsd-src/external/apache2/llvm/dist/llvm/include/llvm/DebugInfo/GSYM/StringTable.h (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 //===- StringTable.h --------------------------------------------*- 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_DEBUGINFO_GSYM_STRINGTABLE_H
10 #define LLVM_DEBUGINFO_GSYM_STRINGTABLE_H
11 
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/DebugInfo/GSYM/Range.h"
15 #include <stdint.h>
16 
17 namespace llvm {
18 namespace gsym {
19 
20 /// String tables in GSYM files are required to start with an empty
21 /// string at offset zero. Strings must be UTF8 NULL terminated strings.
22 struct StringTable {
23   StringRef Data;
StringTableStringTable24   StringTable() : Data() {}
StringTableStringTable25   StringTable(StringRef D) : Data(D) {}
26   StringRef operator[](size_t Offset) const { return getString(Offset); }
getStringStringTable27   StringRef getString(uint32_t Offset) const {
28     if (Offset < Data.size()) {
29       auto End = Data.find('\0', Offset);
30       return Data.substr(Offset, End - Offset);
31     }
32     return StringRef();
33   }
clearStringTable34   void clear() { Data = StringRef(); }
35 };
36 
37 inline raw_ostream &operator<<(raw_ostream &OS, const StringTable &S) {
38   OS << "String table:\n";
39   uint32_t Offset = 0;
40   const size_t Size = S.Data.size();
41   while (Offset < Size) {
42     StringRef Str = S.getString(Offset);
43     OS << HEX32(Offset) << ": \"" << Str << "\"\n";
44     Offset += Str.size() + 1;
45   }
46   return OS;
47 }
48 
49 } // namespace gsym
50 } // namespace llvm
51 #endif // LLVM_DEBUGINFO_GSYM_STRINGTABLE_H
52