LLVM  15.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"
15 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Support/Error.h"
20 
21 using namespace llvm;
22 using namespace llvm::remarks;
23 
24 namespace llvm {
25 class raw_ostream;
26 }
27 
30  if (Obj.isMachO())
31  return StringRef("__remarks");
32  // ELF -> .remarks, but there is no ELF support at this point.
33  return createStringError(std::errc::illegal_byte_sequence,
34  "Unsupported file format.");
35 }
36 
40  if (!SectionName)
41  return SectionName.takeError();
42 
43  for (const object::SectionRef &Section : Obj.sections()) {
44  Expected<StringRef> MaybeName = Section.getName();
45  if (!MaybeName)
46  return MaybeName.takeError();
47  if (*MaybeName != *SectionName)
48  continue;
49 
50  if (Expected<StringRef> Contents = Section.getContents())
51  return *Contents;
52  else
53  return Contents.takeError();
54  }
55  return Optional<StringRef>{};
56 }
57 
58 Remark &RemarkLinker::keep(std::unique_ptr<Remark> Remark) {
59  StrTab.internalize(*Remark);
60  auto Inserted = Remarks.insert(std::move(Remark));
61  return **Inserted.first;
62 }
63 
65  PrependPath = std::string(PrependPathIn);
66 }
67 
68 // Discard remarks with no source location.
69 static bool shouldKeepRemark(const Remark &R) { return R.Loc.hasValue(); }
70 
72  if (!RemarkFormat) {
73  Expected<Format> ParserFormat = magicToFormat(Buffer);
74  if (!ParserFormat)
75  return ParserFormat.takeError();
76  RemarkFormat = *ParserFormat;
77  }
78 
81  *RemarkFormat, Buffer, /*StrTab=*/None,
82  PrependPath ? Optional<StringRef>(StringRef(*PrependPath))
84  if (!MaybeParser)
85  return MaybeParser.takeError();
86 
87  RemarkParser &Parser = **MaybeParser;
88 
89  while (true) {
90  Expected<std::unique_ptr<Remark>> Next = Parser.next();
91  if (Error E = Next.takeError()) {
92  if (E.isA<EndOfFileError>()) {
94  break;
95  }
96  return E;
97  }
98 
99  assert(*Next != nullptr);
100 
101  if (shouldKeepRemark(**Next))
102  keep(std::move(*Next));
103  }
104  return Error::success();
105 }
106 
108  Optional<Format> RemarkFormat) {
110  if (!SectionOrErr)
111  return SectionOrErr.takeError();
112 
113  if (Optional<StringRef> Section = *SectionOrErr)
114  return link(*Section, RemarkFormat);
115  return Error::success();
116 }
117 
121  std::move(const_cast<StringTable &>(StrTab)));
122  if (!MaybeSerializer)
123  return MaybeSerializer.takeError();
124 
125  std::unique_ptr<remarks::RemarkSerializer> Serializer =
126  std::move(*MaybeSerializer);
127 
128  for (const Remark &R : remarks())
129  Serializer->emit(R);
130  return Error::success();
131 }
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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
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:64
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:118
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
Error.h
llvm::Optional
Definition: APInt.h:33
llvm::remarks::RemarkLinker::remarks
iterator_range< iterator > remarks() const
Definition: RemarkLinker.h:89
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
SymbolicFile.h
RemarkLinker.h
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:240
llvm::ARMBuildAttrs::Section
@ Section
Legacy Tags.
Definition: ARMBuildAttributes.h:82
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:38
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
shouldKeepRemark
static bool shouldKeepRemark(const Remark &R)
Definition: RemarkLinker.cpp:69
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:54
llvm::remarks::Remark
A remark type used for both emission and parsing.
Definition: Remark.h:67
DebugLocVerifyLevel::None
@ None
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:83
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:76
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
Remarks
annotation Annotation Remarks
Definition: AnnotationRemarks.cpp:115
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ObjectFile.h
getRemarksSectionName
static Expected< StringRef > getRemarksSectionName(const object::ObjectFile &Obj)
Definition: RemarkLinker.cpp:29
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:1239
llvm::SectionName
Definition: DWARFSection.h:21
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
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:597
llvm::object::ObjectFile::sections
section_iterator_range sections() const
Definition: ObjectFile.h:327
llvm::remarks::createRemarkParserFromMeta
Expected< std::unique_ptr< RemarkParser > > createRemarkParserFromMeta(Format ParserFormat, StringRef Buf, Optional< ParsedStringTable > StrTab=None, Optional< StringRef > ExternalFilePrependPath=None)
Definition: RemarkParser.cpp:88
llvm::remarks::RemarkLinker::link
Error link(StringRef Buffer, Optional< Format > RemarkFormat=None)
Link the remarks found in Buffer.
Definition: RemarkLinker.cpp:71
RemarkSerializer.h
llvm::remarks::Format
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25