[PATCH] D155641: [-Wunsafe-buffer-usage] Do not assert that function parameters have names.

Ziqing Luo via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 18 14:14:37 PDT 2023


ziqingluo-90 created this revision.
ziqingluo-90 added reviewers: jkorous, NoQ, t-rasmud, malavikasamak.
Herald added a project: All.
ziqingluo-90 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

It is possible that a function parameter does not have a name even in a function definition. 
So we should not assert that function parameters always have names.

This patch lets the analysis give up on generating fix-its in cases where a function parameter of a definition has no name.
Later we can come up with a better solution, e.g., generating a name for the parameter.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155641

Files:
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-span.cpp


Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-span.cpp
===================================================================
--- clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-span.cpp
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-span.cpp
@@ -156,4 +156,9 @@
   if (++MyName){}
 }
 
+// CHECK-NOT: fix-it:{{.*}}:
+void parmHasNoName(int *p, int *) { // cannot fix the function because there is one parameter has no name.
+  p[5] = 5;
+}
+
 #endif
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===================================================================
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -1886,8 +1886,11 @@
 
       if (Parm->isImplicit())
         continue;
-      assert(Parm->getIdentifier() &&
-             "A parameter of a function definition has no name");
+      // A parameter of a function definition has no name.
+      // FIXME: We should create a name for the parameter as part of the fix-it.
+      // Go through declarations of the function and look for a name?
+      if (!Parm->getIdentifier())
+        return std::nullopt;
       if (i == ParmIdx)
         // This is our spanified paramter!
         SS << NewTypeText.str() << "(" << Parm->getIdentifier()->getName().str() << ", "


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155641.541723.patch
Type: text/x-patch
Size: 1310 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230718/24dd7e65/attachment.bin>


More information about the cfe-commits mailing list