summarylogtreecommitdiffstats
path: root/hover-record-paddings.patch
blob: fe46a8b6ba4c0f269923bc0baed5a2f37b49c110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index a868d3bb4e3f..1fa4fedb3fbb 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -995,6 +995,52 @@ bool isHardLineBreakAfter(llvm::StringRef Line, llvm::StringRef Rest) {
   return punctuationIndicatesLineBreak(Line) || isHardLineBreakIndicator(Rest);
 }
 
+static uint64_t calcRecordPaddings(const RecordDecl *Record, HoverInfo &HI,
+                                   const ASTContext &Ctx) {
+  std::optional<uint64_t> TotalPaddings;
+  if (const auto *CxxRecord = llvm::dyn_cast<CXXRecordDecl>(Record)) {
+    for (const auto &Base : CxxRecord->bases()) {
+      if (Base.isVirtual())
+        continue;
+      if (const auto *RecordBase = Base.getType()->getAsRecordDecl()) {
+        if (const auto Paddings = calcRecordPaddings(RecordBase, HI, Ctx)) {
+          if (!TotalPaddings)
+            TotalPaddings = Paddings;
+          else
+            *TotalPaddings += Paddings;
+        }
+      }
+    }
+  }
+
+  uint64_t RecordSize = 0;
+  if (auto Size = Ctx.getTypeSizeInCharsIfKnown(Record->getTypeForDecl()))
+    RecordSize = Size->getQuantity() * 8;
+  const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Record);
+  const auto FieldsCount = Layout.getFieldCount();
+  for (const auto &Field : Record->fields()) {
+    const auto FiledID = Field->getFieldIndex();
+    uint64_t FieldSize = 0;
+    if (Field->isBitField())
+      FieldSize = Field->getBitWidthValue(Ctx);
+    else if (auto Size = Ctx.getTypeSizeInCharsIfKnown(Field->getType()))
+      FieldSize = Field->isZeroSize(Ctx) ? 0 : Size->getQuantity() * 8;
+    const auto EndOfField = Layout.getFieldOffset(FiledID) + FieldSize;
+    const auto NextOffset = FiledID < FieldsCount - 1
+                                ? Layout.getFieldOffset(FiledID + 1)
+                                : (RecordSize != 0 ? RecordSize : EndOfField);
+    const auto Padding = NextOffset - EndOfField;
+    if (!TotalPaddings)
+      TotalPaddings = Padding;
+    else if (Record->isUnion())
+      TotalPaddings = std::min(*TotalPaddings, Padding);
+    else
+      *TotalPaddings += Padding;
+  }
+
+  return TotalPaddings ? *TotalPaddings : 0;
+}
+
 void addLayoutInfo(const NamedDecl &ND, HoverInfo &HI) {
   if (ND.isInvalidDecl())
     return;
@@ -1003,8 +1049,11 @@ void addLayoutInfo(const NamedDecl &ND, HoverInfo &HI) {
   if (auto *RD = llvm::dyn_cast<RecordDecl>(&ND)) {
     if (auto Size = Ctx.getTypeSizeInCharsIfKnown(RD->getTypeForDecl()))
       HI.Size = Size->getQuantity() * 8;
-    if (!RD->isDependentType() && RD->isCompleteDefinition())
+    if (!RD->isDependentType() && RD->isCompleteDefinition()) {
       HI.Align = Ctx.getTypeAlign(RD->getTypeForDecl());
+      if (const auto *Record = RD->getDefinition(); Record && HI.Size)
+        HI.Padding = calcRecordPaddings(Record, HI, Ctx);
+    }
     return;
   }
 
@@ -1422,6 +1471,11 @@ static std::string formatOffset(uint64_t OffsetInBits) {
   return Offset;
 }
 
+static bool isKindRecord(index::SymbolKind Kind) {
+  return Kind == index::SymbolKind::Class ||
+         Kind == index::SymbolKind::Struct || Kind == index::SymbolKind::Union;
+}
+
 markup::Document HoverInfo::present() const {
   markup::Document Output;
 
@@ -1488,13 +1542,16 @@ markup::Document HoverInfo::present() const {
     Output.addParagraph().appendText("Offset: " + formatOffset(*Offset));
   if (Size) {
     auto &P = Output.addParagraph().appendText("Size: " + formatSize(*Size));
-    if (Padding && *Padding != 0) {
+    if (Padding && *Padding != 0 && !isKindRecord(Kind)) {
       P.appendText(
           llvm::formatv(" (+{0} padding)", formatSize(*Padding)).str());
     }
     if (Align)
       P.appendText(", alignment " + formatSize(*Align));
   }
+  if (Padding && *Padding != 0 && isKindRecord(Kind)) {
+    Output.addParagraph().appendText("Paddings: " + formatOffset(*Padding));
+  }
 
   if (CalleeArgInfo) {
     assert(CallPassType);