clang API Documentation

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