43 const auto *Struct = Result.Nodes.getNodeAs<RecordDecl>(
"struct");
47 if (Struct->isTemplated())
51 if (Struct->isInvalidDecl())
56 unsigned int TotalBitSize = 0;
57 for (
const FieldDecl *StructField : Struct->fields()) {
61 const QualType StructFieldTy = StructField->getType();
62 if (StructFieldTy->isIncompleteType())
64 const unsigned int StructFieldWidth =
static_cast<unsigned int>(
65 Result.Context->getTypeInfo(StructFieldTy.getTypePtr()).Width);
66 FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
69 TotalBitSize += StructFieldWidth;
72 const uint64_t CharSize = Result.Context->getCharWidth();
73 const CharUnits CurrSize =
74 Result.Context->getASTRecordLayout(Struct).getSize();
75 const CharUnits MinByteSize =
76 CharUnits::fromQuantity(std::max<CharUnits::QuantityType>(
77 std::ceil(
static_cast<float>(TotalBitSize) / CharSize), 1));
78 const CharUnits MaxAlign = CharUnits::fromQuantity(
79 std::ceil(
static_cast<float>(Struct->getMaxAlignment()) / CharSize));
80 const CharUnits CurrAlign =
81 Result.Context->getASTRecordLayout(Struct).getAlignment();
82 const CharUnits NewAlign = computeRecommendedAlignment(MinByteSize);
84 const bool IsPacked = Struct->hasAttr<PackedAttr>();
85 const bool NeedsPacking = (MinByteSize < CurrSize) &&
86 (MaxAlign != NewAlign) && (CurrSize != NewAlign);
87 const bool NeedsAlignment = CurrAlign.getQuantity() != NewAlign.getQuantity();
89 if (!NeedsAlignment && !NeedsPacking)
95 if (NeedsPacking && !IsPacked) {
96 diag(Struct->getLocation(),
97 "accessing fields in struct %0 is inefficient due to padding; only "
98 "needs %1 bytes but is using %2 bytes")
99 << Struct << MinByteSize.getQuantity() << CurrSize.getQuantity()
100 << FixItHint::CreateInsertion(Struct->getEndLoc().getLocWithOffset(1),
101 " __attribute__((packed))");
102 diag(Struct->getLocation(),
103 "use \"__attribute__((packed))\" to reduce the amount of padding "
104 "applied to struct %0",
110 auto *Attribute = Struct->getAttr<AlignedAttr>();
111 const std::string NewAlignQuantity = std::to_string(NewAlign.getQuantity());
113 FixIt = FixItHint::CreateReplacement(
114 Attribute->getRange(),
115 (Twine(
"aligned(") + NewAlignQuantity +
")").str());
117 FixIt = FixItHint::CreateInsertion(
118 Struct->getEndLoc().getLocWithOffset(1),
119 (Twine(
" __attribute__((aligned(") + NewAlignQuantity +
")))").str());
124 if (NeedsAlignment) {
125 diag(Struct->getLocation(),
126 "accessing fields in struct %0 is inefficient due to poor alignment; "
127 "currently aligned to %1 bytes, but recommended alignment is %2 bytes")
128 << Struct << CurrAlign.getQuantity() << NewAlignQuantity << FixIt;
130 diag(Struct->getLocation(),
131 "use \"__attribute__((aligned(%0)))\" to align struct %1 to %0 bytes",
133 << NewAlignQuantity << Struct;