[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
Thu Dec 5 19:15:39 PST 2024


================
@@ -5563,6 +5563,12 @@ ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc,
 ExprResult Sema::BuildCXXDefaultInitExpr(SourceLocation Loc, FieldDecl *Field) {
   assert(Field->hasInClassInitializer());
 
+  // We do not want to aggressively cutoff parsing. Try to recover when
+  // in-class-initializer had errors.
+  if (Field->getInClassInitializer() &&
+      Field->getInClassInitializer()->containsErrors())
+    return Field->getInClassInitializer();
----------------
yronglin wrote:

> I guess the idea here is that we're throwing away too much if we just return an ExprError?
Yes.
> BuildCXXDefaultInitExpr is supposed to, as the name suggests, return a CXXDefaultInitExpr. 
Agree.

Should we remove this code from `BuildCXXDefaultInitExpr`? Because we created`RecoveryExpr` in `Sema::ActOnFinishCXXInClassMemberInitializer`.
```c++
  // If we might have already tried and failed to instantiate, don't try again.
  if (Field->isInvalidDecl())
    return ExprError();
```
and the AST will be:
```
-CompoundStmt 0x12480cb70 <col:12, line:494:1> 
        537:       `-DeclStmt 0x12480cb58 <line:493:3, col:8> 
        538:         `-VarDecl 0x12480ca40 <col:3, col:7> col:5 g 'U':'GH112560::U' listinit 
        539:           `-InitListExpr 0x12480cae8 <col:6, col:7> 'U':'GH112560::U' contains-errors field Field 0x12480c870 'f' 'int' 
        540:             `-CXXDefaultInitExpr 0x12480cb30 <col:7> 'int' contains-errors has rewritten init 
        541:               `-RecoveryExpr 0x12480c910 <line:482:9> 'int' contains-errors 
```

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


More information about the cfe-commits mailing list