[clang] bc8a8f5 - [clang][Sema] Improve `Sema::CheckCXXDefaultArguments` (#97338)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 17 05:09:39 PDT 2024


Author: MagentaTreehouse
Date: 2024-07-17T14:09:35+02:00
New Revision: bc8a8f5415c522f99600171e012d511c010d7309

URL: https://github.com/llvm/llvm-project/commit/bc8a8f5415c522f99600171e012d511c010d7309
DIFF: https://github.com/llvm/llvm-project/commit/bc8a8f5415c522f99600171e012d511c010d7309.diff

LOG: [clang][Sema] Improve `Sema::CheckCXXDefaultArguments` (#97338)

In the second loop in `Sema::CheckCXXDefaultArguments`, we don't need to
re-examine the first parameter with a default argument. Dropped the
first iteration of that loop.

In addition, use the preferred early `continue` for the if-statement in
the loop.

Added: 
    

Modified: 
    clang/lib/Sema/SemaDeclCXX.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index f24912cde275a..2bfb103e8953d 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -1630,9 +1630,6 @@ void Sema::MergeVarDeclExceptionSpecs(VarDecl *New, VarDecl *Old) {
 /// function declaration are well-formed according to C++
 /// [dcl.fct.default].
 void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) {
-  unsigned NumParams = FD->getNumParams();
-  unsigned ParamIdx = 0;
-
   // This checking doesn't make sense for explicit specializations; their
   // default arguments are determined by the declaration we're specializing,
   // not by FD.
@@ -1642,6 +1639,9 @@ void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) {
     if (FTD->isMemberSpecialization())
       return;
 
+  unsigned NumParams = FD->getNumParams();
+  unsigned ParamIdx = 0;
+
   // Find first parameter with a default argument
   for (; ParamIdx < NumParams; ++ParamIdx) {
     ParmVarDecl *Param = FD->getParamDecl(ParamIdx);
@@ -1654,21 +1654,19 @@ void Sema::CheckCXXDefaultArguments(FunctionDecl *FD) {
   //   with a default argument shall have a default argument supplied in this or
   //   a previous declaration, unless the parameter was expanded from a
   //   parameter pack, or shall be a function parameter pack.
-  for (; ParamIdx < NumParams; ++ParamIdx) {
+  for (++ParamIdx; ParamIdx < NumParams; ++ParamIdx) {
     ParmVarDecl *Param = FD->getParamDecl(ParamIdx);
-    if (!Param->hasDefaultArg() && !Param->isParameterPack() &&
-        !(CurrentInstantiationScope &&
-          CurrentInstantiationScope->isLocalPackExpansion(Param))) {
-      if (Param->isInvalidDecl())
-        /* We already complained about this parameter. */;
-      else if (Param->getIdentifier())
-        Diag(Param->getLocation(),
-             diag::err_param_default_argument_missing_name)
+    if (Param->hasDefaultArg() || Param->isParameterPack() ||
+        (CurrentInstantiationScope &&
+         CurrentInstantiationScope->isLocalPackExpansion(Param)))
+      continue;
+    if (Param->isInvalidDecl())
+      /* We already complained about this parameter. */;
+    else if (Param->getIdentifier())
+      Diag(Param->getLocation(), diag::err_param_default_argument_missing_name)
           << Param->getIdentifier();
-      else
-        Diag(Param->getLocation(),
-             diag::err_param_default_argument_missing);
-    }
+    else
+      Diag(Param->getLocation(), diag::err_param_default_argument_missing);
   }
 }
 


        


More information about the cfe-commits mailing list