Skip to content

Commit 437a4f5

Browse files
committed
[-Wunsafe-buffer-usage] Explicitly unsupport fixits for multidimensional C arrays
Proper support for multidimensional arrays is outside of the scope of initial std::array fixits. This change makes sure that we don't accidentally emit fixits for multidimensional arrays until that work is all done. It adds a specific check when transforming VarDecl type to std::array. Also adds corresponding several testscases.
1 parent 7694a6d commit 437a4f5

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/APSInt.h"
2020
#include "llvm/ADT/SmallVector.h"
2121
#include "llvm/ADT/StringRef.h"
22+
#include "llvm/Support/Casting.h"
2223
#include <memory>
2324
#include <optional>
2425
#include <queue>
@@ -2477,6 +2478,9 @@ static FixItList fixVarDeclWithArray(const VarDecl *D, const ASTContext &Ctx,
24772478
if (auto CAT = dyn_cast<clang::ConstantArrayType>(D->getType())) {
24782479
const QualType &ArrayEltT = CAT->getElementType();
24792480
assert(!ArrayEltT.isNull() && "Trying to fix a non-array type variable!");
2481+
// FIXME: support multi-dimensional arrays
2482+
if (isa<clang::ConstantArrayType>(ArrayEltT))
2483+
return {};
24802484

24812485
const SourceLocation IdentifierLoc = getVarDeclIdentifierLoc(D);
24822486

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@ void simple(unsigned idx) {
1010
buffer[idx] = 0;
1111
}
1212

13+
void array2d(unsigned idx) {
14+
int buffer[10][10];
15+
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
16+
buffer[idx][idx] = 0;
17+
}
18+
19+
void array2d_assign_from_elem(unsigned idx) {
20+
int buffer[10][10];
21+
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
22+
int a = buffer[idx][idx];
23+
}
24+
25+
void array2d_use(unsigned);
26+
void array2d_call(unsigned idx) {
27+
int buffer[10][10];
28+
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
29+
array2d_use(buffer[idx][idx]);
30+
}
31+
32+
void array2d_typedef(unsigned idx) {
33+
typedef int ten_ints_t[10];
34+
ten_ints_t buffer[10];
35+
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
36+
buffer[idx][idx] = 0;
37+
}
38+
1339
void whitespace_in_declaration(unsigned idx) {
1440
int buffer_w [ 10 ];
1541
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:35}:"std::array<int, 10> buffer_w"

0 commit comments

Comments
 (0)