Skip to content

Commit 34a2f22

Browse files
committed
Report ValueExtractionSynthesizer errors with DiagnosticsEngine.
1 parent 81df81f commit 34a2f22

File tree

4 files changed

+59
-25
lines changed

4 files changed

+59
-25
lines changed

lib/Interpreter/ValueExtractionSynthesizer.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "cling/Interpreter/Transaction.h"
1414
#include "cling/Interpreter/Value.h"
1515
#include "cling/Utils/AST.h"
16-
#include "cling/Utils/Output.h"
1716

1817
#include "clang/AST/ASTContext.h"
1918
#include "clang/AST/DeclGroup.h"
@@ -204,7 +203,7 @@ namespace {
204203
}
205204

206205
Expr* ValueExtractionSynthesizer::SynthesizeSVRInit(Expr* E) {
207-
if (!m_gClingVD && !FindAndCacheRuntimeDecls())
206+
if (!m_gClingVD && !FindAndCacheRuntimeDecls(E))
208207
return nullptr;
209208

210209
// Build a reference to gCling
@@ -407,54 +406,54 @@ namespace {
407406
return Call.get();
408407
}
409408

410-
static bool VSError(const char* err) {
411-
cling::errs() << "ValueExtractionSynthesizer error: " << err << ".\n";
409+
static bool VSError(clang::Sema* Sema, clang::Expr* E, llvm::StringRef Err) {
410+
DiagnosticsEngine& Diags = Sema->getDiagnostics();
411+
Diags.Report(E->getLocStart(),
412+
Diags.getCustomDiagID(
413+
clang::DiagnosticsEngine::Level::Error,
414+
"ValueExtractionSynthesizer could not find: '%0'."))
415+
<< Err;
412416
return false;
413417
}
414418

415-
bool ValueExtractionSynthesizer::FindAndCacheRuntimeDecls() {
419+
bool ValueExtractionSynthesizer::FindAndCacheRuntimeDecls(clang::Expr* E) {
416420
assert(!m_gClingVD && "Called multiple times!?");
417421
DeclContext* NSD = m_Context->getTranslationUnitDecl();
418422
clang::VarDecl* clingVD = nullptr;
419423
if (m_Sema->getLangOpts().CPlusPlus) {
420424
if (!(NSD = utils::Lookup::Namespace(m_Sema, "cling")))
421-
return VSError("cling namespace not defined");
425+
return VSError(m_Sema, E, "cling namespace");
422426
if (!(NSD = utils::Lookup::Namespace(m_Sema, "runtime", NSD)))
423-
return VSError("cling::runtime namespace not defined");
424-
if (!(clingVD = cast<VarDecl>(utils::Lookup::Named(m_Sema, "gCling",
425-
NSD))))
426-
return VSError("cling::runtime::gCling not defined");
427-
if (!NSD)
428-
return VSError("cling::runtime namespace not defined");
429-
427+
return VSError(m_Sema, E, "cling::runtime namespace");
428+
if (!(clingVD = dyn_cast_or_null<VarDecl>(
429+
utils::Lookup::Named(m_Sema, "gCling", NSD))))
430+
return VSError(m_Sema, E, "cling::runtime::gCling");
430431
if (!(NSD = utils::Lookup::Namespace(m_Sema, "internal", NSD)))
431-
return VSError("cling::runtime::internal namespace not defined");
432+
return VSError(m_Sema, E, "cling::runtime::internal namespace");
432433
}
433434
LookupResult R(*m_Sema, &m_Context->Idents.get("setValueNoAlloc"),
434435
SourceLocation(), Sema::LookupOrdinaryName,
435436
Sema::ForRedeclaration);
436437

437438
m_Sema->LookupQualifiedName(R, NSD);
438439
if (R.empty())
439-
return VSError("Cannot find cling::runtime::internal::setValueNoAlloc");
440+
return VSError(m_Sema, E, "cling::runtime::internal::setValueNoAlloc");
440441

441442
const bool ADL = false;
442443
CXXScopeSpec CSS;
443444
m_UnresolvedNoAlloc = m_Sema->BuildDeclarationNameExpr(CSS, R, ADL).get();
444445
if (!m_UnresolvedNoAlloc)
445-
return VSError("Could not build cling::runtime::internal"
446-
"::setValueNoAlloc");
446+
return VSError(m_Sema, E, "cling::runtime::internal::setValueNoAlloc");
447447

448448
R.clear();
449449
R.setLookupName(&m_Context->Idents.get("setValueWithAlloc"));
450450
m_Sema->LookupQualifiedName(R, NSD);
451451
if (R.empty())
452-
return VSError("Cannot find cling::runtime::internal::setValueWithAlloc");
452+
return VSError(m_Sema, E, "cling::runtime::internal::setValueWithAlloc");
453453

454454
m_UnresolvedWithAlloc = m_Sema->BuildDeclarationNameExpr(CSS, R, ADL).get();
455455
if (!m_UnresolvedWithAlloc)
456-
return VSError("Could not build cling::runtime::internal"
457-
"::setValueWithAlloc");
456+
return VSError(m_Sema, E, "cling::runtime::internal::setValueWithAlloc");
458457

459458
R.clear();
460459
R.setLookupName(&m_Context->Idents.get("copyArray"));
@@ -466,11 +465,11 @@ namespace {
466465
// Once the import of template functions becomes supported by clang,
467466
// this check can be de-activated.
468467
if (!m_isChildInterpreter && R.empty())
469-
return VSError("Cannot find cling::runtime::internal::copyArray");
468+
return VSError(m_Sema, E, "cling::runtime::internal::copyArray");
470469

471470
m_UnresolvedCopyArray = m_Sema->BuildDeclarationNameExpr(CSS, R, ADL).get();
472471
if (!m_UnresolvedCopyArray)
473-
return VSError("Could not build cling::runtime::internal::copyArray");
472+
return VSError(m_Sema, E, "cling::runtime::internal::copyArray");
474473

475474
m_gClingVD = clingVD;
476475
return true;

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-
bool FindAndCacheRuntimeDecls();
93+
bool FindAndCacheRuntimeDecls(clang::Expr*);
9494
};
9595

9696
} // namespace cling

test/Driver/C.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@
1616
int printf(const char*,...);
1717
printf("CHECK 123 %p\n", gCling); // CHECK: CHECK 123
1818

19-
// expected-no-diagnostics
19+
12 // expected-error {{ValueExtractionSynthesizer could not find: 'cling::runtime::internal::setValueNoAlloc'.}}
20+
21+
32 // expected-error {{ValueExtractionSynthesizer could not find: 'cling::runtime::internal::setValueNoAlloc'.}}
22+
2023
.q

test/ErrorRecovery/ABI.C

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,43 @@
77
//------------------------------------------------------------------------------
88

99
// RUN: %cling -C -E -P %s | %cling -nostdinc++ -Xclang -verify 2>&1 | FileCheck %s
10-
// RUN: %cling -C -E -P -DCLING_NO_BUILTIN %s | %cling -nostdinc++ -nobuiltininc -Xclang -verify 2>&1 | FileCheck %s
10+
// RUN: %cling -C -E -P -DCLING_VALEXTRACT_ERR %s | %cling -nostdinc++ -nobuiltininc -Xclang -verify 2>&1 | FileCheck %s
11+
// RUN: %cling -C -E -P -DCLING_VALEXTRACT_ERR2 %s | %cling -nostdinc++ -nobuiltininc -Xclang -verify 2>&1 | FileCheck %s
12+
// RUN: %cling -C -E -P -DCLING_VALEXTRACT_ERR3 %s | %cling -nostdinc++ -nobuiltininc -Xclang -verify 2>&1 | FileCheck %s
13+
// RUN: %cling -C -E -P -DCLING_VALEXTRACT_ERR4 %s | %cling -nostdinc++ -nobuiltininc -Xclang -verify 2>&1 | FileCheck %s
1114

1215
// expected-error@input_line_1:1 {{'new' file not found}}
1316

1417
// CHECK: Warning in cling::IncrementalParser::CheckABICompatibility():
1518
// CHECK-NEXT: Possible C++ standard library mismatch, compiled with {{.*$}}
1619

20+
21+
#if defined(CLING_VALEXTRACT_ERR) || defined(CLING_VALEXTRACT_ERR2) || \
22+
defined(CLING_VALEXTRACT_ERR3) || defined(CLING_VALEXTRACT_ERR4)
23+
24+
struct Trigger {} Tr;
25+
26+
#ifdef CLING_VALEXTRACT_ERR
27+
Tr // expected-error@2 {{ValueExtractionSynthesizer could not find: 'cling namespace'.}}
28+
#endif
29+
30+
#ifdef CLING_VALEXTRACT_ERR2
31+
namespace cling {}
32+
Tr // expected-error@2 {{ValueExtractionSynthesizer could not find: 'cling::runtime namespace'.}}
33+
#endif
34+
35+
#ifdef CLING_VALEXTRACT_ERR3
36+
namespace cling { namespace runtime {} }
37+
Tr // expected-error@2 {{ValueExtractionSynthesizer could not find: 'cling::runtime::gCling'.}}
38+
#endif
39+
40+
#ifdef CLING_VALEXTRACT_ERR4
41+
namespace cling { namespace runtime { void* gCling; namespace internal { void* setValueWithAlloc; void* setValueNoAlloc; } } }
42+
Tr // expected-error@2 {{ValueExtractionSynthesizer could not find: 'cling::runtime::internal::copyArray'.}}
43+
// Make sure not to crash on subsequent attempts
44+
Tr // expected-error@2 {{ValueExtractionSynthesizer could not find: 'cling::runtime::internal::copyArray'.}}
45+
#endif
46+
47+
#endif
48+
1749
.q

0 commit comments

Comments
 (0)