[clang] Respect the [[clang::unsafe_buffer_usage]] attribute for constructors (PR #91777)

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Tue May 14 15:25:15 PDT 2024


================
@@ -2856,7 +2916,7 @@ getFixIts(FixableGadgetSets &FixablesForAllVars, const FixitStrategy &S,
       }
 #ifndef NDEBUG
       Handler.addDebugNoteForVar(
-          VD, F->getBaseStmt()->getBeginLoc(),
+          VD, F->getSourceLoc(),
----------------
haoNoQ wrote:

Hmm right, this is literally the only case where we use it *at all*. Maybe the right thing to do is to make that source location (or whatever we need) a member variable in the `Gadget` class, and pre-populate it in the `findGadgets()` method with the root statement. We don't really care what statement it is anyway do we. We just need something to work with. We can even do this under `#ifndef NDEBUG` because that's the only way we ever use it. And we don't have to define it in every subclass anymore. But if a subclass doesn't like the default value they can still reassign it in constructor.

I.e.
```diff
 class FixableGadget {
+#ifndef NDEBUG
+SourceLocation DebugLoc;
+#endif

 public:
-  FixableGadget(Kind K) : Gadget(K) {}
+  FixableGadget(Kind K, const MatchFinder::MatchResult &Result) : 
+      Gadget(K),
+      DebugLoc(Result.getNodeAs<Stmt>(getDebugName() + "Gadget")->getBeginLoc())
+  {}

+#ifndef NDEBUG
+  SourceLocation getDebugLoc() const { return DebugLoc; }
+#endif
};

 class PointerInitGadget : FixableGadget {
     PointerInitGadget(const MatchFinder::MatchResult &Result)
-        : FixableGadget(Kind::PointerInit),
+        : FixableGadget(Kind::PointerInit, Result),
           PtrInitLHS(Result.Nodes.getNodeAs<VarDecl>(PointerInitLHSTag)),
           PtrInitRHS(Result.Nodes.getNodeAs<DeclRefExpr>(PointerInitRHSTag)) {}

- // And then you no longer need getSourceLoc() in every class.
 };
```

(You don't *have* to do this in order to land the patch. I think it looks great already. I'm just thinking out loud.)

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


More information about the cfe-commits mailing list