LLVM  10.0.0
RemarkLinker.cpp
Go to the documentation of this file.
1 //===- RemarkLinker.cpp ---------------------------------------------------===//
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 provides an implementation of the remark linker.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Error.h"
19 
20 using namespace llvm;
21 using namespace llvm::remarks;
22 
25  if (Obj.isMachO())
26  return StringRef("__remarks");
27  // ELF -> .remarks, but there is no ELF support at this point.
28  return createStringError(std::errc::illegal_byte_sequence,
29  "Unsupported file format.");
30 }
31 
35  if (!SectionName)
36  return SectionName.takeError();
37 
38  for (const object::SectionRef &Section : Obj.sections()) {
39  Expected<StringRef> MaybeName = Section.getName();
40  if (!MaybeName)
41  return MaybeName.takeError();
42  if (*MaybeName != *SectionName)
43  continue;
44 
45  if (Expected<StringRef> Contents = Section.getContents())
46  return *Contents;
47  else
48  return Contents.takeError();
49  }
50  return Optional<StringRef>{};
51 }
52 
53 Remark &RemarkLinker::keep(std::unique_ptr<Remark> Remark) {
54  StrTab.internalize(*Remark);
55  auto Inserted = Remarks.insert(std::move(Remark));
56  return **Inserted.first;
57 }
58 
60  PrependPath = PrependPathIn;
61 }
62 
63 // Discard remarks with no source location.
64 static bool shouldKeepRemark(const Remark &R) { return R.Loc.hasValue(); }
65 
67  if (!RemarkFormat) {
68  Expected<Format> ParserFormat = magicToFormat(Buffer);
69  if (!ParserFormat)
70  return ParserFormat.takeError();
71  RemarkFormat = *ParserFormat;
72  }
73 
76  *RemarkFormat, Buffer, /*StrTab=*/None,
77  PrependPath ? Optional<StringRef>(StringRef(*PrependPath))
79  if (!MaybeParser)
80  return MaybeParser.takeError();
81 
82  RemarkParser &Parser = **MaybeParser;
83 
84  while (true) {
85  Expected<std::unique_ptr<Remark>> Next = Parser.next();
86  if (Error E = Next.takeError()) {
87  if (E.isA<EndOfFileError>()) {
88  consumeError(std::move(E));
89  break;
90  }
91  return E;
92  }
93 
94  assert(*Next != nullptr);
95 
96  if (shouldKeepRemark(**Next))
97  keep(std::move(*Next));
98  }
99  return Error::success();
100 }
101 
103  Optional<Format> RemarkFormat) {
105  if (!SectionOrErr)
106  return SectionOrErr.takeError();
107 
108  if (Optional<StringRef> Section = *SectionOrErr)
109  return link(*Section, RemarkFormat);
110  return Error::success();
111 }
112 
116  std::move(const_cast<StringTable &>(StrTab)));
117  if (!MaybeSerializer)
118  return MaybeSerializer.takeError();
119 
120  std::unique_ptr<remarks::RemarkSerializer> Serializer =
121  std::move(*MaybeSerializer);
122 
123  for (const Remark &R : remarks())
124  Serializer->emit(R);
125  return Error::success();
126 }
const NoneType None
Definition: None.h:23
void setExternalFilePrependPath(StringRef PrependPath)
Set a path to prepend to the external file path.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This class is the base class for all object file types.
Definition: ObjectFile.h:223
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25
A remark type used for both emission and parsing.
Definition: Remark.h:67
void internalize(Remark &R)
Modify R to use strings from this string table.
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
static bool shouldKeepRemark(const Remark &R)
section_iterator_range sections() const
Definition: ObjectFile.h:312
Expected< Format > magicToFormat(StringRef Magic)
Parse and validate a magic number to a remark format.
Optional< RemarkLocation > Loc
The location in the source file of the remark.
Definition: Remark.h:83
Expected< Optional< StringRef > > getRemarksSectionContents(const object::ObjectFile &Obj)
Returns a buffer with the contents of the remarks section depending on the format of the file...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error serialize(raw_ostream &OS, Format RemarksFormat) const
Serialize the linked remarks to the stream OS, using the format RemarkFormat.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1001
Error link(StringRef Buffer, Optional< Format > RemarkFormat=None)
Link the remarks found in Buffer.
virtual Expected< std::unique_ptr< Remark > > next()=0
If no error occurs, this returns a valid Remark object.
iterator_range< iterator > remarks() const
Definition: RemarkLinker.h:86
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
static Expected< StringRef > getRemarksSectionName(const object::ObjectFile &Obj)
Expected< std::unique_ptr< RemarkParser > > createRemarkParserFromMeta(Format ParserFormat, StringRef Buf, Optional< ParsedStringTable > StrTab=None, Optional< StringRef > ExternalFilePrependPath=None)
Parser used to parse a raw buffer to remarks::Remark objects.
Definition: RemarkParser.h:39
Expected< std::unique_ptr< RemarkSerializer > > createRemarkSerializer(Format RemarksFormat, SerializerMode Mode, raw_ostream &OS)
Create a remark serializer.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
cl::opt< std::string > RemarksFormat("lto-pass-remarks-format", cl::desc("The format used for serializing remarks (default: YAML)"), cl::value_desc("format"), cl::init("yaml"))
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
bool isMachO() const
Definition: Binary.h:122
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1197
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81