[clang] [clang][-Wunsafe-buffer-usage] Introduce `[[clang::unsafe_buffer_usage_in_data_call]]` attribute (PR #240558)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 9 13:47:35 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-analysis
Author: Yu Hao (yuhaouy)
<details>
<summary>Changes</summary>
Depends on #<!-- -->240553. The first three commits are from the base PR; only commits after the first three are new in this PR.
Introduce `[[clang::unsafe_buffer_usage_in_data_call]]` and equivalent `[[clang::unsafe_buffer_usage("data")]]` to allow non-std container and view data accessor methods (such as `absl::Span::data`) to opt into `-Wunsafe-buffer-usage-in-data-call` diagnostics.
Unlike the general `[[clang::unsafe_buffer_usage]]` attribute, which warns on every call, this form only warns when the returned pointer is explicitly cast to a pointer to a larger, incomplete, or dependent type, matching the built-in check for `std::span::data`.
---
*Disclosure: AI assistance was used in the development and implementation of this change.*
---
Patch is 23.58 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/240558.diff
13 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+15)
- (modified) clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h (+10)
- (modified) clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def (+1-1)
- (modified) clang/include/clang/Basic/Attr.td (+2-1)
- (modified) clang/include/clang/Basic/AttrDocs.td (+29)
- (modified) clang/include/clang/Basic/DiagnosticGroups.td (+2-1)
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+4-1)
- (modified) clang/lib/Analysis/UnsafeBufferUsage.cpp (+56-17)
- (modified) clang/lib/Sema/AnalysisBasedWarnings.cpp (+18-35)
- (modified) clang/lib/Sema/SemaDeclAttr.cpp (+8-2)
- (modified) clang/test/SemaCXX/attr-unsafe-buffer-usage.cpp (+11)
- (added) clang/test/SemaCXX/warn-unsafe-buffer-usage-in-data-call-annotated.cpp (+37)
- (modified) clang/test/SemaCXX/warn-unsafe-buffer-usage-warning-data-invocation.cpp (+25-2)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 88546d7c95d5d..6daf7c116183f 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -449,6 +449,11 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
and view constructors and factory functions to opt in to
`-Wunsafe-buffer-usage-in-container` diagnostics.
+- Added `[[clang::unsafe_buffer_usage_in_data_call]]` (and equivalent spelling
+ `[[clang::unsafe_buffer_usage("data")]]`) to allow custom container and view
+ data accessor methods to opt in to `-Wunsafe-buffer-usage-in-data-call`
+ diagnostics.
+
- The thread safety attribute `lock_returned` (`RETURN_CAPABILITY`) can now be
applied to function pointers, like the other thread safety function
attributes:
@@ -808,6 +813,16 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
`-Wno-unsafe-buffer-usage-in-libc-call-string-examination` silences `strlen`
while `memcpy` is still diagnosed. (#GH127849)
+- Added `-Wunsafe-buffer-usage-in-data-call` as a diagnostic group under
+ `-Wunsafe-buffer-usage` to control warnings on unsafe casts of pointers
+ returned by container `data()` calls, which were previously diagnosed directly
+ under `-Wunsafe-buffer-usage`.
+
+- `-Wunsafe-buffer-usage-in-data-call` now warns on unsafe casts of pointers
+ returned by methods annotated with
+ `[[clang::unsafe_buffer_usage_in_data_call]]` or
+ `[[clang::unsafe_buffer_usage("data")]]`.
+
- Fixed the `no-` prefix being dropped when diagnosing an unknown
`-Wno-error=` or `-Wno-fatal-errors=` warning option. Clang now preserves
the no- prefix in the diagnostic and spelling suggestion.
diff --git a/clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h b/clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
index f9fb731e489ed..4775bc3b34bad 100644
--- a/clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
+++ b/clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
@@ -167,6 +167,11 @@ class UnsafeBufferUsageHandler {
bool IsRelatedToDecl,
ASTContext &Ctx) = 0;
+ /// Invoked when an unsafe cast of a pointer returned by a container's data()
+ /// method is found.
+ virtual void handleUnsafeDataCall(const ExplicitCastExpr *Cast,
+ ASTContext &Ctx) = 0;
+
#ifndef NDEBUG
public:
bool areDebugNotesRequested() {
@@ -210,6 +215,11 @@ class UnsafeBufferUsageHandler {
virtual bool
ignoreUnsafeBufferInStaticSizedArray(const SourceLocation &Loc) const = 0;
+ /// \return true iff unsafe casts on container data() calls should NOT be
+ /// reported at `Loc`; false otherwise.
+ virtual bool
+ ignoreUnsafeBufferInDataCall(const SourceLocation &Loc) const = 0;
+
virtual std::string
getUnsafeBufferUsageAttributeTextAt(SourceLocation Loc,
StringRef WSSuffix = "") const = 0;
diff --git a/clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def b/clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def
index 7e4d5772ee868..692718a8285e0 100644
--- a/clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def
+++ b/clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def
@@ -36,7 +36,7 @@ WARNING_GADGET(Decrement)
WARNING_OPTIONAL_GADGET(PointerArithmetic)
WARNING_GADGET(UnsafeBufferUsageAttr)
WARNING_GADGET(UnsafeBufferUsageCtorAttr)
-WARNING_GADGET(DataInvocation)
+WARNING_OPTIONAL_GADGET(DataInvocation)
WARNING_GADGET(UniquePtrArrayAccess)
WARNING_OPTIONAL_GADGET(ArraySubscript)
WARNING_OPTIONAL_GADGET(UnsafeLibcFunctionCall)
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 24fd853a2384a..a2bfd7e0e1312 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -5107,7 +5107,8 @@ def ReleaseHandle : InheritableParamAttr {
def UnsafeBufferUsage : InheritableAttr {
let Spellings = [Clang<"unsafe_buffer_usage">,
- Clang<"unsafe_buffer_usage_in_container">];
+ Clang<"unsafe_buffer_usage_in_container">,
+ Clang<"unsafe_buffer_usage_in_data_call">];
let Args = [StringArgument<"Category", 1>];
let Subjects = SubjectList<[Function, Field]>;
let Documentation = [UnsafeBufferUsageDocs];
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 2300c8456ef16..95d8bce538703 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -8471,6 +8471,35 @@ alternatives, though the attribute can be used even when the fix can't be automa
}
```
+- Attribute attached to container data accessor methods: The spellings
+ `[[clang::unsafe_buffer_usage_in_data_call]]` and
+ `[[clang::unsafe_buffer_usage("data")]]` are equivalent and can be placed on
+ methods of container or view types that return a pointer to the underlying
+ buffer (such as `data()` on custom span types) to opt them in to
+ `-Wunsafe-buffer-usage-in-data-call`.
+
+ Unlike the general `[[clang::unsafe_buffer_usage]]` attribute, which warns on
+ every call, this form only warns when the returned pointer is explicitly cast
+ to a pointer to a larger, incomplete, or dependent type, matching the built-in
+ check for `std::span::data`:
+
+ ```c++
+ template <typename T>
+ class CustomSpan {
+ public:
+ [[clang::unsafe_buffer_usage_in_data_call]]
+ constexpr T *data() const noexcept;
+ };
+
+ struct Large { int a, b; };
+
+ void example(CustomSpan<int> s) {
+ int *p = s.data(); // no warning
+ char *bytes = (char *)s.data(); // no warning: cast to smaller type
+ Large *l = (Large *)s.data(); // warning: unsafe invocation of 'data'
+ }
+ ```
+
}];
}
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index eb5a1280bf41e..2c875e4eb5ce3 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -2038,7 +2038,8 @@ def UnsafeBufferUsageInStaticSizedArray : DiagGroup<"unsafe-buffer-usage-in-stat
def UnsafeBufferUsageInFormatAttrCall : DiagGroup<"unsafe-buffer-usage-in-format-attr-call">;
def UnsafeBufferUsageInStringView : DiagGroup<"unsafe-buffer-usage-in-string-view">;
def UnsafeBufferUsageMainArgv : DiagGroup<"unsafe-buffer-usage-main-argv">;
-def UnsafeBufferUsage : DiagGroup<"unsafe-buffer-usage", [UnsafeBufferUsageInContainer, UnsafeBufferUsageInLibcCall, UnsafeBufferUsageInUniquePtrArrayAccess, UnsafeBufferUsageInStaticSizedArray, UnsafeBufferUsageInFormatAttrCall, UnsafeBufferUsageInStringView, UnsafeBufferUsageMainArgv]>;
+def UnsafeBufferUsageInDataCall : DiagGroup<"unsafe-buffer-usage-in-data-call">;
+def UnsafeBufferUsage : DiagGroup<"unsafe-buffer-usage", [UnsafeBufferUsageInContainer, UnsafeBufferUsageInLibcCall, UnsafeBufferUsageInUniquePtrArrayAccess, UnsafeBufferUsageInStaticSizedArray, UnsafeBufferUsageInFormatAttrCall, UnsafeBufferUsageInStringView, UnsafeBufferUsageMainArgv, UnsafeBufferUsageInDataCall]>;
// Warnings and notes InstallAPI verification.
def InstallAPIViolation : DiagGroup<"installapi-violation">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 3e56980e22902..2df0237a28399 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -14087,7 +14087,7 @@ defvar UnsafeBufferVariableMsg =
defvar UnsafeBufferOperationMsg =
"%select{unsafe pointer operation|unsafe pointer arithmetic|"
"unsafe buffer access|function introduces unsafe buffer manipulation|"
- "unsafe invocation of %1|field %1 prone to unsafe buffer manipulation}0";
+ "field %1 prone to unsafe buffer manipulation}0";
defvar UnsafeBufferLibcCallMsg = "function %0 is unsafe";
def warn_unsafe_buffer_variable : Warning<UnsafeBufferVariableMsg>,
InGroup<UnsafeBufferUsage>, DefaultIgnore;
@@ -14144,6 +14144,9 @@ def warn_unsafe_buffer_usage_unique_ptr_array_access : Warning<"direct access us
InGroup<UnsafeBufferUsageInUniquePtrArrayAccess>, DefaultIgnore;
def warn_unsafe_buffer_usage_in_static_sized_array : Warning<"direct access on T[N] is unsafe due to the lack of bounds checking">,
InGroup<UnsafeBufferUsageInStaticSizedArray>, DefaultIgnore;
+def warn_unsafe_buffer_usage_in_data_call : Warning<
+ "unsafe invocation of %0">,
+ InGroup<UnsafeBufferUsageInDataCall>, DefaultIgnore;
#ifndef NDEBUG
// Not a user-facing diagnostic. Useful for debugging false negatives in
// -fsafe-buffer-usage-suggestions (i.e. lack of -Wunsafe-buffer-usage fixits).
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index b68fc2f20dc56..ed16d498dc823 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -293,6 +293,12 @@ ignoreUnsafeBufferInContainer(const Stmt &Node,
return Handler->ignoreUnsafeBufferInContainer(Node.getBeginLoc());
}
+static bool
+ignoreUnsafeBufferInDataCall(const Stmt &Node,
+ const UnsafeBufferUsageHandler *Handler) {
+ return Handler->ignoreUnsafeBufferInDataCall(Node.getBeginLoc());
+}
+
static bool ignoreUnsafeLibcCall(const ASTContext &Ctx, const Stmt &Node,
const UnsafeBufferUsageHandler *Handler) {
if (Ctx.getLangOpts().CPlusPlus)
@@ -2383,7 +2389,9 @@ class UnsafeBufferUsageCtorAttrGadget : public WarningGadget {
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
};
-// Warning gadget for unsafe invocation of span::data method.
+// Warning gadget for unsafe invocation of container or view data() methods
+// (including methods annotated with [[clang::unsafe_buffer_usage_in_data_call]]
+// or [[clang::unsafe_buffer_usage("data")]]).
// Triggers when the pointer returned by the invocation is immediately
// cast to a larger type.
@@ -2401,31 +2409,27 @@ class DataInvocationGadget : public WarningGadget {
}
static bool matches(const Stmt *S, const ASTContext &Ctx,
+ const UnsafeBufferUsageHandler *Handler,
MatchResult &Result) {
auto *CE = dyn_cast<ExplicitCastExpr>(S);
if (!CE)
return false;
- for (auto *Child : CE->children()) {
- if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Child);
- MCE && isDataFunction(MCE)) {
- Result.addNode(OpTag, DynTypedNode::create(*CE));
- return true;
- }
- if (auto *Paren = dyn_cast<ParenExpr>(Child)) {
- if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Paren->getSubExpr());
- MCE && isDataFunction(MCE)) {
- Result.addNode(OpTag, DynTypedNode::create(*CE));
- return true;
- }
- }
- }
- return false;
+ if (ignoreUnsafeBufferInDataCall(*S, Handler))
+ return false;
+ const Expr *Operand = CE->getSubExpr()->IgnoreParens();
+ auto *MCE = dyn_cast<CXXMemberCallExpr>(Operand);
+ if (!MCE || !isDataFunction(MCE))
+ return false;
+ if (!isUnsafeCast(CE, Ctx))
+ return false;
+ Result.addNode(OpTag, DynTypedNode::create(*CE));
+ return true;
}
void handleUnsafeOperation(UnsafeBufferUsageHandler &Handler,
bool IsRelatedToDecl,
ASTContext &Ctx) const override {
- Handler.handleUnsafeOperation(Op, IsRelatedToDecl, Ctx);
+ Handler.handleUnsafeDataCall(Op, Ctx);
}
SourceLocation getSourceLoc() const override { return Op->getBeginLoc(); }
@@ -2439,6 +2443,9 @@ class DataInvocationGadget : public WarningGadget {
if (!callee || !isa<CXXMethodDecl>(callee))
return false;
auto *method = cast<CXXMethodDecl>(callee);
+ if (const auto *Attr = method->getAttr<UnsafeBufferUsageAttr>();
+ Attr && Attr->getCategory() == "data")
+ return true;
if (method->getNameAsString() == "data" &&
method->getParent()->isInStdNamespace() &&
llvm::is_contained({SIZED_CONTAINER_OR_VIEW_LIST},
@@ -2447,6 +2454,32 @@ class DataInvocationGadget : public WarningGadget {
return false;
}
+ // Returns true if `CE` casts the pointer returned by `data()` to a pointer to
+ // a larger type, or if the size of either pointee type cannot be determined.
+ static bool isUnsafeCast(const ExplicitCastExpr *CE, const ASTContext &Ctx) {
+ QualType DestType = CE->getType();
+ if (!isa<PointerType>(DestType))
+ return false;
+ QualType DestPointeeType = DestType->getPointeeType();
+
+ // If destination type is incomplete or dependent, it is unsafe to cast
+ // to anyway, no need to check its type:
+ if (DestPointeeType->isIncompleteType() ||
+ DestPointeeType->isDependentType())
+ return true;
+
+ QualType SrcType = CE->getSubExpr()->getType();
+ if (!SrcType->isPointerType())
+ return false;
+ QualType SrcPointeeType = SrcType->getPointeeType();
+
+ // Check if source type is incomplete or dependent as well:
+ if (SrcPointeeType->isIncompleteType() || SrcPointeeType->isDependentType())
+ return true;
+
+ return Ctx.getTypeSize(SrcPointeeType) < Ctx.getTypeSize(DestPointeeType);
+ }
+
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
};
@@ -5025,6 +5058,8 @@ bool clang::matchUnsafePointers(const DynTypedNode &N, ASTContext &Ctx,
void handleUnsafeUniquePtrArrayAccess(const DynTypedNode &Node,
bool IsRelatedToDecl,
ASTContext &Ctx) override {}
+ void handleUnsafeDataCall(const ExplicitCastExpr *Cast,
+ ASTContext &Ctx) override {}
bool ignoreUnsafeBufferInContainer(const SourceLocation &) const override {
return false;
}
@@ -5042,6 +5077,10 @@ bool clang::matchUnsafePointers(const DynTypedNode &N, ASTContext &Ctx,
const SourceLocation &Loc) const override {
return false;
}
+ bool
+ ignoreUnsafeBufferInDataCall(const SourceLocation &Loc) const override {
+ return false;
+ }
std::string getUnsafeBufferUsageAttributeTextAt(
SourceLocation, StringRef WSSuffix = "") const override {
return "";
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index de5eb3f093e41..6b35316924465 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -2570,41 +2570,6 @@ class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler {
assert(!IsRelatedToDecl && "Not implemented yet!");
auto *ME = cast<MemberExpr>(Operation);
D = ME->getMemberDecl();
- MsgParam = 5;
- } else if (const auto *ECE = dyn_cast<ExplicitCastExpr>(Operation)) {
- QualType destType = ECE->getType();
-
- if (!isa<PointerType>(destType))
- return;
- destType = destType.getTypePtr()->getPointeeType();
-
- // If destination type is incomplete or dependent, it is unsafe to cast
- // to anyway, no need to check its type:
- if (!destType->isIncompleteType() && !destType->isDependentType()) {
- const uint64_t dSize = Ctx.getTypeSize(destType);
- QualType srcType = ECE->getSubExpr()->getType();
-
- assert(srcType->isPointerType());
-
- QualType srcPointeeType = srcType.getTypePtr()->getPointeeType();
-
- // Check if source type is incomplete or dependent as well:
- if (!srcPointeeType->isIncompleteType() &&
- !srcPointeeType->isDependentType()) {
- const uint64_t sSize = Ctx.getTypeSize(srcPointeeType);
-
- if (sSize >= dSize)
- return;
- }
- }
- if (const auto *CE = dyn_cast<CXXMemberCallExpr>(
- ECE->getSubExpr()->IgnoreParens())) {
- D = CE->getMethodDecl();
- }
-
- if (!D)
- return;
-
MsgParam = 4;
}
Loc = Operation->getBeginLoc();
@@ -2774,6 +2739,19 @@ class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler {
<< Node.getSourceRange();
}
+ void handleUnsafeDataCall(const ExplicitCastExpr *Cast,
+ ASTContext &Ctx) override {
+ const auto *CE =
+ dyn_cast<CXXMemberCallExpr>(Cast->getSubExpr()->IgnoreParens());
+ if (!CE)
+ return;
+ const CXXMethodDecl *D = CE->getMethodDecl();
+ if (!D)
+ return;
+ S.Diag(Cast->getBeginLoc(), diag::warn_unsafe_buffer_usage_in_data_call)
+ << D << Cast->getSourceRange();
+ }
+
bool isSafeBufferOptOut(const SourceLocation &Loc) const override {
return S.PP.isSafeBufferOptOut(S.getSourceManager(), Loc);
}
@@ -2799,6 +2777,10 @@ class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler {
diag::warn_unsafe_buffer_usage_in_static_sized_array, Loc);
}
+ bool ignoreUnsafeBufferInDataCall(const SourceLocation &Loc) const override {
+ return S.Diags.isIgnored(diag::warn_unsafe_buffer_usage_in_data_call, Loc);
+ }
+
// Returns the text representation of clang::unsafe_buffer_usage attribute.
// `WSSuffix` holds customized "white-space"s, e.g., newline or whilespace
// characters.
@@ -3109,6 +3091,7 @@ static bool shouldRunUnsafeBufferUsageAnalysis(const Sema &S,
return !Diags.isIgnored(diag::warn_unsafe_buffer_operation, Loc) ||
!Diags.isIgnored(diag::warn_unsafe_buffer_variable, Loc) ||
!Diags.isIgnored(diag::warn_unsafe_buffer_usage_in_container, Loc) ||
+ !Diags.isIgnored(diag::warn_unsafe_buffer_usage_in_data_call, Loc) ||
(S.getLangOpts().CPlusPlus &&
(llvm::any_of(
UnsafeLibcCallDiagIDs,
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index e57f1de9c1100..5a8be9d47fcb4 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -7268,15 +7268,21 @@ static void handleHandleAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
static void handleUnsafeBufferUsage(Sema &S, Decl *D, const ParsedAttr &AL) {
StringRef Category;
- if (AL.getAttrName()->getName() == "unsafe_buffer_usage_in_container") {
+ StringRef AttrName = AL.getAttrName()->getName();
+ normalizeName(AttrName);
+ if (AttrName == "unsafe_buffer_usage_in_container") {
if (!AL.checkExactlyNumArgs(S, 0))
return;
Category = "container";
+ } else if (AttrName == "unsafe_buffer_usage_in_data_call") {
+ if (!AL.checkExactlyNumArgs(S, 0))
+ return;
+ Category = "data";
} else if (AL.getNumArgs() != 0) {
SourceLocation Loc;
if (!S.checkStringLiteralArgumentAttr(AL, 0, Category, &Loc))
return;
- if (Category != "container") {
+ if (Category != "container" && Category != "data") {
S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << Category;
return;
}
diff --git a/clang/test/SemaCXX/attr-unsafe-buffer-usage.cpp b/clang/test/SemaCXX/attr-unsafe-buffer-usage.cpp
index cab67784d9075..e08956c5999e3 100644
--- a/clang/test/SemaCXX/attr-unsafe-buffer-usage.cpp
+++ b/clang/test/SemaCXX/attr-unsafe-buffer-usage.cpp
@@ -4,3 +4,14 @@
[[clang::unsafe_buffer_usage]]
void f(int *buf, int size);
void g(int *buffer [[clang::unsafe_buffer_usage("buffer")]], int size); // expected-warning {{'clang::unsafe_buffer_usage' attribute only applies to functions}}
+
+struct DataHolder {
+ [[clang::unsafe_buffer_usage_in_data_call]]
+ int *data();
+ [[clang::unsafe_buffer_usage("data")]]
+ int *data_str();
+ [[clang::unsafe_buffer_usage_in_data_call("data")]] // expected-error {{'clang::unsafe_buffer_usage_in_data_call' attribute takes no arguments}}
+ int *data_err();
+ [[clang::unsafe_buffer_usage("invalid")]] // expected-warning {{'clang::unsafe_buffer_usage' attribute argument not supported: invalid}}
+ int *invalid_category();
+};
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-in-data-call-annotated.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-in-data-call-annotated.cpp
new file mode 100644
index 0000000000000..5caf0b0192254
--- /dev/null
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-in-data-call-annotated.cpp
@@ -0,...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/240558
More information about the cfe-commits
mailing list