16#include "cling/Interpreter/DynamicLibraryManager.h"
17#include "cling/Interpreter/Interpreter.h"
18#include "cling/Interpreter/InterpreterCallbacks.h"
19#include "cling/Interpreter/Transaction.h"
20#include "cling/Utils/AST.h"
22#include "clang/AST/ASTConsumer.h"
23#include "clang/AST/ASTContext.h"
24#include "clang/AST/DeclBase.h"
25#include "clang/AST/DeclTemplate.h"
26#include "clang/AST/GlobalDecl.h"
27#include "clang/Frontend/CompilerInstance.h"
28#include "clang/Lex/HeaderSearch.h"
29#include "clang/Lex/PPCallbacks.h"
30#include "clang/Lex/Preprocessor.h"
31#include "clang/Parse/Parser.h"
32#include "clang/Sema/Lookup.h"
33#include "clang/Sema/Scope.h"
34#include "clang/Serialization/ASTReader.h"
35#include "clang/Serialization/GlobalModuleIndex.h"
37#include "llvm/Object/ELFObjectFile.h"
38#include "llvm/Object/ObjectFile.h"
40#include "llvm/Support/Error.h"
41#include "llvm/Support/FileSystem.h"
42#include "llvm/Support/Path.h"
43#include "llvm/Support/Process.h"
71 std::string &args, std::string &io, std::string &fname);
74 llvm::StringRef canonicalName);
76 llvm::StringRef canonicalName);
88 m_Interpreter->declare(
"namespace __ROOT_SpecialObjects{}", &T);
98 llvm::StringRef FileName,
100 clang::CharSourceRange ,
101 const clang::FileEntry *FE,
104 const clang::Module * Imported,
105 clang::SrcMgr::CharacteristicKind FileType) {
107 Sema &SemaR = m_Interpreter->getSema();
110 if (!SemaR.isModuleVisible(Imported))
112 "Module %s resolved but not visible!", Imported->Name.c_str());
128 bool isHeaderFile = FileName.endswith(
".h") || FileName.endswith(
".hxx") || FileName.endswith(
".hpp");
132 std::string localString(FileName.str());
134 DeclarationName Name = &SemaR.getASTContext().Idents.get(localString.c_str());
135 LookupResult RHeader(SemaR, Name, sLoc, Sema::LookupOrdinaryName);
142 bool permanent,
bool resolved) {
150 llvm::SmallVectorImpl<char> &RecoveryPath) {
158 Preprocessor& PP = m_Interpreter->getCI()->getPreprocessor();
161 std::string filename(FileName.str().substr(0,FileName.str().find_last_of(
'"')));
162 std::string fname, mode, arguments, io;
165 if (mode.length() > 0) {
166 if (llvm::sys::fs::exists(fname)) {
168 std::string options =
"k";
169 if (mode.find(
"++") != std::string::npos) options +=
"f";
170 if (mode.find(
"g") != std::string::npos) options +=
"g";
171 if (mode.find(
"O") != std::string::npos) options +=
"O";
174 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
175 Parser& P =
const_cast<Parser&
>(m_Interpreter->getParser());
177 clang::Parser::ParserCurTokRestoreRAII fSavedCurToken(P);
181 Token& Tok =
const_cast<Token&
>(P.getCurToken());
182 Tok.setKind(tok::semi);
194 Sema& SemaR = m_Interpreter->getSema();
195 ASTContext& C = SemaR.getASTContext();
196 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, C.getTranslationUnitDecl(),
203 fPPOldFlag = PP.GetSuppressIncludeNotFoundError();
204 PP.SetSuppressIncludeNotFoundError(
true);
213 PP.SetSuppressIncludeNotFoundError(
fPPOldFlag);
224 DeclContext* DC = S->getEntity();
233 clang::DeclContext* MaybeTU = DC;
234 while (MaybeTU && !isa<TranslationUnitDecl>(MaybeTU)) {
236 MaybeTU = MaybeTU->getParent();
238 return isa<FunctionDecl>(DC);
256 if (m_Interpreter->getSema().getDiagnostics().hasErrorOccurred())
294 llvm::Optional<std::string> envUseGMI = llvm::sys::Process::GetEnv(
"ROOT_USE_GMI");
295 if (envUseGMI.hasValue())
299 const CompilerInstance *CI = m_Interpreter->getCI();
300 const LangOptions &LangOpts = CI->getPreprocessor().getLangOpts();
302 if (!LangOpts.Modules)
306 if (LangOpts.isCompilingModule())
312 GlobalModuleIndex *Index = CI->getModuleManager()->getGlobalIndex();
318 GlobalModuleIndex::FileNameHitSet FoundModules;
322 if (Index->lookupIdentifier(Name.getAsString(), FoundModules)) {
323 for (llvm::StringRef FileName : FoundModules) {
324 StringRef ModuleName = llvm::sys::path::stem(FileName);
329 llvm::errs() <<
"Module '" << ModuleName <<
"' already loaded"
330 <<
" for '" << Name.getAsString() <<
"'\n";
336 llvm::errs() <<
"Loading '" << ModuleName <<
"' on demand"
337 <<
" for '" << Name.getAsString() <<
"'\n";
339 m_Interpreter->loadModule(ModuleName);
342 if (loadFirstMatchOnly)
362 if (
Name.getNameKind() != DeclarationName::Identifier)
368 NamespaceDecl* NSD = dyn_cast<NamespaceDecl>(
const_cast<DeclContext*
>(DC));
377 const DeclContext* primaryDC = NSD->getPrimaryContext();
381 Sema &SemaR = m_Interpreter->getSema();
382 LookupResult
R(SemaR, Name, SourceLocation(), Sema::LookupOrdinaryName);
383 R.suppressDiagnostics();
386 = NSD->getQualifiedNameAsString() +
"::" +
Name.getAsString();
392 clang::Scope
S(SemaR.TUScope, clang::Scope::DeclScope, SemaR.getDiagnostics());
393 S.setEntity(
const_cast<DeclContext*
>(DC));
394 Sema::ContextAndScopeRAII pushedDCAndS(SemaR,
const_cast<DeclContext*
>(DC), &S);
397 llvm::SmallVector<NamedDecl*, 4> lookupResults;
398 for(LookupResult::iterator
I =
R.begin(), E =
R.end();
I <
E; ++
I)
399 lookupResults.push_back(*
I);
400 UpdateWithNewDecls(DC, Name, llvm::makeArrayRef(lookupResults.data(),
401 lookupResults.size()));
422 Sema &SemaR = m_Interpreter->getSema();
424 SourceLocation Loc = Tag->getLocation();
425 if (SemaR.getSourceManager().isInSystemHeader(Loc)) {
430 for (
auto ReRD: Tag->redecls()) {
432 if (ReRD->isBeingDefined())
437 if (RecordDecl* RD = dyn_cast<RecordDecl>(Tag)) {
438 ASTContext& C = SemaR.getASTContext();
439 Parser& P =
const_cast<Parser&
>(m_Interpreter->getParser());
448 C.getTypeDeclType(RD),
454 Tag->setHasExternalLexicalStorage(
false);
470 Scope *S,
const FileEntry* FE ) {
476 Sema &SemaR = m_Interpreter->getSema();
485 Sema::LookupNameKind kind =
R.getLookupKind();
486 if (!(kind == Sema::LookupTagName || kind == Sema::LookupOrdinaryName
487 || kind == Sema::LookupNestedNameSpecifierName
488 || kind == Sema::LookupNamespaceName))
493 bool lookupSuccess =
false;
495 Parser &P =
const_cast<Parser &
>(m_Interpreter->getParser());
502 lookupSuccess = SemaR.LookupName(
R, S);
504 if (
R.isSingleResult()) {
505 if (isa<clang::RecordDecl>(
R.getFoundDecl())) {
521 lookupSuccess = FE || SemaR.LookupName(
R, S);
527 std::string incl =
"#include \"";
528 incl += FE->getName();
530 m_Interpreter->declare(incl);
566 if (
R.isForRedeclaration())
571 const Sema::LookupNameKind LookupKind =
R.getLookupKind();
572 if (LookupKind != Sema::LookupOrdinaryName)
576 Sema &SemaR = m_Interpreter->getSema();
577 ASTContext& C = SemaR.getASTContext();
578 Preprocessor &PP = SemaR.getPreprocessor();
579 DeclContext *CurDC = SemaR.CurContext;
580 DeclarationName Name =
R.getLookupName();
583 if(!CurDC || !CurDC->isFunctionOrMethod())
588 Preprocessor::CleanupAndRestoreCacheRAII cleanupPPRAII(PP);
595#if defined(R__MUST_REVISIT)
596#if R__MUST_REVISIT(6,2)
601 if (!fgSetOfSpecials) {
602 fgSetOfSpecials =
new std::set<TObject*>;
604 ((std::set<TObject*>*)fgSetOfSpecials)->insert((
TObject*)*obj);
608 VarDecl *VD = cast_or_null<VarDecl>(utils::Lookup::Named(&SemaR, Name,
613 TObject **address = (
TObject**)m_Interpreter->getAddressOfGlobal(GD);
617 CStyleCastExpr *CStyleCast = cast<CStyleCastExpr>(VD->getInit());
618 Expr* newInit = utils::Synthesize::IntegerLiteralExpr(C, (uint64_t)obj);
619 CStyleCast->setSubExpr(newInit);
626 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
630 QualType QT = C.getPointerType(C.getTypeDeclType(cast<TypeDecl>(TD)));
633 SourceLocation(), Name.getAsIdentifierInfo(), QT,
637 = utils::Synthesize::CStyleCastPtrExpr(&SemaR, QT, (uint64_t)obj);
642 cling::CompilationOptions CO;
643 CO.DeclarationExtraction = 0;
644 CO.ValuePrinting = CompilationOptions::VPDisabled;
645 CO.ResultEvaluation = 0;
646 CO.DynamicScoping = 0;
648 CO.CodeGeneration = 1;
650 cling::Transaction* T =
new cling::Transaction(CO, SemaR);
652 T->setState(cling::Transaction::kCompleted);
654 m_Interpreter->emitAllDecls(T);
656 assert(VD &&
"Cannot be null!");
673 DeclarationName Name =
R.getLookupName();
674 IdentifierInfo* II = Name.getAsIdentifierInfo();
675 SourceLocation Loc =
R.getNameLoc();
676 Sema& SemaRef =
R.getSema();
677 ASTContext& C = SemaRef.getASTContext();
678 DeclContext* TU = C.getTranslationUnitDecl();
679 assert(TU &&
"Must not be null.");
682 clang::FunctionDecl* Wrapper =
nullptr;
685 DeclContext* DCCursor =
Cursor->getEntity();
688 Wrapper = dyn_cast_or_null<FunctionDecl>(DCCursor);
690 if (utils::Analyze::IsWrapper(Wrapper)) {
704 VarDecl* Result = VarDecl::Create(C, TU, Loc, Loc, II, C.DependentTy,
716 SourceRange invalidRange;
717 Wrapper->addAttr(
new (C) AnnotateAttr(invalidRange, C,
"__ResolveAtRuntime", 0));
721 Sema::ContextRAII pushedDC(SemaRef, TU);
732 if (
R.getLookupKind() != Sema::LookupOrdinaryName)
735 if (
R.isForRedeclaration())
741 const Transaction* T = getInterpreter()->getCurrentTransaction();
744 const cling::CompilationOptions& COpts = T->getCompilationOpts();
745 if (!COpts.DynamicScoping)
764 for (Scope* DepScope = S; DepScope; DepScope = DepScope->getParent()) {
765 if (DeclContext* Ctx =
static_cast<DeclContext*
>(DepScope->getEntity())) {
766 if (!Ctx->isDependentContext())
768 if (isa<FunctionDecl>(Ctx))
786 if (
R.isForRedeclaration())
789 if (
R.getLookupKind() != Sema::LookupOrdinaryName)
792 if (!isa<FunctionDecl>(
R.getSema().CurContext))
797 DeclContext* ScopeDC = S->getEntity();
798 if (!ScopeDC || !llvm::isa<FunctionDecl>(ScopeDC))
803 Scope* FnScope = S->getFnParent();
806 auto FD = dyn_cast_or_null<FunctionDecl>(FnScope->getEntity());
807 if (!FD || !utils::Analyze::IsWrapper(FD))
811 Sema& SemaRef =
R.getSema();
812 ASTContext& C = SemaRef.getASTContext();
813 DeclContext* DC = SemaRef.CurContext;
814 assert(DC &&
"Must not be null.");
817 Preprocessor& PP =
R.getSema().getPreprocessor();
820 if (PP.LookAhead(0).isNot(tok::equal)) {
826 DeclarationName Name =
R.getLookupName();
827 IdentifierInfo* II = Name.getAsIdentifierInfo();
828 SourceLocation Loc =
R.getNameLoc();
829 VarDecl* Result = VarDecl::Create(C, DC, Loc, Loc, II,
830 C.getAutoType(QualType(),
831 clang::AutoTypeKeyword::Auto,
837 "Cannot create VarDecl");
844 SourceRange invalidRange;
845 Result->addAttr(
new (C) AnnotateAttr(invalidRange, C,
"__Auto", 0));
856 Sema& SemaR = m_Interpreter->getSema();
857 cling::Transaction TPrev((cling::CompilationOptions(), SemaR));
858 TPrev.append(SemaR.getASTContext().getTranslationUnitDecl());
897 if (
const RecordDecl* RD = dyn_cast<RecordDecl>(D)) {
910 llvm::StringRef canonicalName) {
915 llvm::StringRef canonicalName) {
The file contains utilities which are foundational and could be used across the core component of ROO...
#define R(a, b, c, d, e, f, g, h, i)
bool TCling__LibraryLoadingFailed(const std::string &, const std::string &, bool, bool)
Lookup libraries in LD_LIBRARY_PATH and DYLD_LIBRARY_PATH with mangled_name, which is extracted by er...
void * TCling__LockCompilationDuringUserCodeExecution()
Lock the interpreter.
void TCling__UpdateListsOnCommitted(const cling::Transaction &, Interpreter *)
void TCling__SplitAclicMode(const char *fileName, std::string &mode, std::string &args, std::string &io, std::string &fname)
void TCling__TransactionRollback(const cling::Transaction &)
const char * TCling__GetClassSharedLibs(const char *className)
int TCling__AutoParseCallback(const char *className)
Decl * TCling__GetObjectDecl(TObject *obj)
void TCling__GetNormalizedContext(const ROOT::TMetaUtils::TNormalizedCtxt *&)
void TCling__RestoreInterpreterMutex(void *state)
Re-apply the lock count delta that TCling__ResetInterpreterMutex() caused.
int TCling__CompileMacro(const char *fileName, const char *options)
void * TCling__ResetInterpreterMutex()
Reset the interpreter lock to the state it had before interpreter-related calls happened.
int TCling__AutoLoadCallback(const char *className)
void TCling__LibraryUnloadedRTTI(const void *dyLibHandle, llvm::StringRef canonicalName)
void TCling__PrintStackTrace()
Print a StackTrace!
int TCling__IsAutoLoadNamespaceCandidate(const clang::NamespaceDecl *name)
void TCling__UpdateListsOnUnloaded(const cling::Transaction &)
void TCling__UnlockCompilationDuringUserCodeExecution(void *state)
Unlock the interpreter.
static bool topmostDCIsFunction(Scope *S)
void TCling__InvalidateGlobal(const clang::Decl *)
void TCling__LibraryLoadedRTTI(const void *dyLibHandle, llvm::StringRef canonicalName)
TObject * TCling__GetObjectAddress(const char *Name, void *&LookupCtx)
void TCling__RestoreInterpreterMutex(void *delta)
Re-apply the lock count delta that TCling__ResetInterpreterMutex() caused.
void TCling__TransactionRollback(const cling::Transaction &T)
void TCling__InvalidateGlobal(const clang::Decl *D)
void * TCling__LockCompilationDuringUserCodeExecution()
Lock the interpreter.
void TCling__UpdateListsOnUnloaded(const cling::Transaction &T)
void TCling__GetNormalizedContext(const ROOT::TMetaUtils::TNormalizedCtxt *&normCtxt)
bool TCling__LibraryLoadingFailed(const std::string &errmessage, const std::string &libStem, bool permanent, bool resolved)
Lookup libraries in LD_LIBRARY_PATH and DYLD_LIBRARY_PATH with mangled_name, which is extracted by er...
void TCling__UnlockCompilationDuringUserCodeExecution(void *)
Unlock the interpreter.
const char * TCling__GetClassSharedLibs(const char *className)
int TCling__AutoParseCallback(const char *className)
void TCling__LibraryUnloadedRTTI(const void *dyLibHandle, const char *canonicalName)
void TCling__UpdateListsOnCommitted(const cling::Transaction &T, cling::Interpreter *)
const Decl * TCling__GetObjectDecl(TObject *obj)
int TCling__CompileMacro(const char *fileName, const char *options)
void * TCling__ResetInterpreterMutex()
Reset the interpreter lock to the state it had before interpreter-related calls happened.
int TCling__AutoLoadCallback(const char *className)
void TCling__PrintStackTrace()
Print a StackTrace!
void TCling__LibraryLoadedRTTI(const void *dyLibHandle, const char *canonicalName)
TObject * TCling__GetObjectAddress(const char *Name, void *&LookupCtx)
int TCling__IsAutoLoadNamespaceCandidate(const clang::NamespaceDecl *nsDecl)
void TCling__SplitAclicMode(const char *fileName, string &mode, string &args, string &io, string &fname)
typedef void((*Func_t)())
void LibraryUnloaded(const void *dyLibHandle, llvm::StringRef canonicalName) override
bool tryAutoParseInternal(llvm::StringRef Name, clang::LookupResult &R, clang::Scope *S, const clang::FileEntry *FE=0)
bool tryFindROOTSpecialInternal(clang::LookupResult &R, clang::Scope *S)
void ReturnedFromUserCode(void *stateInfo) override
bool fIsAutoParsingSuspended
bool tryResolveAtRuntimeInternal(clang::LookupResult &R, clang::Scope *S)
bool findInGlobalModuleIndex(clang::DeclarationName Name, bool loadFirstMatchOnly=true)
void PrintStackTrace() override
bool tryInjectImplicitAutoKeyword(clang::LookupResult &R, clang::Scope *S)
bool IsAutoLoadingEnabled()
clang::NamespaceDecl * fROOTSpecialNamespace
void TransactionRollback(const cling::Transaction &T) override
void TransactionCommitted(const cling::Transaction &T) override
TClingCallbacks(cling::Interpreter *interp, bool hasCodeGen)
llvm::DenseMap< llvm::StringRef, clang::DeclarationName > m_LoadedModuleFiles
void DeclDeserialized(const clang::Decl *D) override
void InclusionDirective(clang::SourceLocation, const clang::Token &, llvm::StringRef FileName, bool, clang::CharSourceRange, const clang::FileEntry *, llvm::StringRef, llvm::StringRef, const clang::Module *, clang::SrcMgr::CharacteristicKind) override
void DefinitionShadowed(const clang::NamedDecl *D) override
A previous definition has been shadowed; invalidate TCling' stored data about the old (global) decl.
bool FileNotFound(llvm::StringRef FileName, llvm::SmallVectorImpl< char > &RecoveryPath) override
void * EnteringUserCode() override
bool fIsAutoLoadingRecursively
void UnlockCompilationDuringUserCodeExecution(void *StateInfo) override
bool LibraryLoadingFailed(const std::string &, const std::string &, bool, bool) override
void * LockCompilationDuringUserCodeExecution() override
bool LookupObject(clang::LookupResult &R, clang::Scope *S) override
void LibraryLoaded(const void *dyLibHandle, llvm::StringRef canonicalName) override
bool shouldResolveAtRuntime(clang::LookupResult &R, clang::Scope *S)
void TransactionUnloaded(const cling::Transaction &T) override
Mother of all ROOT objects.
bool ConvertEnvValueToBool(const std::string &value)
RooArgSet S(const RooAbsArg &v1)
constexpr Double_t E()
Base of natural log:
RAII used to store Parser, Sema, Preprocessor state for recursive parsing.
clang::Preprocessor::CleanupAndRestoreCacheRAII fCleanupRAII
clang::Sema::ContextAndScopeRAII fPushedDCAndS