clang-tools 23.0.0git
StructPackAlignCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "clang/AST/ASTContext.h"
11#include "clang/AST/RecordLayout.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include <cmath>
14
15using namespace clang::ast_matchers;
16
17namespace clang::tidy::altera {
18
19void StructPackAlignCheck::registerMatchers(MatchFinder *Finder) {
20 Finder->addMatcher(recordDecl(isStruct(), isDefinition()).bind("struct"),
21 this);
22}
23
24CharUnits
25StructPackAlignCheck::computeRecommendedAlignment(CharUnits MinByteSize) const {
26 CharUnits NewAlign = CharUnits::fromQuantity(1);
27 if (!MinByteSize.isPowerOfTwo()) {
28 CharUnits::QuantityType MSB = MinByteSize.getQuantity();
29 for (; MSB > 0; MSB /= 2) {
30 NewAlign =
31 NewAlign.alignTo(CharUnits::fromQuantity(NewAlign.getQuantity() * 2));
32 // Abort if the computed alignment meets the maximum configured alignment.
33 if (NewAlign.getQuantity() >= MaxConfiguredAlignment)
34 break;
35 }
36 } else {
37 NewAlign = MinByteSize;
38 }
39 return NewAlign;
40}
41
42void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
43 const auto *Struct = Result.Nodes.getNodeAs<RecordDecl>("struct");
44
45 // Do not trigger on templated struct declarations because the packing and
46 // alignment requirements are unknown.
47 if (Struct->isTemplated())
48 return;
49
50 // Packing and alignment requirements for invalid decls are meaningless.
51 if (Struct->isInvalidDecl())
52 return;
53
54 // Get sizing info for the struct.
56 unsigned int TotalBitSize = 0;
57 for (const FieldDecl *StructField : Struct->fields()) {
58 // For each StructField, record how big it is (in bits).
59 // Would be good to use a pair of <offset, size> to advise a better
60 // packing order.
61 const QualType StructFieldTy = StructField->getType();
62 if (StructFieldTy->isIncompleteType())
63 return;
64 const unsigned int StructFieldWidth = static_cast<unsigned int>(
65 Result.Context->getTypeInfo(StructFieldTy.getTypePtr()).Width);
66 FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
67 // FIXME: Recommend a reorganization of the struct (sort by StructField
68 // size, largest to smallest).
69 TotalBitSize += StructFieldWidth;
70 }
71
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);
83
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();
88
89 if (!NeedsAlignment && !NeedsPacking)
90 return;
91
92 // If it's using much more space than it needs, suggest packing.
93 // (Do not suggest packing if it is currently explicitly aligned to what the
94 // minimum byte size would suggest as the new alignment.)
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",
105 DiagnosticIDs::Note)
106 << Struct;
107 }
108
109 FixItHint FixIt;
110 auto *Attribute = Struct->getAttr<AlignedAttr>();
111 const std::string NewAlignQuantity = std::to_string(NewAlign.getQuantity());
112 if (Attribute) {
113 FixIt = FixItHint::CreateReplacement(
114 Attribute->getRange(),
115 (Twine("aligned(") + NewAlignQuantity + ")").str());
116 } else {
117 FixIt = FixItHint::CreateInsertion(
118 Struct->getEndLoc().getLocWithOffset(1),
119 (Twine(" __attribute__((aligned(") + NewAlignQuantity + ")))").str());
120 }
121
122 // And suggest the minimum power-of-two alignment for the struct as a whole
123 // (with and without packing).
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;
129
130 diag(Struct->getLocation(),
131 "use \"__attribute__((aligned(%0)))\" to align struct %1 to %0 bytes",
132 DiagnosticIDs::Note)
133 << NewAlignQuantity << Struct;
134 }
135}
136
138 Options.store(Opts, "MaxConfiguredAlignment", MaxConfiguredAlignment);
139}
140
141} // namespace clang::tidy::altera
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
llvm::StringMap< ClangTidyValue > OptionMap