[clang] [attributes][-Wunsafe-buffer-usage] Support adding unsafe_buffer_usage attribute to struct fields (PR #101585)

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 1 19:31:58 PDT 2024


================
@@ -2261,6 +2262,12 @@ class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler {
         // note_unsafe_buffer_operation doesn't have this mode yet.
         assert(!IsRelatedToDecl && "Not implemented yet!");
         MsgParam = 3;
+      } else if (isa<MemberExpr>(Operation)) {
+        // note_unsafe_buffer_operation doesn't have this mode yet.
+        assert(!IsRelatedToDecl && "Not implemented yet!");
+        auto ME = dyn_cast<MemberExpr>(Operation);
+        name = ME->getMemberDecl()->getName();
----------------
haoNoQ wrote:

I suspect that this one will crash in case of anonymous fields. That said, I also don't think it's even possible to put an attribute on an anonymous field, so we're probably fine.

However, with diagnostics specifically, the tradition isn't to put `name` into the `Diag()` stream; the tradition is to put the decl itself, directly, by pointer into the Diag() stream!

I.e.
```c++
const Decl *D = ME->getMemberDecl();
...
S.Diag(Loc, diag::warn_unsafe_buffer_operation) << MsgParam << D << Range;
```
This way not only it reacts to anonymous objects correctly (I think), but also it automatically puts quotes around the name, which is something we currently forget to do :)

https://github.com/llvm/llvm-project/pull/101585


More information about the cfe-commits mailing list