LayoutOverrideSource.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //===--- LayoutOverrideSource.cpp --Override Record Layouts ---------------===//
  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. #include "clang/Frontend/LayoutOverrideSource.h"
  9. #include "clang/AST/Decl.h"
  10. #include "clang/Basic/CharInfo.h"
  11. #include "llvm/Support/raw_ostream.h"
  12. #include <fstream>
  13. #include <string>
  14. using namespace clang;
  15. /// Parse a simple identifier.
  16. static std::string parseName(StringRef S) {
  17. if (S.empty() || !isAsciiIdentifierStart(S[0]))
  18. return "";
  19. unsigned Offset = 1;
  20. while (Offset < S.size() && isAsciiIdentifierContinue(S[Offset]))
  21. ++Offset;
  22. return S.substr(0, Offset).str();
  23. }
  24. LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {
  25. std::ifstream Input(Filename.str().c_str());
  26. if (!Input.is_open())
  27. return;
  28. // Parse the output of -fdump-record-layouts.
  29. std::string CurrentType;
  30. Layout CurrentLayout;
  31. bool ExpectingType = false;
  32. while (Input.good()) {
  33. std::string Line;
  34. getline(Input, Line);
  35. StringRef LineStr(Line);
  36. // Determine whether the following line will start a
  37. if (LineStr.contains("*** Dumping AST Record Layout")) {
  38. // Flush the last type/layout, if there is one.
  39. if (!CurrentType.empty())
  40. Layouts[CurrentType] = CurrentLayout;
  41. CurrentLayout = Layout();
  42. ExpectingType = true;
  43. continue;
  44. }
  45. // If we're expecting a type, grab it.
  46. if (ExpectingType) {
  47. ExpectingType = false;
  48. StringRef::size_type Pos;
  49. if ((Pos = LineStr.find("struct ")) != StringRef::npos)
  50. LineStr = LineStr.substr(Pos + strlen("struct "));
  51. else if ((Pos = LineStr.find("class ")) != StringRef::npos)
  52. LineStr = LineStr.substr(Pos + strlen("class "));
  53. else if ((Pos = LineStr.find("union ")) != StringRef::npos)
  54. LineStr = LineStr.substr(Pos + strlen("union "));
  55. else
  56. continue;
  57. // Find the name of the type.
  58. CurrentType = parseName(LineStr);
  59. CurrentLayout = Layout();
  60. continue;
  61. }
  62. // Check for the size of the type.
  63. StringRef::size_type Pos = LineStr.find(" Size:");
  64. if (Pos != StringRef::npos) {
  65. // Skip past the " Size:" prefix.
  66. LineStr = LineStr.substr(Pos + strlen(" Size:"));
  67. unsigned long long Size = 0;
  68. (void)LineStr.getAsInteger(10, Size);
  69. CurrentLayout.Size = Size;
  70. continue;
  71. }
  72. // Check for the alignment of the type.
  73. Pos = LineStr.find("Alignment:");
  74. if (Pos != StringRef::npos) {
  75. // Skip past the "Alignment:" prefix.
  76. LineStr = LineStr.substr(Pos + strlen("Alignment:"));
  77. unsigned long long Alignment = 0;
  78. (void)LineStr.getAsInteger(10, Alignment);
  79. CurrentLayout.Align = Alignment;
  80. continue;
  81. }
  82. // Check for the size/alignment of the type.
  83. Pos = LineStr.find("sizeof=");
  84. if (Pos != StringRef::npos) {
  85. /* Skip past the sizeof= prefix. */
  86. LineStr = LineStr.substr(Pos + strlen("sizeof="));
  87. // Parse size.
  88. unsigned long long Size = 0;
  89. (void)LineStr.getAsInteger(10, Size);
  90. CurrentLayout.Size = Size;
  91. Pos = LineStr.find("align=");
  92. if (Pos != StringRef::npos) {
  93. /* Skip past the align= prefix. */
  94. LineStr = LineStr.substr(Pos + strlen("align="));
  95. // Parse alignment.
  96. unsigned long long Alignment = 0;
  97. (void)LineStr.getAsInteger(10, Alignment);
  98. CurrentLayout.Align = Alignment;
  99. }
  100. continue;
  101. }
  102. // Check for the field offsets of the type.
  103. Pos = LineStr.find("FieldOffsets: [");
  104. if (Pos == StringRef::npos)
  105. continue;
  106. LineStr = LineStr.substr(Pos + strlen("FieldOffsets: ["));
  107. while (!LineStr.empty() && isDigit(LineStr[0])) {
  108. // Parse this offset.
  109. unsigned Idx = 1;
  110. while (Idx < LineStr.size() && isDigit(LineStr[Idx]))
  111. ++Idx;
  112. unsigned long long Offset = 0;
  113. (void)LineStr.substr(0, Idx).getAsInteger(10, Offset);
  114. CurrentLayout.FieldOffsets.push_back(Offset);
  115. // Skip over this offset, the following comma, and any spaces.
  116. LineStr = LineStr.substr(Idx + 1);
  117. while (!LineStr.empty() && isWhitespace(LineStr[0]))
  118. LineStr = LineStr.substr(1);
  119. }
  120. }
  121. // Flush the last type/layout, if there is one.
  122. if (!CurrentType.empty())
  123. Layouts[CurrentType] = CurrentLayout;
  124. }
  125. bool
  126. LayoutOverrideSource::layoutRecordType(const RecordDecl *Record,
  127. uint64_t &Size, uint64_t &Alignment,
  128. llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,
  129. llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,
  130. llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)
  131. {
  132. // We can't override unnamed declarations.
  133. if (!Record->getIdentifier())
  134. return false;
  135. // Check whether we have a layout for this record.
  136. llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName());
  137. if (Known == Layouts.end())
  138. return false;
  139. // Provide field layouts.
  140. unsigned NumFields = 0;
  141. for (RecordDecl::field_iterator F = Record->field_begin(),
  142. FEnd = Record->field_end();
  143. F != FEnd; ++F, ++NumFields) {
  144. if (NumFields >= Known->second.FieldOffsets.size())
  145. continue;
  146. FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];
  147. }
  148. // Wrong number of fields.
  149. if (NumFields != Known->second.FieldOffsets.size())
  150. return false;
  151. Size = Known->second.Size;
  152. Alignment = Known->second.Align;
  153. return true;
  154. }
  155. LLVM_DUMP_METHOD void LayoutOverrideSource::dump() {
  156. raw_ostream &OS = llvm::errs();
  157. for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),
  158. LEnd = Layouts.end();
  159. L != LEnd; ++L) {
  160. OS << "Type: blah " << L->first() << '\n';
  161. OS << " Size:" << L->second.Size << '\n';
  162. OS << " Alignment:" << L->second.Align << '\n';
  163. OS << " FieldOffsets: [";
  164. for (unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {
  165. if (I)
  166. OS << ", ";
  167. OS << L->second.FieldOffsets[I];
  168. }
  169. OS << "]\n";
  170. }
  171. }