[clang] [-Wunsafe-buffer-usage] Add FixableGadget for AddAssign in UnspecifiedUntypedContext (PR #71862)

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 14 19:20:50 PST 2023


================
@@ -1766,6 +1815,52 @@ fixUPCAddressofArraySubscriptWithSpan(const UnaryOperator *Node) {
       FixItHint::CreateReplacement(Node->getSourceRange(), SS.str())};
 }
 
+std::optional<FixItList>
+UUCAddAssignGadget::getFixits(const Strategy &S) const {
+  DeclUseList DREs = getClaimedVarUseSites();
+
+  if (DREs.size() != 1)
+    return std::nullopt; // In cases of `Ptr += n` where `Ptr` is not a DRE, we
+                         // give up
+  if (const VarDecl *VD = dyn_cast<VarDecl>(DREs.front()->getDecl())) {
+    if (S.lookup(VD) == Strategy::Kind::Span) {
+      FixItList Fixes;
+      std::stringstream SS;
+      const Stmt *AddAssignNode = getBaseStmt();
+      StringRef varName = VD->getName();
+      const ASTContext &Ctx = VD->getASTContext();
+
+      if (!isNonNegativeIntegerExpr(Offset, VD, Ctx))
+        return std::nullopt;
+
+      std::string SubSpanOffset;
+      const SourceManager &SM = Ctx.getSourceManager();
+      const LangOptions &LangOpts = Ctx.getLangOpts();
+      std::optional<StringRef> ExtentString = getExprText(Offset, SM, LangOpts);
+      
+      if (ExtentString)
+        SubSpanOffset = ExtentString->str();
+      else
+        SubSpanOffset = 
+            getUserFillPlaceHolder(); // FIXME: When does this happen?
----------------
haoNoQ wrote:

Would it make any sense to simply preserve the extent expression as-is, without trying to stash the text? Just fix the code _around_ it instead?

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


More information about the cfe-commits mailing list