[clang] f757ecb - [AST] Fix a crash on invalid bitwidth exprs when preserving the recoveryexprs.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 30 01:52:12 PDT 2020


Author: Haojian Wu
Date: 2020-03-30T10:52:00+02:00
New Revision: f757ecbf85605735195441abefd9c291f5e317cc

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

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

Summary:
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.

Reviewers: sammccall

Subscribers: cfe-commits

Tags: #clang

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

Added: 
    clang/test/Sema/invalid-bitwidth-expr.mm

Modified: 
    clang/lib/AST/ASTContext.cpp
    clang/lib/AST/RecordLayoutBuilder.cpp
    clang/lib/Sema/SemaDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 19f67fc2bb3f..461b155df7bf 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -2161,6 +2161,11 @@ TypeInfo ASTContext::getTypeInfoImpl(const Type *T) const {
     return getTypeInfo(cast<AdjustedType>(T)->getAdjustedType().getTypePtr());
   case Type::ObjCInterface: {
     const auto *ObjCI = cast<ObjCInterfaceType>(T);
+    if (ObjCI->getDecl()->isInvalidDecl()) {
+      Width = 8;
+      Align = 8;
+      break;
+    }
     const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
     Width = toBits(Layout.getSize());
     Align = toBits(Layout.getAlignment());

diff  --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp
index 9a21732b63e3..028e82a5df4d 100644
--- a/clang/lib/AST/RecordLayoutBuilder.cpp
+++ b/clang/lib/AST/RecordLayoutBuilder.cpp
@@ -3222,7 +3222,8 @@ ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
   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 =

diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 9319f4eff919..ea3a0c22c401 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -16172,6 +16172,10 @@ ExprResult Sema::VerifyBitField(SourceLocation FieldLoc,
                                 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;

diff  --git a/clang/test/Sema/invalid-bitwidth-expr.mm b/clang/test/Sema/invalid-bitwidth-expr.mm
new file mode 100644
index 000000000000..fe93cac683ae
--- /dev/null
+++ b/clang/test/Sema/invalid-bitwidth-expr.mm
@@ -0,0 +1,13 @@
+// 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
+
+struct X { int Y: foo(); }; // expected-error {{use of undeclared identifier}}
+
+constexpr int s = sizeof(Ivar);
+constexpr int ss = sizeof(X);


        


More information about the cfe-commits mailing list