[clang] 3333e12 - [clang] add diagnose when member function contains invalid default argument

Congcong Cai via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 25 14:08:45 PDT 2023


Author: Congcong Cai
Date: 2023-04-25T23:08:30+02:00
New Revision: 3333e12753ed9c27fc25c73858a990081b0ceb11

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

LOG: [clang] add diagnose when member function contains invalid default argument

Fixed: https://github.com/llvm/llvm-project/issues/62122
This change pointer to add diagnose message for this code.
```
struct S {
    static int F(int n = 0 ? 0) {
        return 0;
    }
};
```
For default parameter, we should set it as unparsed even if meeting
syntax error because it should be issued in real parser time instead of
set is as invalid directly without diagnose.

Reviewed By: rsmith

Differential Revision: https://reviews.llvm.org/D148372

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Parse/ParseDecl.cpp
    clang/test/Parser/cxx-member-initializers.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a1bb925e8ae24..55ec1cdef52fa 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -321,6 +321,8 @@ Bug Fixes in This Version
   (`#61885 <https://github.com/llvm/llvm-project/issues/61885>`_)
 - Clang constexpr evaluator now treats comparison of [[gnu::weak]]-attributed
   member pointer as an invalid expression.
+- Fix crash when member function contains invalid default argument.
+  (`#62122 <https://github.com/llvm/llvm-project/issues/62122>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 18897188828f1..728879e20de78 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -7381,13 +7381,9 @@ void Parser::ParseParameterDeclarationClause(
           DefArgToks.reset(new CachedTokens);
 
           SourceLocation ArgStartLoc = NextToken().getLocation();
-          if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
-            DefArgToks.reset();
-            Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
-          } else {
-            Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
-                                                      ArgStartLoc);
-          }
+          ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument);
+          Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
+                                                    ArgStartLoc);
         } else {
           // Consume the '='.
           ConsumeToken();

diff  --git a/clang/test/Parser/cxx-member-initializers.cpp b/clang/test/Parser/cxx-member-initializers.cpp
index c29260b731223..57f707cee52be 100644
--- a/clang/test/Parser/cxx-member-initializers.cpp
+++ b/clang/test/Parser/cxx-member-initializers.cpp
@@ -108,4 +108,9 @@ class G {
   // expected-error at -2 {{type name requires a specifier or qualifier}}
   // expected-error at -3 {{expected '>'}}
   // expected-note at -4 {{to match this '<'}}
+
+  void n(int x = 1 ? 2) {}
+  // expected-error at -1 {{expected ':'}}
+  // expected-note at -2 {{to match this '?'}}
+  // expected-error at -3 {{expected expression}}
 };


        


More information about the cfe-commits mailing list