Skip to content

Commit f25935a

Browse files
committed
[clang-tidy] Fix altera-struct-pack-align check for empty structs
Fixes llvm/llvm-project#50962. Reviewed By: whisperity, aaron.ballman Differential Revision: https://reviews.llvm.org/D114292
1 parent d7938b1 commit f25935a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
7777
uint64_t CharSize = Result.Context->getCharWidth();
7878
CharUnits CurrSize = Result.Context->getASTRecordLayout(Struct).getSize();
7979
CharUnits MinByteSize =
80-
CharUnits::fromQuantity(ceil((float)TotalBitSize / CharSize));
80+
CharUnits::fromQuantity(std::max<clang::CharUnits::QuantityType>(
81+
ceil(static_cast<float>(TotalBitSize) / CharSize), 1));
8182
CharUnits MaxAlign = CharUnits::fromQuantity(
8283
ceil((float)Struct->getMaxAlignment() / CharSize));
8384
CharUnits CurrAlign =

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ Changes in existing checks
170170
<clang-tidy/checks/performance-inefficient-vector-operation>` to work when
171171
the vector is a member of a structure.
172172

173+
- Fixed nonsensical suggestion of :doc:`altera-struct-pack-align
174+
<clang-tidy/checks/altera-struct-pack-align>` check for empty structs.
175+
173176
Removed checks
174177
^^^^^^^^^^^^^^
175178

clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,22 @@ void no_trigger_on_instantiation() {
9999
struct bad_align3 instantiated { 'a', 0.001, 'b' };
100100
}
101101

102+
// Make sure that we don't recommend aligning an empty struct to zero bytes (PR#51620)
103+
struct StructWithNoFields {};
104+
105+
struct ContainsStructWithNoFields {
106+
StructWithNoFields s;
107+
};
108+
109+
// Make sure that an empty struct is treated like "char" for padding and alignment purposes
110+
struct ContainsStructWithNoFields2 {
111+
StructWithNoFields s;
112+
double d;
113+
StructWithNoFields t;
114+
};
115+
// 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]
116+
// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((packed))" to reduce the amount of padding applied to struct 'ContainsStructWithNoFields2'
117+
// 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]
118+
// CHECK-MESSAGES: :[[@LINE-8]]:8: note: use "__attribute__((aligned(16)))" to align struct 'ContainsStructWithNoFields2' to 16 bytes
119+
// CHECK-FIXES: __attribute__((packed))
120+
// CHECK-FIXES: __attribute__((aligned(16)));

0 commit comments

Comments
 (0)