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" 35 using namespace clang;
36 using namespace cling;
56 std::string &args, std::string &io, std::string &fname);
58 llvm::StringRef canonicalName);
60 llvm::StringRef canonicalName);
65 : InterpreterCallbacks(interp),
66 fLastLookupCtx(0), fROOTSpecialNamespace(0),
67 fFirstRun(true), fIsAutoloading(false), fIsAutoloadingRecursively(false),
68 fPPOldFlag(false), fPPChanged(false) {
70 m_Interpreter->declare(
"namespace __ROOT_SpecialObjects{}", &T);
79 llvm::StringRef FileName,
81 clang::CharSourceRange ,
82 const clang::FileEntry *FE,
85 const clang::Module *) {
99 std::string localString(FileName.str());
101 Sema &SemaR = m_Interpreter->getSema();
102 DeclarationName
Name = &SemaR.getASTContext().Idents.get(localString.c_str());
103 LookupResult RHeader(SemaR, Name, sLoc, Sema::LookupOrdinaryName);
112 llvm::SmallVectorImpl<char> &RecoveryPath) {
120 Preprocessor& PP = m_Interpreter->getCI()->getPreprocessor();
123 std::string filename(FileName.str().substr(0,FileName.str().find_last_of(
'"')));
124 std::string fname, mode, arguments, io;
127 if (mode.length() > 0) {
128 if (llvm::sys::fs::exists(fname)) {
130 std::string options =
"k";
131 if (mode.find(
"++") != std::string::npos) options +=
"f";
132 if (mode.find(
"g") != std::string::npos) options +=
"g";
133 if (mode.find(
"O") != std::string::npos) options +=
"O";
136 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
137 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
140 Token& Tok =
const_cast<Token&
>(P.getCurToken());
145 Tok.setKind(tok::semi);
157 Sema& SemaR = m_Interpreter->getSema();
158 ASTContext&
C = SemaR.getASTContext();
159 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, C.getTranslationUnitDecl(),
166 fPPOldFlag = PP.GetSuppressIncludeNotFoundError();
167 PP.SetSuppressIncludeNotFoundError(
true);
176 PP.SetSuppressIncludeNotFoundError(
fPPOldFlag);
187 DeclContext* DC = S->getEntity();
196 clang::DeclContext* MaybeTU = DC;
197 while (MaybeTU && !isa<TranslationUnitDecl>(MaybeTU)) {
199 MaybeTU = MaybeTU->getParent();
201 return isa<FunctionDecl>(DC);
214 if (m_Interpreter->getSema().getDiagnostics().hasErrorOccurred())
253 if (Name.getNameKind() != DeclarationName::Identifier)
return false;
260 NamespaceDecl* NSD = dyn_cast<NamespaceDecl>(
const_cast<DeclContext*
>(DC));
267 const DeclContext* primaryDC = NSD->getPrimaryContext();
271 Sema &SemaR = m_Interpreter->getSema();
272 LookupResult
R(SemaR, Name, SourceLocation(), Sema::LookupOrdinaryName);
273 R.suppressDiagnostics();
276 = NSD->getQualifiedNameAsString() +
"::" + Name.getAsString();
282 clang::Scope
S(SemaR.TUScope, clang::Scope::DeclScope, SemaR.getDiagnostics());
283 S.setEntity(const_cast<DeclContext*>(DC));
284 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, const_cast<DeclContext*>(DC), &
S);
287 llvm::SmallVector<NamedDecl*, 4> lookupResults;
288 for(LookupResult::iterator
I = R.begin(),
E = R.end();
I <
E; ++
I)
289 lookupResults.push_back(*
I);
290 UpdateWithNewDecls(DC, Name, llvm::makeArrayRef(lookupResults.data(),
291 lookupResults.size()));
301 Sema &SemaR = m_Interpreter->getSema();
303 SourceLocation Loc = Tag->getLocation();
304 if (SemaR.getSourceManager().isInSystemHeader(Loc)) {
309 for (
auto ReRD: Tag->redecls()) {
311 if (ReRD->isBeingDefined())
316 if (RecordDecl* RD = dyn_cast<RecordDecl>(Tag)) {
317 ASTContext&
C = SemaR.getASTContext();
318 Preprocessor &PP = SemaR.getPreprocessor();
319 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
320 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
321 Parser::ParserCurTokRestoreRAII savedCurToken(P);
322 Sema::DelayedInfoRAII semaInfoRAII(SemaR);
326 Token& Tok =
const_cast<Token&
>(P.getCurToken());
327 Tok.setKind(tok::semi);
333 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, C.getTranslationUnitDecl(),
341 C.getTypeDeclType(RD),
347 Tag->setHasExternalLexicalStorage(
false);
363 Scope *
S,
const FileEntry* FE ) {
364 Sema &SemaR = m_Interpreter->getSema();
373 Sema::LookupNameKind kind = R.getLookupKind();
374 if (!(kind == Sema::LookupTagName || kind == Sema::LookupOrdinaryName
375 || kind == Sema::LookupNestedNameSpecifierName
376 || kind == Sema::LookupNamespaceName))
381 bool lookupSuccess =
false;
382 if (getenv(
"ROOT_MODULES")) {
384 lookupSuccess = FE || SemaR.LookupName(R, S);
389 ASTContext&
C = SemaR.getASTContext();
390 Preprocessor &PP = SemaR.getPreprocessor();
391 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
392 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
393 Parser::ParserCurTokRestoreRAII savedCurToken(P);
396 Token& Tok =
const_cast<Token&
>(P.getCurToken());
397 Tok.setKind(tok::semi);
403 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, C.getTranslationUnitDecl(),
409 lookupSuccess = SemaR.LookupName(R, S);
411 if (R.isSingleResult()) {
412 if (isa<clang::RecordDecl>(R.getFoundDecl())) {
427 lookupSuccess = FE || SemaR.LookupName(R, S);
433 std::string incl =
"#include \"";
434 incl += FE->getName();
436 m_Interpreter->declare(incl);
468 if (R.isForRedeclaration())
473 const Sema::LookupNameKind LookupKind = R.getLookupKind();
474 if (LookupKind != Sema::LookupOrdinaryName)
478 Sema &SemaR = m_Interpreter->getSema();
479 ASTContext&
C = SemaR.getASTContext();
480 Preprocessor &PP = SemaR.getPreprocessor();
481 DeclContext *CurDC = SemaR.CurContext;
482 DeclarationName
Name = R.getLookupName();
485 if(!CurDC || !CurDC->isFunctionOrMethod())
490 Preprocessor::CleanupAndRestoreCacheRAII cleanupPPRAII(PP);
497 #if defined(R__MUST_REVISIT) 498 #if R__MUST_REVISIT(6,2) 503 if (!fgSetOfSpecials) {
504 fgSetOfSpecials =
new std::set<TObject*>;
506 ((std::set<TObject*>*)fgSetOfSpecials)->insert((
TObject*)*obj);
510 VarDecl *VD = cast_or_null<VarDecl>(utils::Lookup::Named(&SemaR, Name,
515 TObject **address = (
TObject**)m_Interpreter->getAddressOfGlobal(GD);
519 CStyleCastExpr *CStyleCast = cast<CStyleCastExpr>(VD->getInit());
520 Expr* newInit = utils::Synthesize::IntegerLiteralExpr(C, (uint64_t)obj);
521 CStyleCast->setSubExpr(newInit);
528 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
532 QualType QT = C.getPointerType(C.getTypeDeclType(cast<TypeDecl>(TD)));
535 SourceLocation(), Name.getAsIdentifierInfo(), QT,
539 = utils::Synthesize::CStyleCastPtrExpr(&SemaR, QT, (uint64_t)obj);
544 cling::CompilationOptions CO;
545 CO.DeclarationExtraction = 0;
546 CO.ValuePrinting = CompilationOptions::VPDisabled;
547 CO.ResultEvaluation = 0;
548 CO.DynamicScoping = 0;
550 CO.CodeGeneration = 1;
552 cling::Transaction*
T =
new cling::Transaction(CO, SemaR);
554 T->setState(cling::Transaction::kCompleted);
556 m_Interpreter->emitAllDecls(T);
558 assert(VD &&
"Cannot be null!");
570 DeclarationName
Name = R.getLookupName();
571 IdentifierInfo* II = Name.getAsIdentifierInfo();
572 SourceLocation Loc = R.getNameLoc();
573 Sema& SemaRef = R.getSema();
574 ASTContext&
C = SemaRef.getASTContext();
575 DeclContext* TU = C.getTranslationUnitDecl();
576 assert(TU &&
"Must not be null.");
579 clang::DeclContext* WrapperDC = S->getEntity();
580 clang::FunctionDecl* Wrapper =
nullptr;
582 if (!WrapperDC || WrapperDC == TU)
584 Wrapper = dyn_cast<FunctionDecl>(WrapperDC);
585 if (Wrapper && utils::Analyze::IsWrapper(Wrapper))
587 WrapperDC = WrapperDC->getParent();
590 VarDecl* Result = VarDecl::Create(C, TU, Loc, Loc, II, C.DependentTy,
602 SourceRange invalidRange;
603 Wrapper->addAttr(
new (C) AnnotateAttr(invalidRange, C,
"__ResolveAtRuntime", 0));
607 Sema::ContextRAII pushedDC(SemaRef, TU);
618 if (R.getLookupKind() != Sema::LookupOrdinaryName)
621 if (R.isForRedeclaration())
627 const Transaction*
T = getInterpreter()->getCurrentTransaction();
630 const cling::CompilationOptions& COpts = T->getCompilationOpts();
631 if (!COpts.DynamicScoping)
650 for (Scope* DepScope = S; DepScope; DepScope = DepScope->getParent()) {
651 if (DeclContext* Ctx = static_cast<DeclContext*>(DepScope->getEntity())) {
652 if (!Ctx->isDependentContext())
654 if (isa<FunctionDecl>(Ctx))
670 if (R.isForRedeclaration())
673 if (R.getLookupKind() != Sema::LookupOrdinaryName)
676 if (!isa<FunctionDecl>(R.getSema().CurContext))
679 Sema& SemaRef = R.getSema();
680 ASTContext&
C = SemaRef.getASTContext();
681 DeclContext* DC = SemaRef.CurContext;
682 assert(DC &&
"Must not be null.");
685 Preprocessor& PP = R.getSema().getPreprocessor();
694 DeclarationName
Name = R.getLookupName();
695 IdentifierInfo* II = Name.getAsIdentifierInfo();
696 SourceLocation Loc = R.getNameLoc();
697 VarDecl* Result = VarDecl::Create(C, DC, Loc, Loc, II,
698 C.getAutoType(QualType(),
699 clang::AutoTypeKeyword::Auto,
707 SourceRange invalidRange;
708 Result->addAttr(
new (C) AnnotateAttr(invalidRange, C,
"__Auto", 0));
722 Sema& SemaR = m_Interpreter->getSema();
723 cling::Transaction TPrev((cling::CompilationOptions(), SemaR));
724 TPrev.append(SemaR.getASTContext().getTranslationUnitDecl());
759 if (
const RecordDecl* RD = dyn_cast<RecordDecl>(D)) {
772 llvm::StringRef canonicalName) {
777 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)
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)
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
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)