10 #include "clang/Frontend/CompilerInstance.h" 11 #include "clang/Lex/PreprocessorOptions.h" 12 #include "clang/Serialization/ASTReader.h" 24 const SrcMgr::ContentCache &ContentCache,
27 if (!FilesToRecord.count(File))
31 if (!ContentCache.getRawBuffer())
34 InMemoryFs.addFile(File->getName(), 0,
35 llvm::MemoryBuffer::getMemBufferCopy(
36 ContentCache.getRawBuffer()->getBuffer()));
38 FilesToRecord.erase(File);
44 for (
auto FileEntry : FilesToRecord)
45 llvm::errs() <<
"Did not record contents for input file: " 46 << FileEntry->getName() <<
"\n";
51 llvm::DenseSet<const FileEntry *> FilesToRecord;
56 IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFS)
57 : Recorder(std::make_unique<
FileRecorder>()), Compiler(*CI),
59 Sources(Compiler.getSourceManager()),
61 Diags(new DiagnosticIDs, new DiagnosticOptions,
62 new ForwardingDiagnosticConsumer(Compiler.getDiagnosticClient())),
63 LangOpts(Compiler.getLangOpts()) {
66 OverlayFS->pushOverlay(InMemoryFs);
68 Diags.setSourceManager(&Sources);
70 LangOpts.Modules =
false;
72 auto HSO = std::make_shared<HeaderSearchOptions>();
73 *HSO = Compiler.getHeaderSearchOpts();
75 HeaderInfo = std::make_unique<HeaderSearch>(HSO, Sources, Diags, LangOpts,
76 &Compiler.getTarget());
78 auto PO = std::make_shared<PreprocessorOptions>();
79 *PO = Compiler.getPreprocessorOpts();
81 PP = std::make_unique<clang::Preprocessor>(PO, Diags, LangOpts, Sources,
82 *HeaderInfo, ModuleLoader,
85 PP->Initialize(Compiler.getTarget(), Compiler.getAuxTarget());
86 InitializePreprocessor(*PP, *PO, Compiler.getPCHContainerReader(),
87 Compiler.getFrontendOpts());
88 ApplyHeaderSearchOptions(*HeaderInfo, *HSO, LangOpts,
89 Compiler.getTarget().getTriple());
98 void ExpandModularHeadersPPCallbacks::handleModuleFile(
99 serialization::ModuleFile *MF) {
103 if (VisitedModules.count(MF))
105 VisitedModules.insert(MF);
109 Compiler.getASTReader()->visitInputFiles(
111 [
this](
const serialization::InputFile &IF,
bool ) {
112 Recorder->addNecessaryFile(IF.getFile());
115 for (
auto Import : MF->Imports)
116 handleModuleFile(Import);
119 void ExpandModularHeadersPPCallbacks::parseToLocation(SourceLocation
Loc) {
121 for (
unsigned I = 0, N = Sources.loaded_sloc_entry_size(); I != N; ++I) {
122 Sources.getLoadedSLocEntry(I,
nullptr);
125 for (
auto It = Sources.fileinfo_begin(); It != Sources.fileinfo_end(); ++It) {
126 Recorder->recordFileContent(It->getFirst(), *It->getSecond(), *InMemoryFs);
128 Recorder->checkAllFilesRecorded();
130 if (!StartedLexing) {
131 StartedLexing =
true;
132 PP->Lex(CurrentToken);
134 while (!CurrentToken.is(tok::eof) &&
135 Sources.isBeforeInTranslationUnit(CurrentToken.getLocation(),
Loc)) {
136 PP->Lex(CurrentToken);
140 void ExpandModularHeadersPPCallbacks::FileChanged(
141 SourceLocation Loc, FileChangeReason Reason,
142 SrcMgr::CharacteristicKind FileType, FileID PrevFID = FileID()) {
143 if (!EnteredMainFile) {
144 EnteredMainFile =
true;
145 PP->EnterMainSourceFile();
149 void ExpandModularHeadersPPCallbacks::InclusionDirective(
150 SourceLocation DirectiveLoc,
const Token &IncludeToken,
151 StringRef IncludedFilename,
bool IsAngled, CharSourceRange FilenameRange,
152 const FileEntry *IncludedFile, StringRef SearchPath, StringRef RelativePath,
153 const Module *Imported, SrcMgr::CharacteristicKind FileType) {
155 serialization::ModuleFile *MF =
156 Compiler.getASTReader()->getModuleManager().lookup(
157 Imported->getASTFile());
158 handleModuleFile(MF);
160 parseToLocation(DirectiveLoc);
163 void ExpandModularHeadersPPCallbacks::EndOfMainFile() {
164 while (!CurrentToken.is(tok::eof))
165 PP->Lex(CurrentToken);
171 void ExpandModularHeadersPPCallbacks::Ident(SourceLocation Loc, StringRef) {
172 parseToLocation(Loc);
174 void ExpandModularHeadersPPCallbacks::PragmaDirective(SourceLocation Loc,
175 PragmaIntroducerKind) {
176 parseToLocation(Loc);
178 void ExpandModularHeadersPPCallbacks::PragmaComment(SourceLocation Loc,
179 const IdentifierInfo *,
181 parseToLocation(Loc);
183 void ExpandModularHeadersPPCallbacks::PragmaDetectMismatch(SourceLocation Loc,
186 parseToLocation(Loc);
188 void ExpandModularHeadersPPCallbacks::PragmaDebug(SourceLocation Loc,
190 parseToLocation(Loc);
192 void ExpandModularHeadersPPCallbacks::PragmaMessage(SourceLocation Loc,
196 parseToLocation(Loc);
198 void ExpandModularHeadersPPCallbacks::PragmaDiagnosticPush(SourceLocation Loc,
200 parseToLocation(Loc);
202 void ExpandModularHeadersPPCallbacks::PragmaDiagnosticPop(SourceLocation Loc,
204 parseToLocation(Loc);
206 void ExpandModularHeadersPPCallbacks::PragmaDiagnostic(SourceLocation Loc,
210 parseToLocation(Loc);
212 void ExpandModularHeadersPPCallbacks::HasInclude(SourceLocation Loc, StringRef,
213 bool, Optional<FileEntryRef>,
214 SrcMgr::CharacteristicKind) {
215 parseToLocation(Loc);
217 void ExpandModularHeadersPPCallbacks::PragmaOpenCLExtension(
218 SourceLocation NameLoc,
const IdentifierInfo *, SourceLocation StateLoc,
221 parseToLocation(NameLoc);
223 void ExpandModularHeadersPPCallbacks::PragmaWarning(SourceLocation Loc,
224 StringRef, ArrayRef<int>) {
225 parseToLocation(Loc);
227 void ExpandModularHeadersPPCallbacks::PragmaWarningPush(SourceLocation Loc,
229 parseToLocation(Loc);
231 void ExpandModularHeadersPPCallbacks::PragmaWarningPop(SourceLocation Loc) {
232 parseToLocation(Loc);
234 void ExpandModularHeadersPPCallbacks::PragmaAssumeNonNullBegin(
235 SourceLocation Loc) {
236 parseToLocation(Loc);
238 void ExpandModularHeadersPPCallbacks::PragmaAssumeNonNullEnd(
239 SourceLocation Loc) {
240 parseToLocation(Loc);
242 void ExpandModularHeadersPPCallbacks::MacroExpands(
const Token &MacroNameTok,
243 const MacroDefinition &,
247 parseToLocation(Range.getBegin());
249 void ExpandModularHeadersPPCallbacks::MacroDefined(
const Token &MacroNameTok,
250 const MacroDirective *
MD) {
251 parseToLocation(MD->getLocation());
253 void ExpandModularHeadersPPCallbacks::MacroUndefined(
254 const Token &,
const MacroDefinition &,
const MacroDirective *Undef) {
256 parseToLocation(Undef->getLocation());
258 void ExpandModularHeadersPPCallbacks::Defined(
const Token &MacroNameTok,
259 const MacroDefinition &,
262 parseToLocation(Range.getBegin());
264 void ExpandModularHeadersPPCallbacks::SourceRangeSkipped(
265 SourceRange Range, SourceLocation EndifLoc) {
267 parseToLocation(EndifLoc);
269 void ExpandModularHeadersPPCallbacks::If(SourceLocation Loc, SourceRange,
270 ConditionValueKind) {
271 parseToLocation(Loc);
273 void ExpandModularHeadersPPCallbacks::Elif(SourceLocation Loc, SourceRange,
274 ConditionValueKind, SourceLocation) {
275 parseToLocation(Loc);
277 void ExpandModularHeadersPPCallbacks::Ifdef(SourceLocation Loc,
const Token &,
278 const MacroDefinition &) {
279 parseToLocation(Loc);
281 void ExpandModularHeadersPPCallbacks::Ifndef(SourceLocation Loc,
const Token &,
282 const MacroDefinition &) {
283 parseToLocation(Loc);
285 void ExpandModularHeadersPPCallbacks::Else(SourceLocation Loc, SourceLocation) {
286 parseToLocation(Loc);
288 void ExpandModularHeadersPPCallbacks::Endif(SourceLocation Loc,
290 parseToLocation(Loc);
SourceLocation Loc
'#' location in the include directive
Some operations such as code completion produce a set of candidates.
llvm::IntrusiveRefCntPtr< llvm::vfs::InMemoryFileSystem > InMemoryFileSystem
bool IsAngled
true if this was an include with angle brackets
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
CharSourceRange Range
SourceRange for the file name.