Skip to content

Commit da71bd1

Browse files
committed
Fix crash in ValueExtractionSynthesizer::FindAndCacheRuntimeDecls() and report errors.
1 parent 4fe4969 commit da71bd1

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

lib/Interpreter/ValueExtractionSynthesizer.cpp

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include "cling/Interpreter/Interpreter.h"
1313
#include "cling/Interpreter/Transaction.h"
1414
#include "cling/Interpreter/Value.h"
15-
1615
#include "cling/Utils/AST.h"
16+
#include "cling/Utils/Output.h"
1717

1818
#include "clang/AST/ASTContext.h"
1919
#include "clang/AST/DeclGroup.h"
@@ -205,8 +205,8 @@ namespace {
205205
}
206206

207207
Expr* ValueExtractionSynthesizer::SynthesizeSVRInit(Expr* E) {
208-
if (!m_gClingVD)
209-
FindAndCacheRuntimeDecls();
208+
if (!m_gClingVD && !FindAndCacheRuntimeDecls())
209+
return nullptr;
210210

211211
// Build a reference to gCling
212212
ExprResult gClingDRE
@@ -408,34 +408,54 @@ namespace {
408408
return Call.get();
409409
}
410410

411-
void ValueExtractionSynthesizer::FindAndCacheRuntimeDecls() {
411+
static bool VSError(const char* err) {
412+
cling::errs() << "ValueExtractionSynthesizer error: " << err << ".\n";
413+
return false;
414+
}
415+
416+
bool ValueExtractionSynthesizer::FindAndCacheRuntimeDecls() {
412417
assert(!m_gClingVD && "Called multiple times!?");
413418
DeclContext* NSD = m_Context->getTranslationUnitDecl();
419+
clang::VarDecl* clingVD = nullptr;
414420
if (m_Sema->getLangOpts().CPlusPlus) {
415-
NSD = utils::Lookup::Namespace(m_Sema, "cling");
416-
NSD = utils::Lookup::Namespace(m_Sema, "runtime", NSD);
417-
m_gClingVD = cast<VarDecl>(utils::Lookup::Named(m_Sema, "gCling", NSD));
418-
NSD = utils::Lookup::Namespace(m_Sema, "internal",NSD);
421+
if (!(NSD = utils::Lookup::Namespace(m_Sema, "cling")))
422+
return VSError("cling namespace not defined");
423+
if (!(NSD = utils::Lookup::Namespace(m_Sema, "runtime", NSD)))
424+
return VSError("cling::runtime namespace not defined");
425+
if (!(clingVD = cast<VarDecl>(utils::Lookup::Named(m_Sema, "gCling",
426+
NSD))))
427+
return VSError("cling::runtime::gCling not defined");
428+
if (!NSD)
429+
return VSError("cling::runtime namespace not defined");
430+
431+
if (!(NSD = utils::Lookup::Namespace(m_Sema, "internal", NSD)))
432+
return VSError("cling::runtime::internal namespace not defined");
419433
}
420434
LookupResult R(*m_Sema, &m_Context->Idents.get("setValueNoAlloc"),
421435
SourceLocation(), Sema::LookupOrdinaryName,
422436
Sema::ForRedeclaration);
423437

424438
m_Sema->LookupQualifiedName(R, NSD);
425-
assert(!R.empty()
426-
&& "Cannot find cling::runtime::internal::setValueNoAlloc");
439+
if (R.empty())
440+
return VSError("Cannot find cling::runtime::internal::setValueNoAlloc");
427441

442+
const bool ADL = false;
428443
CXXScopeSpec CSS;
429-
m_UnresolvedNoAlloc
430-
= m_Sema->BuildDeclarationNameExpr(CSS, R, /*ADL*/ false).get();
444+
m_UnresolvedNoAlloc = m_Sema->BuildDeclarationNameExpr(CSS, R, ADL).get();
445+
if (!m_UnresolvedNoAlloc)
446+
return VSError("Could not build cling::runtime::internal"
447+
"::setValueNoAlloc");
431448

432449
R.clear();
433450
R.setLookupName(&m_Context->Idents.get("setValueWithAlloc"));
434451
m_Sema->LookupQualifiedName(R, NSD);
435-
assert(!R.empty()
436-
&& "Cannot find cling::runtime::internal::setValueWithAlloc");
437-
m_UnresolvedWithAlloc
438-
= m_Sema->BuildDeclarationNameExpr(CSS, R, /*ADL*/ false).get();
452+
if (R.empty())
453+
return VSError("Cannot find cling::runtime::internal::setValueWithAlloc");
454+
455+
m_UnresolvedWithAlloc = m_Sema->BuildDeclarationNameExpr(CSS, R, ADL).get();
456+
if (!m_UnresolvedWithAlloc)
457+
return VSError("Could not build cling::runtime::internal"
458+
"::setValueWithAlloc");
439459

440460
R.clear();
441461
R.setLookupName(&m_Context->Idents.get("copyArray"));
@@ -446,10 +466,15 @@ namespace {
446466
// parent interpreter, but it will fail, because this is a template function.
447467
// Once the import of template functions becomes supported by clang,
448468
// this check can be de-activated.
449-
if (!m_isChildInterpreter)
450-
assert(!R.empty() && "Cannot find cling::runtime::internal::copyArray");
451-
m_UnresolvedCopyArray
452-
= m_Sema->BuildDeclarationNameExpr(CSS, R, /*ADL*/ false).get();
469+
if (!m_isChildInterpreter && R.empty())
470+
return VSError("Cannot find cling::runtime::internal::copyArray");
471+
472+
m_UnresolvedCopyArray = m_Sema->BuildDeclarationNameExpr(CSS, R, ADL).get();
473+
if (!m_UnresolvedCopyArray)
474+
return VSError("Could not build cling::runtime::internal::copyArray");
475+
476+
m_gClingVD = clingVD;
477+
return true;
453478
}
454479
} // end namespace cling
455480

lib/Interpreter/ValueExtractionSynthesizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ namespace cling {
9090

9191
// Find and cache cling::runtime::gCling, setValueNoAlloc,
9292
// setValueWithAlloc on first request.
93-
void FindAndCacheRuntimeDecls();
93+
bool FindAndCacheRuntimeDecls();
9494
};
9595

9696
} // namespace cling

0 commit comments

Comments
 (0)