From: Artem Belevich Date: Wed, 17 Jan 2018 19:29:39 +0000 (+0000) Subject: [DeclPrinter] Fix two cases that crash clang -ast-print. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=62d06f7f32aef0ea61b1b9d87bb413ab79e175d5;p=clang [DeclPrinter] Fix two cases that crash clang -ast-print. Both are related to handling anonymous structures. * clang didn't handle () around an anonymous struct variable. * clang also crashed on syntax errors that could lead to other syntactic constructs following the declaration of an anonymous struct. While the code is invalid, that's not a good reason to panic compiler. Differential Revision: https://reviews.llvm.org/D41788 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@322742 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/DeclPrinter.cpp b/lib/AST/DeclPrinter.cpp index b792c5920a..e82144b0ae 100644 --- a/lib/AST/DeclPrinter.cpp +++ b/lib/AST/DeclPrinter.cpp @@ -128,9 +128,7 @@ static QualType GetBaseType(QualType T) { // FIXME: This should be on the Type class! QualType BaseType = T; while (!BaseType->isSpecifierType()) { - if (isa(BaseType)) - break; - else if (const PointerType* PTy = BaseType->getAs()) + if (const PointerType *PTy = BaseType->getAs()) BaseType = PTy->getPointeeType(); else if (const BlockPointerType *BPy = BaseType->getAs()) BaseType = BPy->getPointeeType(); @@ -144,8 +142,11 @@ static QualType GetBaseType(QualType T) { BaseType = RTy->getPointeeType(); else if (const AutoType *ATy = BaseType->getAs()) BaseType = ATy->getDeducedType(); + else if (const ParenType *PTy = BaseType->getAs()) + BaseType = PTy->desugar(); else - llvm_unreachable("Unknown declarator!"); + // This must be a syntax error. + break; } return BaseType; } diff --git a/test/Sema/ast-print.c b/test/Sema/ast-print.c index f701b12209..83a08bf245 100644 --- a/test/Sema/ast-print.c +++ b/test/Sema/ast-print.c @@ -15,6 +15,10 @@ struct blah { }; }; +// This used to crash clang. +struct { +}(s1); + int foo(const struct blah *b) { // CHECK: return b->b; return b->b; diff --git a/test/SemaCXX/ast-print-crash.cpp b/test/SemaCXX/ast-print-crash.cpp new file mode 100644 index 0000000000..c108f666d7 --- /dev/null +++ b/test/SemaCXX/ast-print-crash.cpp @@ -0,0 +1,12 @@ +// RUN: not %clang_cc1 -triple %ms_abi_triple -ast-print %s -std=gnu++11 \ +// RUN: | FileCheck %s + +// The test compiles a file with a syntax error which used to cause a crash with +// -ast-print. Compilation fails due to the syntax error, but compiler should +// not crash and print out whatever it manager to parse. + +// CHECK: struct { +// CHECK-NEXT: } dont_crash_on_syntax_error; +// CHECK-NEXT: decltype(nullptr) p; +struct { +} dont_crash_on_syntax_error /* missing ; */ decltype(nullptr) p;