Skip to content

Commit

Permalink
Add Interpreter::verifyDiagnostics and Interpreter::executeInvocation…
Browse files Browse the repository at this point in the history
… methods.

Moves the implementation details outside of cling.cpp.
  • Loading branch information
marsupial committed Jul 26, 2017
1 parent 8720965 commit 71f3ecc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 40 deletions.
17 changes: 17 additions & 0 deletions include/cling/Interpreter/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,23 @@ namespace cling {
///
void addModule(llvm::Module* module, int OptLevel);

///\brief Execute compiler invocation for output.
/// This should not be called in interactive sessions, rather when cling
/// is used to generate output: cling -E <in-file> -o <out-file>
/// Safe to call even if Interpreter::isValid() returns false.
///
///\returns Whether execution was successful.
///
CompilationResult executeInvocation() const;

///\brief Run the diagnostics verifier if cling was invoked with the -verify
/// flag. Of particular importance to the test-suite.
/// Safe to call even if Interpreter::isValid() returns false.
///
///\returns The number of errors reported or -1 if Interpreter was invalid.
///
int verifyDiagnostics() const;

void GenerateAutoloadingMap(llvm::StringRef inFile, llvm::StringRef outFile,
bool enableMacros = false, bool enableLogs = true);

Expand Down
1 change: 1 addition & 0 deletions lib/Interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
set(LIBS
clangDriver
clangFrontend
clangFrontendTool
clangParse
clangSema
clangAST
Expand Down
31 changes: 31 additions & 0 deletions lib/Interpreter/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/Utils.h"
#include "clang/Lex/ExternalPreprocessorSource.h"
#include "clang/FrontendTool/Utils.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Parse/Parser.h"
Expand Down Expand Up @@ -1591,6 +1592,36 @@ namespace cling {
T.setState(Transaction::kCommitted);
}

int Interpreter::verifyDiagnostics() const {
clang::CompilerInstance* CI = getCIOrNull();
if (!CI)
return -1;

unsigned Errs = CI->getDiagnostics().getClient()->getNumErrors();
if (CI->getDiagnosticOpts().VerifyDiagnostics) {
// If there was an error that came from the verifier we must return 1 as
// an exit code for the process. This will make the test fail as expected.
clang::DiagnosticConsumer* Client = CI->getDiagnostics().getClient();
Client->EndSourceFile();
Errs = Client->getNumErrors();

// The interpreter expects BeginSourceFile/EndSourceFiles to be balanced.
Client->BeginSourceFile(CI->getLangOpts(), &CI->getPreprocessor());
}
return Errs;
}

Interpreter::CompilationResult Interpreter::executeInvocation() const {
if (!m_Opts.CompilerOpts.HasOutput)
return kMoreInputExpected;

if (clang::CompilerInstance* CI = getCIOrNull()) {
if (clang::ExecuteCompilerInvocation(CI))
return kSuccess;
}
return kFailure;
}

namespace runtime {
namespace internal {
Value EvaluateDynamicExpression(Interpreter* interp, DynamicExprInfo* DEI,
Expand Down
2 changes: 0 additions & 2 deletions tools/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ set(LLVM_NO_DEAD_STRIP 1)
if(BUILD_SHARED_LIBS)
set(LIBS
LLVMSupport
clangFrontendTool
clingInterpreter
clingMetaProcessor
clingUserInterface
Expand All @@ -24,7 +23,6 @@ if(BUILD_SHARED_LIBS)
else()
set(LIBS
LLVMSupport
clangFrontendTool
clingUserInterface
)
add_cling_executable(cling
Expand Down
50 changes: 12 additions & 38 deletions tools/driver/cling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#include "cling/MetaProcessor/MetaProcessor.h"
#include "cling/UserInterface/UserInterface.h"

#include "clang/Basic/LangOptions.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/FrontendTool/Utils.h"

#include "llvm/Support/Signals.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/ManagedStatic.h"
Expand All @@ -28,29 +24,6 @@
#include <crtdbg.h>
#endif

// If we are running with -verify a reported has to be returned as unsuccess.
// This is relevant especially for the test suite.
static int checkDiagErrors(clang::CompilerInstance* CI, unsigned* OutErrs = 0) {

unsigned Errs = CI->getDiagnostics().getClient()->getNumErrors();

if (CI->getDiagnosticOpts().VerifyDiagnostics) {
// If there was an error that came from the verifier we must return 1 as
// an exit code for the process. This will make the test fail as expected.
clang::DiagnosticConsumer* Client = CI->getDiagnostics().getClient();
Client->EndSourceFile();
Errs = Client->getNumErrors();

// The interpreter expects BeginSourceFile/EndSourceFiles to be balanced.
Client->BeginSourceFile(CI->getLangOpts(), &CI->getPreprocessor());
}

if (OutErrs)
*OutErrs = Errs;

return Errs ? EXIT_FAILURE : EXIT_SUCCESS;
}


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

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

unsigned ErrsReported = 0;
if (clang::CompilerInstance* CI = Interp.getCIOrNull()) {
// If output requested and execution succeeded let the DiagnosticsEngine
// determine the result code
if (Opts.CompilerOpts.HasOutput && ExecuteCompilerInvocation(CI))
return checkDiagErrors(CI);
using namespace cling;
const Interpreter::CompilationResult ExecRes = Interp.executeInvocation();
if (ExecRes != Interpreter::kFailure) {
const int ErrsReported = Interp.verifyDiagnostics();

checkDiagErrors(CI, &ErrsReported);
}
// If output succeeded and diagnostics verified, we are done.
if (ExecRes == Interpreter::kSuccess && !ErrsReported)
return EXIT_SUCCESS;

// If no errors have been reported, try perror
if (ErrsReported == 0)
// FIXME: This info is barely useful
if (ErrsReported <= 0)
::fprintf(stderr, "Unknown error");
} else
::perror("Could not create Interpreter instance");

return EXIT_FAILURE;
Expand Down Expand Up @@ -138,5 +112,5 @@ int main( int argc, char **argv ) {
::fflush(stdout);
::fflush(stderr);

return checkDiagErrors(Interp.getCI());
return Interp.verifyDiagnostics() ? EXIT_FAILURE : EXIT_SUCCESS;
}
1 change: 1 addition & 0 deletions tools/libcling/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(SOURCES
set(LIBS
clangDriver
clangFrontend
clangFrontendTool
clangParse
clangSema
clangAST
Expand Down

0 comments on commit 71f3ecc

Please sign in to comment.