[clang-tidy] Fix altera-struct-pack-align crash for struct fields with incomplete type

We can only use ASTContext::getTypeInfo for complete types.

This fixes bugzilla issue 50313.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D102569
This commit is contained in:
Georgy Komarov 2021-05-16 08:27:46 +03:00
parent 2c9688d201
commit ab92a4c26f
No known key found for this signature in database
GPG Key ID: 195B8622FE88ED46
2 changed files with 11 additions and 2 deletions

View File

@ -58,9 +58,11 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
// For each StructField, record how big it is (in bits).
// Would be good to use a pair of <offset, size> to advise a better
// packing order.
QualType StructFieldTy = StructField->getType();
if (StructFieldTy->isIncompleteType())
return;
unsigned int StructFieldWidth =
(unsigned int)Result.Context
->getTypeInfo(StructField->getType().getTypePtr())
(unsigned int)Result.Context->getTypeInfo(StructFieldTy.getTypePtr())
.Width;
FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
// FIXME: Recommend a reorganization of the struct (sort by StructField

View File

@ -0,0 +1,7 @@
// RUN: %check_clang_tidy -expect-clang-tidy-error %s altera-struct-pack-align %t -- -header-filter=.*
struct A;
struct B {
A a;
// CHECK-MESSAGES: :[[@LINE-1]]:5: error: field has incomplete type 'A' [clang-diagnostic-error]
};