Skip to content

Commit

Permalink
Revert "[Clang][MS] Remove assertion on BaseOffset can't be smaller t…
Browse files Browse the repository at this point in the history
…han Size."

This reverts commit 5d54213.

Breaks check-clang on Windows, see https://reviews.llvm.org/D152472#4422913
  • Loading branch information
nico committed Jun 14, 2023
1 parent dbdd637 commit 9c56035
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 201 deletions.
6 changes: 0 additions & 6 deletions clang/include/clang/Frontend/LayoutOverrideSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ namespace clang {
/// The alignment of the record.
uint64_t Align;

/// The offsets of non-virtual base classes in the record.
SmallVector<CharUnits, 8> BaseOffsets;

/// The offsets of virtual base classes in the record.
SmallVector<CharUnits, 8> VBaseOffsets;

/// The offsets of the fields, in source order.
SmallVector<uint64_t, 8> FieldOffsets;
};
Expand Down
25 changes: 2 additions & 23 deletions clang/lib/AST/RecordLayoutBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2926,7 +2926,8 @@ void MicrosoftRecordLayoutBuilder::layoutNonVirtualBase(
bool FoundBase = false;
if (UseExternalLayout) {
FoundBase = External.getExternalNVBaseOffset(BaseDecl, BaseOffset);
if (BaseOffset > Size) {
if (FoundBase) {
assert(BaseOffset >= Size && "base offset already allocated");
Size = BaseOffset;
}
}
Expand Down Expand Up @@ -3722,28 +3723,6 @@ void ASTContext::DumpRecordLayout(const RecordDecl *RD, raw_ostream &OS,
if (Target->defaultsToAIXPowerAlignment())
OS << " PreferredAlignment:" << toBits(Info.getPreferredAlignment())
<< "\n";
if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
OS << " BaseOffsets: [";
const CXXRecordDecl *Base = nullptr;
for (auto I : CXXRD->bases()) {
if (I.isVirtual())
continue;
if (Base)
OS << ", ";
Base = I.getType()->getAsCXXRecordDecl();
OS << Info.CXXInfo->BaseOffsets[Base].getQuantity();
}
OS << "]>\n";
OS << " VBaseOffsets: [";
const CXXRecordDecl *VBase = nullptr;
for (auto I : CXXRD->vbases()) {
if (VBase)
OS << ", ";
VBase = I.getType()->getAsCXXRecordDecl();
OS << Info.CXXInfo->VBaseOffsets[VBase].VBaseOffset.getQuantity();
}
OS << "]>\n";
}
OS << " FieldOffsets: [";
for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) {
if (i)
Expand Down
103 changes: 23 additions & 80 deletions clang/lib/Frontend/LayoutOverrideSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//===----------------------------------------------------------------------===//
#include "clang/Frontend/LayoutOverrideSource.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/Basic/CharInfo.h"
#include "llvm/Support/raw_ostream.h"
#include <fstream>
Expand All @@ -27,18 +26,6 @@ static std::string parseName(StringRef S) {
return S.substr(0, Offset).str();
}

/// Parse an unsigned integer and move S to the next non-digit character.
static bool parseUnsigned(StringRef &S, unsigned long long &ULL) {
if (S.empty() || !isDigit(S[0]))
return false;
unsigned Idx = 1;
while (Idx < S.size() && isDigit(S[Idx]))
++Idx;
(void)S.substr(0, Idx).getAsInteger(10, ULL);
S = S.substr(Idx);
return true;
}

LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {
std::ifstream Input(Filename.str().c_str());
if (!Input.is_open())
Expand Down Expand Up @@ -93,8 +80,8 @@ LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {
LineStr = LineStr.substr(Pos + strlen(" Size:"));

unsigned long long Size = 0;
if (parseUnsigned(LineStr, Size))
CurrentLayout.Size = Size;
(void)LineStr.getAsInteger(10, Size);
CurrentLayout.Size = Size;
continue;
}

Expand All @@ -105,22 +92,21 @@ LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {
LineStr = LineStr.substr(Pos + strlen("Alignment:"));

unsigned long long Alignment = 0;
if (parseUnsigned(LineStr, Alignment))
CurrentLayout.Align = Alignment;
(void)LineStr.getAsInteger(10, Alignment);
CurrentLayout.Align = Alignment;
continue;
}

// Check for the size/alignment of the type. The number follows "size=" or
// "align=" indicates number of bytes.
// Check for the size/alignment of the type.
Pos = LineStr.find("sizeof=");
if (Pos != StringRef::npos) {
/* Skip past the sizeof= prefix. */
LineStr = LineStr.substr(Pos + strlen("sizeof="));

// Parse size.
unsigned long long Size = 0;
if (parseUnsigned(LineStr, Size))
CurrentLayout.Size = Size * 8;
(void)LineStr.getAsInteger(10, Size);
CurrentLayout.Size = Size;

Pos = LineStr.find("align=");
if (Pos != StringRef::npos) {
Expand All @@ -129,59 +115,34 @@ LayoutOverrideSource::LayoutOverrideSource(StringRef Filename) {

// Parse alignment.
unsigned long long Alignment = 0;
if (parseUnsigned(LineStr, Alignment))
CurrentLayout.Align = Alignment * 8;
(void)LineStr.getAsInteger(10, Alignment);
CurrentLayout.Align = Alignment;
}

continue;
}

// Check for the field offsets of the type.
Pos = LineStr.find("FieldOffsets: [");
if (Pos != StringRef::npos) {
LineStr = LineStr.substr(Pos + strlen("FieldOffsets: ["));
while (!LineStr.empty() && isDigit(LineStr[0])) {
unsigned long long Offset = 0;
if (parseUnsigned(LineStr, Offset))
CurrentLayout.FieldOffsets.push_back(Offset);

// Skip over this offset, the following comma, and any spaces.
LineStr = LineStr.substr(1);
while (!LineStr.empty() && isWhitespace(LineStr[0]))
LineStr = LineStr.substr(1);
}
}
if (Pos == StringRef::npos)
continue;

// Check for the base offsets.
Pos = LineStr.find("BaseOffsets: [");
if (Pos != StringRef::npos) {
LineStr = LineStr.substr(Pos + strlen("BaseOffsets: ["));
while (!LineStr.empty() && isDigit(LineStr[0])) {
unsigned long long Offset = 0;
if (parseUnsigned(LineStr, Offset))
CurrentLayout.BaseOffsets.push_back(CharUnits::fromQuantity(Offset));
LineStr = LineStr.substr(Pos + strlen("FieldOffsets: ["));
while (!LineStr.empty() && isDigit(LineStr[0])) {
// Parse this offset.
unsigned Idx = 1;
while (Idx < LineStr.size() && isDigit(LineStr[Idx]))
++Idx;

// Skip over this offset, the following comma, and any spaces.
LineStr = LineStr.substr(1);
while (!LineStr.empty() && isWhitespace(LineStr[0]))
LineStr = LineStr.substr(1);
}
}
unsigned long long Offset = 0;
(void)LineStr.substr(0, Idx).getAsInteger(10, Offset);

// Check for the virtual base offsets.
Pos = LineStr.find("VBaseOffsets: [");
if (Pos != StringRef::npos) {
LineStr = LineStr.substr(Pos + strlen("VBaseOffsets: ["));
while (!LineStr.empty() && isDigit(LineStr[0])) {
unsigned long long Offset = 0;
if (parseUnsigned(LineStr, Offset))
CurrentLayout.VBaseOffsets.push_back(CharUnits::fromQuantity(Offset));
CurrentLayout.FieldOffsets.push_back(Offset);

// Skip over this offset, the following comma, and any spaces.
// Skip over this offset, the following comma, and any spaces.
LineStr = LineStr.substr(Idx + 1);
while (!LineStr.empty() && isWhitespace(LineStr[0]))
LineStr = LineStr.substr(1);
while (!LineStr.empty() && isWhitespace(LineStr[0]))
LineStr = LineStr.substr(1);
}
}
}

Expand Down Expand Up @@ -221,24 +182,6 @@ LayoutOverrideSource::layoutRecordType(const RecordDecl *Record,
if (NumFields != Known->second.FieldOffsets.size())
return false;

// Provide base offsets.
if (const auto *RD = dyn_cast<CXXRecordDecl>(Record)) {
unsigned NumNB = 0;
unsigned NumVB = 0;
for (const auto &I : RD->vbases()) {
if (NumVB >= Known->second.VBaseOffsets.size())
continue;
const CXXRecordDecl *VBase = I.getType()->getAsCXXRecordDecl();
VirtualBaseOffsets[VBase] = Known->second.VBaseOffsets[NumVB++];
}
for (const auto &I : RD->bases()) {
if (I.isVirtual() || NumNB >= Known->second.BaseOffsets.size())
continue;
const CXXRecordDecl *Base = I.getType()->getAsCXXRecordDecl();
BaseOffsets[Base] = Known->second.BaseOffsets[NumNB++];
}
}

Size = Known->second.Size;
Alignment = Known->second.Align;
return true;
Expand Down
49 changes: 0 additions & 49 deletions clang/test/CodeGenCXX/Inputs/override-layout-ms.layout

This file was deleted.

43 changes: 0 additions & 43 deletions clang/test/CodeGenCXX/override-layout-ms.cpp

This file was deleted.

0 comments on commit 9c56035

Please sign in to comment.