LayoutOverrideSource.cpp 6.4 KB

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