blob: cdaedd440959ccdd3abbcaea357101909b845fb6 [file] [log] [blame]
Frank Derry Wanye156b1272020-09-08 09:35:14 -04001//===--- StructPackAlignCheck.cpp - clang-tidy ----------------------------===//
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
9#include "StructPackAlignCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/AST/RecordLayout.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
Piotr Zegarc8644b12023-08-26 17:50:50 +000013#include <cmath>
Frank Derry Wanye156b1272020-09-08 09:35:14 -040014
15using namespace clang::ast_matchers;
16
Carlos Galvez7d2ea6c2023-01-14 17:40:54 +000017namespace clang::tidy::altera {
Frank Derry Wanye156b1272020-09-08 09:35:14 -040018
19void StructPackAlignCheck::registerMatchers(MatchFinder *Finder) {
Matheus Izvekovee1f1322022-10-31 19:34:23 +010020 Finder->addMatcher(recordDecl(isStruct(), isDefinition(),
21 unless(isExpansionInSystemHeader()))
22 .bind("struct"),
23 this);
Frank Derry Wanye156b1272020-09-08 09:35:14 -040024}
25
26CharUnits
Piotr Zegar24a75872023-08-26 19:00:25 +000027StructPackAlignCheck::computeRecommendedAlignment(CharUnits MinByteSize) const {
Frank Derry Wanye156b1272020-09-08 09:35:14 -040028 CharUnits NewAlign = CharUnits::fromQuantity(1);
29 if (!MinByteSize.isPowerOfTwo()) {
Piotr Zegar44b38fe2023-08-26 19:25:37 +000030 CharUnits::QuantityType MSB = MinByteSize.getQuantity();
Frank Derry Wanye156b1272020-09-08 09:35:14 -040031 for (; MSB > 0; MSB /= 2) {
Piotr Zegar44b38fe2023-08-26 19:25:37 +000032 NewAlign =
33 NewAlign.alignTo(CharUnits::fromQuantity(NewAlign.getQuantity() * 2));
Frank Derry Wanye156b1272020-09-08 09:35:14 -040034 // Abort if the computed alignment meets the maximum configured alignment.
35 if (NewAlign.getQuantity() >= MaxConfiguredAlignment)
36 break;
37 }
38 } else {
39 NewAlign = MinByteSize;
40 }
41 return NewAlign;
42}
43
44void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
45 const auto *Struct = Result.Nodes.getNodeAs<RecordDecl>("struct");
46
47 // Do not trigger on templated struct declarations because the packing and
48 // alignment requirements are unknown.
49 if (Struct->isTemplated())
50 return;
51
Balazs Benicse1d06732021-11-29 09:56:43 +010052 // Packing and alignment requirements for invalid decls are meaningless.
53 if (Struct->isInvalidDecl())
54 return;
55
Frank Derry Wanye156b1272020-09-08 09:35:14 -040056 // Get sizing info for the struct.
57 llvm::SmallVector<std::pair<unsigned int, unsigned int>, 10> FieldSizes;
58 unsigned int TotalBitSize = 0;
59 for (const FieldDecl *StructField : Struct->fields()) {
60 // For each StructField, record how big it is (in bits).
61 // Would be good to use a pair of <offset, size> to advise a better
62 // packing order.
Georgy Komarovab92a4c2021-05-16 08:27:46 +030063 QualType StructFieldTy = StructField->getType();
64 if (StructFieldTy->isIncompleteType())
65 return;
Frank Derry Wanye156b1272020-09-08 09:35:14 -040066 unsigned int StructFieldWidth =
Georgy Komarovab92a4c2021-05-16 08:27:46 +030067 (unsigned int)Result.Context->getTypeInfo(StructFieldTy.getTypePtr())
Frank Derry Wanye156b1272020-09-08 09:35:14 -040068 .Width;
69 FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
70 // FIXME: Recommend a reorganization of the struct (sort by StructField
71 // size, largest to smallest).
72 TotalBitSize += StructFieldWidth;
73 }
74
75 uint64_t CharSize = Result.Context->getCharWidth();
76 CharUnits CurrSize = Result.Context->getASTRecordLayout(Struct).getSize();
77 CharUnits MinByteSize =
Fabian Wolfff25935a2022-04-20 16:52:23 +020078 CharUnits::fromQuantity(std::max<clang::CharUnits::QuantityType>(
79 ceil(static_cast<float>(TotalBitSize) / CharSize), 1));
Frank Derry Wanye156b1272020-09-08 09:35:14 -040080 CharUnits MaxAlign = CharUnits::fromQuantity(
81 ceil((float)Struct->getMaxAlignment() / CharSize));
82 CharUnits CurrAlign =
83 Result.Context->getASTRecordLayout(Struct).getAlignment();
84 CharUnits NewAlign = computeRecommendedAlignment(MinByteSize);
85
86 bool IsPacked = Struct->hasAttr<PackedAttr>();
87 bool NeedsPacking = (MinByteSize < CurrSize) && (MaxAlign != NewAlign) &&
88 (CurrSize != NewAlign);
89 bool NeedsAlignment = CurrAlign.getQuantity() != NewAlign.getQuantity();
90
91 if (!NeedsAlignment && !NeedsPacking)
92 return;
93
94 // If it's using much more space than it needs, suggest packing.
95 // (Do not suggest packing if it is currently explicitly aligned to what the
96 // minimum byte size would suggest as the new alignment.)
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",
108 DiagnosticIDs::Note)
109 << Struct;
110 }
111
112 FixItHint FixIt;
Piotr Zegarfc2a9ad2023-08-27 09:32:28 +0000113 auto *Attribute = Struct->getAttr<AlignedAttr>();
Frank Derry Wanye156b1272020-09-08 09:35:14 -0400114 std::string NewAlignQuantity = std::to_string((int)NewAlign.getQuantity());
115 if (Attribute) {
Nathan James00c7d662021-03-02 21:57:16 +0000116 FixIt = FixItHint::CreateReplacement(
117 Attribute->getRange(),
118 (Twine("aligned(") + NewAlignQuantity + ")").str());
Frank Derry Wanye156b1272020-09-08 09:35:14 -0400119 } else {
Nathan James00c7d662021-03-02 21:57:16 +0000120 FixIt = FixItHint::CreateInsertion(
121 Struct->getEndLoc().getLocWithOffset(1),
122 (Twine(" __attribute__((aligned(") + NewAlignQuantity + ")))").str());
Frank Derry Wanye156b1272020-09-08 09:35:14 -0400123 }
124
125 // And suggest the minimum power-of-two alignment for the struct as a whole
126 // (with and without packing).
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;
132
133 diag(Struct->getLocation(),
134 "use \"__attribute__((aligned(%0)))\" to align struct %1 to %0 bytes",
135 DiagnosticIDs::Note)
136 << NewAlignQuantity << Struct;
137 }
138}
139
140void StructPackAlignCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
141 Options.store(Opts, "MaxConfiguredAlignment", MaxConfiguredAlignment);
142}
143
Carlos Galvez7d2ea6c2023-01-14 17:40:54 +0000144} // namespace clang::tidy::altera