diff options
| author | Daniel Dunbar <daniel@zuster.org> | 2008-10-16 02:34:03 +0000 |
|---|---|---|
| committer | Daniel Dunbar <daniel@zuster.org> | 2008-10-16 02:34:03 +0000 |
| commit | 4290d46bd4b1adf33d5891ef94c00ded0a78f56f (patch) | |
| tree | ea85d5fb38357175e582e89195420c135c003f2e /clang/lib/Sema | |
| parent | 33332bce173c6eb9c8cdbbabb274836aaea3c3da (diff) | |
| download | bcm5719-llvm-4290d46bd4b1adf33d5891ef94c00ded0a78f56f.tar.gz bcm5719-llvm-4290d46bd4b1adf33d5891ef94c00ded0a78f56f.zip | |
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.
llvm-svn: 57623
Diffstat (limited to 'clang/lib/Sema')
| -rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 14 | ||||
| -rw-r--r-- | clang/lib/Sema/SemaDeclAttr.cpp | 4 |
2 files changed, 16 insertions, 2 deletions
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index fee732f2f56..e32da244de9 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -1939,6 +1939,20 @@ Sema::DeclTy *Sema::ActOnTagStruct(Scope *S, TagDecl::TagKind Kind, TagKind TK, // 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); diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 3ab0c91cb9c..5c04bf0b922 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -256,7 +256,7 @@ static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { } 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 @@ static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { 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()); |

