Skip to content

Commit d7be913

Browse files
committed
[BoundsSafety] Re-introduce -funique-traps as a legacy alias of -fbounds-safety-unique-traps
In rdar://158088410 (#11455) the `-funique-traps` flag was removed in favor of a different implementation controlled by the `-fbounds-safety-unique-traps` flag. It turns out the `-funique-traps` flag is being used by an adopter of `-fbounds-safety`. To unbreak adopters of the flag this patch reintroduces the old flag (and its negation) as an alias of `-fbounds-safety-unique-traps` (`-fno-bounds-safety-unique-traps`) along with a diagnostic warning that `-funique-traps` (`-fno-unique-traps`) is deprecated. This patch doesn't use the `Alias<>` mixin in the `clang/Driver/Options.td` so "technically" at the implementation level `-funique-traps` isn't an alias of `-fbounds-safety-unique-traps`. This is done so that is possible distinguish use of the legacy flag from the new flag so that it is possible emit a deprecation warning. However, from the user's perspective `-funique-traps` and `-fbounds-safety-unique-traps` are aliases. We should remove `-funique-traps` eventually and that is tracked by rdar://162215869. rdar://162204734 (cherry picked from commit 38b7776)
1 parent 01cd1cd commit d7be913

File tree

4 files changed

+109
-4
lines changed

4 files changed

+109
-4
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,14 +2025,26 @@ def fno_bounds_safety_bringup_missing_checks : Flag<["-"], "fno-bounds-safety-br
20252025

20262026
defm bounds_safety_unique_traps : BoolFOption<"bounds-safety-unique-traps",
20272027
CodeGenOpts<"BoundsSafetyUniqueTraps">, DefaultFalse,
2028-
PosFlag<SetTrue, [], [CC1Option],
2028+
PosFlag<SetTrue, [], [ClangOption, CC1Option],
20292029
"Make trap instructions unique by preventing traps from being merged by the"
20302030
" optimizer. This is useful for preserving the debug information on traps "
20312031
"in optimized code. This makes it easier to debug programs at the cost of "
20322032
"increased code size">,
2033-
NegFlag<SetFalse, [], [CC1Option],
2033+
NegFlag<SetFalse, [], [ClangOption, CC1Option],
20342034
"Don't try to make trap instructions unique. This allows trap instructions "
20352035
"to be merged by the optimizer.">>;
2036+
2037+
// Legacy aliases
2038+
// TODO(dliew): Remove these (rdar://162215869).
2039+
// This isn't declared using `Alias<fbounds_safety_unique_traps>` because that
2040+
// prevents identifying that this flag as distinct from
2041+
// `fbounds_safety_unique_traps`.
2042+
defm bounds_safety_legacy_unique_traps : BoolOptionWithoutMarshalling<"f", "unique-traps",
2043+
PosFlag<SetTrue, [], [ClangOption],
2044+
"legacy alias for -fbounds-safety-unique-traps">,
2045+
NegFlag<SetFalse, [], [ClangOption],
2046+
"legacy alias for -fno-bounds-safety-unique-traps">>;
2047+
20362048
// TO_UPSTREAM(BoundsSafety) OFF
20372049

20382050
defm lifetime_safety : BoolFOption<

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7099,8 +7099,43 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
70997099
<< "-ftrap-function-returns" << "-ftrap-function=";
71007100
}
71017101

7102-
Args.addOptInFlag(CmdArgs, options::OPT_fbounds_safety_unique_traps,
7103-
options::OPT_fno_bounds_safety_unique_traps);
7102+
// Diagnose uses of legacy `-funique-traps` flags.
7103+
// TODO(dliew): Remove this once all uses of the legacy flag are removed
7104+
// (rdar://162215869).
7105+
if (Arg *A =
7106+
Args.getLastArg(options::OPT_fbounds_safety_legacy_unique_traps,
7107+
options::OPT_fno_bounds_safety_legacy_unique_traps)) {
7108+
7109+
StringRef ReplacementArg;
7110+
if (A->getOption().matches(options::OPT_fbounds_safety_legacy_unique_traps))
7111+
ReplacementArg = getDriverOptTable().getOptionName(
7112+
options::OPT_fbounds_safety_unique_traps);
7113+
else
7114+
ReplacementArg = getDriverOptTable().getOptionName(
7115+
options::OPT_fno_bounds_safety_unique_traps);
7116+
D.Diag(diag::warn_drv_deprecated_arg)
7117+
<< A->getSpelling() << 1 << ReplacementArg;
7118+
}
7119+
7120+
// Pass the flag to enable unique traps if necessary. This handles both the
7121+
// legacy and new flag name for unique traps such that the old flags are
7122+
// treated as aliases of the new flags.
7123+
// TODO(dliew): Remove support for the legacy flag (rdar://162215869).
7124+
if (Arg *A =
7125+
Args.getLastArg(options::OPT_fbounds_safety_unique_traps,
7126+
options::OPT_fno_bounds_safety_unique_traps,
7127+
options::OPT_fbounds_safety_legacy_unique_traps,
7128+
options::OPT_fno_bounds_safety_legacy_unique_traps)) {
7129+
7130+
// Note we only pass the flag for enabling unique traps to cc1. It isn't
7131+
// necessary to pass the flag to disable unique traps because it is cc1's
7132+
// default behavior.
7133+
if (A->getOption().matches(options::OPT_fbounds_safety_unique_traps) ||
7134+
A->getOption().matches(
7135+
options::OPT_fbounds_safety_legacy_unique_traps)) {
7136+
CmdArgs.push_back("-fbounds-safety-unique-traps");
7137+
}
7138+
}
71047139
/* TO_UPSTREAM(BoundsSafety) OFF*/
71057140

71067141
// Handle -f[no-]wrapv and -f[no-]strict-overflow, which are used by both
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Simple use of legacy flag
2+
// RUN: %clang -fbounds-safety -funique-traps -### %s 2>&1 | FileCheck --check-prefix=POS %s
3+
// RUN: %clang -fbounds-safety -fno-unique-traps -### %s 2>&1 | FileCheck --check-prefix=NEG %s
4+
5+
// Mix legacy flag with new flag
6+
// RUN: %clang -fbounds-safety -funique-traps -fbounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=POS %s
7+
// RUN: %clang -fbounds-safety -fno-unique-traps -fbounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=NEG %s
8+
9+
// Last legacy flag wins for warning
10+
// RUN: %clang -fbounds-safety -funique-traps -fno-unique-traps -funique-traps -### %s 2>&1 | FileCheck --check-prefix=POS %s
11+
// RUN: %clang -fbounds-safety -funique-traps -fno-unique-traps -### %s 2>&1 | FileCheck --check-prefix=NEG %s
12+
13+
// No warning
14+
// RUN: %clang -fbounds-safety -fbounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=NONE %s
15+
// RUN: %clang -fbounds-safety -fno-bounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=NONE %s
16+
// RUN: %clang -fbounds-safety -### %s 2>&1 | FileCheck --check-prefix=NONE %s
17+
18+
// POS: warning: argument '-funique-traps' is deprecated, use 'fbounds-safety-unique-traps' instead [-Wdeprecated]
19+
// NEG: warning: argument '-fno-unique-traps' is deprecated, use 'fno-bounds-safety-unique-traps' instead [-Wdeprecated]
20+
// NONE-NOT: warning: argument '-f{{(no-)?}}unique-traps' is deprecated
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %clang -fbounds-safety -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
2+
3+
// Simple use of new flag
4+
// RUN: %clang -fbounds-safety -fbounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=ENABLED %s
5+
// RUN: %clang -fbounds-safety -fno-bounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
6+
7+
// New flag used multiple times
8+
// RUN: %clang -fbounds-safety -fbounds-safety-unique-traps -fno-bounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
9+
// RUN: %clang -fbounds-safety -fno-bounds-safety-unique-traps -fbounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=ENABLED %s
10+
// RUN: %clang -fbounds-safety -fbounds-safety-unique-traps -fno-bounds-safety-unique-traps -fbounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=ENABLED %s
11+
// RUN: %clang -fbounds-safety -fno-bounds-safety-unique-traps -fbounds-safety-unique-traps -fno-bounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
12+
13+
// Simple use of legacy flag
14+
// RUN: %clang -fbounds-safety -fbounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=ENABLED %s
15+
// RUN: %clang -fbounds-safety -fno-unique-traps -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
16+
17+
// Legacy flag used multiple times
18+
// RUN: %clang -fbounds-safety -funique-traps -fno-unique-traps -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
19+
// RUN: %clang -fbounds-safety -fno-unique-traps -funique-traps -### %s 2>&1 | FileCheck --check-prefix=ENABLED %s
20+
// RUN: %clang -fbounds-safety -funique-traps -fno-unique-traps -funique-traps -### %s 2>&1 | FileCheck --check-prefix=ENABLED %s
21+
// RUN: %clang -fbounds-safety -fno-unique-traps -funique-traps -fno-unique-traps -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
22+
23+
// Mixed use of legacy and new flag
24+
// RUN: %clang -fbounds-safety -funique-traps -fno-bounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
25+
// RUN: %clang -fbounds-safety -funique-traps -fbounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=ENABLED %s
26+
27+
// RUN: %clang -fbounds-safety -fno-unique-traps -fno-bounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
28+
// RUN: %clang -fbounds-safety -fno-unique-traps -fbounds-safety-unique-traps -### %s 2>&1 | FileCheck --check-prefix=ENABLED %s
29+
30+
// RUN: %clang -fbounds-safety -fbounds-safety-unique-traps -fno-unique-traps -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
31+
// RUN: %clang -fbounds-safety -fbounds-safety-unique-traps -funique-traps -### %s 2>&1 | FileCheck --check-prefix=ENABLED %s
32+
33+
// RUN: %clang -fbounds-safety -fno-bounds-safety-unique-traps -fno-unique-traps -### %s 2>&1 | FileCheck --check-prefix=DISABLED %s
34+
// RUN: %clang -fbounds-safety -fno-bounds-safety-unique-traps -funique-traps -### %s 2>&1 | FileCheck --check-prefix=ENABLED %s
35+
36+
37+
// ENABLED: -fbounds-safety-unique-traps
38+
// DISABLED-NOT: -fbounds-safety-unique-traps

0 commit comments

Comments
 (0)