]> granicus.if.org Git - clang/commitdiff
Handle packed attribute correctly
authorAnders Carlsson <andersca@mac.com>
Sat, 16 Feb 2008 00:29:18 +0000 (00:29 +0000)
committerAnders Carlsson <andersca@mac.com>
Sat, 16 Feb 2008 00:29:18 +0000 (00:29 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@47197 91177308-0d34-0410-b5e6-96231b3b80d8

Sema/Sema.h
Sema/SemaDecl.cpp
include/clang/Basic/DiagnosticKinds.def

index 348f83b7a23af1b4c494d6dca14d13559f4de6f3..ae357507a48c1130585719a85288be8a81f951ae 100644 (file)
@@ -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);
                            
index 8f1085c0ccd0a491e1774c6337ea269e6950b3e7..f4f7c3fa10ad39197fe9d578b0d45e06c6df0280 100644 (file)
@@ -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<TagDecl>(d))
+    TD->addAttr(new PackedAttr);
+  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.
+    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.
index 1c97120a72ecb9aa51f0a8b74eafa48bfd6a2feb..198183918d189538ca30ece6cc11b4206cde13d2 100644 (file)
@@ -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,