Skip to content

Commit 7077896

Browse files
authored
[NFCI][sanitizer] Refactor parseSanitizeTrapArgs (llvm#119797)
parseSanitizeTrapArgs follows the general pattern of "compute the sanitizer mask based on the default plus opt-in (if supported) minus opt-out". This patch refactors the functionality into a generalized function, parseSanitizeArgs, which will be useful for future sanitizer flag parsing.
1 parent 6c4e70f commit 7077896

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -247,39 +247,49 @@ static SanitizerMask setGroupBits(SanitizerMask Kinds) {
247247
return Kinds;
248248
}
249249

250-
static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
251-
const llvm::opt::ArgList &Args,
252-
bool DiagnoseErrors) {
253-
SanitizerMask TrapRemove; // During the loop below, the accumulated set of
254-
// sanitizers disabled by the current sanitizer
255-
// argument or any argument after it.
256-
SanitizerMask TrappingKinds;
257-
SanitizerMask TrappingSupportedWithGroups = setGroupBits(TrappingSupported);
250+
// Computes the sanitizer mask based on the default plus opt-in (if supported)
251+
// minus opt-out.
252+
static SanitizerMask
253+
parseSanitizeArgs(const Driver &D, const llvm::opt::ArgList &Args,
254+
bool DiagnoseErrors, SanitizerMask Supported,
255+
SanitizerMask Default, int OptInID, int OptOutID) {
256+
SanitizerMask Remove; // During the loop below, the accumulated set of
257+
// sanitizers disabled by the current sanitizer
258+
// argument or any argument after it.
259+
SanitizerMask Kinds;
260+
SanitizerMask SupportedWithGroups = setGroupBits(Supported);
258261

259262
for (const llvm::opt::Arg *Arg : llvm::reverse(Args)) {
260-
if (Arg->getOption().matches(options::OPT_fsanitize_trap_EQ)) {
263+
if (Arg->getOption().matches(OptInID)) {
261264
Arg->claim();
262265
SanitizerMask Add = parseArgValues(D, Arg, true);
263-
Add &= ~TrapRemove;
264-
SanitizerMask InvalidValues = Add & ~TrappingSupportedWithGroups;
266+
Add &= ~Remove;
267+
SanitizerMask InvalidValues = Add & ~SupportedWithGroups;
265268
if (InvalidValues && DiagnoseErrors) {
266269
SanitizerSet S;
267270
S.Mask = InvalidValues;
268271
D.Diag(diag::err_drv_unsupported_option_argument)
269272
<< Arg->getSpelling() << toString(S);
270273
}
271-
TrappingKinds |= expandSanitizerGroups(Add) & ~TrapRemove;
272-
} else if (Arg->getOption().matches(options::OPT_fno_sanitize_trap_EQ)) {
274+
Kinds |= expandSanitizerGroups(Add) & ~Remove;
275+
} else if (Arg->getOption().matches(OptOutID)) {
273276
Arg->claim();
274-
TrapRemove |=
275-
expandSanitizerGroups(parseArgValues(D, Arg, DiagnoseErrors));
277+
Remove |= expandSanitizerGroups(parseArgValues(D, Arg, DiagnoseErrors));
276278
}
277279
}
278280

279-
// Apply default trapping behavior.
280-
TrappingKinds |= TrappingDefault & ~TrapRemove;
281+
// Apply default behavior.
282+
Kinds |= Default & ~Remove;
283+
284+
return Kinds;
285+
}
281286

282-
return TrappingKinds;
287+
static SanitizerMask parseSanitizeTrapArgs(const Driver &D,
288+
const llvm::opt::ArgList &Args,
289+
bool DiagnoseErrors) {
290+
return parseSanitizeArgs(D, Args, DiagnoseErrors, TrappingSupported,
291+
TrappingDefault, options::OPT_fsanitize_trap_EQ,
292+
options::OPT_fno_sanitize_trap_EQ);
283293
}
284294

285295
bool SanitizerArgs::needsFuzzerInterceptors() const {

0 commit comments

Comments
 (0)