LLVM  14.0.0git
SymbolRemappingReader.cpp
Go to the documentation of this file.
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 
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Twine.h"
18 
19 using namespace llvm;
20 
22 
23 /// Load a set of name remappings from a text file.
24 ///
25 /// See the documentation at the top of the file for an explanation of
26 /// the expected format.
28  line_iterator LineIt(B, /*SkipBlanks=*/true, '#');
29 
30  auto ReportError = [&](Twine Msg) {
31  return llvm::make_error<SymbolRemappingParseError>(
32  B.getBufferIdentifier(), LineIt.line_number(), Msg);
33  };
34 
35  for (; !LineIt.is_at_eof(); ++LineIt) {
36  StringRef Line = *LineIt;
37  Line = Line.ltrim(' ');
38  // line_iterator only detects comments starting in column 1.
39  if (Line.startswith("#") || Line.empty())
40  continue;
41 
43  Line.split(Parts, ' ', /*MaxSplits*/-1, /*KeepEmpty*/false);
44 
45  if (Parts.size() != 3)
46  return ReportError("Expected 'kind mangled_name mangled_name', "
47  "found '" + Line + "'");
48 
50  Optional<FK> FragmentKind = StringSwitch<Optional<FK>>(Parts[0])
51  .Case("name", FK::Name)
52  .Case("type", FK::Type)
53  .Case("encoding", FK::Encoding)
54  .Default(None);
55  if (!FragmentKind)
56  return ReportError("Invalid kind, expected 'name', 'type', or 'encoding',"
57  " found '" + Parts[0] + "'");
58 
60  switch (Canonicalizer.addEquivalence(*FragmentKind, Parts[1], Parts[2])) {
61  case EE::Success:
62  break;
63 
64  case EE::ManglingAlreadyUsed:
65  return ReportError("Manglings '" + Parts[1] + "' and '" + Parts[2] + "' "
66  "have both been used in prior remappings. Move this "
67  "remapping earlier in the file.");
68 
69  case EE::InvalidFirstMangling:
70  return ReportError("Could not demangle '" + Parts[1] + "' "
71  "as a <" + Parts[0] + ">; invalid mangling?");
72 
73  case EE::InvalidSecondMangling:
74  return ReportError("Could not demangle '" + Parts[2] + "' "
75  "as a <" + Parts[0] + ">; invalid mangling?");
76  }
77  }
78 
79  return Error::success();
80 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:286
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::line_iterator
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::StringRef::ltrim
LLVM_NODISCARD StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Definition: StringRef.h:842
llvm::ItaniumManglingCanonicalizer::FragmentKind
FragmentKind
Definition: ItaniumManglingCanonicalizer.h:58
llvm::Optional
Definition: APInt.h:33
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:50
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:749
Twine.h
llvm::SymbolRemappingReader::read
Error read(MemoryBuffer &B)
Read remappings from the given buffer, which must live as long as the remapper.
Definition: SymbolRemappingReader.cpp:27
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LineIterator.h
llvm::None
const NoneType None
Definition: None.h:23
llvm::SymbolRemappingParseError::ID
static char ID
Definition: SymbolRemappingReader.h:85
SymbolRemappingReader.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Success
#define Success
Definition: AArch64Disassembler.cpp:260
StringSwitch.h
llvm::line_iterator::is_at_eof
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
Definition: LineIterator.h:60
llvm::ItaniumManglingCanonicalizer::EquivalenceError
EquivalenceError
Definition: ItaniumManglingCanonicalizer.h:43
llvm::line_iterator::line_number
int64_t line_number() const
Return the current line number. May return any number at EOF.
Definition: LineIterator.h:66
llvm::ItaniumManglingCanonicalizer::addEquivalence
EquivalenceError addEquivalence(FragmentKind Kind, StringRef First, StringRef Second)
Add an equivalence between First and Second.
Definition: ItaniumManglingCanonicalizer.cpp:220
llvm::msgpack::Type
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:49
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42