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

Congcong Cai via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 14 13:24:47 PDT 2023


HerrCai0907 created this revision.
HerrCai0907 added reviewers: aaron.ballman, cjdb.
Herald added a project: All.
HerrCai0907 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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;
      }
  };

The old code only marks the Decl invalid without emiting diagnoise.
It will miss some error and crash CodeGen step.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D148372

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


Index: clang/test/Parser/cxx-member-initializers.cpp
===================================================================
--- clang/test/Parser/cxx-member-initializers.cpp
+++ clang/test/Parser/cxx-member-initializers.cpp
@@ -108,4 +108,6 @@
   // 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 {{expected initializer}}
 };
Index: clang/lib/Parse/ParseDecl.cpp
===================================================================
--- clang/lib/Parse/ParseDecl.cpp
+++ clang/lib/Parse/ParseDecl.cpp
@@ -7383,6 +7383,7 @@
           SourceLocation ArgStartLoc = NextToken().getLocation();
           if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
             DefArgToks.reset();
+            Diag(ArgStartLoc, diag::err_expected) << "initializer";
             Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
           } else {
             Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -303,6 +303,8 @@
 - Fix a failed assertion due to an invalid source location when trying to form
   a coverage report for an unresolved constructor expression.
   (`#62105 <https://github.com/llvm/llvm-project/issues/62105>`_)
+- Fix crash when member function contains invalid default argument.
+  (`#62122 <https://github.com/llvm/llvm-project/issues/62122>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148372.513725.patch
Type: text/x-patch
Size: 1681 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230414/1b3ea612/attachment.bin>


More information about the cfe-commits mailing list