Path: blob/main/contrib/llvm-project/clang/lib/Frontend/LayoutOverrideSource.cpp
35232 views
//===--- LayoutOverrideSource.cpp --Override Record Layouts ---------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7#include "clang/Frontend/LayoutOverrideSource.h"8#include "clang/AST/Decl.h"9#include "clang/AST/DeclCXX.h"10#include "clang/Basic/CharInfo.h"11#include "llvm/Support/raw_ostream.h"12#include <fstream>13#include <string>1415using namespace clang;1617/// Parse a simple identifier.18static std::string parseName(StringRef S) {19if (S.empty() || !isAsciiIdentifierStart(S[0]))20return "";2122unsigned Offset = 1;23while (Offset < S.size() && isAsciiIdentifierContinue(S[Offset]))24++Offset;2526return S.substr(0, Offset).str();27}2829/// Parse an unsigned integer and move S to the next non-digit character.30static bool parseUnsigned(StringRef &S, unsigned long long &ULL) {31if (S.empty() || !isDigit(S[0]))32return false;33unsigned Idx = 1;34while (Idx < S.size() && isDigit(S[Idx]))35++Idx;36(void)S.substr(0, Idx).getAsInteger(10, ULL);37S = S.substr(Idx);38return true;39}4041LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {42std::ifstream Input(Filename.str().c_str());43if (!Input.is_open())44return;4546// Parse the output of -fdump-record-layouts.47std::string CurrentType;48Layout CurrentLayout;49bool ExpectingType = false;5051while (Input.good()) {52std::string Line;53getline(Input, Line);5455StringRef LineStr(Line);5657// Determine whether the following line will start a58if (LineStr.contains("*** Dumping AST Record Layout")) {59// Flush the last type/layout, if there is one.60if (!CurrentType.empty())61Layouts[CurrentType] = CurrentLayout;62CurrentLayout = Layout();6364ExpectingType = true;65continue;66}6768// If we're expecting a type, grab it.69if (ExpectingType) {70ExpectingType = false;7172StringRef::size_type Pos;73if ((Pos = LineStr.find("struct ")) != StringRef::npos)74LineStr = LineStr.substr(Pos + strlen("struct "));75else if ((Pos = LineStr.find("class ")) != StringRef::npos)76LineStr = LineStr.substr(Pos + strlen("class "));77else if ((Pos = LineStr.find("union ")) != StringRef::npos)78LineStr = LineStr.substr(Pos + strlen("union "));79else80continue;8182// Find the name of the type.83CurrentType = parseName(LineStr);84CurrentLayout = Layout();85continue;86}8788// Check for the size of the type.89StringRef::size_type Pos = LineStr.find(" Size:");90if (Pos != StringRef::npos) {91// Skip past the " Size:" prefix.92LineStr = LineStr.substr(Pos + strlen(" Size:"));9394unsigned long long Size = 0;95if (parseUnsigned(LineStr, Size))96CurrentLayout.Size = Size;97continue;98}99100// Check for the alignment of the type.101Pos = LineStr.find("Alignment:");102if (Pos != StringRef::npos) {103// Skip past the "Alignment:" prefix.104LineStr = LineStr.substr(Pos + strlen("Alignment:"));105106unsigned long long Alignment = 0;107if (parseUnsigned(LineStr, Alignment))108CurrentLayout.Align = Alignment;109continue;110}111112// Check for the size/alignment of the type. The number follows "size=" or113// "align=" indicates number of bytes.114Pos = LineStr.find("sizeof=");115if (Pos != StringRef::npos) {116/* Skip past the sizeof= prefix. */117LineStr = LineStr.substr(Pos + strlen("sizeof="));118119// Parse size.120unsigned long long Size = 0;121if (parseUnsigned(LineStr, Size))122CurrentLayout.Size = Size * 8;123124Pos = LineStr.find("align=");125if (Pos != StringRef::npos) {126/* Skip past the align= prefix. */127LineStr = LineStr.substr(Pos + strlen("align="));128129// Parse alignment.130unsigned long long Alignment = 0;131if (parseUnsigned(LineStr, Alignment))132CurrentLayout.Align = Alignment * 8;133}134135continue;136}137138// Check for the field offsets of the type.139Pos = LineStr.find("FieldOffsets: [");140if (Pos != StringRef::npos) {141LineStr = LineStr.substr(Pos + strlen("FieldOffsets: ["));142while (!LineStr.empty() && isDigit(LineStr[0])) {143unsigned long long Offset = 0;144if (parseUnsigned(LineStr, Offset))145CurrentLayout.FieldOffsets.push_back(Offset);146147// Skip over this offset, the following comma, and any spaces.148LineStr = LineStr.substr(1);149LineStr = LineStr.drop_while(isWhitespace);150}151}152153// Check for the virtual base offsets.154Pos = LineStr.find("VBaseOffsets: [");155if (Pos != StringRef::npos) {156LineStr = LineStr.substr(Pos + strlen("VBaseOffsets: ["));157while (!LineStr.empty() && isDigit(LineStr[0])) {158unsigned long long Offset = 0;159if (parseUnsigned(LineStr, Offset))160CurrentLayout.VBaseOffsets.push_back(CharUnits::fromQuantity(Offset));161162// Skip over this offset, the following comma, and any spaces.163LineStr = LineStr.substr(1);164LineStr = LineStr.drop_while(isWhitespace);165}166continue;167}168169// Check for the base offsets.170Pos = LineStr.find("BaseOffsets: [");171if (Pos != StringRef::npos) {172LineStr = LineStr.substr(Pos + strlen("BaseOffsets: ["));173while (!LineStr.empty() && isDigit(LineStr[0])) {174unsigned long long Offset = 0;175if (parseUnsigned(LineStr, Offset))176CurrentLayout.BaseOffsets.push_back(CharUnits::fromQuantity(Offset));177178// Skip over this offset, the following comma, and any spaces.179LineStr = LineStr.substr(1);180LineStr = LineStr.drop_while(isWhitespace);181}182}183}184185// Flush the last type/layout, if there is one.186if (!CurrentType.empty())187Layouts[CurrentType] = CurrentLayout;188}189190bool191LayoutOverrideSource::layoutRecordType(const RecordDecl *Record,192uint64_t &Size, uint64_t &Alignment,193llvm::DenseMap<const FieldDecl *, uint64_t> &FieldOffsets,194llvm::DenseMap<const CXXRecordDecl *, CharUnits> &BaseOffsets,195llvm::DenseMap<const CXXRecordDecl *, CharUnits> &VirtualBaseOffsets)196{197// We can't override unnamed declarations.198if (!Record->getIdentifier())199return false;200201// Check whether we have a layout for this record.202llvm::StringMap<Layout>::iterator Known = Layouts.find(Record->getName());203if (Known == Layouts.end())204return false;205206// Provide field layouts.207unsigned NumFields = 0;208for (RecordDecl::field_iterator F = Record->field_begin(),209FEnd = Record->field_end();210F != FEnd; ++F, ++NumFields) {211if (NumFields >= Known->second.FieldOffsets.size())212continue;213214FieldOffsets[*F] = Known->second.FieldOffsets[NumFields];215}216217// Wrong number of fields.218if (NumFields != Known->second.FieldOffsets.size())219return false;220221// Provide base offsets.222if (const auto *RD = dyn_cast<CXXRecordDecl>(Record)) {223unsigned NumNB = 0;224unsigned NumVB = 0;225for (const auto &I : RD->vbases()) {226if (NumVB >= Known->second.VBaseOffsets.size())227continue;228const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();229VirtualBaseOffsets[VBase] = Known->second.VBaseOffsets[NumVB++];230}231for (const auto &I : RD->bases()) {232if (I.isVirtual() || NumNB >= Known->second.BaseOffsets.size())233continue;234const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl();235BaseOffsets[Base] = Known->second.BaseOffsets[NumNB++];236}237}238239Size = Known->second.Size;240Alignment = Known->second.Align;241return true;242}243244LLVM_DUMP_METHOD void LayoutOverrideSource::dump() {245raw_ostream &OS = llvm::errs();246for (llvm::StringMap<Layout>::iterator L = Layouts.begin(),247LEnd = Layouts.end();248L != LEnd; ++L) {249OS << "Type: blah " << L->first() << '\n';250OS << " Size:" << L->second.Size << '\n';251OS << " Alignment:" << L->second.Align << '\n';252OS << " FieldOffsets: [";253for (unsigned I = 0, N = L->second.FieldOffsets.size(); I != N; ++I) {254if (I)255OS << ", ";256OS << L->second.FieldOffsets[I];257}258OS << "]\n";259}260}261262263264