clang-tools  15.0.0git
StructPackAlignCheck.cpp
Go to the documentation of this file.
1 //===--- 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"
13 #include <math.h>
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace altera {
20 
21 void StructPackAlignCheck::registerMatchers(MatchFinder *Finder) {
22  Finder->addMatcher(recordDecl(isStruct(), isDefinition(),
23  unless(isExpansionInSystemHeader()))
24  .bind("struct"),
25  this);
26 }
27 
28 CharUnits
29 StructPackAlignCheck::computeRecommendedAlignment(CharUnits MinByteSize) {
30  CharUnits NewAlign = CharUnits::fromQuantity(1);
31  if (!MinByteSize.isPowerOfTwo()) {
32  int MSB = (int)MinByteSize.getQuantity();
33  for (; MSB > 0; MSB /= 2) {
34  NewAlign = NewAlign.alignTo(
35  CharUnits::fromQuantity(((int)NewAlign.getQuantity()) * 2));
36  // Abort if the computed alignment meets the maximum configured alignment.
37  if (NewAlign.getQuantity() >= MaxConfiguredAlignment)
38  break;
39  }
40  } else {
41  NewAlign = MinByteSize;
42  }
43  return NewAlign;
44 }
45 
46 void StructPackAlignCheck::check(const MatchFinder::MatchResult &Result) {
47  const auto *Struct = Result.Nodes.getNodeAs<RecordDecl>("struct");
48 
49  // Do not trigger on templated struct declarations because the packing and
50  // alignment requirements are unknown.
51  if (Struct->isTemplated())
52  return;
53 
54  // Packing and alignment requirements for invalid decls are meaningless.
55  if (Struct->isInvalidDecl())
56  return;
57 
58  // Get sizing info for the struct.
59  llvm::SmallVector<std::pair<unsigned int, unsigned int>, 10> FieldSizes;
60  unsigned int TotalBitSize = 0;
61  for (const FieldDecl *StructField : Struct->fields()) {
62  // For each StructField, record how big it is (in bits).
63  // Would be good to use a pair of <offset, size> to advise a better
64  // packing order.
65  QualType StructFieldTy = StructField->getType();
66  if (StructFieldTy->isIncompleteType())
67  return;
68  unsigned int StructFieldWidth =
69  (unsigned int)Result.Context->getTypeInfo(StructFieldTy.getTypePtr())
70  .Width;
71  FieldSizes.emplace_back(StructFieldWidth, StructField->getFieldIndex());
72  // FIXME: Recommend a reorganization of the struct (sort by StructField
73  // size, largest to smallest).
74  TotalBitSize += StructFieldWidth;
75  }
76 
77  uint64_t CharSize = Result.Context->getCharWidth();
78  CharUnits CurrSize = Result.Context->getASTRecordLayout(Struct).getSize();
79  CharUnits MinByteSize =
80  CharUnits::fromQuantity(std::max<clang::CharUnits::QuantityType>(
81  ceil(static_cast<float>(TotalBitSize) / CharSize), 1));
82  CharUnits MaxAlign = CharUnits::fromQuantity(
83  ceil((float)Struct->getMaxAlignment() / CharSize));
84  CharUnits CurrAlign =
85  Result.Context->getASTRecordLayout(Struct).getAlignment();
86  CharUnits NewAlign = computeRecommendedAlignment(MinByteSize);
87 
88  bool IsPacked = Struct->hasAttr<PackedAttr>();
89  bool NeedsPacking = (MinByteSize < CurrSize) && (MaxAlign != NewAlign) &&
90  (CurrSize != NewAlign);
91  bool NeedsAlignment = CurrAlign.getQuantity() != NewAlign.getQuantity();
92 
93  if (!NeedsAlignment && !NeedsPacking)
94  return;
95 
96  // If it's using much more space than it needs, suggest packing.
97  // (Do not suggest packing if it is currently explicitly aligned to what the
98  // minimum byte size would suggest as the new alignment.)
99  if (NeedsPacking && !IsPacked) {
100  diag(Struct->getLocation(),
101  "accessing fields in struct %0 is inefficient due to padding; only "
102  "needs %1 bytes but is using %2 bytes")
103  << Struct << (int)MinByteSize.getQuantity()
104  << (int)CurrSize.getQuantity()
105  << FixItHint::CreateInsertion(Struct->getEndLoc().getLocWithOffset(1),
106  " __attribute__((packed))");
107  diag(Struct->getLocation(),
108  "use \"__attribute__((packed))\" to reduce the amount of padding "
109  "applied to struct %0",
110  DiagnosticIDs::Note)
111  << Struct;
112  }
113 
114  FixItHint FixIt;
115  AlignedAttr *Attribute = Struct->getAttr<AlignedAttr>();
116  std::string NewAlignQuantity = std::to_string((int)NewAlign.getQuantity());
117  if (Attribute) {
118  FixIt = FixItHint::CreateReplacement(
119  Attribute->getRange(),
120  (Twine("aligned(") + NewAlignQuantity + ")").str());
121  } else {
122  FixIt = FixItHint::CreateInsertion(
123  Struct->getEndLoc().getLocWithOffset(1),
124  (Twine(" __attribute__((aligned(") + NewAlignQuantity + ")))").str());
125  }
126 
127  // And suggest the minimum power-of-two alignment for the struct as a whole
128  // (with and without packing).
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;
134 
135  diag(Struct->getLocation(),
136  "use \"__attribute__((aligned(%0)))\" to align struct %1 to %0 bytes",
137  DiagnosticIDs::Note)
138  << NewAlignQuantity << Struct;
139  }
140 }
141 
142 void StructPackAlignCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
143  Options.store(Opts, "MaxConfiguredAlignment", MaxConfiguredAlignment);
144 }
145 
146 } // namespace altera
147 } // namespace tidy
148 } // namespace clang
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::clangd::check
bool check(llvm::StringRef File, llvm::Optional< Range > LineRange, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:273
clang::ast_matchers
Definition: AbseilMatcher.h:14
FixIt
llvm::Optional< FixItHint > FixIt
Definition: UppercaseLiteralSuffixCheck.cpp:63
StructPackAlignCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27