[PATCH] D76953: [AST] Fix a crash on invalid bitwidth exprs when preserving the recoveryexprs.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 27 14:17:18 PDT 2020


hokein created this revision.
hokein added a reviewer: sammccall.
Herald added a project: clang.

If the bitwith expr contains errors, we mark the field decl invalid.

This patch also tweaks the behavior of ObjCInterfaceDecl to be consistent with
existing RecordDecl -- getObjCLayout method is only called with valid decls.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76953

Files:
  clang/lib/AST/ASTContext.cpp
  clang/lib/AST/RecordLayoutBuilder.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/test/Sema/invalid-bitwidth-expr.cpp
  clang/test/Sema/invalid-bitwidth-expr.mm


Index: clang/test/Sema/invalid-bitwidth-expr.mm
===================================================================
--- /dev/null
+++ clang/test/Sema/invalid-bitwidth-expr.mm
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fobjc-runtime=gcc -frecovery-ast -verify %s
+// RUN: %clang_cc1 -fobjc-runtime=gcc -fno-recovery-ast -verify %s
+ at interface Ivar
+{
+  int Foo : foo(); // expected-error {{use of undeclared identifier}}
+};
+ at end
+
+constexpr int s = sizeof(Ivar);
Index: clang/test/Sema/invalid-bitwidth-expr.cpp
===================================================================
--- /dev/null
+++ clang/test/Sema/invalid-bitwidth-expr.cpp
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -verify -fsyntax-only -frecovery-ast %s
+// RUN: %clang_cc1 -verify -fsyntax-only -fno-recovery-ast %s
+
+struct X { int Y: foo(); }; // expected-error {{use of undeclared identifier}}
+constexpr int ss = sizeof(X);
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -16172,6 +16172,10 @@
                                 IdentifierInfo *FieldName,
                                 QualType FieldTy, bool IsMsStruct,
                                 Expr *BitWidth, bool *ZeroWidth) {
+  assert(BitWidth);
+  if (BitWidth->containsErrors())
+    return ExprError();
+
   // Default to true; that shouldn't confuse checks for emptiness
   if (ZeroWidth)
     *ZeroWidth = true;
Index: clang/lib/AST/RecordLayoutBuilder.cpp
===================================================================
--- clang/lib/AST/RecordLayoutBuilder.cpp
+++ clang/lib/AST/RecordLayoutBuilder.cpp
@@ -3222,7 +3222,8 @@
   if (D->hasExternalLexicalStorage() && !D->getDefinition())
     getExternalSource()->CompleteType(const_cast<ObjCInterfaceDecl*>(D));
   D = D->getDefinition();
-  assert(D && D->isThisDeclarationADefinition() && "Invalid interface decl!");
+  assert(D && !D->isInvalidDecl() && D->isThisDeclarationADefinition() &&
+         "Invalid interface decl!");
 
   // Look up this layout, if already laid out, return what we have.
   const ObjCContainerDecl *Key =
Index: clang/lib/AST/ASTContext.cpp
===================================================================
--- clang/lib/AST/ASTContext.cpp
+++ clang/lib/AST/ASTContext.cpp
@@ -2161,6 +2161,12 @@
     return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr());
   case Type::ObjCInterface: {
     const auto *ObjCI = cast<ObjCInterfaceType>(T);
+    if (ObjCI->getDecl()->isInvalidDecl()) {
+      // FIXME: are the numbers correct?
+      Width = 8;
+      Align = 8;
+      break;
+    }
     const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
     Width = toBits(Layout.getSize());
     Align = toBits(Layout.getAlignment());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76953.253213.patch
Type: text/x-patch
Size: 2817 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200327/224ef4f6/attachment.bin>


More information about the cfe-commits mailing list