From: Anders Carlsson Date: Sat, 16 Feb 2008 00:29:18 +0000 (+0000) Subject: Handle packed attribute correctly X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ad148069328e73c839c9284f70635cbc0d366bf8;p=clang Handle packed attribute correctly git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@47197 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Sema/Sema.h b/Sema/Sema.h index 348f83b7a2..ae357507a4 100644 --- a/Sema/Sema.h +++ b/Sema/Sema.h @@ -267,7 +267,8 @@ private: void HandleOCUVectorTypeAttribute(TypedefDecl *d, AttributeList *rawAttr); void HandleAlignedAttribute(Decl *d, AttributeList *rawAttr); - + void HandlePackedAttribute(Decl *d, AttributeList *rawAttr); + void WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method, bool &IncompleteImpl); diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index 8f1085c0cc..f4f7c3fa10 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -1242,6 +1242,7 @@ Sema::DeclTy *Sema::ActOnTag(Scope *S, unsigned TagType, TagKind TK, S->AddDecl(New); } + HandleDeclAttributes(New, Attr, 0); return New; } @@ -1300,6 +1301,9 @@ Sema::DeclTy *Sema::ActOnField(Scope *S, DeclTy *TagDecl, else assert(0 && "Sema::ActOnField(): Unknown TagDecl"); + HandleDeclAttributes(NewFD, D.getDeclSpec().getAttributes(), + D.getAttributes()); + if (D.getInvalidType() || InvalidDecl) NewFD->setInvalidDecl(); return NewFD; @@ -1772,10 +1776,11 @@ void Sema::HandleDeclAttribute(Decl *New, AttributeList *rawAttr) { if (!newType.isNull()) // install the new addr spaced type into the decl vDecl->setType(newType); } - } else if (attrLen == 7 && !memcmp(attrName, "aligned", 7)) { - HandleAlignedAttribute(New, rawAttr); - } - + } else if (attrLen == 7 && !memcmp(attrName, "aligned", 7)) + HandleAlignedAttribute(New, rawAttr); + else if (attrLen == 6 && !memcmp(attrName, "packed", 6)) + HandlePackedAttribute(New, rawAttr); + // FIXME: add other attributes... } @@ -1920,6 +1925,32 @@ QualType Sema::HandleVectorTypeAttribute(QualType curType, return Context.getVectorType(curType, vectorSize/typeSize); } +void Sema::HandlePackedAttribute(Decl *d, AttributeList *rawAttr) +{ + // check the attribute arguments. + if (rawAttr->getNumArgs() > 0) { + Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments, + std::string("0")); + return; + } + + if (TagDecl *TD = dyn_cast(d)) + TD->addAttr(new PackedAttr); + else if (FieldDecl *FD = dyn_cast(d)) { + // If the alignment is less than or equal to 8 bits, the packed attribute + // has no effect. + if (Context.getTypeAlign(FD->getType(), SourceLocation()) <= 8) + Diag(rawAttr->getAttributeLoc(), + diag::warn_attribute_ignored_for_field_of_type, + rawAttr->getAttributeName()->getName(), + FD->getType().getAsString()); + else + TD->addAttr(new PackedAttr); + } else + Diag(rawAttr->getAttributeLoc(), diag::warn_attribute_ignored, + rawAttr->getAttributeName()->getName()); +} + void Sema::HandleAlignedAttribute(Decl *d, AttributeList *rawAttr) { // check the attribute arguments. diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def index 1c97120a72..198183918d 100644 --- a/include/clang/Basic/DiagnosticKinds.def +++ b/include/clang/Basic/DiagnosticKinds.def @@ -579,6 +579,10 @@ DIAG(err_ocuvector_component_access, ERROR, "vector component access limited to variables") DIAG(err_attribute_address_space_not_int, ERROR, "address space attribute requires an integer constant") +DIAG(warn_attribute_ignored, WARNING, + "'%0' attribute ignored") +DIAG(warn_attribute_ignored_for_field_of_type, WARNING, + "'%0' attribute ignored for field of type '%1'") // Function Parameter Semantic Analysis. DIAG(err_param_with_void_type, ERROR,