LLVM  13.0.0git
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 = std::string(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>()) {
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 }
llvm::remarks::StringTable::internalize
void internalize(Remark &R)
Modify R to use strings from this string table.
Definition: RemarkStringTable.cpp:41
llvm::remarks::createRemarkSerializer
Expected< std::unique_ptr< RemarkSerializer > > createRemarkSerializer(Format RemarksFormat, SerializerMode Mode, raw_ostream &OS)
Create a remark serializer.
Definition: RemarkSerializer.cpp:21
llvm
Definition: AllocatorList.h:23
llvm::remarks::magicToFormat
Expected< Format > magicToFormat(StringRef Magic)
Parse and validate a magic number to a remark format.
Definition: RemarkFormat.cpp:35
llvm::remarks::StringTable
The string table used for serializing remarks.
Definition: RemarkStringTable.h:36
llvm::remarks::SerializerMode::Standalone
@ Standalone
StringRef.h
llvm::remarks::RemarkLinker::setExternalFilePrependPath
void setExternalFilePrependPath(StringRef PrependPath)
Set a path to prepend to the external file path.
Definition: RemarkLinker.cpp:59
RemarkParser.h
llvm::remarks::RemarkLinker::serialize
Error serialize(raw_ostream &OS, Format RemarksFormat) const
Serialize the linked remarks to the stream OS, using the format RemarkFormat.
Definition: RemarkLinker.cpp:113
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
Error.h
llvm::Optional
Definition: APInt.h:33
llvm::remarks::RemarkLinker::remarks
iterator_range< iterator > remarks() const
Definition: RemarkLinker.h:85
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
RemarkLinker.h
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1006
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:78
llvm::remarks::getRemarksSectionContents
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.
Definition: RemarkLinker.cpp:33
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
shouldKeepRemark
static bool shouldKeepRemark(const Remark &R)
Definition: RemarkLinker.cpp:64
llvm::remarks::RemarkParser::next
virtual Expected< std::unique_ptr< Remark > > next()=0
If no error occurs, this returns a valid Remark object.
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::remarks::Remark
A remark type used for both emission and parsing.
Definition: Remark.h:67
llvm::None
const NoneType None
Definition: None.h:23
llvm::RemarksFormat
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"))
Definition: ThinLTOCodeGenerator.cpp:81
llvm::object::SectionRef
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:80
llvm::remarks
Definition: AsmPrinter.h:77
llvm::remarks::EndOfFileError
Definition: RemarkParser.h:26
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
BitstreamRemarkContainer.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
getRemarksSectionName
static Expected< StringRef > getRemarksSectionName(const object::ObjectFile &Obj)
Definition: RemarkLinker.cpp:24
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm::remarks::RemarkParser
Parser used to parse a raw buffer to remarks::Remark objects.
Definition: RemarkParser.h:39
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1202
llvm::SectionName
Definition: DWARFSection.h:21
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::object::Binary::isMachO
bool isMachO() const
Definition: Binary.h:124
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:557
llvm::object::ObjectFile::sections
section_iterator_range sections() const
Definition: ObjectFile.h:322
llvm::remarks::createRemarkParserFromMeta
Expected< std::unique_ptr< RemarkParser > > createRemarkParserFromMeta(Format ParserFormat, StringRef Buf, Optional< ParsedStringTable > StrTab=None, Optional< StringRef > ExternalFilePrependPath=None)
Definition: RemarkParser.cpp:89
llvm::remarks::RemarkLinker::link
Error link(StringRef Buffer, Optional< Format > RemarkFormat=None)
Link the remarks found in Buffer.
Definition: RemarkLinker.cpp:66
RemarkSerializer.h
llvm::remarks::Format
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25