[PATCH] D104097: [analyzer] Fix calculating offset for fields with an empty type

Georgy Komarov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 11 05:53:20 PDT 2021


jubnzv updated this revision to Diff 351423.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D104097/new/

https://reviews.llvm.org/D104097

Files:
  clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
  clang/test/Analysis/padding_no_unique_address.cpp


Index: clang/test/Analysis/padding_no_unique_address.cpp
===================================================================
--- /dev/null
+++ clang/test/Analysis/padding_no_unique_address.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_analyze_cc1 -std=c++14 -triple x86_64-linux-gnu -analyzer-checker=optin.performance -analyzer-config optin.performance.Padding:AllowedPad=2 -verify %s
+
+class Empty {}; // no-warning
+
+// expected-warning at +1{{Excessive padding in 'struct NoUniqueAddressWarn' (6 padding}}
+struct NoUniqueAddressWarn {
+  char c1;
+  [[no_unique_address]] Empty empty;
+  int i;
+  char c2;
+};
+
+struct NoUniqueAddressNoWarn { // no-warning
+  char c1;
+  [[no_unique_address]] Empty empty;
+  char c2;
+};
+
Index: clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp
@@ -193,6 +193,11 @@
     CharUnits PaddingSum;
     CharUnits Offset = ASTContext.toCharUnitsFromBits(RL.getFieldOffset(0));
     for (const FieldDecl *FD : RD->fields()) {
+      // Skip field that is a subobject of zero size, marked with
+      // [[no_unique_address]] or an empty bitfield, because its address can be
+      // set the same as the other fields addresses.
+      if (FD->isZeroSize(ASTContext))
+        continue;
       // This checker only cares about the padded size of the
       // field, and not the data size. If the field is a record
       // with tail padding, then we won't put that number in our
@@ -249,7 +254,7 @@
       RetVal.Field = FD;
       auto &Ctx = FD->getASTContext();
       auto Info = Ctx.getTypeInfoInChars(FD->getType());
-      RetVal.Size = Info.Width;
+      RetVal.Size = FD->isZeroSize(Ctx) ? CharUnits::Zero() : Info.Width;
       RetVal.Align = Info.Align;
       assert(llvm::isPowerOf2_64(RetVal.Align.getQuantity()));
       if (auto Max = FD->getMaxAlignment())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104097.351423.patch
Type: text/x-patch
Size: 1987 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210611/8811434d/attachment.bin>


More information about the cfe-commits mailing list