[clang] 4b5f17e - [-Wunsafe-buffer-usage] Do not assert that function parameters have names
Ziqing Luo via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 19 14:14:38 PDT 2023
Author: Ziqing Luo
Date: 2023-07-19T14:14:28-07:00
New Revision: 4b5f17e008c684998a5ee10454d34714736eb6c5
URL: https://github.com/llvm/llvm-project/commit/4b5f17e008c684998a5ee10454d34714736eb6c5
DIFF: https://github.com/llvm/llvm-project/commit/4b5f17e008c684998a5ee10454d34714736eb6c5.diff
LOG: [-Wunsafe-buffer-usage] Do not assert that function parameters have names
It is possible that a function parameter does not have a name even in
a function definition. This patch deals with such cases in generating
function overload fix-its for safe buffers.
Reviewed by: NoQ (Artem Dergachev)
Differential revision: https://reviews.llvm.org/D155641
Added:
Modified:
clang/lib/Analysis/UnsafeBufferUsage.cpp
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-span.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index 142c56beddafea..495d171c618c99 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -1885,9 +1885,12 @@ createOverloadsForFixedParams(unsigned ParmIdx, StringRef NewTyText,
const ParmVarDecl *Parm = FD->getParamDecl(i);
if (Parm->isImplicit())
- continue;
- assert(Parm->getIdentifier() &&
- "A parameter of a function definition has no name");
+ continue;
+ // FIXME: If a parameter has no name, it is unused in the
+ // definition. So we could just leave it as it is.
+ if (!Parm->getIdentifier())
+ // If a parameter of a function definition has no name:
+ return std::nullopt;
if (i == ParmIdx)
// This is our spanified paramter!
SS << NewTypeText.str() << "(" << Parm->getIdentifier()->getName().str() << ", "
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-span.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-span.cpp
index cef6afd5933b3c..85210dd4d337bd 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-span.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-parm-span.cpp
@@ -156,4 +156,9 @@ void macroIdentifier(int *MACRO_NAME) { // The fix-it ends with a macro. It will
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
More information about the cfe-commits
mailing list