From ab92a4c26f54170bf72706ad29c0fb151a177590 Mon Sep 17 00:00:00 2001 From: Georgy Komarov Date: Sun, 16 May 2021 08:27:46 +0300 Subject: [PATCH] [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 --- clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp | 6 ++++-- .../test/clang-tidy/checkers/altera-struct-pack-align-no-crash.cpp | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align-no-crash.cpp diff --git a/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp b/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp index a2178be..ef5fe41 100644 --- a/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp +++ b/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp @@ -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 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 diff --git a/clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align-no-crash.cpp b/clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align-no-crash.cpp new file mode 100644 index 0000000..660addc --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align-no-crash.cpp @@ -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] +}; -- 2.7.4