llvm-cxxmap.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //===- llvm-cxxmap.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. // llvm-cxxmap computes a correspondence between old symbol names and new
  10. // symbol names based on a symbol equivalence file.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/ADT/DenseMap.h"
  14. #include "llvm/ADT/DenseSet.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Support/CommandLine.h"
  17. #include "llvm/Support/FileSystem.h"
  18. #include "llvm/Support/InitLLVM.h"
  19. #include "llvm/Support/LineIterator.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "llvm/Support/SymbolRemappingReader.h"
  22. #include "llvm/Support/WithColor.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. using namespace llvm;
  25. cl::OptionCategory CXXMapCategory("CXX Map Options");
  26. cl::opt<std::string> OldSymbolFile(cl::Positional, cl::Required,
  27. cl::desc("<symbol-file>"),
  28. cl::cat(CXXMapCategory));
  29. cl::opt<std::string> NewSymbolFile(cl::Positional, cl::Required,
  30. cl::desc("<symbol-file>"),
  31. cl::cat(CXXMapCategory));
  32. cl::opt<std::string> RemappingFile("remapping-file", cl::Required,
  33. cl::desc("Remapping file"),
  34. cl::cat(CXXMapCategory));
  35. cl::alias RemappingFileA("r", cl::aliasopt(RemappingFile),
  36. cl::cat(CXXMapCategory));
  37. cl::opt<std::string> OutputFilename("output", cl::value_desc("output"),
  38. cl::init("-"), cl::desc("Output file"),
  39. cl::cat(CXXMapCategory));
  40. cl::alias OutputFilenameA("o", cl::aliasopt(OutputFilename),
  41. cl::cat(CXXMapCategory));
  42. cl::opt<bool> WarnAmbiguous(
  43. "Wambiguous",
  44. cl::desc("Warn on equivalent symbols in the output symbol list"),
  45. cl::cat(CXXMapCategory));
  46. cl::opt<bool> WarnIncomplete(
  47. "Wincomplete",
  48. cl::desc("Warn on input symbols missing from output symbol list"),
  49. cl::cat(CXXMapCategory));
  50. static void warn(Twine Message, Twine Whence = "",
  51. std::string Hint = "") {
  52. WithColor::warning();
  53. std::string WhenceStr = Whence.str();
  54. if (!WhenceStr.empty())
  55. errs() << WhenceStr << ": ";
  56. errs() << Message << "\n";
  57. if (!Hint.empty())
  58. WithColor::note() << Hint << "\n";
  59. }
  60. static void exitWithError(Twine Message, Twine Whence = "",
  61. std::string Hint = "") {
  62. WithColor::error();
  63. std::string WhenceStr = Whence.str();
  64. if (!WhenceStr.empty())
  65. errs() << WhenceStr << ": ";
  66. errs() << Message << "\n";
  67. if (!Hint.empty())
  68. WithColor::note() << Hint << "\n";
  69. ::exit(1);
  70. }
  71. static void exitWithError(Error E, StringRef Whence = "") {
  72. exitWithError(toString(std::move(E)), Whence);
  73. }
  74. static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
  75. exitWithError(EC.message(), Whence);
  76. }
  77. static void remapSymbols(MemoryBuffer &OldSymbolFile,
  78. MemoryBuffer &NewSymbolFile,
  79. MemoryBuffer &RemappingFile,
  80. raw_ostream &Out) {
  81. // Load the remapping file and prepare to canonicalize symbols.
  82. SymbolRemappingReader Reader;
  83. if (Error E = Reader.read(RemappingFile))
  84. exitWithError(std::move(E));
  85. // Canonicalize the new symbols.
  86. DenseMap<SymbolRemappingReader::Key, StringRef> MappedNames;
  87. DenseSet<StringRef> UnparseableSymbols;
  88. for (line_iterator LineIt(NewSymbolFile, /*SkipBlanks=*/true, '#');
  89. !LineIt.is_at_eof(); ++LineIt) {
  90. StringRef Symbol = *LineIt;
  91. auto K = Reader.insert(Symbol);
  92. if (!K) {
  93. UnparseableSymbols.insert(Symbol);
  94. continue;
  95. }
  96. auto ItAndIsNew = MappedNames.insert({K, Symbol});
  97. if (WarnAmbiguous && !ItAndIsNew.second &&
  98. ItAndIsNew.first->second != Symbol) {
  99. warn("symbol " + Symbol + " is equivalent to earlier symbol " +
  100. ItAndIsNew.first->second,
  101. NewSymbolFile.getBufferIdentifier() + ":" +
  102. Twine(LineIt.line_number()),
  103. "later symbol will not be the target of any remappings");
  104. }
  105. }
  106. // Figure out which new symbol each old symbol is equivalent to.
  107. for (line_iterator LineIt(OldSymbolFile, /*SkipBlanks=*/true, '#');
  108. !LineIt.is_at_eof(); ++LineIt) {
  109. StringRef Symbol = *LineIt;
  110. auto K = Reader.lookup(Symbol);
  111. StringRef NewSymbol = MappedNames.lookup(K);
  112. if (NewSymbol.empty()) {
  113. if (WarnIncomplete && !UnparseableSymbols.count(Symbol)) {
  114. warn("no new symbol matches old symbol " + Symbol,
  115. OldSymbolFile.getBufferIdentifier() + ":" +
  116. Twine(LineIt.line_number()));
  117. }
  118. continue;
  119. }
  120. Out << Symbol << " " << NewSymbol << "\n";
  121. }
  122. }
  123. int main(int argc, const char *argv[]) {
  124. InitLLVM X(argc, argv);
  125. cl::HideUnrelatedOptions({&CXXMapCategory, &getColorCategory()});
  126. cl::ParseCommandLineOptions(argc, argv, "LLVM C++ mangled name remapper\n");
  127. auto OldSymbolBufOrError = MemoryBuffer::getFileOrSTDIN(OldSymbolFile);
  128. if (!OldSymbolBufOrError)
  129. exitWithErrorCode(OldSymbolBufOrError.getError(), OldSymbolFile);
  130. auto NewSymbolBufOrError = MemoryBuffer::getFileOrSTDIN(NewSymbolFile);
  131. if (!NewSymbolBufOrError)
  132. exitWithErrorCode(NewSymbolBufOrError.getError(), NewSymbolFile);
  133. auto RemappingBufOrError = MemoryBuffer::getFileOrSTDIN(RemappingFile);
  134. if (!RemappingBufOrError)
  135. exitWithErrorCode(RemappingBufOrError.getError(), RemappingFile);
  136. std::error_code EC;
  137. raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_TextWithCRLF);
  138. if (EC)
  139. exitWithErrorCode(EC, OutputFilename);
  140. remapSymbols(*OldSymbolBufOrError.get(), *NewSymbolBufOrError.get(),
  141. *RemappingBufOrError.get(), OS);
  142. }