clang  14.0.0git
TestModuleFileExtension.cpp
Go to the documentation of this file.
1 //===-- TestModuleFileExtension.cpp - Module Extension Tester -------------===//
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 //===----------------------------------------------------------------------===//
11 #include "llvm/ADT/Hashing.h"
12 #include "llvm/Bitstream/BitstreamWriter.h"
13 #include "llvm/Support/raw_ostream.h"
14 #include <cstdio>
15 using namespace clang;
16 using namespace clang::serialization;
17 
19 
20 TestModuleFileExtension::Writer::~Writer() { }
21 
22 void TestModuleFileExtension::Writer::writeExtensionContents(
23  Sema &SemaRef,
24  llvm::BitstreamWriter &Stream) {
25  using namespace llvm;
26 
27  // Write an abbreviation for this record.
28  auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
29  Abv->Add(BitCodeAbbrevOp(FIRST_EXTENSION_RECORD_ID));
30  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of characters
31  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // message
32  auto Abbrev = Stream.EmitAbbrev(std::move(Abv));
33 
34  // Write a message into the extension block.
36  {
37  auto Ext = static_cast<TestModuleFileExtension *>(getExtension());
38  raw_svector_ostream OS(Message);
39  OS << "Hello from " << Ext->BlockName << " v" << Ext->MajorVersion << "."
40  << Ext->MinorVersion;
41  }
42  uint64_t Record[] = {FIRST_EXTENSION_RECORD_ID, Message.size()};
43  Stream.EmitRecordWithBlob(Abbrev, Record, Message);
44 }
45 
46 TestModuleFileExtension::Reader::Reader(ModuleFileExtension *Ext,
47  const llvm::BitstreamCursor &InStream)
48  : ModuleFileExtensionReader(Ext), Stream(InStream)
49 {
50  // Read the extension block.
52  while (true) {
54  Stream.advanceSkippingSubblocks();
55  if (!MaybeEntry)
56  (void)MaybeEntry.takeError();
57  llvm::BitstreamEntry Entry = MaybeEntry.get();
58 
59  switch (Entry.Kind) {
60  case llvm::BitstreamEntry::SubBlock:
61  case llvm::BitstreamEntry::EndBlock:
63  return;
64 
65  case llvm::BitstreamEntry::Record:
66  break;
67  }
68 
69  Record.clear();
70  StringRef Blob;
71  Expected<unsigned> MaybeRecCode =
72  Stream.readRecord(Entry.ID, Record, &Blob);
73  if (!MaybeRecCode)
74  fprintf(stderr, "Failed reading rec code: %s\n",
75  toString(MaybeRecCode.takeError()).c_str());
76  switch (MaybeRecCode.get()) {
78  StringRef Message = Blob.substr(0, Record[0]);
79  fprintf(stderr, "Read extension block message: %s\n",
80  Message.str().c_str());
81  break;
82  }
83  }
84  }
85 }
86 
87 TestModuleFileExtension::Reader::~Reader() { }
88 
90 
93  return { BlockName, MajorVersion, MinorVersion, UserInfo };
94 }
95 
97  ExtensionHashBuilder &HBuilder) const {
98  if (Hashed) {
99  HBuilder.add(BlockName);
100  HBuilder.add(MajorVersion);
101  HBuilder.add(MinorVersion);
102  HBuilder.add(UserInfo);
103  }
104 }
105 
106 std::unique_ptr<ModuleFileExtensionWriter>
108  return std::unique_ptr<ModuleFileExtensionWriter>(new Writer(this));
109 }
110 
111 std::unique_ptr<ModuleFileExtensionReader>
113  const ModuleFileExtensionMetadata &Metadata,
114  ASTReader &Reader, serialization::ModuleFile &Mod,
115  const llvm::BitstreamCursor &Stream)
116 {
117  assert(Metadata.BlockName == BlockName && "Wrong block name");
118  if (std::make_pair(Metadata.MajorVersion, Metadata.MinorVersion) !=
119  std::make_pair(MajorVersion, MinorVersion)) {
120  Reader.getDiags().Report(Mod.ImportLoc,
121  diag::err_test_module_file_extension_version)
122  << BlockName << Metadata.MajorVersion << Metadata.MinorVersion
123  << MajorVersion << MinorVersion;
124  return nullptr;
125  }
126 
127  return std::unique_ptr<ModuleFileExtensionReader>(
128  new TestModuleFileExtension::Reader(this, Stream));
129 }
130 
132  std::string Buffer;
133  llvm::raw_string_ostream OS(Buffer);
134  OS << BlockName << ":" << MajorVersion << ":" << MinorVersion << ":" << Hashed
135  << ":" << UserInfo;
136  return OS.str();
137 }
clang::TestModuleFileExtension::~TestModuleFileExtension
~TestModuleFileExtension() override
Definition: TestModuleFileExtension.cpp:89
llvm
Definition: Dominators.h:30
clang::TestModuleFileExtension::createExtensionReader
std::unique_ptr< ModuleFileExtensionReader > createExtensionReader(const ModuleFileExtensionMetadata &Metadata, ASTReader &Reader, serialization::ModuleFile &Mod, const llvm::BitstreamCursor &Stream) override
Definition: TestModuleFileExtension.cpp:112
TestModuleFileExtension.h
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:20
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::TestModuleFileExtension::str
std::string str() const
Definition: TestModuleFileExtension.cpp:131
clang::TestModuleFileExtension::hashExtension
void hashExtension(ExtensionHashBuilder &HBuilder) const override
Definition: TestModuleFileExtension.cpp:96
llvm::SmallVector< uint64_t, 4 >
clang::ModuleFileExtension
An abstract superclass that describes a custom extension to the module/precompiled header file format...
Definition: ModuleFileExtension.h:66
clang::serialization::FIRST_EXTENSION_RECORD_ID
@ FIRST_EXTENSION_RECORD_ID
The first record ID allocated to the extensions themselves.
Definition: ASTBitCodes.h:416
llvm::Expected
Definition: LLVM.h:41
clang::serialization
Definition: AbstractBasicReader.h:15
clang::TestModuleFileExtension::ID
static char ID
Definition: TestModuleFileExtension.h:47
clang::threadSafety::sx::toString
std::string toString(const til::SExpr *E)
Definition: ThreadSafetyCommon.h:89
llvm::SmallString
Definition: LLVM.h:37
clang::ModuleFileExtensionMetadata::MajorVersion
unsigned MajorVersion
The major version of the extension data.
Definition: ModuleFileExtension.h:44
clang::ASTReader::getDiags
DiagnosticsEngine & getDiags() const
Definition: ASTReader.h:1526
clang::ASTWriter
Writes an AST file containing the contents of a translation unit.
Definition: ASTWriter.h:99
clang::TestModuleFileExtension
A module file extension used for testing purposes.
Definition: TestModuleFileExtension.h:20
clang::syntax::NodeRole::Message
@ Message
clang::Sema
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:354
clang::ModuleFileExtensionMetadata::MinorVersion
unsigned MinorVersion
The minor version of the extension data.
Definition: ModuleFileExtension.h:47
clang::ASTReader
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:342
clang
Definition: CalledOnceCheck.h:17
clang::ModuleFileExtensionMetadata::BlockName
std::string BlockName
The name used to identify this particular extension block within the resulting module file.
Definition: ModuleFileExtension.h:41
FrontendDiagnostic.h
clang::serialization::ModuleFile::ImportLoc
SourceLocation ImportLoc
The source location where this module was first imported.
Definition: ModuleFile.h:218
ASTReader.h
clang::ModuleFileExtensionMetadata
Metadata for a module file extension.
Definition: ModuleFileExtension.h:36
clang::TestModuleFileExtension::createExtensionWriter
std::unique_ptr< ModuleFileExtensionWriter > createExtensionWriter(ASTWriter &Writer) override
Definition: TestModuleFileExtension.cpp:107
clang::ModuleFileExtensionReader
Abstract base class that reads a module file extension block from a module file.
Definition: ModuleFileExtension.h:140
clang::TestModuleFileExtension::getExtensionMetadata
ModuleFileExtensionMetadata getExtensionMetadata() const override
Definition: TestModuleFileExtension.cpp:92
clang::serialization::ModuleFile
Information about a module that has been loaded by the ASTReader.
Definition: ModuleFile.h:111
clang::ento::ObjKind::OS
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
clang::DiagnosticsEngine::Report
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1523