brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.2 KiB · 0a60e00 Raw
264 lines · cpp
1//===--- LayoutOverrideSource.cpp --Override Record Layouts ---------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8#include "clang/Frontend/LayoutOverrideSource.h"9#include "clang/AST/Decl.h"10#include "clang/AST/DeclCXX.h"11#include "clang/AST/Type.h"12#include "clang/Basic/CharInfo.h"13#include "llvm/Support/raw_ostream.h"14#include <fstream>15#include <string>16 17using namespace clang;18 19/// Parse a simple identifier.20static std::string parseName(StringRef S) {21  if (S.empty() || !isAsciiIdentifierStart(S[0]))22    return "";23 24  unsigned Offset = 1;25  while (Offset < S.size() && isAsciiIdentifierContinue(S[Offset]))26    ++Offset;27 28  return S.substr(0, Offset).str();29}30 31/// Parse an unsigned integer and move S to the next non-digit character.32static bool parseUnsigned(StringRef &S, unsigned long long &ULL) {33  if (S.empty() || !isDigit(S[0]))34    return false;35  unsigned Idx = 1;36  while (Idx < S.size() && isDigit(S[Idx]))37    ++Idx;38  (void)S.substr(0, Idx).getAsInteger(10, ULL);39  S = S.substr(Idx);40  return true;41}42 43LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {44  std::ifstream Input(Filename.str().c_str());45  if (!Input.is_open())46    return;47 48  // Parse the output of -fdump-record-layouts.49  std::string CurrentType;50  Layout CurrentLayout;51  bool ExpectingType = false;52 53  while (Input.good()) {54    std::string Line;55    getline(Input, Line);56 57    StringRef LineStr(Line);58 59    // Determine whether the following line will start a60    if (LineStr.contains("*** Dumping AST Record Layout")) {61      // Flush the last type/layout, if there is one.62      if (!CurrentType.empty())63        Layouts[CurrentType] = CurrentLayout;64      CurrentLayout = Layout();65 66      ExpectingType = true;67      continue;68    }69 70    // If we're expecting a type, grab it.71    if (ExpectingType) {72      ExpectingType = false;73 74      StringRef::size_type Pos;75      if ((Pos = LineStr.find("struct ")) != StringRef::npos)76        LineStr = LineStr.substr(Pos + strlen("struct "));77      else if ((Pos = LineStr.find("class ")) != StringRef::npos)78        LineStr = LineStr.substr(Pos + strlen("class "));79      else if ((Pos = LineStr.find("union ")) != StringRef::npos)80        LineStr = LineStr.substr(Pos + strlen("union "));81      else82        continue;83 84      // Find the name of the type.85      CurrentType = parseName(LineStr);86      CurrentLayout = Layout();87      continue;88    }89 90    // Check for the size of the type.91    StringRef::size_type Pos = LineStr.find(" Size:");92    if (Pos != StringRef::npos) {93      // Skip past the " Size:" prefix.94      LineStr = LineStr.substr(Pos + strlen(" Size:"));95 96      unsigned long long Size = 0;97      if (parseUnsigned(LineStr, Size))98        CurrentLayout.Size = Size;99      continue;100    }101 102    // Check for the alignment of the type.103    Pos = LineStr.find("Alignment:");104    if (Pos != StringRef::npos) {105      // Skip past the "Alignment:" prefix.106      LineStr = LineStr.substr(Pos + strlen("Alignment:"));107 108      unsigned long long Alignment = 0;109      if (parseUnsigned(LineStr, Alignment))110        CurrentLayout.Align = Alignment;111      continue;112    }113 114    // Check for the size/alignment of the type. The number follows "size=" or115    // "align=" indicates number of bytes.116    Pos = LineStr.find("sizeof=");117    if (Pos != StringRef::npos) {118      /* Skip past the sizeof= prefix. */119      LineStr = LineStr.substr(Pos + strlen("sizeof="));120 121      // Parse size.122      unsigned long long Size = 0;123      if (parseUnsigned(LineStr, Size))124        CurrentLayout.Size = Size * 8;125 126      Pos = LineStr.find("align=");127      if (Pos != StringRef::npos) {128        /* Skip past the align= prefix. */129        LineStr = LineStr.substr(Pos + strlen("align="));130 131        // Parse alignment.132        unsigned long long Alignment = 0;133        if (parseUnsigned(LineStr, Alignment))134          CurrentLayout.Align = Alignment * 8;135      }136 137      continue;138    }139 140    // Check for the field offsets of the type.141    Pos = LineStr.find("FieldOffsets: [");142    if (Pos != StringRef::npos) {143      LineStr = LineStr.substr(Pos + strlen("FieldOffsets: ["));144      while (!LineStr.empty() && isDigit(LineStr[0])) {145        unsigned long long Offset = 0;146        if (parseUnsigned(LineStr, Offset))147          CurrentLayout.FieldOffsets.push_back(Offset);148 149        // Skip over this offset, the following comma, and any spaces.150        LineStr = LineStr.substr(1);151        LineStr = LineStr.drop_while(isWhitespace);152      }153    }154 155    // Check for the virtual base offsets.156    Pos = LineStr.find("VBaseOffsets: [");157    if (Pos != StringRef::npos) {158      LineStr = LineStr.substr(Pos + strlen("VBaseOffsets: ["));159      while (!LineStr.empty() && isDigit(LineStr[0])) {160        unsigned long long Offset = 0;161        if (parseUnsigned(LineStr, Offset))162          CurrentLayout.VBaseOffsets.push_back(CharUnits::fromQuantity(Offset));163 164        // Skip over this offset, the following comma, and any spaces.165        LineStr = LineStr.substr(1);166        LineStr = LineStr.drop_while(isWhitespace);167      }168      continue;169    }170 171    // Check for the base offsets.172    Pos = LineStr.find("BaseOffsets: [");173    if (Pos != StringRef::npos) {174      LineStr = LineStr.substr(Pos + strlen("BaseOffsets: ["));175      while (!LineStr.empty() && isDigit(LineStr[0])) {176        unsigned long long Offset = 0;177        if (parseUnsigned(LineStr, Offset))178          CurrentLayout.BaseOffsets.push_back(CharUnits::fromQuantity(Offset));179 180        // Skip over this offset, the following comma, and any spaces.181        LineStr = LineStr.substr(1);182        LineStr = LineStr.drop_while(isWhitespace);183      }184    }185  }186 187  // Flush the last type/layout, if there is one.188  if (!CurrentType.empty())189    Layouts[CurrentType] = CurrentLayout;190}191 192bool193LayoutOverrideSource::layoutRecordType(const RecordDecl *Record,194  uint64_t &Size, uint64_t &Alignment,195  llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,196  llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,197  llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)198{199  // We can't override unnamed declarations.200  if (!Record->getIdentifier())201    return false;202 203  // Check whether we have a layout for this record.204  llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName());205  if (Known == Layouts.end())206    return false;207 208  // Provide field layouts.209  unsigned NumFields = 0;210  for (RecordDecl::field_iterator F = Record->field_begin(),211                               FEnd = Record->field_end();212       F != FEnd; ++F, ++NumFields) {213    if (NumFields >= Known->second.FieldOffsets.size())214      continue;215 216    FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];217  }218 219  // Wrong number of fields.220  if (NumFields != Known->second.FieldOffsets.size())221    return false;222 223  // Provide base offsets.224  if (const auto *RD = dyn_cast<CXXRecordDecl>(Record)) {225    unsigned NumNB = 0;226    unsigned NumVB = 0;227    for (const auto &I : RD->vbases()) {228      if (NumVB >= Known->second.VBaseOffsets.size())229        continue;230      const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();231      VirtualBaseOffsets[VBase] = Known->second.VBaseOffsets[NumVB++];232    }233    for (const auto &I : RD->bases()) {234      if (I.isVirtual() || NumNB >= Known->second.BaseOffsets.size())235        continue;236      const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl();237      BaseOffsets[Base] = Known->second.BaseOffsets[NumNB++];238    }239  }240 241  Size = Known->second.Size;242  Alignment = Known->second.Align;243  return true;244}245 246LLVM_DUMP_METHOD void LayoutOverrideSource::dump() {247  raw_ostream &OS = llvm::errs();248  for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),249                                      LEnd = Layouts.end();250       L != LEnd; ++L) {251    OS << "Type: blah " << L->first() << '\n';252    OS << "  Size:" << L->second.Size << '\n';253    OS << "  Alignment:" << L->second.Align << '\n';254    OS << "  FieldOffsets: [";255    for (unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {256      if (I)257        OS << ", ";258      OS << L->second.FieldOffsets[I];259    }260    OS << "]\n";261  }262}263 264