Skip to content

Commit fd5f06e

Browse files
authored
[clang-repl] Fix the process return code if diagnostics occurred. (#89879)
Should fix the failure seen in the pre-merge infrastructure of #89804.
1 parent 7a77b76 commit fd5f06e

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

clang/test/Interpreter/fail.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
// FIXME: There're some inconsistencies between interactive and non-interactive
2-
// modes. For example, when clang-repl runs in the interactive mode, issues an
3-
// error, and then successfully recovers if we decide it's a success then for
4-
// the non-interactive mode the exit code should be a failure.
5-
// RUN: clang-repl "int x = 10;" "int y=7; err;" "int y = 10;"
61
// REQUIRES: host-supports-jit
72
// UNSUPPORTED: system-aix
8-
// RUN: cat %s | not clang-repl | FileCheck %s
9-
BOOM!
3+
// clang-repl can be called from the prompt in non-interactive mode as a
4+
// calculator in shell scripts, for example. In that case if there is an error
5+
// we should set the exit code as failure.
6+
// RUN: not clang-repl "int x = 10;" "int y=7; err;" "int y = 10;"
7+
8+
// In interactive (REPL) mode, we can have errors but we should exit with
9+
// success because errors in the input code are part of the interactive use.
10+
// RUN: cat %s | clang-repl | FileCheck %s
11+
12+
// However, interactive mode should fail when we specified -verify and there
13+
// was a diagnostic mismatches. This will make the testsuite fail as intended.
14+
// RUN: cat %s | not clang-repl -Xcc -Xclang -Xcc -verify | FileCheck %s
15+
16+
BOOM! // expected-error {{intended to fail the -verify test}}
1017
extern "C" int printf(const char *, ...);
1118
int i = 42;
1219
auto r1 = printf("i = %d\n", i);

clang/tools/clang-repl/ClangRepl.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,15 @@ int main(int argc, const char **argv) {
215215
} else
216216
Interp = ExitOnErr(clang::Interpreter::create(std::move(CI)));
217217

218+
bool HasError = false;
219+
218220
for (const std::string &input : OptInputs) {
219-
if (auto Err = Interp->ParseAndExecute(input))
221+
if (auto Err = Interp->ParseAndExecute(input)) {
220222
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
223+
HasError = true;
224+
}
221225
}
222226

223-
bool HasError = false;
224-
225227
if (OptInputs.empty()) {
226228
llvm::LineEditor LE("clang-repl");
227229
std::string Input;
@@ -241,18 +243,13 @@ int main(int argc, const char **argv) {
241243
break;
242244
}
243245
if (Input == R"(%undo)") {
244-
if (auto Err = Interp->Undo()) {
246+
if (auto Err = Interp->Undo())
245247
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
246-
HasError = true;
247-
}
248248
} else if (Input.rfind("%lib ", 0) == 0) {
249-
if (auto Err = Interp->LoadDynamicLibrary(Input.data() + 5)) {
249+
if (auto Err = Interp->LoadDynamicLibrary(Input.data() + 5))
250250
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
251-
HasError = true;
252-
}
253251
} else if (auto Err = Interp->ParseAndExecute(Input)) {
254252
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
255-
HasError = true;
256253
}
257254

258255
Input = "";

0 commit comments

Comments
 (0)