10#include "clang/AST/ASTContext.h"
11#include "clang/AST/RecordLayout.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
20 Finder->addMatcher(recordDecl(isStruct(), isDefinition(),
21 unless(isExpansionInSystemHeader()))
27StructPackAlignCheck::computeRecommendedAlignment(CharUnits MinByteSize)
const {
28 CharUnits NewAlign = CharUnits::fromQuantity(1);
29 if (!MinByteSize.isPowerOfTwo()) {
30 CharUnits::QuantityType MSB = MinByteSize.getQuantity();
31 for (; MSB > 0; MSB /= 2) {
33 NewAlign.alignTo(CharUnits::fromQuantity(NewAlign.getQuantity() * 2));
35 if (NewAlign.getQuantity() >= MaxConfiguredAlignment)
39 NewAlign = MinByteSize;
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 QualType StructFieldTy = StructField->getType();
64 if (StructFieldTy->isIncompleteType())
66 unsigned int StructFieldWidth =
67 (
unsigned int)Result.Context->getTypeInfo(StructFieldTy.getTypePtr())
69 FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
72 TotalBitSize += StructFieldWidth;
75 uint64_t CharSize = Result.Context->getCharWidth();
76 CharUnits CurrSize = Result.Context->getASTRecordLayout(Struct).getSize();
77 CharUnits MinByteSize =
78 CharUnits::fromQuantity(std::max<clang::CharUnits::QuantityType>(
79 ceil(
static_cast<float>(TotalBitSize) / CharSize), 1));
80 CharUnits MaxAlign = CharUnits::fromQuantity(
81 ceil((
float)Struct->getMaxAlignment() / CharSize));
83 Result.Context->getASTRecordLayout(Struct).getAlignment();
84 CharUnits NewAlign = computeRecommendedAlignment(MinByteSize);
86 bool IsPacked = Struct->hasAttr<PackedAttr>();
87 bool NeedsPacking = (MinByteSize < CurrSize) && (MaxAlign != NewAlign) &&
88 (CurrSize != NewAlign);
89 bool NeedsAlignment = CurrAlign.getQuantity() != NewAlign.getQuantity();
91 if (!NeedsAlignment && !NeedsPacking)
97 if (NeedsPacking && !IsPacked) {
98 diag(Struct->getLocation(),
99 "accessing fields in struct %0 is inefficient due to padding; only "
100 "needs %1 bytes but is using %2 bytes")
101 << Struct << (int)MinByteSize.getQuantity()
102 << (int)CurrSize.getQuantity()
103 << FixItHint::CreateInsertion(Struct->getEndLoc().getLocWithOffset(1),
104 " __attribute__((packed))");
105 diag(Struct->getLocation(),
106 "use \"__attribute__((packed))\" to reduce the amount of padding "
107 "applied to struct %0",
113 auto *Attribute = Struct->getAttr<AlignedAttr>();
114 std::string NewAlignQuantity = std::to_string((
int)NewAlign.getQuantity());
116 FixIt = FixItHint::CreateReplacement(
117 Attribute->getRange(),
118 (Twine(
"aligned(") + NewAlignQuantity +
")").str());
120 FixIt = FixItHint::CreateInsertion(
121 Struct->getEndLoc().getLocWithOffset(1),
122 (Twine(
" __attribute__((aligned(") + NewAlignQuantity +
")))").str());
127 if (NeedsAlignment) {
128 diag(Struct->getLocation(),
129 "accessing fields in struct %0 is inefficient due to poor alignment; "
130 "currently aligned to %1 bytes, but recommended alignment is %2 bytes")
131 << Struct << (int)CurrAlign.getQuantity() << NewAlignQuantity <<
FixIt;
133 diag(Struct->getLocation(),
134 "use \"__attribute__((aligned(%0)))\" to align struct %1 to %0 bytes",
136 << NewAlignQuantity << Struct;
141 Options.
store(Opts,
"MaxConfiguredAlignment", MaxConfiguredAlignment);
std::optional< FixItHint > FixIt
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
llvm::StringMap< ClangTidyValue > OptionMap