Skip to content

Commit 821a4b4

Browse files
author
MalavikaSamak
committed
[Wunsafe-buffer-usage] Turn off unsafe-buffer warning for methods annotated with clang::unsafe_buffer_usage attribute
Unsafe operation in methods that are already annotated with clang::unsafe_buffer_usage attribute, should not trigger a warning. This is because, the developer has already identified the method as unsafe and warning at every unsafe operation is redundant. (rdar://138644831)
1 parent 76e73ae commit 821a4b4

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ Removed Compiler Flags
109109

110110
Attribute Changes in Clang
111111
--------------------------
112+
Adding [[clang::unsafe_buffer_usage]] attribute to a method definition now turns off all -Wunsafe-buffer-usage
113+
related warnings within the method body.
112114

113115
Improvements to Clang's diagnostics
114116
-----------------------------------

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,6 +2610,9 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
26102610

26112611
// The Callback function that performs analyses:
26122612
auto CallAnalyzers = [&](const Decl *Node) -> void {
2613+
if (Node->hasAttr<UnsafeBufferUsageAttr>())
2614+
return;
2615+
26132616
// Perform unsafe buffer usage analysis:
26142617
if (!Diags.isIgnored(diag::warn_unsafe_buffer_operation,
26152618
Node->getBeginLoc()) ||

clang/test/SemaCXX/warn-unsafe-buffer-usage-function-attr.cpp

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,15 @@ struct HoldsUnsafeMembers {
119119

120120
[[clang::unsafe_buffer_usage]]
121121
HoldsUnsafeMembers(int i)
122-
: FromCtor(i), // expected-warning{{function introduces unsafe buffer manipulation}}
123-
FromCtor2{i} // expected-warning{{function introduces unsafe buffer manipulation}}
124-
{}
122+
: FromCtor(i),
123+
FromCtor2{i} {}
125124

126125
HoldsUnsafeMembers(float f)
127126
: HoldsUnsafeMembers(0) {} // expected-warning{{function introduces unsafe buffer manipulation}}
128127

129128
UnsafeMembers FromCtor;
130129
UnsafeMembers FromCtor2;
131-
UnsafeMembers FromField{3}; // expected-warning 2{{function introduces unsafe buffer manipulation}}
130+
UnsafeMembers FromField{3}; // expected-warning {{function introduces unsafe buffer manipulation}}
132131
};
133132

134133
struct SubclassUnsafeMembers : public UnsafeMembers {
@@ -138,8 +137,7 @@ struct SubclassUnsafeMembers : public UnsafeMembers {
138137

139138
[[clang::unsafe_buffer_usage]]
140139
SubclassUnsafeMembers(int i)
141-
: UnsafeMembers(i) // expected-warning{{function introduces unsafe buffer manipulation}}
142-
{}
140+
: UnsafeMembers(i){}
143141
};
144142

145143
// https://github.com/llvm/llvm-project/issues/80482
@@ -245,3 +243,40 @@ struct AggregateViaDefaultInit {
245243
void testAggregateViaDefaultInit() {
246244
AggregateViaDefaultInit A;
247245
};
246+
247+
struct A {
248+
int arr[2];
249+
250+
[[clang::unsafe_buffer_usage]]
251+
int *ptr;
252+
};
253+
254+
namespace std{
255+
template <typename T> class span {
256+
257+
T *elements;
258+
259+
public:
260+
261+
constexpr span(T *, unsigned){}
262+
263+
template<class Begin, class End>
264+
constexpr span(Begin first, End last){}
265+
266+
constexpr T* data() const noexcept {
267+
return elements;
268+
}
269+
};
270+
}
271+
272+
[[clang::unsafe_buffer_usage]]
273+
void check_no_warnings(unsigned idx) {
274+
int *arr = new int[20];
275+
276+
int k = arr[idx]; // no-warning
277+
278+
std::span<int> sp = {arr, 20}; // no-warning
279+
A *ptr = reinterpret_cast<A*> (sp.data()); // no-warning
280+
A a;
281+
a.ptr = arr; // no-warning
282+
}

0 commit comments

Comments
 (0)