Skip to content

Commit 3cc3be8

Browse files
committed
[clang-repl] Add host exception support check utility flag.
Add host exception support check utility flag. This is needed to not run tests that require exception support in few buildbots that lacks related symbols for some reason. Reviewed By: lhames Differential Revision: https://reviews.llvm.org/D129242
1 parent 72ea1a7 commit 3cc3be8

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// clang-format off
2+
// REQUIRES: host-supports-jit, host-supports-exception
3+
// UNSUPPORTED: system-aix
4+
// XFAIL: arm, arm64-apple, windows-msvc, windows-gnu
5+
// RUN: cat %s | clang-repl | FileCheck %s
6+
extern "C" int printf(const char *, ...);
7+
8+
int f() { throw "Simple exception"; return 0; }
9+
int checkException() { try { printf("Running f()\n"); f(); } catch (const char *e) { printf("%s\n", e); } return 0; }
10+
auto r1 = checkException();
11+
// CHECK: Running f()
12+
// CHECK-NEXT: Simple exception
13+
14+
%quit

clang/test/lit.cfg.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
if config.clang_examples:
7171
config.available_features.add('examples')
7272

73-
def have_host_jit_support():
73+
def have_host_jit_feature_support(feature_name):
7474
clang_repl_exe = lit.util.which('clang-repl', config.clang_tools_dir)
7575

7676
if not clang_repl_exe:
@@ -79,7 +79,7 @@ def have_host_jit_support():
7979

8080
try:
8181
clang_repl_cmd = subprocess.Popen(
82-
[clang_repl_exe, '--host-supports-jit'], stdout=subprocess.PIPE)
82+
[clang_repl_exe, '--host-supports-' + feature_name], stdout=subprocess.PIPE)
8383
except OSError:
8484
print('could not exec clang-repl')
8585
return False
@@ -89,9 +89,12 @@ def have_host_jit_support():
8989

9090
return 'true' in clang_repl_out
9191

92-
if have_host_jit_support():
92+
if have_host_jit_feature_support('jit'):
9393
config.available_features.add('host-supports-jit')
9494

95+
if have_host_jit_feature_support('exception'):
96+
config.available_features.add('host-supports-exception')
97+
9598
if config.clang_staticanalyzer:
9699
config.available_features.add('staticanalyzer')
97100
tools.append('clang-check')

clang/tools/clang-repl/ClangRepl.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ static llvm::cl::list<std::string>
2828
llvm::cl::CommaSeparated);
2929
static llvm::cl::opt<bool> OptHostSupportsJit("host-supports-jit",
3030
llvm::cl::Hidden);
31+
static llvm::cl::opt<bool> OptHostSupportsException("host-supports-exception",
32+
llvm::cl::Hidden);
3133
static llvm::cl::list<std::string> OptInputs(llvm::cl::Positional,
3234
llvm::cl::desc("[code to run]"));
3335

@@ -65,6 +67,42 @@ static int checkDiagErrors(const clang::CompilerInstance *CI) {
6567
return Errs ? EXIT_FAILURE : EXIT_SUCCESS;
6668
}
6769

70+
// Check if the host environment supports c++ exception handling
71+
// by querying the existence of symbol __cxa_throw.
72+
static bool checkExceptionSupport() {
73+
auto J = llvm::orc::LLJITBuilder().create();
74+
if (!J) {
75+
llvm::consumeError(J.takeError());
76+
return false;
77+
}
78+
79+
std::vector<const char *> Dummy;
80+
auto CI = clang::IncrementalCompilerBuilder::create(Dummy);
81+
if (!CI) {
82+
llvm::consumeError(CI.takeError());
83+
return false;
84+
}
85+
86+
auto Interp = clang::Interpreter::create(std::move(*CI));
87+
if (!Interp) {
88+
llvm::consumeError(Interp.takeError());
89+
return false;
90+
}
91+
92+
if (auto Err = (*Interp)->ParseAndExecute("")) {
93+
llvm::consumeError(std::move(Err));
94+
return false;
95+
}
96+
97+
auto Sym = (*Interp)->getSymbolAddress("__cxa_throw");
98+
if (!Sym) {
99+
llvm::consumeError(Sym.takeError());
100+
return false;
101+
}
102+
103+
return true;
104+
}
105+
68106
llvm::ExitOnError ExitOnErr;
69107
int main(int argc, const char **argv) {
70108
ExitOnErr.setBanner("clang-repl: ");
@@ -87,6 +125,14 @@ int main(int argc, const char **argv) {
87125
return 0;
88126
}
89127

128+
if (OptHostSupportsException) {
129+
if (checkExceptionSupport())
130+
llvm::outs() << "true\n";
131+
else
132+
llvm::outs() << "false\n";
133+
return 0;
134+
}
135+
90136
// FIXME: Investigate if we could use runToolOnCodeWithArgs from tooling. It
91137
// can replace the boilerplate code for creation of the compiler instance.
92138
auto CI = ExitOnErr(clang::IncrementalCompilerBuilder::create(ClangArgv));

0 commit comments

Comments
 (0)