10 #include "clang/Basic/FileManager.h"
11 #include "clang/Basic/TargetInfo.h"
12 #include "clang/Frontend/CompilerInstance.h"
13 #include "clang/Lex/PreprocessorOptions.h"
14 #include "clang/Serialization/ASTReader.h"
26 const SrcMgr::ContentCache &ContentCache,
29 if (!FilesToRecord.count(File))
33 if (!ContentCache.getRawBuffer())
36 InMemoryFs.addFile(File->getName(), 0,
37 llvm::MemoryBuffer::getMemBufferCopy(
38 ContentCache.getRawBuffer()->getBuffer()));
40 FilesToRecord.erase(File);
46 for (
auto FileEntry : FilesToRecord)
47 llvm::errs() <<
"Did not record contents for input file: "
48 << FileEntry->getName() <<
"\n";
53 llvm::DenseSet<const FileEntry *> FilesToRecord;
58 IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS)
61 Sources(Compiler.getSourceManager()),
63 Diags(new DiagnosticIDs, new DiagnosticOptions,
64 new ForwardingDiagnosticConsumer(Compiler.getDiagnosticClient())),
65 LangOpts(Compiler.getLangOpts()) {
68 OverlayFS->pushOverlay(InMemoryFs);
70 Diags.setSourceManager(&Sources);
72 LangOpts.Modules =
false;
74 auto HSO = std::make_shared<HeaderSearchOptions>();
75 *HSO = Compiler.getHeaderSearchOpts();
77 HeaderInfo = std::make_unique<HeaderSearch>(HSO, Sources, Diags, LangOpts,
78 &Compiler.getTarget());
80 auto PO = std::make_shared<PreprocessorOptions>();
81 *PO = Compiler.getPreprocessorOpts();
83 PP = std::make_unique<clang::Preprocessor>(PO, Diags, LangOpts, Sources,
84 *HeaderInfo, ModuleLoader,
87 PP->Initialize(Compiler.getTarget(), Compiler.getAuxTarget());
88 InitializePreprocessor(*PP, *PO, Compiler.getPCHContainerReader(),
89 Compiler.getFrontendOpts());
90 ApplyHeaderSearchOptions(*HeaderInfo, *HSO, LangOpts,
91 Compiler.getTarget().getTriple());
100 void ExpandModularHeadersPPCallbacks::handleModuleFile(
101 serialization::ModuleFile *MF) {
105 if (VisitedModules.count(MF))
107 VisitedModules.insert(MF);
111 Compiler.getASTReader()->visitInputFiles(
113 [
this](
const serialization::InputFile &IF,
bool ) {
114 Recorder->addNecessaryFile(IF.getFile());
117 for (
auto Import : MF->Imports)
118 handleModuleFile(Import);
121 void ExpandModularHeadersPPCallbacks::parseToLocation(SourceLocation
Loc) {
123 for (
unsigned I = 0, N = Sources.loaded_sloc_entry_size(); I != N; ++I) {
124 Sources.getLoadedSLocEntry(I,
nullptr);
127 for (
auto It = Sources.fileinfo_begin(); It != Sources.fileinfo_end(); ++It) {
128 Recorder->recordFileContent(It->getFirst(), *It->getSecond(), *InMemoryFs);
130 Recorder->checkAllFilesRecorded();
132 if (!StartedLexing) {
133 StartedLexing =
true;
134 PP->Lex(CurrentToken);
136 while (!CurrentToken.is(tok::eof) &&
137 Sources.isBeforeInTranslationUnit(CurrentToken.getLocation(),
Loc)) {
138 PP->Lex(CurrentToken);
142 void ExpandModularHeadersPPCallbacks::FileChanged(
143 SourceLocation
Loc, FileChangeReason Reason,
144 SrcMgr::CharacteristicKind FileType, FileID PrevFID = FileID()) {
145 if (!EnteredMainFile) {
146 EnteredMainFile =
true;
147 PP->EnterMainSourceFile();
151 void ExpandModularHeadersPPCallbacks::InclusionDirective(
152 SourceLocation DirectiveLoc,
const Token &IncludeToken,
153 StringRef IncludedFilename,
bool IsAngled, CharSourceRange FilenameRange,
154 const FileEntry *IncludedFile, StringRef SearchPath, StringRef RelativePath,
155 const Module *Imported, SrcMgr::CharacteristicKind FileType) {
157 serialization::ModuleFile *MF =
158 Compiler.getASTReader()->getModuleManager().lookup(
159 Imported->getASTFile());
160 handleModuleFile(MF);
162 parseToLocation(DirectiveLoc);
165 void ExpandModularHeadersPPCallbacks::EndOfMainFile() {
166 while (!CurrentToken.is(tok::eof))
167 PP->Lex(CurrentToken);
173 void ExpandModularHeadersPPCallbacks::Ident(SourceLocation
Loc, StringRef) {
174 parseToLocation(
Loc);
176 void ExpandModularHeadersPPCallbacks::PragmaDirective(SourceLocation
Loc,
177 PragmaIntroducerKind) {
178 parseToLocation(
Loc);
180 void ExpandModularHeadersPPCallbacks::PragmaComment(SourceLocation
Loc,
181 const IdentifierInfo *,
183 parseToLocation(
Loc);
185 void ExpandModularHeadersPPCallbacks::PragmaDetectMismatch(SourceLocation
Loc,
188 parseToLocation(
Loc);
190 void ExpandModularHeadersPPCallbacks::PragmaDebug(SourceLocation
Loc,
192 parseToLocation(
Loc);
194 void ExpandModularHeadersPPCallbacks::PragmaMessage(SourceLocation
Loc,
198 parseToLocation(
Loc);
200 void ExpandModularHeadersPPCallbacks::PragmaDiagnosticPush(SourceLocation
Loc,
202 parseToLocation(
Loc);
204 void ExpandModularHeadersPPCallbacks::PragmaDiagnosticPop(SourceLocation
Loc,
206 parseToLocation(
Loc);
208 void ExpandModularHeadersPPCallbacks::PragmaDiagnostic(SourceLocation
Loc,
212 parseToLocation(
Loc);
214 void ExpandModularHeadersPPCallbacks::HasInclude(SourceLocation
Loc, StringRef,
215 bool, Optional<FileEntryRef>,
216 SrcMgr::CharacteristicKind) {
217 parseToLocation(
Loc);
219 void ExpandModularHeadersPPCallbacks::PragmaOpenCLExtension(
220 SourceLocation NameLoc,
const IdentifierInfo *, SourceLocation StateLoc,
223 parseToLocation(NameLoc);
225 void ExpandModularHeadersPPCallbacks::PragmaWarning(SourceLocation
Loc,
226 StringRef, ArrayRef<int>) {
227 parseToLocation(
Loc);
229 void ExpandModularHeadersPPCallbacks::PragmaWarningPush(SourceLocation
Loc,
231 parseToLocation(
Loc);
233 void ExpandModularHeadersPPCallbacks::PragmaWarningPop(SourceLocation
Loc) {
234 parseToLocation(
Loc);
236 void ExpandModularHeadersPPCallbacks::PragmaAssumeNonNullBegin(
237 SourceLocation
Loc) {
238 parseToLocation(
Loc);
240 void ExpandModularHeadersPPCallbacks::PragmaAssumeNonNullEnd(
241 SourceLocation
Loc) {
242 parseToLocation(
Loc);
244 void ExpandModularHeadersPPCallbacks::MacroExpands(
const Token &MacroNameTok,
245 const MacroDefinition &,
249 parseToLocation(
Range.getBegin());
251 void ExpandModularHeadersPPCallbacks::MacroDefined(
const Token &MacroNameTok,
252 const MacroDirective *
MD) {
253 parseToLocation(
MD->getLocation());
255 void ExpandModularHeadersPPCallbacks::MacroUndefined(
256 const Token &,
const MacroDefinition &,
const MacroDirective *Undef) {
258 parseToLocation(Undef->getLocation());
260 void ExpandModularHeadersPPCallbacks::Defined(
const Token &MacroNameTok,
261 const MacroDefinition &,
264 parseToLocation(
Range.getBegin());
266 void ExpandModularHeadersPPCallbacks::SourceRangeSkipped(
267 SourceRange
Range, SourceLocation EndifLoc) {
269 parseToLocation(EndifLoc);
271 void ExpandModularHeadersPPCallbacks::If(SourceLocation
Loc, SourceRange,
272 ConditionValueKind) {
273 parseToLocation(
Loc);
275 void ExpandModularHeadersPPCallbacks::Elif(SourceLocation
Loc, SourceRange,
276 ConditionValueKind, SourceLocation) {
277 parseToLocation(
Loc);
279 void ExpandModularHeadersPPCallbacks::Ifdef(SourceLocation
Loc,
const Token &,
280 const MacroDefinition &) {
281 parseToLocation(
Loc);
283 void ExpandModularHeadersPPCallbacks::Ifndef(SourceLocation
Loc,
const Token &,
284 const MacroDefinition &) {
285 parseToLocation(
Loc);
287 void ExpandModularHeadersPPCallbacks::Else(SourceLocation
Loc, SourceLocation) {
288 parseToLocation(
Loc);
290 void ExpandModularHeadersPPCallbacks::Endif(SourceLocation
Loc,
292 parseToLocation(
Loc);