Skip to content

Commit b6688fb

Browse files
committed
[-Wunsafe-buffer-usage] Bail on fixits for arrays whose size is auto-deduced
C++ allows the array size to be omitted in declaration if it can be deduced from the initializer. E. g.: int array [] = { 1, 2, 3 }; std::array<T, N> on the other hand requires the size N to be provided. Currently the fixit produced would use empty space for N which would lead to a compilation failure. This patch makes the fixit-producing machine just bail in such cases and adds a FIXME to be addressed later.
1 parent 849f643 commit b6688fb

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2516,6 +2516,15 @@ static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
25162516
if (!MaybeArraySizeTxt)
25172517
return {};
25182518
const llvm::StringRef ArraySizeTxt = MaybeArraySizeTxt->trim();
2519+
if (ArraySizeTxt.empty()) {
2520+
// FIXME: Support array size getting determined from the initializer.
2521+
// Examples:
2522+
// int arr1[] = {0, 1, 2};
2523+
// int arr2{3, 4, 5};
2524+
// We might be able to preserve the non-specified size with `auto` and `std::to_array`:
2525+
// auto arr1 = std::to_array<int>({0, 1, 2});
2526+
return {};
2527+
}
25192528

25202529
std::optional<StringRef> IdentText =
25212530
getVarDeclIdentifierText(D, Ctx.getSourceManager(), Ctx.getLangOpts());

clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-local-var-array.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ void comments_in_declaration(unsigned idx) {
2222
buffer_w[idx] = 0;
2323
}
2424

25+
void auto_size(unsigned idx) {
26+
int buffer[] = {0, 1, 2};
27+
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
28+
// FIXME: implement support
29+
30+
buffer[idx] = 0;
31+
}
32+
33+
void universal_initialization(unsigned idx) {
34+
int buffer[] {0, 1, 2};
35+
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
36+
// FIXME: implement support
37+
38+
buffer[idx] = 0;
39+
}
40+
2541
void multi_decl1(unsigned idx) {
2642
int a, buffer[10];
2743
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]

0 commit comments

Comments
 (0)