2015-11-04 02:33:07 +08:00
|
|
|
//===-- TestModuleFileExtension.cpp - Module Extension Tester -------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-11-04 02:33:07 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "TestModuleFileExtension.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
|
|
|
#include "clang/Serialization/ASTReader.h"
|
|
|
|
#include "llvm/ADT/Hashing.h"
|
2019-07-04 06:40:07 +08:00
|
|
|
#include "llvm/Bitstream/BitstreamWriter.h"
|
2015-11-04 02:33:07 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-11-05 09:34:19 +08:00
|
|
|
#include <cstdio>
|
2015-11-04 02:33:07 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::serialization;
|
|
|
|
|
2021-03-02 01:52:15 +08:00
|
|
|
char TestModuleFileExtension::ID = 0;
|
|
|
|
|
2015-11-04 02:33:07 +08:00
|
|
|
TestModuleFileExtension::Writer::~Writer() { }
|
|
|
|
|
|
|
|
void TestModuleFileExtension::Writer::writeExtensionContents(
|
2015-12-09 06:43:32 +08:00
|
|
|
Sema &SemaRef,
|
2015-11-04 02:33:07 +08:00
|
|
|
llvm::BitstreamWriter &Stream) {
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
// Write an abbreviation for this record.
|
2017-01-05 06:36:43 +08:00
|
|
|
auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
|
2015-11-04 02:33:07 +08:00
|
|
|
Abv->Add(BitCodeAbbrevOp(FIRST_EXTENSION_RECORD_ID));
|
|
|
|
Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of characters
|
|
|
|
Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // message
|
2017-01-05 06:36:43 +08:00
|
|
|
auto Abbrev = Stream.EmitAbbrev(std::move(Abv));
|
2015-11-04 02:33:07 +08:00
|
|
|
|
|
|
|
// Write a message into the extension block.
|
|
|
|
SmallString<64> Message;
|
|
|
|
{
|
|
|
|
auto Ext = static_cast<TestModuleFileExtension *>(getExtension());
|
|
|
|
raw_svector_ostream OS(Message);
|
|
|
|
OS << "Hello from " << Ext->BlockName << " v" << Ext->MajorVersion << "."
|
|
|
|
<< Ext->MinorVersion;
|
|
|
|
}
|
2016-07-02 19:41:41 +08:00
|
|
|
uint64_t Record[] = {FIRST_EXTENSION_RECORD_ID, Message.size()};
|
2015-11-04 02:33:07 +08:00
|
|
|
Stream.EmitRecordWithBlob(Abbrev, Record, Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
TestModuleFileExtension::Reader::Reader(ModuleFileExtension *Ext,
|
|
|
|
const llvm::BitstreamCursor &InStream)
|
|
|
|
: ModuleFileExtensionReader(Ext), Stream(InStream)
|
|
|
|
{
|
|
|
|
// Read the extension block.
|
|
|
|
SmallVector<uint64_t, 4> Record;
|
|
|
|
while (true) {
|
2019-06-27 03:50:12 +08:00
|
|
|
llvm::Expected<llvm::BitstreamEntry> MaybeEntry =
|
|
|
|
Stream.advanceSkippingSubblocks();
|
|
|
|
if (!MaybeEntry)
|
|
|
|
(void)MaybeEntry.takeError();
|
|
|
|
llvm::BitstreamEntry Entry = MaybeEntry.get();
|
|
|
|
|
2015-11-04 02:33:07 +08:00
|
|
|
switch (Entry.Kind) {
|
|
|
|
case llvm::BitstreamEntry::SubBlock:
|
|
|
|
case llvm::BitstreamEntry::EndBlock:
|
|
|
|
case llvm::BitstreamEntry::Error:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case llvm::BitstreamEntry::Record:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Record.clear();
|
|
|
|
StringRef Blob;
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<unsigned> MaybeRecCode =
|
|
|
|
Stream.readRecord(Entry.ID, Record, &Blob);
|
|
|
|
if (!MaybeRecCode)
|
|
|
|
fprintf(stderr, "Failed reading rec code: %s\n",
|
|
|
|
toString(MaybeRecCode.takeError()).c_str());
|
|
|
|
switch (MaybeRecCode.get()) {
|
2015-11-04 02:33:07 +08:00
|
|
|
case FIRST_EXTENSION_RECORD_ID: {
|
|
|
|
StringRef Message = Blob.substr(0, Record[0]);
|
|
|
|
fprintf(stderr, "Read extension block message: %s\n",
|
|
|
|
Message.str().c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TestModuleFileExtension::Reader::~Reader() { }
|
|
|
|
|
|
|
|
TestModuleFileExtension::~TestModuleFileExtension() { }
|
|
|
|
|
|
|
|
ModuleFileExtensionMetadata
|
|
|
|
TestModuleFileExtension::getExtensionMetadata() const {
|
|
|
|
return { BlockName, MajorVersion, MinorVersion, UserInfo };
|
|
|
|
}
|
|
|
|
|
2021-09-04 02:12:27 +08:00
|
|
|
void TestModuleFileExtension::hashExtension(
|
|
|
|
ExtensionHashBuilder &HBuilder) const {
|
2015-11-04 02:33:07 +08:00
|
|
|
if (Hashed) {
|
2021-09-04 02:12:27 +08:00
|
|
|
HBuilder.add(BlockName);
|
|
|
|
HBuilder.add(MajorVersion);
|
|
|
|
HBuilder.add(MinorVersion);
|
|
|
|
HBuilder.add(UserInfo);
|
2015-11-04 02:33:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<ModuleFileExtensionWriter>
|
|
|
|
TestModuleFileExtension::createExtensionWriter(ASTWriter &) {
|
|
|
|
return std::unique_ptr<ModuleFileExtensionWriter>(new Writer(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<ModuleFileExtensionReader>
|
|
|
|
TestModuleFileExtension::createExtensionReader(
|
|
|
|
const ModuleFileExtensionMetadata &Metadata,
|
|
|
|
ASTReader &Reader, serialization::ModuleFile &Mod,
|
|
|
|
const llvm::BitstreamCursor &Stream)
|
|
|
|
{
|
|
|
|
assert(Metadata.BlockName == BlockName && "Wrong block name");
|
|
|
|
if (std::make_pair(Metadata.MajorVersion, Metadata.MinorVersion) !=
|
|
|
|
std::make_pair(MajorVersion, MinorVersion)) {
|
|
|
|
Reader.getDiags().Report(Mod.ImportLoc,
|
|
|
|
diag::err_test_module_file_extension_version)
|
|
|
|
<< BlockName << Metadata.MajorVersion << Metadata.MinorVersion
|
|
|
|
<< MajorVersion << MinorVersion;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::unique_ptr<ModuleFileExtensionReader>(
|
|
|
|
new TestModuleFileExtension::Reader(this, Stream));
|
|
|
|
}
|
2021-02-09 23:30:14 +08:00
|
|
|
|
2021-03-02 20:14:00 +08:00
|
|
|
std::string TestModuleFileExtension::str() const {
|
|
|
|
std::string Buffer;
|
|
|
|
llvm::raw_string_ostream OS(Buffer);
|
|
|
|
OS << BlockName << ":" << MajorVersion << ":" << MinorVersion << ":" << Hashed
|
|
|
|
<< ":" << UserInfo;
|
|
|
|
return OS.str();
|
2021-02-09 23:30:14 +08:00
|
|
|
}
|