[cfe-commits] r57623 - in /cfe/trunk: include/clang/AST/Attr.h include/clang/AST/RecordLayout.h lib/AST/ASTContext.cpp lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclAttr.cpp test/Sema/pragma-pack-2.c
Daniel Dunbar
daniel at zuster.org
Wed Oct 15 19:34:05 PDT 2008
Author: ddunbar
Date: Wed Oct 15 21:34:03 2008
New Revision: 57623
URL: http://llvm.org/viewvc/llvm-project?rev=57623&view=rev
Log:
Implement #pragma pack use in structure packing. The general approach
is to encode the state of the #pragma pack stack as an attribute when
the structure is declared.
- Extend PackedAttr to take an alignment (in bits), and reuse for
both __attribute__((packed)) (which takes no argument, instead
packing tightly (to "minimize the memory required") and for #pragma
pack (which allows specification of the maximum alignment in
bytes). __attribute__((packed)) is just encoded as Alignment=1.
This conflates two related but different mechanisms, but it didn't
seem worth another attribute.
- I have attempted to follow the MSVC semantics as opposed to the gcc
ones, since if I understand correctly #pragma pack originated with
MSVC. The semantics are generally equivalent except when the stack
is altered during the definition of a structure; its not clear if
anyone does this in practice. See testcase if curious.
Added:
cfe/trunk/test/Sema/pragma-pack-2.c
Modified:
cfe/trunk/include/clang/AST/Attr.h
cfe/trunk/include/clang/AST/RecordLayout.h
cfe/trunk/lib/AST/ASTContext.cpp
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/lib/Sema/SemaDeclAttr.cpp
Modified: cfe/trunk/include/clang/AST/Attr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Attr.h?rev=57623&r1=57622&r2=57623&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Attr.h (original)
+++ cfe/trunk/include/clang/AST/Attr.h Wed Oct 15 21:34:03 2008
@@ -82,9 +82,14 @@
};
class PackedAttr : public Attr {
+ unsigned Alignment;
+
public:
- PackedAttr() : Attr(Packed) {}
-
+ PackedAttr(unsigned alignment) : Attr(Packed), Alignment(alignment) {}
+
+ /// getAlignment - The specified alignment in bits.
+ unsigned getAlignment() const { return Alignment; }
+
// Implement isa/cast/dyncast/etc.
static bool classof(const Attr *A) {
return A->getKind() == Packed;
@@ -96,7 +101,8 @@
unsigned Alignment;
public:
AlignedAttr(unsigned alignment) : Attr(Aligned), Alignment(alignment) {}
-
+
+ /// getAlignment - The specified alignment in bits.
unsigned getAlignment() const { return Alignment; }
// Implement isa/cast/dyncast/etc.
Modified: cfe/trunk/include/clang/AST/RecordLayout.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/RecordLayout.h?rev=57623&r1=57622&r2=57623&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/RecordLayout.h (original)
+++ cfe/trunk/include/clang/AST/RecordLayout.h Wed Oct 15 21:34:03 2008
@@ -60,9 +60,11 @@
void SetAlignment(unsigned A) { Alignment = A; }
- /// LayoutField - Field layout.
+ /// LayoutField - Field layout. StructPacking is the specified
+ /// packing alignment (maximum alignment) in bits to use for the
+ /// structure, or 0 if no packing alignment is specified.
void LayoutField(const FieldDecl *FD, unsigned FieldNo,
- bool IsUnion, bool StructIsPacked,
+ bool IsUnion, unsigned StructPacking,
ASTContext &Context);
ASTRecordLayout(const ASTRecordLayout&); // DO NOT IMPLEMENT
Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=57623&r1=57622&r2=57623&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Wed Oct 15 21:34:03 2008
@@ -371,12 +371,17 @@
/// LayoutField - Field layout.
void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
- bool IsUnion, bool StructIsPacked,
+ bool IsUnion, unsigned StructPacking,
ASTContext &Context) {
- bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
+ unsigned FieldPacking = StructPacking;
uint64_t FieldOffset = IsUnion ? 0 : Size;
uint64_t FieldSize;
unsigned FieldAlign;
+
+ // FIXME: Should this override struct packing? Probably we want to
+ // take the minimum?
+ if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
+ FieldPacking = PA->getAlignment();
if (const Expr *BitWidthExpr = FD->getBitWidth()) {
// TODO: Need to check this algorithm on other targets!
@@ -388,9 +393,14 @@
Context.getTypeInfo(FD->getType());
uint64_t TypeSize = FieldInfo.first;
+ // Determine the alignment of this bitfield. The packing
+ // attributes define a maximum and the alignment attribute defines
+ // a minimum.
+ // FIXME: What is the right behavior when the specified alignment
+ // is smaller than the specified packing?
FieldAlign = FieldInfo.second;
- if (FieldIsPacked)
- FieldAlign = 1;
+ if (FieldPacking)
+ FieldAlign = std::min(FieldAlign, FieldPacking);
if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
FieldAlign = std::max(FieldAlign, AA->getAlignment());
@@ -418,8 +428,15 @@
FieldAlign = FieldInfo.second;
}
- if (FieldIsPacked)
- FieldAlign = 8;
+ // Determine the alignment of this bitfield. The packing
+ // attributes define a maximum and the alignment attribute defines
+ // a minimum. Additionally, the packing alignment must be at least
+ // a byte for non-bitfields.
+ //
+ // FIXME: What is the right behavior when the specified alignment
+ // is smaller than the specified packing?
+ if (FieldPacking)
+ FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
FieldAlign = std::max(FieldAlign, AA->getAlignment());
@@ -470,7 +487,9 @@
}
Entry = NewEntry;
- bool IsPacked = D->getAttr<PackedAttr>();
+ unsigned StructPacking = 0;
+ if (const PackedAttr *PA = D->getAttr<PackedAttr>())
+ StructPacking = PA->getAlignment();
if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
@@ -481,7 +500,7 @@
for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
IVE = D->ivar_end(); IVI != IVE; ++IVI) {
const ObjCIvarDecl* Ivar = (*IVI);
- NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
+ NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
}
// Finally, round the size of the total struct up to the alignment of the
@@ -507,9 +526,12 @@
Entry = NewEntry;
NewEntry->InitializeLayout(D->getNumMembers());
- bool StructIsPacked = D->getAttr<PackedAttr>();
bool IsUnion = D->isUnion();
+ unsigned StructPacking = 0;
+ if (const PackedAttr *PA = D->getAttr<PackedAttr>())
+ StructPacking = PA->getAlignment();
+
if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
AA->getAlignment()));
@@ -518,7 +540,7 @@
// the future, this will need to be tweakable by targets.
for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
const FieldDecl *FD = D->getMember(i);
- NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
+ NewEntry->LayoutField(FD, i, IsUnion, StructPacking, *this);
}
// Finally, round the size of the total struct up to the alignment of the
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=57623&r1=57622&r2=57623&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Oct 15 21:34:03 2008
@@ -1939,6 +1939,20 @@
// Add it to the decl chain.
PushOnScopeChains(New, S);
}
+
+ // Handle #pragma pack: if the #pragma pack stack has non-default
+ // alignment, make up a packed attribute for this decl. These
+ // attributes are checked when the ASTContext lays out the
+ // structure.
+ //
+ // It is important for implementing the correct semantics that this
+ // happen here (in act on tag decl). The #pragma pack stack is
+ // maintained as a result of parser callbacks which can occur at
+ // many points during the parsing of a struct declaration (because
+ // the #pragma tokens are effectively skipped over during the
+ // parsing of the struct).
+ if (unsigned Alignment = PackContext.getAlignment())
+ New->addAttr(new PackedAttr(Alignment * 8));
if (Attr)
ProcessDeclAttributeList(New, Attr);
Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=57623&r1=57622&r2=57623&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Wed Oct 15 21:34:03 2008
@@ -256,7 +256,7 @@
}
if (TagDecl *TD = dyn_cast<TagDecl>(d))
- TD->addAttr(new PackedAttr());
+ TD->addAttr(new PackedAttr(1));
else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
// If the alignment is less than or equal to 8 bits, the packed attribute
// has no effect.
@@ -266,7 +266,7 @@
diag::warn_attribute_ignored_for_field_of_type,
Attr.getName()->getName(), FD->getType().getAsString());
else
- FD->addAttr(new PackedAttr());
+ FD->addAttr(new PackedAttr(1));
} else
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored,
Attr.getName()->getName());
Added: cfe/trunk/test/Sema/pragma-pack-2.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/pragma-pack-2.c?rev=57623&view=auto
==============================================================================
--- cfe/trunk/test/Sema/pragma-pack-2.c (added)
+++ cfe/trunk/test/Sema/pragma-pack-2.c Wed Oct 15 21:34:03 2008
@@ -0,0 +1,66 @@
+// RUN: clang -triple i686-apple-darwin9 %s -fsyntax-only -verify
+
+#include <stddef.h>
+
+#pragma pack(4)
+
+// Baseline
+struct s0 {
+ char f0;
+ int f1;
+};
+extern int a0[offsetof(struct s0, f1) == 4 ? 1 : -1];
+
+#pragma pack(push, 2)
+struct s1 {
+ char f0;
+ int f1;
+};
+extern int a1[offsetof(struct s1, f1) == 2 ? 1 : -1];
+#pragma pack(pop)
+
+// Test scope of definition
+
+#pragma pack(push, 2)
+struct s2_0 {
+#pragma pack(pop)
+ char f0;
+ int f1;
+};
+extern int a2_0[offsetof(struct s2_0, f1) == 2 ? 1 : -1];
+
+struct s2_1 {
+ char f0;
+#pragma pack(push, 2)
+ int f1;
+#pragma pack(pop)
+};
+extern int a2_1[offsetof(struct s2_1, f1) == 4 ? 1 : -1];
+
+struct s2_2 {
+ char f0;
+ int f1;
+#pragma pack(push, 2)
+};
+#pragma pack(pop)
+extern int a2_2[offsetof(struct s2_2, f1) == 4 ? 1 : -1];
+
+struct s2_3 {
+ char f0;
+#pragma pack(push, 2)
+ struct s2_3_0 {
+#pragma pack(pop)
+ int f0;
+ } f1;
+};
+extern int a2_3[offsetof(struct s2_3, f1) == 2 ? 1 : -1];
+
+struct s2_4 {
+ char f0;
+ struct s2_4_0 {
+ int f0;
+#pragma pack(push, 2)
+ } f1;
+#pragma pack(pop)
+};
+extern int a2_4[offsetof(struct s2_4, f1) == 4 ? 1 : -1];
More information about the cfe-commits
mailing list