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