22#include "clang/Frontend/FrontendDiagnostic.h"
23#include "clang/Lex/HeaderSearchOptions.h"
24#include "clang/Lex/Preprocessor.h"
25#include "clang/Sema/Sema.h"
26#include "clang/Serialization/ASTReader.h"
27#include "clang/Serialization/Module.h"
29#include "llvm/ADT/Hashing.h"
30#include "llvm/Bitstream/BitstreamWriter.h"
31#include "llvm/Support/FileSystem.h"
32#include "llvm/Support/MemoryBuffer.h"
33#include "llvm/Support/Path.h"
34#include "llvm/Support/raw_ostream.h"
56 const clang::LangOptions &Opts = SemaRef.getLangOpts();
57 const clang::Preprocessor &PP = SemaRef.getPreprocessor();
59 llvm::StringRef CachePath = PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleCachePath;
60 std::string RdictsStart =
"lib" + Opts.CurrentModule +
"_";
61 const std::string RdictsEnd =
"_rdict.pcm";
64 using namespace clang;
65 using namespace clang::serialization;
67 auto Abv = std::make_shared<BitCodeAbbrev>();
68 Abv->Add(BitCodeAbbrevOp(FIRST_EXTENSION_RECORD_ID));
69 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
70 auto Abbrev = Stream.EmitAbbrev(std::move(Abv));
71 auto Abv1 = std::make_shared<BitCodeAbbrev>();
72 Abv1->Add(BitCodeAbbrevOp(FIRST_EXTENSION_RECORD_ID + 1));
73 Abv1->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
74 auto Abbrev1 = Stream.EmitAbbrev(std::move(Abv1));
78 for (llvm::sys::fs::directory_iterator DirIt(CachePath, EC), DirEnd; DirIt != DirEnd && !EC; DirIt.increment(EC)) {
79 StringRef FilePath(DirIt->path());
80 if (llvm::sys::fs::is_directory(FilePath))
82 StringRef FileName = llvm::sys::path::filename(FilePath);
83 if (FileName.startswith(RdictsStart) && FileName.endswith(RdictsEnd)) {
85 uint64_t Record[] = {FIRST_EXTENSION_RECORD_ID};
86 Stream.EmitRecordWithBlob(Abbrev, Record, FileName);
88 uint64_t Record1[] = {FIRST_EXTENSION_RECORD_ID + 1};
89 std::ifstream fp(FilePath, std::ios::binary);
90 std::ostringstream os;
92 Stream.EmitRecordWithBlob(Abbrev1, Record1, StringRef(os.str()));
95 EC = llvm::sys::fs::remove(FilePath);
96 assert(!EC &&
"Unable to close _rdict file");
104 clang::serialization::ModuleFile &Mod,
105 const llvm::BitstreamCursor &InStream)
106 : ModuleFileExtensionReader(Ext), Stream(InStream)
109 llvm::SmallVector<uint64_t, 4> Record;
110 llvm::StringRef CurrentRdictName;
112 llvm::BitstreamEntry Entry = llvm::cantFail(
Stream.advanceSkippingSubblocks());
113 switch (Entry.Kind) {
114 case llvm::BitstreamEntry::SubBlock:
115 case llvm::BitstreamEntry::EndBlock:
116 case llvm::BitstreamEntry::Error:
return;
118 case llvm::BitstreamEntry::Record:
break;
122 llvm::StringRef Blob;
123 unsigned RecCode = llvm::cantFail(
Stream.readRecord(Entry.ID, Record, &Blob));
124 using namespace clang::serialization;
126 case FIRST_EXTENSION_RECORD_ID: {
127 CurrentRdictName = Blob;
130 case FIRST_EXTENSION_RECORD_ID + 1: {
132 std::string ResolvedFileName
134 llvm::StringRef ModDir = llvm::sys::path::parent_path(ResolvedFileName);
135 llvm::SmallString<255> FullRdictName = ModDir;
136 llvm::sys::path::append(FullRdictName, CurrentRdictName);
150 const std::string UserInfo =
"";
163std::unique_ptr<clang::ModuleFileExtensionWriter>
166 return std::unique_ptr<clang::ModuleFileExtensionWriter>(
new Writer(
this));
169std::unique_ptr<clang::ModuleFileExtensionReader>
171 clang::ASTReader &
Reader, clang::serialization::ModuleFile &Mod,
172 const llvm::BitstreamCursor &Stream)
174 return std::unique_ptr<clang::ModuleFileExtensionReader>(
constexpr uint16_t ROOT_CLING_RDICT_VERSION_MINOR
Rdict module extension minor version number.
const std::string ROOT_CLING_RDICT_BLOCK_NAME
Rdict module extension block name.
constexpr uint16_t ROOT_CLING_RDICT_VERSION_MAJOR
Rdict module extension major version number.
The file contains facilities to work with C++ module files extensions used to store rdict files.
void TCling__RegisterRdictForLoadPCM(const std::string &pcmFileNameFullPath, llvm::StringRef *pcmContent)
llvm::BitstreamCursor Stream
Reader(clang::ModuleFileExtension *Ext, clang::ASTReader &Reader, clang::serialization::ModuleFile &Mod, const llvm::BitstreamCursor &InStream)
void writeExtensionContents(clang::Sema &SemaRef, llvm::BitstreamWriter &Stream) override
std::unique_ptr< clang::ModuleFileExtensionReader > createExtensionReader(const clang::ModuleFileExtensionMetadata &Metadata, clang::ASTReader &Reader, clang::serialization::ModuleFile &Mod, const llvm::BitstreamCursor &Stream) override
clang::ModuleFileExtensionMetadata getExtensionMetadata() const override
std::unique_ptr< clang::ModuleFileExtensionWriter > createExtensionWriter(clang::ASTWriter &Writer) override
llvm::hash_code hashExtension(llvm::hash_code Code) const override
~TClingRdictModuleFileExtension() override