aboutsummaryrefslogtreecommitdiff
path: root/lib/Sema/SemaDecl.cpp
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2008-10-16 02:34:03 +0000
committerDaniel Dunbar <daniel@zuster.org>2008-10-16 02:34:03 +0000
commit3b0db908ebd07eaa26bc90deba5e826de00fe515 (patch)
tree2316573acc7083d4f2a9a4aecdf8beb50bab7713 /lib/Sema/SemaDecl.cpp
parentdc914c876cd2a4308c81a9bb0ac07033f2117c23 (diff)
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57623 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaDecl.cpp')
-rw-r--r--lib/Sema/SemaDecl.cpp14
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index fee732f2f5..e32da244de 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/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);