SymbolRemappingReader.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //===- SymbolRemappingReader.cpp - Read symbol remapping file -------------===//
  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. // This file contains definitions needed for reading and applying symbol
  10. // remapping files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Support/SymbolRemappingReader.h"
  14. #include "llvm/ADT/StringSwitch.h"
  15. #include "llvm/ADT/Twine.h"
  16. #include "llvm/Support/LineIterator.h"
  17. #include "llvm/Support/MemoryBuffer.h"
  18. using namespace llvm;
  19. char SymbolRemappingParseError::ID;
  20. /// Load a set of name remappings from a text file.
  21. ///
  22. /// See the documentation at the top of the file for an explanation of
  23. /// the expected format.
  24. Error SymbolRemappingReader::read(MemoryBuffer &B) {
  25. line_iterator LineIt(B, /*SkipBlanks=*/true, '#');
  26. auto ReportError = [&](Twine Msg) {
  27. return llvm::make_error<SymbolRemappingParseError>(
  28. B.getBufferIdentifier(), LineIt.line_number(), Msg);
  29. };
  30. for (; !LineIt.is_at_eof(); ++LineIt) {
  31. StringRef Line = *LineIt;
  32. Line = Line.ltrim(' ');
  33. // line_iterator only detects comments starting in column 1.
  34. if (Line.startswith("#") || Line.empty())
  35. continue;
  36. SmallVector<StringRef, 4> Parts;
  37. Line.split(Parts, ' ', /*MaxSplits*/-1, /*KeepEmpty*/false);
  38. if (Parts.size() != 3)
  39. return ReportError("Expected 'kind mangled_name mangled_name', "
  40. "found '" + Line + "'");
  41. using FK = ItaniumManglingCanonicalizer::FragmentKind;
  42. Optional<FK> FragmentKind = StringSwitch<Optional<FK>>(Parts[0])
  43. .Case("name", FK::Name)
  44. .Case("type", FK::Type)
  45. .Case("encoding", FK::Encoding)
  46. .Default(None);
  47. if (!FragmentKind)
  48. return ReportError("Invalid kind, expected 'name', 'type', or 'encoding',"
  49. " found '" + Parts[0] + "'");
  50. using EE = ItaniumManglingCanonicalizer::EquivalenceError;
  51. switch (Canonicalizer.addEquivalence(*FragmentKind, Parts[1], Parts[2])) {
  52. case EE::Success:
  53. break;
  54. case EE::ManglingAlreadyUsed:
  55. return ReportError("Manglings '" + Parts[1] + "' and '" + Parts[2] + "' "
  56. "have both been used in prior remappings. Move this "
  57. "remapping earlier in the file.");
  58. case EE::InvalidFirstMangling:
  59. return ReportError("Could not demangle '" + Parts[1] + "' "
  60. "as a <" + Parts[0] + ">; invalid mangling?");
  61. case EE::InvalidSecondMangling:
  62. return ReportError("Could not demangle '" + Parts[2] + "' "
  63. "as a <" + Parts[0] + ">; invalid mangling?");
  64. }
  65. }
  66. return Error::success();
  67. }