RemarkStringTable.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- RemarkStringTable.h - Serializing string table ----------*- C++/-*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This class is used to deduplicate and serialize a string table used for
  15. // generating remarks.
  16. //
  17. // For parsing a string table, use ParsedStringTable in RemarkParser.h
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_REMARKS_REMARKSTRINGTABLE_H
  21. #define LLVM_REMARKS_REMARKSTRINGTABLE_H
  22. #include "llvm/ADT/StringMap.h"
  23. #include "llvm/Support/Allocator.h"
  24. #include <vector>
  25. namespace llvm {
  26. class raw_ostream;
  27. class StringRef;
  28. namespace remarks {
  29. struct ParsedStringTable;
  30. struct Remark;
  31. /// The string table used for serializing remarks.
  32. /// This table can be for example serialized in a section to be consumed after
  33. /// the compilation.
  34. struct StringTable {
  35. /// The string table containing all the unique strings used in the output.
  36. /// It maps a string to an unique ID.
  37. StringMap<unsigned, BumpPtrAllocator> StrTab;
  38. /// Total size of the string table when serialized.
  39. size_t SerializedSize = 0;
  40. StringTable() = default;
  41. /// Disable copy.
  42. StringTable(const StringTable &) = delete;
  43. StringTable &operator=(const StringTable &) = delete;
  44. /// Should be movable.
  45. StringTable(StringTable &&) = default;
  46. StringTable &operator=(StringTable &&) = default;
  47. /// Construct a string table from a ParsedStringTable.
  48. StringTable(const ParsedStringTable &Other);
  49. /// Add a string to the table. It returns an unique ID of the string.
  50. std::pair<unsigned, StringRef> add(StringRef Str);
  51. /// Modify \p R to use strings from this string table. If the string table
  52. /// does not contain the strings, it adds them.
  53. void internalize(Remark &R);
  54. /// Serialize the string table to a stream. It is serialized as a little
  55. /// endian uint64 (the size of the table in bytes) followed by a sequence of
  56. /// NULL-terminated strings, where the N-th string is the string with the ID N
  57. /// in the StrTab map.
  58. void serialize(raw_ostream &OS) const;
  59. /// Serialize the string table to a vector. This allows users to do the actual
  60. /// writing to file/memory/other.
  61. /// The string with the ID == N should be the N-th element in the vector.
  62. std::vector<StringRef> serialize() const;
  63. };
  64. } // end namespace remarks
  65. } // end namespace llvm
  66. #endif // LLVM_REMARKS_REMARKSTRINGTABLE_H
  67. #ifdef __GNUC__
  68. #pragma GCC diagnostic pop
  69. #endif