Skip to content

Commit 71f3ecc

Browse files
committed
Add Interpreter::verifyDiagnostics and Interpreter::executeInvocation methods.
Moves the implementation details outside of cling.cpp.
1 parent 8720965 commit 71f3ecc

File tree

6 files changed

+62
-40
lines changed

6 files changed

+62
-40
lines changed

include/cling/Interpreter/Interpreter.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,23 @@ namespace cling {
768768
///
769769
void addModule(llvm::Module* module, int OptLevel);
770770

771+
///\brief Execute compiler invocation for output.
772+
/// This should not be called in interactive sessions, rather when cling
773+
/// is used to generate output: cling -E <in-file> -o <out-file>
774+
/// Safe to call even if Interpreter::isValid() returns false.
775+
///
776+
///\returns Whether execution was successful.
777+
///
778+
CompilationResult executeInvocation() const;
779+
780+
///\brief Run the diagnostics verifier if cling was invoked with the -verify
781+
/// flag. Of particular importance to the test-suite.
782+
/// Safe to call even if Interpreter::isValid() returns false.
783+
///
784+
///\returns The number of errors reported or -1 if Interpreter was invalid.
785+
///
786+
int verifyDiagnostics() const;
787+
771788
void GenerateAutoloadingMap(llvm::StringRef inFile, llvm::StringRef outFile,
772789
bool enableMacros = false, bool enableLogs = true);
773790

lib/Interpreter/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
set(LIBS
1010
clangDriver
1111
clangFrontend
12+
clangFrontendTool
1213
clangParse
1314
clangSema
1415
clangAST

lib/Interpreter/Interpreter.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "clang/Frontend/CompilerInstance.h"
4747
#include "clang/Frontend/Utils.h"
4848
#include "clang/Lex/ExternalPreprocessorSource.h"
49+
#include "clang/FrontendTool/Utils.h"
4950
#include "clang/Lex/HeaderSearch.h"
5051
#include "clang/Lex/Preprocessor.h"
5152
#include "clang/Parse/Parser.h"
@@ -1591,6 +1592,36 @@ namespace cling {
15911592
T.setState(Transaction::kCommitted);
15921593
}
15931594

1595+
int Interpreter::verifyDiagnostics() const {
1596+
clang::CompilerInstance* CI = getCIOrNull();
1597+
if (!CI)
1598+
return -1;
1599+
1600+
unsigned Errs = CI->getDiagnostics().getClient()->getNumErrors();
1601+
if (CI->getDiagnosticOpts().VerifyDiagnostics) {
1602+
// If there was an error that came from the verifier we must return 1 as
1603+
// an exit code for the process. This will make the test fail as expected.
1604+
clang::DiagnosticConsumer* Client = CI->getDiagnostics().getClient();
1605+
Client->EndSourceFile();
1606+
Errs = Client->getNumErrors();
1607+
1608+
// The interpreter expects BeginSourceFile/EndSourceFiles to be balanced.
1609+
Client->BeginSourceFile(CI->getLangOpts(), &CI->getPreprocessor());
1610+
}
1611+
return Errs;
1612+
}
1613+
1614+
Interpreter::CompilationResult Interpreter::executeInvocation() const {
1615+
if (!m_Opts.CompilerOpts.HasOutput)
1616+
return kMoreInputExpected;
1617+
1618+
if (clang::CompilerInstance* CI = getCIOrNull()) {
1619+
if (clang::ExecuteCompilerInvocation(CI))
1620+
return kSuccess;
1621+
}
1622+
return kFailure;
1623+
}
1624+
15941625
namespace runtime {
15951626
namespace internal {
15961627
Value EvaluateDynamicExpression(Interpreter* interp, DynamicExprInfo* DEI,

tools/driver/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ set(LLVM_NO_DEAD_STRIP 1)
1212
if(BUILD_SHARED_LIBS)
1313
set(LIBS
1414
LLVMSupport
15-
clangFrontendTool
1615
clingInterpreter
1716
clingMetaProcessor
1817
clingUserInterface
@@ -24,7 +23,6 @@ if(BUILD_SHARED_LIBS)
2423
else()
2524
set(LIBS
2625
LLVMSupport
27-
clangFrontendTool
2826
clingUserInterface
2927
)
3028
add_cling_executable(cling

tools/driver/cling.cpp

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include "cling/MetaProcessor/MetaProcessor.h"
1212
#include "cling/UserInterface/UserInterface.h"
1313

14-
#include "clang/Basic/LangOptions.h"
15-
#include "clang/Frontend/CompilerInstance.h"
16-
#include "clang/FrontendTool/Utils.h"
17-
1814
#include "llvm/Support/Signals.h"
1915
#include "llvm/Support/PrettyStackTrace.h"
2016
#include "llvm/Support/ManagedStatic.h"
@@ -28,29 +24,6 @@
2824
#include <crtdbg.h>
2925
#endif
3026

31-
// If we are running with -verify a reported has to be returned as unsuccess.
32-
// This is relevant especially for the test suite.
33-
static int checkDiagErrors(clang::CompilerInstance* CI, unsigned* OutErrs = 0) {
34-
35-
unsigned Errs = CI->getDiagnostics().getClient()->getNumErrors();
36-
37-
if (CI->getDiagnosticOpts().VerifyDiagnostics) {
38-
// If there was an error that came from the verifier we must return 1 as
39-
// an exit code for the process. This will make the test fail as expected.
40-
clang::DiagnosticConsumer* Client = CI->getDiagnostics().getClient();
41-
Client->EndSourceFile();
42-
Errs = Client->getNumErrors();
43-
44-
// The interpreter expects BeginSourceFile/EndSourceFiles to be balanced.
45-
Client->BeginSourceFile(CI->getLangOpts(), &CI->getPreprocessor());
46-
}
47-
48-
if (OutErrs)
49-
*OutErrs = Errs;
50-
51-
return Errs ? EXIT_FAILURE : EXIT_SUCCESS;
52-
}
53-
5427

5528
int main( int argc, char **argv ) {
5629

@@ -83,18 +56,19 @@ int main( int argc, char **argv ) {
8356
if (Opts.Help || Opts.ShowVersion)
8457
return EXIT_SUCCESS;
8558

86-
unsigned ErrsReported = 0;
87-
if (clang::CompilerInstance* CI = Interp.getCIOrNull()) {
88-
// If output requested and execution succeeded let the DiagnosticsEngine
89-
// determine the result code
90-
if (Opts.CompilerOpts.HasOutput && ExecuteCompilerInvocation(CI))
91-
return checkDiagErrors(CI);
59+
using namespace cling;
60+
const Interpreter::CompilationResult ExecRes = Interp.executeInvocation();
61+
if (ExecRes != Interpreter::kFailure) {
62+
const int ErrsReported = Interp.verifyDiagnostics();
9263

93-
checkDiagErrors(CI, &ErrsReported);
94-
}
64+
// If output succeeded and diagnostics verified, we are done.
65+
if (ExecRes == Interpreter::kSuccess && !ErrsReported)
66+
return EXIT_SUCCESS;
9567

96-
// If no errors have been reported, try perror
97-
if (ErrsReported == 0)
68+
// FIXME: This info is barely useful
69+
if (ErrsReported <= 0)
70+
::fprintf(stderr, "Unknown error");
71+
} else
9872
::perror("Could not create Interpreter instance");
9973

10074
return EXIT_FAILURE;
@@ -138,5 +112,5 @@ int main( int argc, char **argv ) {
138112
::fflush(stdout);
139113
::fflush(stderr);
140114

141-
return checkDiagErrors(Interp.getCI());
115+
return Interp.verifyDiagnostics() ? EXIT_FAILURE : EXIT_SUCCESS;
142116
}

tools/libcling/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set(SOURCES
1313
set(LIBS
1414
clangDriver
1515
clangFrontend
16+
clangFrontendTool
1617
clangParse
1718
clangSema
1819
clangAST

0 commit comments

Comments
 (0)