LLVM  10.0.0svn
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 }
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:69
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:48
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:270
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:31
LLVM_NODISCARD StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Definition: StringRef.h:803
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
bool is_at_eof() const
Return true if we&#39;ve reached EOF or are an "end" iterator.
Definition: LineIterator.h:49
int64_t line_number() const
Return the current line number. May return any number at EOF.
Definition: LineIterator.h:55
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static void LLVM_ATTRIBUTE_NORETURN ReportError(uint64_t StartOffset, const char *ErrorMsg)
Error read(MemoryBuffer &B)
Read remappings from the given buffer, which must live as long as the remapper.
size_t size() const
Definition: SmallVector.h:52
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
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:710
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
#define Success
EquivalenceError addEquivalence(FragmentKind Kind, StringRef First, StringRef Second)
Add an equivalence between First and Second.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48