fwolff created this revision. fwolff added reviewers: alexfh, whisperity, hokein. fwolff added a project: clang-tools-extra. Herald added subscribers: carlosgalvezp, rnkovacs, xazax.hun. fwolff requested review of this revision. Herald added a subscriber: cfe-commits.
Fixes PR#51620 <https://bugs.llvm.org/show_bug.cgi?id=51620>. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D114292 Files: clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp Index: clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp +++ clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp @@ -99,3 +99,22 @@ struct bad_align3 instantiated { 'a', 0.001, 'b' }; } +// Make sure that we don't recommend aligning an empty struct to zero bytes (PR#51620) +struct StructWithNoFields {}; + +struct ContainsStructWithNoFields { + StructWithNoFields s; +}; + +// Make sure that an empty struct is treated like "char" for padding and alignment purposes +struct ContainsStructWithNoFields2 { + StructWithNoFields s; + double d; + StructWithNoFields t; +}; +// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'ContainsStructWithNoFields2' is inefficient due to padding; only needs 10 bytes but is using 24 bytes [altera-struct-pack-align] +// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((packed))" to reduce the amount of padding applied to struct 'ContainsStructWithNoFields2' +// CHECK-MESSAGES: :[[@LINE-7]]:8: warning: accessing fields in struct 'ContainsStructWithNoFields2' is inefficient due to poor alignment; currently aligned to 8 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align] +// CHECK-MESSAGES: :[[@LINE-8]]:8: note: use "__attribute__((aligned(16)))" to align struct 'ContainsStructWithNoFields2' to 16 bytes +// CHECK-FIXES: __attribute__((packed)) +// CHECK-FIXES: __attribute__((aligned(16))); Index: clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp +++ clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp @@ -73,7 +73,8 @@ uint64_t CharSize = Result.Context->getCharWidth(); CharUnits CurrSize = Result.Context->getASTRecordLayout(Struct).getSize(); CharUnits MinByteSize = - CharUnits::fromQuantity(ceil((float)TotalBitSize / CharSize)); + CharUnits::fromQuantity(std::max<clang::CharUnits::QuantityType>( + ceil((float)TotalBitSize / CharSize), 1)); CharUnits MaxAlign = CharUnits::fromQuantity( ceil((float)Struct->getMaxAlignment() / CharSize)); CharUnits CurrAlign =
Index: clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp +++ clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp @@ -99,3 +99,22 @@ struct bad_align3 instantiated { 'a', 0.001, 'b' }; } +// Make sure that we don't recommend aligning an empty struct to zero bytes (PR#51620) +struct StructWithNoFields {}; + +struct ContainsStructWithNoFields { + StructWithNoFields s; +}; + +// Make sure that an empty struct is treated like "char" for padding and alignment purposes +struct ContainsStructWithNoFields2 { + StructWithNoFields s; + double d; + StructWithNoFields t; +}; +// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'ContainsStructWithNoFields2' is inefficient due to padding; only needs 10 bytes but is using 24 bytes [altera-struct-pack-align] +// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((packed))" to reduce the amount of padding applied to struct 'ContainsStructWithNoFields2' +// CHECK-MESSAGES: :[[@LINE-7]]:8: warning: accessing fields in struct 'ContainsStructWithNoFields2' is inefficient due to poor alignment; currently aligned to 8 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align] +// CHECK-MESSAGES: :[[@LINE-8]]:8: note: use "__attribute__((aligned(16)))" to align struct 'ContainsStructWithNoFields2' to 16 bytes +// CHECK-FIXES: __attribute__((packed)) +// CHECK-FIXES: __attribute__((aligned(16))); Index: clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp +++ clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp @@ -73,7 +73,8 @@ uint64_t CharSize = Result.Context->getCharWidth(); CharUnits CurrSize = Result.Context->getASTRecordLayout(Struct).getSize(); CharUnits MinByteSize = - CharUnits::fromQuantity(ceil((float)TotalBitSize / CharSize)); + CharUnits::fromQuantity(std::max<clang::CharUnits::QuantityType>( + ceil((float)TotalBitSize / CharSize), 1)); CharUnits MaxAlign = CharUnits::fromQuantity( ceil((float)Struct->getMaxAlignment() / CharSize)); CharUnits CurrAlign =
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits