RemarkStringTable.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //===- RemarkStringTable.cpp ----------------------------------------------===//
  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. // Implementation of the Remark string table used at remark generation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Remarks/RemarkStringTable.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/Remarks/Remark.h"
  15. #include "llvm/Remarks/RemarkParser.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <vector>
  18. using namespace llvm;
  19. using namespace llvm::remarks;
  20. StringTable::StringTable(const ParsedStringTable &Other) {
  21. for (unsigned i = 0, e = Other.size(); i < e; ++i)
  22. if (Expected<StringRef> MaybeStr = Other[i])
  23. add(*MaybeStr);
  24. else
  25. llvm_unreachable("Unexpected error while building remarks string table.");
  26. }
  27. std::pair<unsigned, StringRef> StringTable::add(StringRef Str) {
  28. size_t NextID = StrTab.size();
  29. auto KV = StrTab.insert({Str, NextID});
  30. // If it's a new string, add it to the final size.
  31. if (KV.second)
  32. SerializedSize += KV.first->first().size() + 1; // +1 for the '\0'
  33. // Can be either NextID or the previous ID if the string is already there.
  34. return {KV.first->second, KV.first->first()};
  35. }
  36. void StringTable::internalize(Remark &R) {
  37. auto Impl = [&](StringRef &S) { S = add(S).second; };
  38. Impl(R.PassName);
  39. Impl(R.RemarkName);
  40. Impl(R.FunctionName);
  41. if (R.Loc)
  42. Impl(R.Loc->SourceFilePath);
  43. for (Argument &Arg : R.Args) {
  44. Impl(Arg.Key);
  45. Impl(Arg.Val);
  46. if (Arg.Loc)
  47. Impl(Arg.Loc->SourceFilePath);
  48. }
  49. }
  50. void StringTable::serialize(raw_ostream &OS) const {
  51. // Emit the sequence of strings.
  52. for (StringRef Str : serialize()) {
  53. OS << Str;
  54. // Explicitly emit a '\0'.
  55. OS.write('\0');
  56. }
  57. }
  58. std::vector<StringRef> StringTable::serialize() const {
  59. std::vector<StringRef> Strings{StrTab.size()};
  60. for (const auto &KV : StrTab)
  61. Strings[KV.second] = KV.first();
  62. return Strings;
  63. }