Skip to content

[clang][Sema] Improve Sema::CheckCXXDefaultArguments #97338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

MagentaTreehouse
Copy link
Contributor

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 1, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 1, 2024

@llvm/pr-subscribers-clang

Author: None (MagentaTreehouse)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/97338.diff

1 Files Affected:

  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+17-17)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 59487bf57baa9..86f9e78c11dd4 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,21 @@ 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)
-          << Param->getIdentifier();
-      else
-        Diag(Param->getLocation(),
-             diag::err_param_default_argument_missing);
-    }
+    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);
   }
 }
 

Copy link

github-actions bot commented Jul 1, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

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.
@MagentaTreehouse MagentaTreehouse force-pushed the improve-sema-check-default-args branch from a5b2046 to 87c77b3 Compare July 1, 2024 19:50
@cor3ntin cor3ntin merged commit bc8a8f5 into llvm:main Jul 17, 2024
7 checks passed
@MagentaTreehouse MagentaTreehouse deleted the improve-sema-check-default-args branch July 17, 2024 16:55
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
Summary:
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.

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60250866
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants