Skip to content

Commit 1acac5c

Browse files
authored
[sanitizer] Fix empty string in unsupported argument error for -fsanitize-trap (#136549)
When using `-fsanitize-trap` with a sanitizer group that doesn't support trapping, an empty argument is passed to `err_drv_unsupported_option_argument`. Use new `toStringWithGroups` for the diagnostic.
1 parent e2c1dd5 commit 1acac5c

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ static std::string describeSanitizeArg(const llvm::opt::Arg *A,
160160
/// Sanitizers set.
161161
static std::string toString(const clang::SanitizerSet &Sanitizers);
162162

163+
/// Produce a string containing comma-separated names of sanitizers and
164+
/// sanitizer groups in \p Sanitizers set.
165+
static std::string toStringWithGroups(const clang::SanitizerSet &Sanitizers);
166+
163167
/// Return true if an execute-only target disallows data access to code
164168
/// sections.
165169
static bool isExecuteOnlyTarget(const llvm::Triple &Triple,
@@ -289,7 +293,7 @@ parseSanitizeArgs(const Driver &D, const llvm::opt::ArgList &Args,
289293
SanitizerSet SetToDiagnose;
290294
SetToDiagnose.Mask |= KindsToDiagnose;
291295
D.Diag(diag::err_drv_unsupported_option_argument)
292-
<< Arg->getSpelling() << toString(SetToDiagnose);
296+
<< Arg->getSpelling() << toStringWithGroups(SetToDiagnose);
293297
DiagnosedAlwaysOutViolations |= KindsToDiagnose;
294298
}
295299
}
@@ -305,7 +309,7 @@ parseSanitizeArgs(const Driver &D, const llvm::opt::ArgList &Args,
305309
SanitizerSet SetToDiagnose;
306310
SetToDiagnose.Mask |= KindsToDiagnose;
307311
D.Diag(diag::err_drv_unsupported_option_argument)
308-
<< Arg->getSpelling() << toString(SetToDiagnose);
312+
<< Arg->getSpelling() << toStringWithGroups(SetToDiagnose);
309313
DiagnosedAlwaysInViolations |= KindsToDiagnose;
310314
}
311315
}
@@ -1200,6 +1204,19 @@ static std::string toString(const clang::SanitizerMaskCutoffs &Cutoffs) {
12001204
return llvm::join(Res, ",");
12011205
}
12021206

1207+
static std::string toStringWithGroups(const clang::SanitizerSet &Sanitizers) {
1208+
std::string Res;
1209+
#define SANITIZER(NAME, ID) \
1210+
if (Sanitizers.has(SanitizerKind::ID)) { \
1211+
if (!Res.empty()) \
1212+
Res += ","; \
1213+
Res += NAME; \
1214+
}
1215+
#define SANITIZER_GROUP(NAME, ID, ALIAS) SANITIZER(NAME, ID##Group)
1216+
#include "clang/Basic/Sanitizers.def"
1217+
return Res;
1218+
}
1219+
12031220
static void addSpecialCaseListOpt(const llvm::opt::ArgList &Args,
12041221
llvm::opt::ArgStringList &CmdArgs,
12051222
const char *SCLOptFlag,

clang/test/Driver/fsanitize.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@
317317
// RUN: not %clang --target=aarch64-linux -fsanitize=memtag -I +mte %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANMT-NOMT-1
318318
// CHECK-SANMT-NOMT-1: '-fsanitize=memtag-stack' requires hardware support (+memtag)
319319

320+
// RUN: not %clang --target=aarch64-linux-android31 -fsanitize-trap=memtag -march=armv8-a+memtag -c %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANMT-TRAP
321+
// CHECK-SANMT-TRAP: error: unsupported argument 'memtag' to option '-fsanitize-trap='
322+
320323
// RUN: %clang --target=x86_64-linux-gnu -fsanitize=address -fsanitize-address-use-after-scope %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-USE-AFTER-SCOPE
321324
// RUN: %clang_cl --target=x86_64-windows -fsanitize=address -fsanitize-address-use-after-scope -### -- %s 2>&1 | FileCheck %s --check-prefix=CHECK-USE-AFTER-SCOPE
322325
// CHECK-USE-AFTER-SCOPE: -cc1{{.*}}-fsanitize-address-use-after-scope

0 commit comments

Comments
 (0)