Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TClingRdictModuleFileExtension.cxx
Go to the documentation of this file.
1/// \file TClingRdictModuleFileExtension.cxx
2///
3/// \brief The file contains facilities to work with C++ module files extensions
4/// used to store rdict files.
5///
6/// \author Vassil Vassilev <vvasilev@cern.ch>
7///
8/// \date May, 2019
9///
10/*************************************************************************
11 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers. *
12 * All rights reserved. *
13 * *
14 * For the licensing terms see $ROOTSYS/LICENSE. *
15 * For the list of contributors see $ROOTSYS/README/CREDITS. *
16 *************************************************************************/
17
19
20#include "TClingUtils.h"
21
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
28#include "llvm/ADT/Hashing.h"
29#include "llvm/Bitstream/BitstreamWriter.h"
30#include "llvm/Support/FileSystem.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/Path.h"
33#include "llvm/Support/raw_ostream.h"
34
35#include <fstream>
36#include <sstream>
37
38/// Rdict module extension block name.
39const std::string ROOT_CLING_RDICT_BLOCK_NAME = "root.cling.rdict";
40
41/// Rdict module extension major version number.
42constexpr uint16_t ROOT_CLING_RDICT_VERSION_MAJOR = 1;
43
44/// Rdict module extension minor version number.
45///
46/// When the format changes IN ANY WAY, this number should be incremented.
47constexpr uint16_t ROOT_CLING_RDICT_VERSION_MINOR = 1;
48
49
51
52void TClingRdictModuleFileExtension::Writer::writeExtensionContents(clang::Sema &SemaRef, llvm::BitstreamWriter &Stream)
53{
54
55 const clang::LangOptions &Opts = SemaRef.getLangOpts();
56 const clang::Preprocessor &PP = SemaRef.getPreprocessor();
57
58 llvm::StringRef CachePath = PP.getHeaderSearchInfo().getHeaderSearchOpts().ModuleCachePath;
59 std::string RdictsStart = "lib" + Opts.CurrentModule + "_";
60 const std::string RdictsEnd = "_rdict.pcm";
61
62 using namespace llvm;
63 using namespace clang;
64 using namespace clang::serialization;
65 // Write an abbreviation for this record.
66 auto Abv = std::make_shared<BitCodeAbbrev>();
67 Abv->Add(BitCodeAbbrevOp(FIRST_EXTENSION_RECORD_ID));
68 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
69 auto Abbrev = Stream.EmitAbbrev(std::move(Abv));
70 auto Abv1 = std::make_shared<BitCodeAbbrev>();
71 Abv1->Add(BitCodeAbbrevOp(FIRST_EXTENSION_RECORD_ID + 1));
72 Abv1->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
73 auto Abbrev1 = Stream.EmitAbbrev(std::move(Abv1));
74
75 // Write a dict files into the extension block.
76 std::error_code EC;
77 for (llvm::sys::fs::directory_iterator DirIt(CachePath, EC), DirEnd; DirIt != DirEnd && !EC; DirIt.increment(EC)) {
78 StringRef FilePath(DirIt->path());
79 if (llvm::sys::fs::is_directory(FilePath))
80 continue;
81 StringRef FileName = llvm::sys::path::filename(FilePath);
82 if (FileName.startswith(RdictsStart) && FileName.endswith(RdictsEnd)) {
83
84 uint64_t Record[] = {FIRST_EXTENSION_RECORD_ID};
85 Stream.EmitRecordWithBlob(Abbrev, Record, FileName);
86
87 uint64_t Record1[] = {FIRST_EXTENSION_RECORD_ID + 1};
88 std::ifstream fp(FilePath.str(), std::ios::binary);
89 std::ostringstream os;
90 os << fp.rdbuf();
91 Stream.EmitRecordWithBlob(Abbrev1, Record1, StringRef(os.str()));
92 fp.close();
93
94 EC = llvm::sys::fs::remove(FilePath);
95 assert(!EC && "Unable to close _rdict file");
96 }
97 }
98}
99
100extern "C" void TCling__RegisterRdictForLoadPCM(const std::string &pcmFileNameFullPath, llvm::StringRef *pcmContent);
101
102TClingRdictModuleFileExtension::Reader::Reader(clang::ModuleFileExtension *Ext, clang::ASTReader &Reader,
103 clang::serialization::ModuleFile &Mod,
104 const llvm::BitstreamCursor &InStream)
105 : ModuleFileExtensionReader(Ext), Stream(InStream)
106{
107 // Read the extension block.
108 llvm::SmallVector<uint64_t, 4> Record;
109 llvm::StringRef CurrentRdictName;
110 while (true) {
111 llvm::BitstreamEntry Entry = llvm::cantFail(Stream.advanceSkippingSubblocks());
112 switch (Entry.Kind) {
113 case llvm::BitstreamEntry::SubBlock:
114 case llvm::BitstreamEntry::EndBlock:
115 case llvm::BitstreamEntry::Error: return;
116
117 case llvm::BitstreamEntry::Record: break;
118 }
119
120 Record.clear();
121 llvm::StringRef Blob;
122 unsigned RecCode = llvm::cantFail(Stream.readRecord(Entry.ID, Record, &Blob));
123 using namespace clang::serialization;
124 switch (RecCode) {
125 case FIRST_EXTENSION_RECORD_ID: {
126 CurrentRdictName = Blob;
127 break;
128 }
129 case FIRST_EXTENSION_RECORD_ID + 1: {
130 // FIXME: Remove the string copy in fPendingRdicts.
131 std::string ResolvedFileName
132 = ROOT::TMetaUtils::GetRealPath(Mod.FileName);
133 llvm::StringRef ModDir = llvm::sys::path::parent_path(ResolvedFileName);
134 llvm::SmallString<255> FullRdictName = ModDir;
135 llvm::sys::path::append(FullRdictName, CurrentRdictName);
136 TCling__RegisterRdictForLoadPCM(FullRdictName.str().str(), &Blob);
137 break;
138 }
139 }
140 }
141}
142
144
146
147clang::ModuleFileExtensionMetadata TClingRdictModuleFileExtension::getExtensionMetadata() const
148{
149 const std::string UserInfo = "";
151}
152
153void TClingRdictModuleFileExtension::hashExtension(ExtensionHashBuilder &HBuilder) const
154{
155 HBuilder.add(ROOT_CLING_RDICT_BLOCK_NAME);
156 HBuilder.add(ROOT_CLING_RDICT_VERSION_MAJOR);
157 HBuilder.add(ROOT_CLING_RDICT_VERSION_MINOR);
158}
159
160std::unique_ptr<clang::ModuleFileExtensionWriter>
162{
163 return std::unique_ptr<clang::ModuleFileExtensionWriter>(new Writer(this));
164}
165
166std::unique_ptr<clang::ModuleFileExtensionReader>
167TClingRdictModuleFileExtension::createExtensionReader(const clang::ModuleFileExtensionMetadata &Metadata,
168 clang::ASTReader &Reader, clang::serialization::ModuleFile &Mod,
169 const llvm::BitstreamCursor &Stream)
170{
171 return std::unique_ptr<clang::ModuleFileExtensionReader>(
172 new TClingRdictModuleFileExtension::Reader(this, Reader, Mod, Stream));
173}
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)
Definition TCling.cxx:588
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
void hashExtension(ExtensionHashBuilder &HBuilder) const override
std::string GetRealPath(const std::string &path)