clang API Documentation
00001 //===--- LayoutOverrideSource.cpp --Override Record Layouts ---------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 #include "clang/Frontend/LayoutOverrideSource.h" 00010 #include "clang/AST/Decl.h" 00011 #include "clang/Basic/CharInfo.h" 00012 #include "llvm/Support/raw_ostream.h" 00013 #include <fstream> 00014 #include <string> 00015 00016 using namespace clang; 00017 00018 /// \brief Parse a simple identifier. 00019 static std::string parseName(StringRef S) { 00020 if (S.empty() || !isIdentifierHead(S[0])) 00021 return ""; 00022 00023 unsigned Offset = 1; 00024 while (Offset < S.size() && isIdentifierBody(S[Offset])) 00025 ++Offset; 00026 00027 return S.substr(0, Offset).str(); 00028 } 00029 00030 LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) { 00031 std::ifstream Input(Filename.str().c_str()); 00032 if (!Input.is_open()) 00033 return; 00034 00035 // Parse the output of -fdump-record-layouts. 00036 std::string CurrentType; 00037 Layout CurrentLayout; 00038 bool ExpectingType = false; 00039 00040 while (Input.good()) { 00041 std::string Line; 00042 getline(Input, Line); 00043 00044 StringRef LineStr(Line); 00045 00046 // Determine whether the following line will start a 00047 if (LineStr.find("*** Dumping AST Record Layout") != StringRef::npos) { 00048 // Flush the last type/layout, if there is one. 00049 if (!CurrentType.empty()) 00050 Layouts[CurrentType] = CurrentLayout; 00051 CurrentLayout = Layout(); 00052 00053 ExpectingType = true; 00054 continue; 00055 } 00056 00057 // If we're expecting a type, grab it. 00058 if (ExpectingType) { 00059 ExpectingType = false; 00060 00061 StringRef::size_type Pos; 00062 if ((Pos = LineStr.find("struct ")) != StringRef::npos) 00063 LineStr = LineStr.substr(Pos + strlen("struct ")); 00064 else if ((Pos = LineStr.find("class ")) != StringRef::npos) 00065 LineStr = LineStr.substr(Pos + strlen("class ")); 00066 else if ((Pos = LineStr.find("union ")) != StringRef::npos) 00067 LineStr = LineStr.substr(Pos + strlen("union ")); 00068 else 00069 continue; 00070 00071 // Find the name of the type. 00072 CurrentType = parseName(LineStr); 00073 CurrentLayout = Layout(); 00074 continue; 00075 } 00076 00077 // Check for the size of the type. 00078 StringRef::size_type Pos = LineStr.find(" Size:"); 00079 if (Pos != StringRef::npos) { 00080 // Skip past the " Size:" prefix. 00081 LineStr = LineStr.substr(Pos + strlen(" Size:")); 00082 00083 unsigned long long Size = 0; 00084 (void)LineStr.getAsInteger(10, Size); 00085 CurrentLayout.Size = Size; 00086 continue; 00087 } 00088 00089 // Check for the alignment of the type. 00090 Pos = LineStr.find("Alignment:"); 00091 if (Pos != StringRef::npos) { 00092 // Skip past the "Alignment:" prefix. 00093 LineStr = LineStr.substr(Pos + strlen("Alignment:")); 00094 00095 unsigned long long Alignment = 0; 00096 (void)LineStr.getAsInteger(10, Alignment); 00097 CurrentLayout.Align = Alignment; 00098 continue; 00099 } 00100 00101 // Check for the size/alignment of the type. 00102 Pos = LineStr.find("sizeof="); 00103 if (Pos != StringRef::npos) { 00104 /* Skip past the sizeof= prefix. */ 00105 LineStr = LineStr.substr(Pos + strlen("sizeof=")); 00106 00107 // Parse size. 00108 unsigned long long Size = 0; 00109 (void)LineStr.getAsInteger(10, Size); 00110 CurrentLayout.Size = Size; 00111 00112 Pos = LineStr.find("align="); 00113 if (Pos != StringRef::npos) { 00114 /* Skip past the align= prefix. */ 00115 LineStr = LineStr.substr(Pos + strlen("align=")); 00116 00117 // Parse alignment. 00118 unsigned long long Alignment = 0; 00119 (void)LineStr.getAsInteger(10, Alignment); 00120 CurrentLayout.Align = Alignment; 00121 } 00122 00123 continue; 00124 } 00125 00126 // Check for the field offsets of the type. 00127 Pos = LineStr.find("FieldOffsets: ["); 00128 if (Pos == StringRef::npos) 00129 continue; 00130 00131 LineStr = LineStr.substr(Pos + strlen("FieldOffsets: [")); 00132 while (!LineStr.empty() && isDigit(LineStr[0])) { 00133 // Parse this offset. 00134 unsigned Idx = 1; 00135 while (Idx < LineStr.size() && isDigit(LineStr[Idx])) 00136 ++Idx; 00137 00138 unsigned long long Offset = 0; 00139 (void)LineStr.substr(0, Idx).getAsInteger(10, Offset); 00140 00141 CurrentLayout.FieldOffsets.push_back(Offset); 00142 00143 // Skip over this offset, the following comma, and any spaces. 00144 LineStr = LineStr.substr(Idx + 1); 00145 while (!LineStr.empty() && isWhitespace(LineStr[0])) 00146 LineStr = LineStr.substr(1); 00147 } 00148 } 00149 00150 // Flush the last type/layout, if there is one. 00151 if (!CurrentType.empty()) 00152 Layouts[CurrentType] = CurrentLayout; 00153 } 00154 00155 bool 00156 LayoutOverrideSource::layoutRecordType(const RecordDecl *Record, 00157 uint64_t &Size, uint64_t &Alignment, 00158 llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets, 00159 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets, 00160 llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets) 00161 { 00162 // We can't override unnamed declarations. 00163 if (!Record->getIdentifier()) 00164 return false; 00165 00166 // Check whether we have a layout for this record. 00167 llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName()); 00168 if (Known == Layouts.end()) 00169 return false; 00170 00171 // Provide field layouts. 00172 unsigned NumFields = 0; 00173 for (RecordDecl::field_iterator F = Record->field_begin(), 00174 FEnd = Record->field_end(); 00175 F != FEnd; ++F, ++NumFields) { 00176 if (NumFields >= Known->second.FieldOffsets.size()) 00177 continue; 00178 00179 FieldOffsets[*F] = Known->second.FieldOffsets[NumFields]; 00180 } 00181 00182 // Wrong number of fields. 00183 if (NumFields != Known->second.FieldOffsets.size()) 00184 return false; 00185 00186 Size = Known->second.Size; 00187 Alignment = Known->second.Align; 00188 return true; 00189 } 00190 00191 void LayoutOverrideSource::dump() { 00192 raw_ostream &OS = llvm::errs(); 00193 for (llvm::StringMap<Layout>::iterator L = Layouts.begin(), 00194 LEnd = Layouts.end(); 00195 L != LEnd; ++L) { 00196 OS << "Type: blah " << L->first() << '\n'; 00197 OS << " Size:" << L->second.Size << '\n'; 00198 OS << " Alignment:" << L->second.Align << '\n'; 00199 OS << " FieldOffsets: ["; 00200 for (unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) { 00201 if (I) 00202 OS << ", "; 00203 OS << L->second.FieldOffsets[I]; 00204 } 00205 OS << "]\n"; 00206 } 00207 } 00208