From 083d9280ea17e6e64f4283108c441c33e9140f9d Mon Sep 17 00:00:00 2001
From: Vassil Vassilev <vasil.georgiev.vasilev@cern.ch>
Date: Thu, 18 Oct 2012 11:56:20 +0000
Subject: [PATCH] Add super efficient execute function. It doesn't do any fancy
 things with the declarations - it just compiles and runs given expression or
 statement.

git-svn-id: http://root.cern.ch/svn/root/trunk@46636 27541ba8-7e3a-0410-8455-c3a389f83636
---
 .../include/cling/Interpreter/Interpreter.h   | 12 +++++++
 .../cling/lib/Interpreter/Interpreter.cpp     | 32 ++++++++++++++++++-
 interpreter/cling/test/Interfaces/execute.C   |  8 +++++
 3 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100644 interpreter/cling/test/Interfaces/execute.C

diff --git a/interpreter/cling/include/cling/Interpreter/Interpreter.h b/interpreter/cling/include/cling/Interpreter/Interpreter.h
index f5f78e4d653..d4714a47a50 100644
--- a/interpreter/cling/include/cling/Interpreter/Interpreter.h
+++ b/interpreter/cling/include/cling/Interpreter/Interpreter.h
@@ -364,6 +364,18 @@ namespace cling {
     ///
     CompilationResult echo(const std::string& input, StoredValueRef* V = 0);
 
+    ///\brief Compiles input line and runs.
+    ///
+    /// The interface is the fastest way to compile and run a statement or
+    /// expression. It just wraps the input and runs the wrapper, without any
+    /// other "magic"
+    ///
+    /// @param[in] input - The input containing only expressions.
+    ///
+    ///\returns Whether the operation was fully successful.
+    ///
+    CompilationResult execute(const std::string& input);
+
     ///\brief Loads header file or shared library.
     ///
     ///\param [in] filename - The file to loaded.
diff --git a/interpreter/cling/lib/Interpreter/Interpreter.cpp b/interpreter/cling/lib/Interpreter/Interpreter.cpp
index 3e03fa1f95b..85211c8ee15 100644
--- a/interpreter/cling/lib/Interpreter/Interpreter.cpp
+++ b/interpreter/cling/lib/Interpreter/Interpreter.cpp
@@ -213,7 +213,7 @@ namespace cling {
       // Set up the gCling variable
       std::stringstream initializer;
       initializer << "gCling=(cling::Interpreter*)" << (uintptr_t)this << ";";
-      evaluate(initializer.str());
+      execute(initializer.str());
     }
     else {
       declare("#include \"cling/Interpreter/CValuePrinter.h\"");
@@ -444,6 +444,36 @@ namespace cling {
     return EvaluateInternal(input, CO, V);
   }
 
+  Interpreter::CompilationResult
+  Interpreter::execute(const std::string& input) {
+    CompilationOptions CO;
+    CO.DeclarationExtraction = 0;
+    CO.ValuePrinting = 0;
+    CO.ResultEvaluation = 0;
+    CO.DynamicScoping = 0;
+    CO.Debug = isPrintingAST();
+
+    DiagnosticsEngine& Diag = getCI()->getDiagnostics();
+    // Disable warnings which doesn't make sense when using the prompt
+    // This gets reset with the clang::Diagnostics().Reset()
+    Diag.setDiagnosticMapping(clang::diag::warn_unused_expr,
+                              clang::diag::MAP_IGNORE, SourceLocation());
+    Diag.setDiagnosticMapping(clang::diag::warn_unused_call,
+                              clang::diag::MAP_IGNORE, SourceLocation());
+    Diag.setDiagnosticMapping(clang::diag::warn_unused_comparison,
+                              clang::diag::MAP_IGNORE, SourceLocation());
+
+    // Wrap the expression
+    std::string WrapperName;
+    std::string Wrapper = input;
+    WrapInput(Wrapper, WrapperName);
+    if (m_IncrParser->Compile(Wrapper, CO) == IncrementalParser::kSuccess)
+      if (RunFunction(WrapperName, QualType()))
+        return Interpreter::kSuccess;
+
+    return Interpreter::kFailure;
+  }
+
   void Interpreter::WrapInput(std::string& input, std::string& fname) {
     fname = createUniqueWrapper();
     input.insert(0, "void " + fname + "() {\n ");
diff --git a/interpreter/cling/test/Interfaces/execute.C b/interpreter/cling/test/Interfaces/execute.C
new file mode 100644
index 00000000000..5377015e749
--- /dev/null
+++ b/interpreter/cling/test/Interfaces/execute.C
@@ -0,0 +1,8 @@
+// RUN: cat %s | %cling 2>&1 | FileCheck %s
+
+#include "cling/Interpreter/Interpreter.h"
+
+gCling->execute("1;");
+extern "C" int printf(const char* fmt, ...);
+gCling->execute("printf(\"%d\", printf(\"%d\",1));");
+// CHECK: 11
-- 
GitLab