45 const auto *Struct = Result.Nodes.getNodeAs<RecordDecl>(
"struct");
49 if (Struct->isTemplated())
53 if (Struct->isInvalidDecl())
57 llvm::SmallVector<std::pair<unsigned int, unsigned int>, 10> FieldSizes;
58 unsigned int TotalBitSize = 0;
59 for (
const FieldDecl *StructField : Struct->fields()) {
63 const QualType StructFieldTy = StructField->getType();
64 if (StructFieldTy->isIncompleteType())
66 const unsigned int StructFieldWidth =
67 (
unsigned int)Result.Context->getTypeInfo(StructFieldTy.getTypePtr())
69 FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
72 TotalBitSize += StructFieldWidth;
75 const uint64_t CharSize = Result.Context->getCharWidth();
76 const CharUnits CurrSize =
77 Result.Context->getASTRecordLayout(Struct).getSize();
78 const CharUnits MinByteSize =
79 CharUnits::fromQuantity(std::max<clang::CharUnits::QuantityType>(
80 std::ceil(
static_cast<float>(TotalBitSize) / CharSize), 1));
81 const CharUnits MaxAlign = CharUnits::fromQuantity(
82 std::ceil((
float)Struct->getMaxAlignment() / CharSize));
83 const CharUnits CurrAlign =
84 Result.Context->getASTRecordLayout(Struct).getAlignment();
85 const CharUnits NewAlign = computeRecommendedAlignment(MinByteSize);
87 const bool IsPacked = Struct->hasAttr<PackedAttr>();
88 const bool NeedsPacking = (MinByteSize < CurrSize) &&
89 (MaxAlign != NewAlign) && (CurrSize != NewAlign);
90 const bool NeedsAlignment = CurrAlign.getQuantity() != NewAlign.getQuantity();
92 if (!NeedsAlignment && !NeedsPacking)
98 if (NeedsPacking && !IsPacked) {
99 diag(Struct->getLocation(),
100 "accessing fields in struct %0 is inefficient due to padding; only "
101 "needs %1 bytes but is using %2 bytes")
102 << Struct << (int)MinByteSize.getQuantity()
103 << (int)CurrSize.getQuantity()
104 << FixItHint::CreateInsertion(Struct->getEndLoc().getLocWithOffset(1),
105 " __attribute__((packed))");
106 diag(Struct->getLocation(),
107 "use \"__attribute__((packed))\" to reduce the amount of padding "
108 "applied to struct %0",
114 auto *Attribute = Struct->getAttr<AlignedAttr>();
115 const std::string NewAlignQuantity =
116 std::to_string((
int)NewAlign.getQuantity());
118 FixIt = FixItHint::CreateReplacement(
119 Attribute->getRange(),
120 (Twine(
"aligned(") + NewAlignQuantity +
")").str());
122 FixIt = FixItHint::CreateInsertion(
123 Struct->getEndLoc().getLocWithOffset(1),
124 (Twine(
" __attribute__((aligned(") + NewAlignQuantity +
")))").str());
129 if (NeedsAlignment) {
130 diag(Struct->getLocation(),
131 "accessing fields in struct %0 is inefficient due to poor alignment; "
132 "currently aligned to %1 bytes, but recommended alignment is %2 bytes")
133 << Struct << (int)CurrAlign.getQuantity() << NewAlignQuantity << FixIt;
135 diag(Struct->getLocation(),
136 "use \"__attribute__((aligned(%0)))\" to align struct %1 to %0 bytes",
138 << NewAlignQuantity << Struct;