Skip to content

Commit

Permalink
[DeclPrinter] Fix two cases that crash clang -ast-print.
Browse files Browse the repository at this point in the history
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

llvm-svn: 322742
  • Loading branch information
Artem-B committed Jan 17, 2018
1 parent 9f3fe42 commit 224879e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
9 changes: 5 additions & 4 deletions clang/lib/AST/DeclPrinter.cpp
Expand Up @@ -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<TypedefType>(BaseType))
break;
else if (const PointerType* PTy = BaseType->getAs<PointerType>())
if (const PointerType *PTy = BaseType->getAs<PointerType>())
BaseType = PTy->getPointeeType();
else if (const BlockPointerType *BPy = BaseType->getAs<BlockPointerType>())
BaseType = BPy->getPointeeType();
Expand All @@ -144,8 +142,11 @@ static QualType GetBaseType(QualType T) {
BaseType = RTy->getPointeeType();
else if (const AutoType *ATy = BaseType->getAs<AutoType>())
BaseType = ATy->getDeducedType();
else if (const ParenType *PTy = BaseType->getAs<ParenType>())
BaseType = PTy->desugar();
else
llvm_unreachable("Unknown declarator!");
// This must be a syntax error.
break;
}
return BaseType;
}
Expand Down
4 changes: 4 additions & 0 deletions clang/test/Sema/ast-print.c
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions clang/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;

0 comments on commit 224879e

Please sign in to comment.