14 #include "cling/Interpreter/Interpreter.h" 15 #include "cling/Interpreter/InterpreterCallbacks.h" 16 #include "cling/Interpreter/Transaction.h" 17 #include "cling/Utils/AST.h" 19 #include "clang/AST/ASTConsumer.h" 20 #include "clang/AST/ASTContext.h" 21 #include "clang/AST/DeclBase.h" 22 #include "clang/AST/DeclTemplate.h" 23 #include "clang/AST/GlobalDecl.h" 24 #include "clang/Lex/Preprocessor.h" 25 #include "clang/Parse/Parser.h" 26 #include "clang/Sema/Lookup.h" 27 #include "clang/Sema/Scope.h" 29 #include "clang/Frontend/CompilerInstance.h" 30 #include "clang/Lex/PPCallbacks.h" 31 #include "llvm/Support/FileSystem.h" 36 using namespace clang;
37 using namespace cling;
58 std::string &args, std::string &io, std::string &fname);
60 llvm::StringRef canonicalName);
62 llvm::StringRef canonicalName);
67 : InterpreterCallbacks(interp),
68 fLastLookupCtx(0), fROOTSpecialNamespace(0),
69 fFirstRun(true), fIsAutoloading(false), fIsAutoloadingRecursively(false),
70 fPPOldFlag(false), fPPChanged(false) {
72 m_Interpreter->declare(
"namespace __ROOT_SpecialObjects{}", &T);
81 llvm::StringRef FileName,
83 clang::CharSourceRange ,
84 const clang::FileEntry *FE,
87 const clang::Module *) {
101 std::string localString(FileName.str());
103 Sema &SemaR = m_Interpreter->getSema();
104 DeclarationName
Name = &SemaR.getASTContext().Idents.get(localString.c_str());
105 LookupResult RHeader(SemaR, Name, sLoc, Sema::LookupOrdinaryName);
114 llvm::SmallVectorImpl<char> &RecoveryPath) {
122 Preprocessor& PP = m_Interpreter->getCI()->getPreprocessor();
125 std::string filename(FileName.str().substr(0,FileName.str().find_last_of(
'"')));
126 std::string fname, mode, arguments, io;
129 if (mode.length() > 0) {
130 if (llvm::sys::fs::exists(fname)) {
132 std::string options =
"k";
133 if (mode.find(
"++") != std::string::npos) options +=
"f";
134 if (mode.find(
"g") != std::string::npos) options +=
"g";
135 if (mode.find(
"O") != std::string::npos) options +=
"O";
138 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
139 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
142 Token& Tok =
const_cast<Token&
>(P.getCurToken());
147 Tok.setKind(tok::semi);
159 Sema& SemaR = m_Interpreter->getSema();
160 ASTContext&
C = SemaR.getASTContext();
161 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, C.getTranslationUnitDecl(),
168 fPPOldFlag = PP.GetSuppressIncludeNotFoundError();
169 PP.SetSuppressIncludeNotFoundError(
true);
178 PP.SetSuppressIncludeNotFoundError(
fPPOldFlag);
189 DeclContext* DC = S->getEntity();
198 clang::DeclContext* MaybeTU = DC;
199 while (MaybeTU && !isa<TranslationUnitDecl>(MaybeTU)) {
201 MaybeTU = MaybeTU->getParent();
203 return isa<FunctionDecl>(DC);
216 if (m_Interpreter->getSema().getDiagnostics().hasErrorOccurred())
255 if (Name.getNameKind() != DeclarationName::Identifier)
return false;
262 NamespaceDecl* NSD = dyn_cast<NamespaceDecl>(
const_cast<DeclContext*
>(DC));
269 const DeclContext* primaryDC = NSD->getPrimaryContext();
273 Sema &SemaR = m_Interpreter->getSema();
274 LookupResult
R(SemaR, Name, SourceLocation(), Sema::LookupOrdinaryName);
275 R.suppressDiagnostics();
278 = NSD->getQualifiedNameAsString() +
"::" + Name.getAsString();
284 clang::Scope
S(SemaR.TUScope, clang::Scope::DeclScope, SemaR.getDiagnostics());
285 S.setEntity(const_cast<DeclContext*>(DC));
286 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, const_cast<DeclContext*>(DC), &
S);
289 llvm::SmallVector<NamedDecl*, 4> lookupResults;
290 for(LookupResult::iterator
I = R.begin(),
E = R.end();
I <
E; ++
I)
291 lookupResults.push_back(*
I);
292 UpdateWithNewDecls(DC, Name, llvm::makeArrayRef(lookupResults.data(),
293 lookupResults.size()));
303 Sema &SemaR = m_Interpreter->getSema();
305 SourceLocation Loc = Tag->getLocation();
306 if (SemaR.getSourceManager().isInSystemHeader(Loc)) {
311 for (
auto ReRD: Tag->redecls()) {
313 if (ReRD->isBeingDefined())
318 if (RecordDecl* RD = dyn_cast<RecordDecl>(Tag)) {
319 ASTContext&
C = SemaR.getASTContext();
320 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
329 C.getTypeDeclType(RD),
335 Tag->setHasExternalLexicalStorage(
false);
351 Scope *
S,
const FileEntry* FE ) {
352 Sema &SemaR = m_Interpreter->getSema();
361 Sema::LookupNameKind kind = R.getLookupKind();
362 if (!(kind == Sema::LookupTagName || kind == Sema::LookupOrdinaryName
363 || kind == Sema::LookupNestedNameSpecifierName
364 || kind == Sema::LookupNamespaceName))
369 bool lookupSuccess =
false;
370 if (getenv(
"ROOT_MODULES")) {
372 lookupSuccess = FE || SemaR.LookupName(R, S);
377 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
384 lookupSuccess = SemaR.LookupName(R, S);
386 if (R.isSingleResult()) {
387 if (isa<clang::RecordDecl>(R.getFoundDecl())) {
403 lookupSuccess = FE || SemaR.LookupName(R, S);
409 std::string incl =
"#include \"";
410 incl += FE->getName();
412 m_Interpreter->declare(incl);
444 if (R.isForRedeclaration())
449 const Sema::LookupNameKind LookupKind = R.getLookupKind();
450 if (LookupKind != Sema::LookupOrdinaryName)
454 Sema &SemaR = m_Interpreter->getSema();
455 ASTContext&
C = SemaR.getASTContext();
456 Preprocessor &PP = SemaR.getPreprocessor();
457 DeclContext *CurDC = SemaR.CurContext;
458 DeclarationName
Name = R.getLookupName();
461 if(!CurDC || !CurDC->isFunctionOrMethod())
466 Preprocessor::CleanupAndRestoreCacheRAII cleanupPPRAII(PP);
473 #if defined(R__MUST_REVISIT) 474 #if R__MUST_REVISIT(6,2) 479 if (!fgSetOfSpecials) {
480 fgSetOfSpecials =
new std::set<TObject*>;
482 ((std::set<TObject*>*)fgSetOfSpecials)->insert((
TObject*)*obj);
486 VarDecl *VD = cast_or_null<VarDecl>(utils::Lookup::Named(&SemaR, Name,
491 TObject **address = (
TObject**)m_Interpreter->getAddressOfGlobal(GD);
495 CStyleCastExpr *CStyleCast = cast<CStyleCastExpr>(VD->getInit());
496 Expr* newInit = utils::Synthesize::IntegerLiteralExpr(C, (uint64_t)obj);
497 CStyleCast->setSubExpr(newInit);
504 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
508 QualType QT = C.getPointerType(C.getTypeDeclType(cast<TypeDecl>(TD)));
511 SourceLocation(), Name.getAsIdentifierInfo(), QT,
515 = utils::Synthesize::CStyleCastPtrExpr(&SemaR, QT, (uint64_t)obj);
520 cling::CompilationOptions CO;
521 CO.DeclarationExtraction = 0;
522 CO.ValuePrinting = CompilationOptions::VPDisabled;
523 CO.ResultEvaluation = 0;
524 CO.DynamicScoping = 0;
526 CO.CodeGeneration = 1;
528 cling::Transaction*
T =
new cling::Transaction(CO, SemaR);
530 T->setState(cling::Transaction::kCompleted);
532 m_Interpreter->emitAllDecls(T);
534 assert(VD &&
"Cannot be null!");
546 DeclarationName
Name = R.getLookupName();
547 IdentifierInfo* II = Name.getAsIdentifierInfo();
548 SourceLocation Loc = R.getNameLoc();
549 Sema& SemaRef = R.getSema();
550 ASTContext&
C = SemaRef.getASTContext();
551 DeclContext* TU = C.getTranslationUnitDecl();
552 assert(TU &&
"Must not be null.");
555 clang::FunctionDecl* Wrapper =
nullptr;
558 DeclContext* DCCursor = Cursor->getEntity();
561 Wrapper = dyn_cast_or_null<FunctionDecl>(DCCursor);
563 if (utils::Analyze::IsWrapper(Wrapper)) {
570 }
while ((Cursor = Cursor->getParent()));
572 VarDecl* Result = VarDecl::Create(C, TU, Loc, Loc, II, C.DependentTy,
584 SourceRange invalidRange;
585 Wrapper->addAttr(
new (C) AnnotateAttr(invalidRange, C,
"__ResolveAtRuntime", 0));
589 Sema::ContextRAII pushedDC(SemaRef, TU);
600 if (R.getLookupKind() != Sema::LookupOrdinaryName)
603 if (R.isForRedeclaration())
609 const Transaction*
T = getInterpreter()->getCurrentTransaction();
612 const cling::CompilationOptions& COpts = T->getCompilationOpts();
613 if (!COpts.DynamicScoping)
632 for (Scope* DepScope = S; DepScope; DepScope = DepScope->getParent()) {
633 if (DeclContext* Ctx = static_cast<DeclContext*>(DepScope->getEntity())) {
634 if (!Ctx->isDependentContext())
636 if (isa<FunctionDecl>(Ctx))
652 if (R.isForRedeclaration())
655 if (R.getLookupKind() != Sema::LookupOrdinaryName)
658 if (!isa<FunctionDecl>(R.getSema().CurContext))
661 Sema& SemaRef = R.getSema();
662 ASTContext&
C = SemaRef.getASTContext();
663 DeclContext* DC = SemaRef.CurContext;
664 assert(DC &&
"Must not be null.");
667 Preprocessor& PP = R.getSema().getPreprocessor();
676 DeclarationName
Name = R.getLookupName();
677 IdentifierInfo* II = Name.getAsIdentifierInfo();
678 SourceLocation Loc = R.getNameLoc();
679 VarDecl* Result = VarDecl::Create(C, DC, Loc, Loc, II,
680 C.getAutoType(QualType(),
681 clang::AutoTypeKeyword::Auto,
689 SourceRange invalidRange;
690 Result->addAttr(
new (C) AnnotateAttr(invalidRange, C,
"__Auto", 0));
704 Sema& SemaR = m_Interpreter->getSema();
705 cling::Transaction TPrev((cling::CompilationOptions(), SemaR));
706 TPrev.append(SemaR.getASTContext().getTranslationUnitDecl());
741 if (
const RecordDecl* RD = dyn_cast<RecordDecl>(D)) {
754 llvm::StringRef canonicalName) {
759 llvm::StringRef canonicalName) {
virtual void TransactionUnloaded(const cling::Transaction &T)
clang::NamespaceDecl * fROOTSpecialNamespace
bool equal(double d1, double d2, double stol=10000)
void TCling__GetNormalizedContext(const ROOT::TMetaUtils::TNormalizedCtxt *&)
int TCling__AutoLoadCallback(const char *className)
Decl * TCling__GetObjectDecl(TObject *obj)
int TCling__CompileMacro(const char *fileName, const char *options)
virtual void DeclDeserialized(const clang::Decl *D)
RAII used to store Parser, Sema, Preprocessor state for recursive parsing.
bool tryAutoParseInternal(llvm::StringRef Name, clang::LookupResult &R, clang::Scope *S, const clang::FileEntry *FE=0)
virtual bool FileNotFound(llvm::StringRef FileName, llvm::SmallVectorImpl< char > &RecoveryPath)
static bool topmostDCIsFunction(Scope *S)
void Init(TClassEdit::TInterpreterLookupHelper *helper)
bool tryFindROOTSpecialInternal(clang::LookupResult &R, clang::Scope *S)
int TCling__AutoParseCallback(const char *className)
clang::Preprocessor::CleanupAndRestoreCacheRAII fCleanupRAII
void TCling__PrintStackTrace()
Print a StackTrace!
virtual void LibraryLoaded(const void *dyLibHandle, llvm::StringRef canonicalName)
virtual void LibraryUnloaded(const void *dyLibHandle, llvm::StringRef canonicalName)
const char * TCling__GetClassSharedLibs(const char *className)
RooArgSet S(const RooAbsArg &v1)
bool tryResolveAtRuntimeInternal(clang::LookupResult &R, clang::Scope *S)
void TCling__SplitAclicMode(const char *fileName, std::string &mode, std::string &args, std::string &io, std::string &fname)
bool tryInjectImplicitAutoKeyword(clang::LookupResult &R, clang::Scope *S)
bool fIsAutoParsingSuspended
bool fIsAutoloadingRecursively
clang::Sema::ContextAndScopeRAII fPushedDCAndS
virtual void TransactionCommitted(const cling::Transaction &T)
bool IsAutoloadingEnabled()
void TCling__LibraryUnloadedRTTI(const void *dyLibHandle, llvm::StringRef canonicalName)
virtual void InclusionDirective(clang::SourceLocation, const clang::Token &, llvm::StringRef FileName, bool, clang::CharSourceRange, const clang::FileEntry *, llvm::StringRef, llvm::StringRef, const clang::Module *)
Print a TSeq at the prompt:
virtual void PrintStackTrace()
Mother of all ROOT objects.
typedef void((*Func_t)())
void TCling__UpdateListsOnUnloaded(const cling::Transaction &)
TClingCallbacks(cling::Interpreter *interp)
bool shouldResolveAtRuntime(clang::LookupResult &R, clang::Scope *S)
int TCling__IsAutoLoadNamespaceCandidate(const clang::NamespaceDecl *name)
void TCling__TransactionRollback(const cling::Transaction &)
void TCling__LibraryLoadedRTTI(const void *dyLibHandle, llvm::StringRef canonicalName)
TObject * TCling__GetObjectAddress(const char *Name, void *&LookupCtx)
void TCling__UpdateListsOnCommitted(const cling::Transaction &, Interpreter *)
virtual void TransactionRollback(const cling::Transaction &T)
virtual bool LookupObject(clang::LookupResult &R, clang::Scope *S)