Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 49 additions & 13 deletions core/dictgen/src/LinkdefReader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
#include "clang/AST/ASTContext.h"

#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"

#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Lex/Pragma.h"

#include "cling/Interpreter/CIFactory.h"
Expand Down Expand Up @@ -1056,26 +1058,60 @@ bool LinkdefReader::Parse(SelectionRules &sr, llvm::StringRef code, const std::v
parserArgsC.push_back(parserArgs[i].c_str());
}

std::string Std = "-std=c++" +
std::to_string(cling::CIFactory::CxxStdCompiledWith());
//parserArgsC.push_back("-E");
parserArgsC.push_back("-fsyntax-only");
parserArgsC.push_back("-U__CINT__");
// parserArgsC.push_back(Std.c_str());
// Extract all #pragmas
std::unique_ptr<llvm::MemoryBuffer> memBuf = llvm::MemoryBuffer::getMemBuffer(code, "CLING #pragma extraction");
clang::CompilerInstance *pragmaCI =
cling::CIFactory::createCI(std::move(memBuf), parserArgsC.size(), &parserArgsC[0], llvmdir,
std::nullopt /*Consumer*/, {} /*ModuleFileExtension*/, true /*OnlyLex*/);

clang::Preprocessor &PP = pragmaCI->getPreprocessor();
clang::DiagnosticConsumer &DClient = pragmaCI->getDiagnosticClient();
DClient.BeginSourceFile(pragmaCI->getLangOpts(), &PP);
struct PragmaCollectAction : public clang::SyntaxOnlyAction {
LinkdefReader &fLDR;
cling::Interpreter &fInterp;
PragmaCollectAction(LinkdefReader &ldr, cling::Interpreter &Interp) : fLDR(ldr), fInterp(Interp) {}
bool BeginSourceFileAction(clang::CompilerInstance &CI) override
{
if (CI.getLangOpts().Modules)
cling::CIFactory::collectModule(CI);
clang::Preprocessor &PP = CI.getPreprocessor();
// Attach the handlers before we have started. PP takes the ownership.
PP.AddPragmaHandler(new PragmaLinkCollector(fLDR));
PP.AddPragmaHandler(new PragmaCreateCollector(fLDR));
PP.AddPragmaHandler(new PragmaExtraInclude(fLDR));
PP.AddPragmaHandler(new PragmaIoReadInclude(fLDR));
// cling::CIFactory::setupCompiler(&CI, fInterp.getOptions().CompilerOpts);
return clang::SyntaxOnlyAction::BeginSourceFileAction(CI);
}

void ExecuteAction() override
{
clang::CompilerInstance &CI = getCompilerInstance();
if (!CI.hasPreprocessor())
return;

// FIXME: Move the truncation aspect of this into Sema, we delayed this
// till here so the source manager would be initialized.
if (hasCodeCompletionSupport() && !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
CI.createCodeCompletionConsumer();

// FIXME: Reduce the code duplication across these collector classes.
PragmaLinkCollector pragmaLinkCollector(*this);
PragmaCreateCollector pragmaCreateCollector(*this);
PragmaExtraInclude pragmaExtraInclude(*this);
PragmaIoReadInclude pragmaIoReadInclude(*this);
// Use a code completion consumer?
clang::CodeCompleteConsumer *CompletionConsumer = nullptr;
if (CI.hasCodeCompletionConsumer())
CompletionConsumer = &CI.getCodeCompletionConsumer();

PP.AddPragmaHandler(&pragmaLinkCollector);
PP.AddPragmaHandler(&pragmaCreateCollector);
PP.AddPragmaHandler(&pragmaExtraInclude);
PP.AddPragmaHandler(&pragmaIoReadInclude);
if (!CI.hasSema())
CI.createSema(getTranslationUnitKind(), CompletionConsumer);
}

} Act(*this, fInterp);

pragmaCI->ExecuteAction(Act);
clang::Preprocessor &PP = pragmaCI->getPreprocessor();

// Start parsing the specified input file.
PP.EnterMainSourceFile();
Expand All @@ -1085,5 +1121,5 @@ bool LinkdefReader::Parse(SelectionRules &sr, llvm::StringRef code, const std::v
} while (tok.isNot(clang::tok::annot_repl_input_end));

fSelectionRules = nullptr;
return 0 == DClient.getNumErrors();
return 0 == pragmaCI->getDiagnosticClient().getNumErrors();
}
2 changes: 2 additions & 0 deletions interpreter/cling/include/cling/Interpreter/CIFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace cling {
using ModuleFileExtensions =
std::vector<std::shared_ptr<clang::ModuleFileExtension>>;

void collectModule(clang::CompilerInstance &CI);
// TODO: Add overload that takes file not MemoryBuffer

clang::CompilerInstance*
Expand All @@ -48,6 +49,7 @@ namespace cling {
std::optional<std::unique_ptr<clang::ASTConsumer>> consumerOpt,
const ModuleFileExtensions& moduleExtensions,
bool OnlyLex = false);
unsigned CxxStdCompiledWith();
} // namespace CIFactory
} // namespace cling
#endif // CLING_CIFACTORY_H
9 changes: 9 additions & 0 deletions interpreter/cling/include/cling/Interpreter/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace cling {
class CompilationOptions;
class DynamicLibraryManager;
class IncrementalCUDADeviceCompiler;
class IncrementalAction;
class IncrementalExecutor;
class IncrementalParser;
class InterpreterCallbacks;
Expand Down Expand Up @@ -184,6 +185,14 @@ namespace cling {
///
std::unique_ptr<llvm::orc::ThreadSafeContext> TSCtx;

///\brief compiler instance.
///
std::unique_ptr<clang::CompilerInstance> m_CI;

///\brief The FrontendAction
///
std::unique_ptr<IncrementalAction> m_Act;

///\brief Cling's execution engine - a well wrapped llvm execution engine.
///
std::unique_ptr<IncrementalExecutor> m_Executor;
Expand Down
Loading
Loading