[clang] [clang] Fix a crash issue that caused by handling of fields with initializers in nested anonymous unions (PR #113049)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 20 08:01:38 PST 2024


yronglin wrote:

Sorry for the very late update, I had some troubles in the past two months.

This update check the number of C++11 in-class-initializer in the class and update `CXXRecordDecl::DefinitionData::HasInClassInitializer` when calling `FieldDecl::hasInClassInitializer()`.

I think we might go futhur to create `RecoveryExpr` for invalid in-class-initializer in `FieldDecl`, but this will interrupt the processing of the rest class members. e.g. The "default member initializer for 'r' needed" will missing because `FibTree *l` is invalid in the following:
```C++
namespace use_self {
  struct FibTree {
    int n;
    FibTree *l = // expected-note {{declared here}}
      n > 1 ? new FibTree{n-1} : &fib0; // expected-error {{default member initializer for 'l' needed}}
    FibTree *r = // expected-note {{declared here}}
      n > 2 ? new FibTree{n-2} : &fib0; // expected-error {{default member initializer for 'r' needed}}
    int v = l->v + r->v;

    static FibTree fib0;
  };
  FibTree FibTree::fib0{0, nullptr, nullptr, 1};

  int fib(int n) { return FibTree{n}.v; }
}
```

The `hadError` flag will be true after filled in `FibTree *l`.
https://github.com/llvm/llvm-project/blob/fce917d39d97b8697e04fc52b1727307fc341212/clang/lib/Sema/SemaInit.cpp#L895-L912

https://github.com/llvm/llvm-project/pull/113049


More information about the cfe-commits mailing list