Skip to content

Commit

Permalink
[clang-repl] Fix the process return code if diagnostics occurred. (#8…
Browse files Browse the repository at this point in the history
…9879)

Should fix the failure seen in the pre-merge infrastructure of #89804.
  • Loading branch information
vgvassilev committed Apr 25, 2024
1 parent 7a77b76 commit fd5f06e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
21 changes: 14 additions & 7 deletions clang/test/Interpreter/fail.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
// 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;"
// REQUIRES: host-supports-jit
// UNSUPPORTED: system-aix
// RUN: cat %s | not clang-repl | FileCheck %s
BOOM!
// clang-repl can be called from the prompt in non-interactive mode as a
// calculator in shell scripts, for example. In that case if there is an error
// we should set the exit code as failure.
// RUN: not clang-repl "int x = 10;" "int y=7; err;" "int y = 10;"

// In interactive (REPL) mode, we can have errors but we should exit with
// success because errors in the input code are part of the interactive use.
// RUN: cat %s | clang-repl | FileCheck %s

// However, interactive mode should fail when we specified -verify and there
// was a diagnostic mismatches. This will make the testsuite fail as intended.
// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify | FileCheck %s

BOOM! // expected-error {{intended to fail the -verify test}}
extern "C" int printf(const char *, ...);
int i = 42;
auto r1 = printf("i = %d\n", i);
Expand Down
17 changes: 7 additions & 10 deletions clang/tools/clang-repl/ClangRepl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,15 @@ int main(int argc, const char **argv) {
} else
Interp = ExitOnErr(clang::Interpreter::create(std::move(CI)));

bool HasError = false;

for (const std::string &input : OptInputs) {
if (auto Err = Interp->ParseAndExecute(input))
if (auto Err = Interp->ParseAndExecute(input)) {
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
HasError = true;
}
}

bool HasError = false;

if (OptInputs.empty()) {
llvm::LineEditor LE("clang-repl");
std::string Input;
Expand All @@ -241,18 +243,13 @@ int main(int argc, const char **argv) {
break;
}
if (Input == R"(%undo)") {
if (auto Err = Interp->Undo()) {
if (auto Err = Interp->Undo())
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
HasError = true;
}
} else if (Input.rfind("%lib ", 0) == 0) {
if (auto Err = Interp->LoadDynamicLibrary(Input.data() + 5)) {
if (auto Err = Interp->LoadDynamicLibrary(Input.data() + 5))
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
HasError = true;
}
} else if (auto Err = Interp->ParseAndExecute(Input)) {
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
HasError = true;
}

Input = "";
Expand Down

0 comments on commit fd5f06e

Please sign in to comment.