blob: 264c890ce8f12eb6d6151668840b59511e4b9a49 [file] [log] [blame]
Richard Smith327f0552018-09-13 18:51:44 +00001//===- SymbolRemappingReader.cpp - Read symbol remapping file -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains definitions needed for reading and applying symbol
11// remapping files.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Support/SymbolRemappingReader.h"
16#include "llvm/ADT/StringSwitch.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/Support/LineIterator.h"
19
20using namespace llvm;
21
22char SymbolRemappingParseError::ID;
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.
28Error SymbolRemappingReader::read(MemoryBuffer &B) {
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.startswith("#") || Line.empty())
41 continue;
42
43 SmallVector<StringRef, 4> Parts;
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
50 using FK = ItaniumManglingCanonicalizer::FragmentKind;
51 Optional<FK> FragmentKind = StringSwitch<Optional<FK>>(Parts[0])
52 .Case("name", FK::Name)
53 .Case("type", FK::Type)
54 .Case("encoding", FK::Encoding)
55 .Default(None);
56 if (!FragmentKind)
57 return ReportError("Invalid kind, expected 'name', 'type', or 'encoding',"
58 " found '" + Parts[0] + "'");
59
60 using EE = ItaniumManglingCanonicalizer::EquivalenceError;
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}