Skip to content

Commit 4ea44eb

Browse files
authored
[WebAssembly] Fix EH feature flags when compiling multiple files (#124374)
#124042 caused a problem that when invoking `clang` with multiple files, the static `HasRun` variables were set when processing the first file so the appropriate feature flags were not added from the second file. This fixes the problem by making those `HasRun` variables just normal variables within the enclosing function.
1 parent 07ed818 commit 4ea44eb

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

Diff for: clang/lib/Driver/ToolChains/WebAssembly.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,15 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
344344
}
345345
}
346346

347+
bool HasBannedIncompatibleOptionsForWasmEHSjLj = false;
348+
bool HasEnabledFeaturesForWasmEHSjLj = false;
349+
347350
// Bans incompatible options for Wasm EH / SjLj. We don't allow using
348351
// different modes for EH and SjLj.
349352
auto BanIncompatibleOptionsForWasmEHSjLj = [&](StringRef CurOption) {
350-
static bool HasRun = false;
351-
if (HasRun)
353+
if (HasBannedIncompatibleOptionsForWasmEHSjLj)
352354
return;
355+
HasBannedIncompatibleOptionsForWasmEHSjLj = true;
353356
if (DriverArgs.hasFlag(options::OPT_mno_exception_handing,
354357
options::OPT_mexception_handing, false))
355358
getDriver().Diag(diag::err_drv_argument_not_allowed_with)
@@ -373,14 +376,13 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
373376
<< CurOption << Option;
374377
}
375378
}
376-
HasRun = true;
377379
};
378380

379381
// Enable necessary features for Wasm EH / SjLj in the backend.
380382
auto EnableFeaturesForWasmEHSjLj = [&]() {
381-
static bool HasRun = false;
382-
if (HasRun)
383+
if (HasEnabledFeaturesForWasmEHSjLj)
383384
return;
385+
HasEnabledFeaturesForWasmEHSjLj = true;
384386
CC1Args.push_back("-target-feature");
385387
CC1Args.push_back("+exception-handling");
386388
// The standardized Wasm EH spec requires multivalue and reference-types.
@@ -390,7 +392,6 @@ void WebAssembly::addClangTargetOptions(const ArgList &DriverArgs,
390392
CC1Args.push_back("+reference-types");
391393
// Backend needs '-exception-model=wasm' to use Wasm EH instructions
392394
CC1Args.push_back("-exception-model=wasm");
393-
HasRun = true;
394395
};
395396

396397
if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions)) {

Diff for: clang/test/Driver/wasm-toolchain.c

+8
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,14 @@
224224
// RUN: | FileCheck -check-prefix=WASM_LEGACY_EH_NO_EH %s
225225
// WASM_LEGACY_EH_NO_EH: invalid argument '-wasm-use-legacy-eh' not allowed with '-mno-exception-handling'
226226

227+
// When invoking clang with multiple files in a single command line, target
228+
// feature flags should be equally added to the multiple clang-cc1 command lines
229+
// RUN: %clang -### --target=wasm32-unknown-unknown \
230+
// RUN: --sysroot=/foo %s %s -mllvm -wasm-enable-sjlj 2>&1 \
231+
// RUN: | FileCheck -check-prefix=WASM_SJLJ_MULTI_FILES %s
232+
// WASM_SJLJ_MULTI_FILES: "-cc1" {{.*}} "-target-feature" "+exception-handling" "-target-feature" "+multivalue" "-target-feature" "+reference-types" "-exception-model=wasm"
233+
// WASM_SJLJ_MULTI_FILES: "-cc1" {{.*}} "-target-feature" "+exception-handling" "-target-feature" "+multivalue" "-target-feature" "+reference-types" "-exception-model=wasm"
234+
227235
// RUN: %clang -### %s -fsanitize=address --target=wasm32-unknown-emscripten 2>&1 | FileCheck -check-prefix=CHECK-ASAN-EMSCRIPTEN %s
228236
// CHECK-ASAN-EMSCRIPTEN: "-fsanitize=address"
229237
// CHECK-ASAN-EMSCRIPTEN: "-fsanitize-address-globals-dead-stripping"

0 commit comments

Comments
 (0)