LLVM 20.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
16#include "llvm/ADT/Twine.h"
19
20using namespace llvm;
21
23
24/// Load a set of name remappings from a text file.
25///
26/// See the documentation at the top of the file for an explanation of
27/// the expected format.
29 line_iterator LineIt(B, /*SkipBlanks=*/true, '#');
30
31 auto ReportError = [&](Twine Msg) {
32 return llvm::make_error<SymbolRemappingParseError>(
33 B.getBufferIdentifier(), LineIt.line_number(), Msg);
34 };
35
36 for (; !LineIt.is_at_eof(); ++LineIt) {
37 StringRef Line = *LineIt;
38 Line = Line.ltrim(' ');
39 // line_iterator only detects comments starting in column 1.
40 if (Line.starts_with("#") || Line.empty())
41 continue;
42
44 Line.split(Parts, ' ', /*MaxSplits*/-1, /*KeepEmpty*/false);
45
46 if (Parts.size() != 3)
47 return ReportError("Expected 'kind mangled_name mangled_name', "
48 "found '" + Line + "'");
49
51 std::optional<FK> FragmentKind = StringSwitch<std::optional<FK>>(Parts[0])
52 .Case("name", FK::Name)
53 .Case("type", FK::Type)
54 .Case("encoding", FK::Encoding)
55 .Default(std::nullopt);
56 if (!FragmentKind)
57 return ReportError("Invalid kind, expected 'name', 'type', or 'encoding',"
58 " found '" + Parts[0] + "'");
59
61 switch (Canonicalizer.addEquivalence(*FragmentKind, Parts[1], Parts[2])) {
62 case EE::Success:
63 break;
64
65 case EE::ManglingAlreadyUsed:
66 return ReportError("Manglings '" + Parts[1] + "' and '" + Parts[2] + "' "
67 "have both been used in prior remappings. Move this "
68 "remapping earlier in the file.");
69
70 case EE::InvalidFirstMangling:
71 return ReportError("Could not demangle '" + Parts[1] + "' "
72 "as a <" + Parts[0] + ">; invalid mangling?");
73
74 case EE::InvalidSecondMangling:
75 return ReportError("Could not demangle '" + Parts[2] + "' "
76 "as a <" + Parts[0] + ">; invalid mangling?");
77 }
78 }
79
80 return Error::success();
81}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
EquivalenceError addEquivalence(FragmentKind Kind, StringRef First, StringRef Second)
Add an equivalence between First and Second.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
size_t size() const
Definition: SmallVector.h:91
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
Error read(MemoryBuffer &B)
Read remappings from the given buffer, which must live as long as the remapper.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:33
int64_t line_number() const
Return the current line number. May return any number at EOF.
Definition: LineIterator.h:66
bool is_at_eof() const
Return true if we've reached EOF or are an "end" iterator.
Definition: LineIterator.h:60
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18