14#include "cling/Interpreter/DynamicLibraryManager.h"
15#include "cling/Interpreter/Interpreter.h"
16#include "cling/Interpreter/InterpreterCallbacks.h"
17#include "cling/Interpreter/Transaction.h"
18#include "cling/Utils/AST.h"
20#include "clang/AST/ASTConsumer.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/DeclBase.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/GlobalDecl.h"
25#include "clang/Frontend/CompilerInstance.h"
26#include "clang/Lex/HeaderSearch.h"
27#include "clang/Lex/PPCallbacks.h"
28#include "clang/Lex/Preprocessor.h"
29#include "clang/Parse/Parser.h"
30#include "clang/Sema/Lookup.h"
31#include "clang/Sema/Scope.h"
32#include "clang/Serialization/ASTReader.h"
33#include "clang/Serialization/GlobalModuleIndex.h"
35#include "llvm/Object/ELFObjectFile.h"
36#include "llvm/Object/ObjectFile.h"
38#include "llvm/Support/Error.h"
39#include "llvm/Support/FileSystem.h"
40#include "llvm/Support/Path.h"
68 std::string &args, std::string &io, std::string &fname);
71 llvm::StringRef canonicalName);
73 llvm::StringRef canonicalName);
85 m_Interpreter->declare(
"namespace __ROOT_SpecialObjects{}", &
T);
95 llvm::StringRef FileName,
97 clang::CharSourceRange ,
98 const clang::FileEntry *FE,
101 const clang::Module * Imported) {
103 Sema &SemaR = m_Interpreter->getSema();
106 if (!SemaR.isModuleVisible(Imported))
108 "Module %s resolved but not visible!", Imported->Name.c_str());
124 bool isHeaderFile = FileName.endswith(
".h") || FileName.endswith(
".hxx") || FileName.endswith(
".hpp");
128 std::string localString(FileName.str());
130 DeclarationName
Name = &SemaR.getASTContext().Idents.get(localString.c_str());
131 LookupResult RHeader(SemaR,
Name, sLoc, Sema::LookupOrdinaryName);
138 bool permanent,
bool resolved) {
146 llvm::SmallVectorImpl<char> &RecoveryPath) {
154 Preprocessor& PP = m_Interpreter->getCI()->getPreprocessor();
157 std::string filename(FileName.str().substr(0,FileName.str().find_last_of(
'"')));
158 std::string fname, mode, arguments, io;
161 if (mode.length() > 0) {
162 if (llvm::sys::fs::exists(fname)) {
164 std::string options =
"k";
165 if (mode.find(
"++") != std::string::npos) options +=
"f";
166 if (mode.find(
"g") != std::string::npos) options +=
"g";
167 if (mode.find(
"O") != std::string::npos) options +=
"O";
170 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
171 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
174 Token& Tok =
const_cast<Token&
>(
P.getCurToken());
179 Tok.setKind(tok::semi);
191 Sema& SemaR = m_Interpreter->getSema();
192 ASTContext&
C = SemaR.getASTContext();
193 Sema::ContextAndScopeRAII pushedDCAndS(SemaR,
C.getTranslationUnitDecl(),
200 fPPOldFlag = PP.GetSuppressIncludeNotFoundError();
201 PP.SetSuppressIncludeNotFoundError(
true);
210 PP.SetSuppressIncludeNotFoundError(
fPPOldFlag);
221 DeclContext* DC =
S->getEntity();
230 clang::DeclContext* MaybeTU = DC;
231 while (MaybeTU && !isa<TranslationUnitDecl>(MaybeTU)) {
233 MaybeTU = MaybeTU->getParent();
235 return isa<FunctionDecl>(DC);
253 if (m_Interpreter->getSema().getDiagnostics().hasErrorOccurred())
291 const CompilerInstance *CI = m_Interpreter->getCI();
292 const LangOptions &LangOpts = CI->getPreprocessor().getLangOpts();
294 if (!LangOpts.Modules)
298 if (LangOpts.isCompilingModule())
304 GlobalModuleIndex *
Index = CI->getModuleManager()->getGlobalIndex();
310 GlobalModuleIndex::FileNameHitSet FoundModules;
314 if (
Index->lookupIdentifier(
Name.getAsString(), FoundModules)) {
315 for (
auto FileName : FoundModules) {
316 StringRef ModuleName = llvm::sys::path::stem(*FileName);
318 m_Interpreter->loadModule(ModuleName);
320 if (loadFirstMatchOnly)
342 if (
Name.getNameKind() != DeclarationName::Identifier)
348 NamespaceDecl* NSD = dyn_cast<NamespaceDecl>(
const_cast<DeclContext*
>(DC));
355 const DeclContext* primaryDC = NSD->getPrimaryContext();
359 Sema &SemaR = m_Interpreter->getSema();
360 LookupResult
R(SemaR,
Name, SourceLocation(), Sema::LookupOrdinaryName);
361 R.suppressDiagnostics();
364 = NSD->getQualifiedNameAsString() +
"::" +
Name.getAsString();
370 clang::Scope
S(SemaR.TUScope, clang::Scope::DeclScope, SemaR.getDiagnostics());
371 S.setEntity(
const_cast<DeclContext*
>(DC));
372 Sema::ContextAndScopeRAII pushedDCAndS(SemaR,
const_cast<DeclContext*
>(DC), &
S);
375 llvm::SmallVector<NamedDecl*, 4> lookupResults;
376 for(LookupResult::iterator
I =
R.begin(),
E =
R.end();
I <
E; ++
I)
377 lookupResults.push_back(*
I);
378 UpdateWithNewDecls(DC,
Name, llvm::makeArrayRef(lookupResults.data(),
379 lookupResults.size()));
400 Sema &SemaR = m_Interpreter->getSema();
402 SourceLocation Loc = Tag->getLocation();
403 if (SemaR.getSourceManager().isInSystemHeader(Loc)) {
408 for (
auto ReRD: Tag->redecls()) {
410 if (ReRD->isBeingDefined())
415 if (RecordDecl* RD = dyn_cast<RecordDecl>(Tag)) {
416 ASTContext&
C = SemaR.getASTContext();
417 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
426 C.getTypeDeclType(RD),
432 Tag->setHasExternalLexicalStorage(
false);
448 Scope *
S,
const FileEntry* FE ) {
454 Sema &SemaR = m_Interpreter->getSema();
463 Sema::LookupNameKind kind =
R.getLookupKind();
464 if (!(kind == Sema::LookupTagName || kind == Sema::LookupOrdinaryName
465 || kind == Sema::LookupNestedNameSpecifierName
466 || kind == Sema::LookupNamespaceName))
471 bool lookupSuccess =
false;
473 Parser &
P =
const_cast<Parser &
>(m_Interpreter->getParser());
480 lookupSuccess = SemaR.LookupName(
R,
S);
482 if (
R.isSingleResult()) {
483 if (isa<clang::RecordDecl>(
R.getFoundDecl())) {
499 lookupSuccess = FE || SemaR.LookupName(
R,
S);
505 std::string incl =
"#include \"";
506 incl += FE->getName();
508 m_Interpreter->declare(incl);
544 if (
R.isForRedeclaration())
549 const Sema::LookupNameKind LookupKind =
R.getLookupKind();
550 if (LookupKind != Sema::LookupOrdinaryName)
554 Sema &SemaR = m_Interpreter->getSema();
555 ASTContext&
C = SemaR.getASTContext();
556 Preprocessor &PP = SemaR.getPreprocessor();
557 DeclContext *CurDC = SemaR.CurContext;
558 DeclarationName
Name =
R.getLookupName();
561 if(!CurDC || !CurDC->isFunctionOrMethod())
566 Preprocessor::CleanupAndRestoreCacheRAII cleanupPPRAII(PP);
573#if defined(R__MUST_REVISIT)
574#if R__MUST_REVISIT(6,2)
579 if (!fgSetOfSpecials) {
580 fgSetOfSpecials =
new std::set<TObject*>;
582 ((std::set<TObject*>*)fgSetOfSpecials)->insert((
TObject*)*obj);
586 VarDecl *VD = cast_or_null<VarDecl>(utils::Lookup::Named(&SemaR,
Name,
591 TObject **address = (
TObject**)m_Interpreter->getAddressOfGlobal(GD);
595 CStyleCastExpr *CStyleCast = cast<CStyleCastExpr>(VD->getInit());
596 Expr* newInit = utils::Synthesize::IntegerLiteralExpr(
C, (uint64_t)obj);
597 CStyleCast->setSubExpr(newInit);
604 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
608 QualType QT =
C.getPointerType(
C.getTypeDeclType(cast<TypeDecl>(TD)));
611 SourceLocation(),
Name.getAsIdentifierInfo(), QT,
615 = utils::Synthesize::CStyleCastPtrExpr(&SemaR, QT, (uint64_t)obj);
620 cling::CompilationOptions CO;
621 CO.DeclarationExtraction = 0;
622 CO.ValuePrinting = CompilationOptions::VPDisabled;
623 CO.ResultEvaluation = 0;
624 CO.DynamicScoping = 0;
626 CO.CodeGeneration = 1;
628 cling::Transaction*
T =
new cling::Transaction(CO, SemaR);
630 T->setState(cling::Transaction::kCompleted);
632 m_Interpreter->emitAllDecls(
T);
634 assert(VD &&
"Cannot be null!");
651 DeclarationName
Name =
R.getLookupName();
652 IdentifierInfo* II =
Name.getAsIdentifierInfo();
653 SourceLocation Loc =
R.getNameLoc();
654 Sema& SemaRef =
R.getSema();
655 ASTContext&
C = SemaRef.getASTContext();
656 DeclContext* TU =
C.getTranslationUnitDecl();
657 assert(TU &&
"Must not be null.");
660 clang::FunctionDecl* Wrapper =
nullptr;
663 DeclContext* DCCursor =
Cursor->getEntity();
666 Wrapper = dyn_cast_or_null<FunctionDecl>(DCCursor);
668 if (utils::Analyze::IsWrapper(Wrapper)) {
682 VarDecl* Result = VarDecl::Create(
C, TU, Loc, Loc, II,
C.DependentTy,
694 SourceRange invalidRange;
695 Wrapper->addAttr(
new (
C) AnnotateAttr(invalidRange,
C,
"__ResolveAtRuntime", 0));
699 Sema::ContextRAII pushedDC(SemaRef, TU);
710 if (
R.getLookupKind() != Sema::LookupOrdinaryName)
713 if (
R.isForRedeclaration())
719 const Transaction*
T = getInterpreter()->getCurrentTransaction();
722 const cling::CompilationOptions& COpts =
T->getCompilationOpts();
723 if (!COpts.DynamicScoping)
742 for (Scope* DepScope =
S; DepScope; DepScope = DepScope->getParent()) {
743 if (DeclContext* Ctx =
static_cast<DeclContext*
>(DepScope->getEntity())) {
744 if (!Ctx->isDependentContext())
746 if (isa<FunctionDecl>(Ctx))
764 if (
R.isForRedeclaration())
767 if (
R.getLookupKind() != Sema::LookupOrdinaryName)
770 if (!isa<FunctionDecl>(
R.getSema().CurContext))
775 DeclContext* ScopeDC =
S->getEntity();
776 if (!ScopeDC || !llvm::isa<FunctionDecl>(ScopeDC))
781 Scope* FnScope =
S->getFnParent();
784 auto FD = dyn_cast_or_null<FunctionDecl>(FnScope->getEntity());
785 if (!FD || !utils::Analyze::IsWrapper(FD))
789 Sema& SemaRef =
R.getSema();
790 ASTContext&
C = SemaRef.getASTContext();
791 DeclContext* DC = SemaRef.CurContext;
792 assert(DC &&
"Must not be null.");
795 Preprocessor& PP =
R.getSema().getPreprocessor();
798 if (PP.LookAhead(0).isNot(tok::equal)) {
804 DeclarationName
Name =
R.getLookupName();
805 IdentifierInfo* II =
Name.getAsIdentifierInfo();
806 SourceLocation Loc =
R.getNameLoc();
807 VarDecl* Result = VarDecl::Create(
C, DC, Loc, Loc, II,
808 C.getAutoType(QualType(),
809 clang::AutoTypeKeyword::Auto,
815 "Cannot create VarDecl");
822 SourceRange invalidRange;
823 Result->addAttr(
new (
C) AnnotateAttr(invalidRange,
C,
"__Auto", 0));
834 Sema& SemaR = m_Interpreter->getSema();
835 cling::Transaction TPrev((cling::CompilationOptions(), SemaR));
836 TPrev.append(SemaR.getASTContext().getTranslationUnitDecl());
875 if (
const RecordDecl* RD = dyn_cast<RecordDecl>(D)) {
888 llvm::StringRef canonicalName) {
893 llvm::StringRef canonicalName) {
#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)
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)
void DeclDeserialized(const clang::Decl *D) 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
void InclusionDirective(clang::SourceLocation, const clang::Token &, llvm::StringRef FileName, bool, clang::CharSourceRange, const clang::FileEntry *, llvm::StringRef, llvm::StringRef, const clang::Module *) override
Mother of all ROOT objects.
RooCmdArg Index(RooCategory &icat)
RooArgSet S(const RooAbsArg &v1)
void Init(TClassEdit::TInterpreterLookupHelper *helper)
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