From 041b7f508533417bcda4feaa03d6c16ff85275f5 Mon Sep 17 00:00:00 2001 From: Malavika Samak Date: Tue, 25 Feb 2025 09:39:26 -0800 Subject: [PATCH] [Wunsafe-buffer-usage] Turn off unsafe-buffer warning for methods annotated with clang::unsafe_buffer_usage attribute (#125671) 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 --------- Co-authored-by: MalavikaSamak --- clang/docs/ReleaseNotes.rst | 2 + clang/lib/Sema/AnalysisBasedWarnings.cpp | 3 + ...warn-unsafe-buffer-usage-function-attr.cpp | 75 +++++++++++++++++-- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 657340c17050..3a0eab66fd58 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -124,6 +124,8 @@ Removed Compiler Flags Attribute Changes in Clang -------------------------- +Adding [[clang::unsafe_buffer_usage]] attribute to a method definition now turns off all -Wunsafe-buffer-usage +related warnings within the method body. - The ``no_sanitize`` attribute now accepts both ``gnu`` and ``clang`` names. - Clang now diagnoses use of declaration attributes on void parameters. (#GH108819) diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp index f21e571e6e0c..afdc0eaab0a4 100644 --- a/clang/lib/Sema/AnalysisBasedWarnings.cpp +++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp @@ -2566,6 +2566,9 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings( // The Callback function that performs analyses: auto CallAnalyzers = [&](const Decl *Node) -> void { + if (Node->hasAttr()) + return; + // Perform unsafe buffer usage analysis: if (!Diags.isIgnored(diag::warn_unsafe_buffer_operation, Node->getBeginLoc()) || diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-function-attr.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-function-attr.cpp index 724d444638b5..f3abe8792840 100644 --- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-function-attr.cpp +++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-function-attr.cpp @@ -119,16 +119,15 @@ struct HoldsUnsafeMembers { [[clang::unsafe_buffer_usage]] HoldsUnsafeMembers(int i) - : FromCtor(i), // expected-warning{{function introduces unsafe buffer manipulation}} - FromCtor2{i} // expected-warning{{function introduces unsafe buffer manipulation}} - {} + : FromCtor(i), + FromCtor2{i} {} HoldsUnsafeMembers(float f) : HoldsUnsafeMembers(0) {} // expected-warning{{function introduces unsafe buffer manipulation}} UnsafeMembers FromCtor; UnsafeMembers FromCtor2; - UnsafeMembers FromField{3}; // expected-warning 2{{function introduces unsafe buffer manipulation}} + UnsafeMembers FromField{3}; // expected-warning {{function introduces unsafe buffer manipulation}} }; struct SubclassUnsafeMembers : public UnsafeMembers { @@ -138,8 +137,7 @@ struct SubclassUnsafeMembers : public UnsafeMembers { [[clang::unsafe_buffer_usage]] SubclassUnsafeMembers(int i) - : UnsafeMembers(i) // expected-warning{{function introduces unsafe buffer manipulation}} - {} + : UnsafeMembers(i){} }; // https://github.com/llvm/llvm-project/issues/80482 @@ -245,3 +243,68 @@ struct AggregateViaDefaultInit { void testAggregateViaDefaultInit() { AggregateViaDefaultInit A; }; + +struct A { + int arr[2]; + + [[clang::unsafe_buffer_usage]] + int *ptr; +}; + +namespace std{ + template class span { + + T *elements; + + public: + + constexpr span(T *, unsigned){} + + template + constexpr span(Begin first, End last){} + + constexpr T* data() const noexcept { + return elements; + } + }; +} + +[[clang::unsafe_buffer_usage]] +void check_no_warnings(unsigned idx) { + int *arr = new int[20]; + + int k = arr[idx]; // no-warning + + std::span sp = {arr, 20}; // no-warning + A *ptr = reinterpret_cast (sp.data()); // no-warning + A a; + a.ptr = arr; // no-warning +} + +[[clang::unsafe_buffer_usage]] +void check_no_warning_variadic(unsigned idx, int arr[20], ...) { + int k = arr[idx]; // no-warning + + std::span sp = {arr, 20}; // no-warning + A *ptr = reinterpret_cast (sp.data()); // no-warning + A a; + a.ptr = arr; // no-warning +} + +template +[[clang::unsafe_buffer_usage]] +void check_no_warnings_template(unsigned idx, T* arr) { + int k = arr[idx]; // no-warning + + std::span sp = {arr, 20}; // no-warning + A *ptr = reinterpret_cast (sp.data()); // no-warning + A a; + a.ptr = arr; // no-warning +} + +void invoke_methods() { + int array[20]; + check_no_warnings(30); //expected-warning{{function introduces unsafe buffer manipulation}} + check_no_warning_variadic(15, array); //expected-warning{{function introduces unsafe buffer manipulation}} + check_no_warnings_template(10, array); //expected-warning{{function introduces unsafe buffer manipulation}} +}