Skip to content

Commit cf1c64b

Browse files
committed
[-Wunsafe-buffer-usage] Replace assert that declarations are always found
Differential Revision: https://reviews.llvm.org/D157018
1 parent d687caa commit cf1c64b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,8 @@ class DeclUseTracker {
888888

889889
const DeclStmt *lookupDecl(const VarDecl *VD) const {
890890
auto It = Defs.find(VD);
891-
assert(It != Defs.end() && "Definition never discovered!");
891+
if (It == Defs.end())
892+
return nullptr;
892893
return It->second;
893894
}
894895
};
@@ -2053,7 +2054,10 @@ static FixItList fixVariableWithSpan(const VarDecl *VD,
20532054
ASTContext &Ctx,
20542055
UnsafeBufferUsageHandler &Handler) {
20552056
const DeclStmt *DS = Tracker.lookupDecl(VD);
2056-
assert(DS && "Fixing non-local variables not implemented yet!");
2057+
if (!DS) {
2058+
DEBUG_NOTE_DECL_FAIL(VD, " : variables declared this way not implemented yet");
2059+
return {};
2060+
}
20572061
if (!DS->isSingleDecl()) {
20582062
// FIXME: to support handling multiple `VarDecl`s in a single `DeclStmt`
20592063
DEBUG_NOTE_DECL_FAIL(VD, " : multiple VarDecls");

clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,15 @@ void implied_unclaimed_var(int *b) { // expected-warning{{'b' is an unsafe poin
6666
b++; // expected-note{{used in pointer arithmetic here}} \
6767
// debug-note{{safe buffers debug: failed to produce fixit for 'b' : has an unclaimed use}}
6868
}
69+
70+
int *a = new int[3]; // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
71+
// debug-note{{safe buffers debug: failed to produce fixit for 'a' : neither local nor a parameter}}
72+
void test_globals() {
73+
a[7] = 4; // expected-note{{used in buffer access here}}
74+
}
75+
76+
void test_decomp_decl() {
77+
int a[2] = {1, 2};
78+
auto [x, y] = a;
79+
x = 9;
80+
}

0 commit comments

Comments
 (0)