[clang] 9caee57 - [clang-repl] Fix incorrect return code

Jun Zhang via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 31 04:14:27 PDT 2022


Author: Jun Zhang
Date: 2022-07-31T19:07:05+08:00
New Revision: 9caee577ef0f97242a233174be8a980bc4d5446e

URL: https://github.com/llvm/llvm-project/commit/9caee577ef0f97242a233174be8a980bc4d5446e
DIFF: https://github.com/llvm/llvm-project/commit/9caee577ef0f97242a233174be8a980bc4d5446e.diff

LOG: [clang-repl] Fix incorrect return code

Without this patch, clang-repl incorrectly pass some tests when there's
error occured.

Signed-off-by: Jun Zhang <jun at junz.org>

Differential Revision: https://reviews.llvm.org/D130422

Added: 
    clang/test/Interpreter/fail.cpp

Modified: 
    clang/tools/clang-repl/ClangRepl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/test/Interpreter/fail.cpp b/clang/test/Interpreter/fail.cpp
new file mode 100644
index 0000000000000..1f02dc425ece2
--- /dev/null
+++ b/clang/test/Interpreter/fail.cpp
@@ -0,0 +1,17 @@
+// FIXME: There're some inconsistencies between interactive and non-interactive
+// modes. For example, when clang-repl runs in the interactive mode, issues an
+// error, and then successfully recovers if we decide it's a success then for
+// the non-interactive mode the exit code should be a failure.
+// RUN: clang-repl "int x = 10;" "int y=7; err;" "int y = 10;"
+// RUN: clang-repl "int i = 10;" 'extern "C" int printf(const char*,...);' \
+// RUN:            'auto r1 = printf("i = %d\n", i);' | FileCheck --check-prefix=CHECK-DRIVER %s
+// REQUIRES: host-supports-jit
+// UNSUPPORTED: system-aix
+// CHECK-DRIVER: i = 10
+// RUN: cat %s | not clang-repl | FileCheck %s
+BOOM!
+extern "C" int printf(const char *, ...);
+int i = 42;
+auto r1 = printf("i = %d\n", i);
+// CHECK: i = 42
+%quit

diff  --git a/clang/tools/clang-repl/ClangRepl.cpp b/clang/tools/clang-repl/ClangRepl.cpp
index d3253738c6da1..8f661a57e9906 100644
--- a/clang/tools/clang-repl/ClangRepl.cpp
+++ b/clang/tools/clang-repl/ClangRepl.cpp
@@ -50,7 +50,7 @@ static void LLVMErrorHandler(void *UserData, const char *Message,
 
 // 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(const clang::CompilerInstance *CI) {
+static int checkDiagErrors(const clang::CompilerInstance *CI, bool HasError) {
   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
@@ -62,7 +62,7 @@ static int checkDiagErrors(const clang::CompilerInstance *CI) {
     // The interpreter expects BeginSourceFile/EndSourceFiles to be balanced.
     Client->BeginSourceFile(CI->getLangOpts(), &CI->getPreprocessor());
   }
-  return Errs ? EXIT_FAILURE : EXIT_SUCCESS;
+  return (Errs || HasError) ? EXIT_FAILURE : EXIT_SUCCESS;
 }
 
 llvm::ExitOnError ExitOnErr;
@@ -107,6 +107,8 @@ int main(int argc, const char **argv) {
       llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
   }
 
+  bool HasError = false;
+
   if (OptInputs.empty()) {
     llvm::LineEditor LE("clang-repl");
     // FIXME: Add LE.setListCompleter
@@ -114,13 +116,17 @@ int main(int argc, const char **argv) {
       if (*Line == R"(%quit)")
         break;
       if (*Line == R"(%undo)") {
-        if (auto Err = Interp->Undo())
+        if (auto Err = Interp->Undo()) {
           llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+          HasError = true;
+        }
         continue;
       }
 
-      if (auto Err = Interp->ParseAndExecute(*Line))
+      if (auto Err = Interp->ParseAndExecute(*Line)) {
         llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+        HasError = true;
+      }
     }
   }
 
@@ -129,5 +135,5 @@ int main(int argc, const char **argv) {
   // later errors use the default handling behavior instead.
   llvm::remove_fatal_error_handler();
 
-  return checkDiagErrors(Interp->getCompilerInstance());
+  return checkDiagErrors(Interp->getCompilerInstance(), HasError);
 }


        


More information about the cfe-commits mailing list