LayoutOverrideSource.cpp 6.2 KB

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