[clang-tools-extra] f25935a - [clang-tidy] Fix `altera-struct-pack-align` check for empty structs
Fabian Wolff via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 20 07:56:29 PDT 2022
Author: Fabian Wolff
Date: 2022-04-20T16:55:29+02:00
New Revision: f25935a000917f2c06b52bbc7273e20a82543782
URL: https://github.com/llvm/llvm-project/commit/f25935a000917f2c06b52bbc7273e20a82543782
DIFF: https://github.com/llvm/llvm-project/commit/f25935a000917f2c06b52bbc7273e20a82543782.diff
LOG: [clang-tidy] Fix `altera-struct-pack-align` check for empty structs
Fixes https://github.com/llvm/llvm-project/issues/50962.
Reviewed By: whisperity, aaron.ballman
Differential Revision: https://reviews.llvm.org/D114292
Added:
Modified:
clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp b/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
index 6ae53512fca5e..b5f8d082d8877 100644
--- a/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
+++ b/clang-tools-extra/clang-tidy/altera/StructPackAlignCheck.cpp
@@ -77,7 +77,8 @@ void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
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(static_cast<float>(TotalBitSize) / CharSize), 1));
CharUnits MaxAlign = CharUnits::fromQuantity(
ceil((float)Struct->getMaxAlignment() / CharSize));
CharUnits CurrAlign =
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 50d5b8ecc72c1..491bc92f4ef38 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -170,6 +170,9 @@ Changes in existing checks
<clang-tidy/checks/performance-inefficient-vector-operation>` to work when
the vector is a member of a structure.
+- Fixed nonsensical suggestion of :doc:`altera-struct-pack-align
+ <clang-tidy/checks/altera-struct-pack-align>` check for empty structs.
+
Removed checks
^^^^^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp b/clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp
index 615b6cafe87a2..472372ffe35c1 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/altera-struct-pack-align.cpp
@@ -99,3 +99,22 @@ void no_trigger_on_instantiation() {
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)));
More information about the cfe-commits
mailing list