[cfe-commits] r47216 - in /cfe/trunk: AST/ASTContext.cpp Sema/SemaDecl.cpp include/clang/AST/Attr.h include/clang/Basic/DiagnosticKinds.def

Anders Carlsson andersca at mac.com
Sat Feb 16 11:51:27 PST 2008


Author: andersca
Date: Sat Feb 16 13:51:27 2008
New Revision: 47216

URL: http://llvm.org/viewvc/llvm-project?rev=47216&view=rev
Log:
Better handling of the aligned attribute.

Modified:
    cfe/trunk/AST/ASTContext.cpp
    cfe/trunk/Sema/SemaDecl.cpp
    cfe/trunk/include/clang/AST/Attr.h
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def

Modified: cfe/trunk/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/AST/ASTContext.cpp?rev=47216&r1=47215&r2=47216&view=diff

==============================================================================
--- cfe/trunk/AST/ASTContext.cpp (original)
+++ cfe/trunk/AST/ASTContext.cpp Sat Feb 16 13:51:27 2008
@@ -319,6 +319,9 @@
   unsigned RecordAlign = 8;  // Default alignment = 1 byte = 8 bits.
 
   if (D->getKind() != Decl::Union) {
+    if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
+      RecordAlign = std::max(RecordAlign, AA->getAlignment());
+        
     bool StructIsPacked = D->getAttr<PackedAttr>();
     
     // Layout each field, for now, just sequentially, respecting alignment.  In
@@ -333,13 +336,26 @@
         // query getTypeInfo about these, so we figure it out here.
         // Flexible array members don't have any size, but they
         // have to be aligned appropriately for their element type.
-        const ArrayType* ATy = FD->getType()->getAsArrayType();
-        FieldAlign = FieldIsPacked ? 8 : getTypeAlign(ATy->getElementType(), L);
+        
+        if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
+          FieldAlign = AA->getAlignment();
+        else if (FieldIsPacked)
+          FieldAlign = 8;
+        else {
+          const ArrayType* ATy = FD->getType()->getAsArrayType();
+          FieldAlign = getTypeAlign(ATy->getElementType(), L);
+        }
         FieldSize = 0;
       } else {
         std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
         FieldSize = FieldInfo.first;
-        FieldAlign = FieldIsPacked ? 8 : FieldInfo.second;
+        
+        if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
+          FieldAlign = AA->getAlignment();
+        else if (FieldIsPacked)
+          FieldAlign = 8;
+        else
+          FieldAlign = FieldInfo.second;
       }
 
       // Round up the current record size to the field's alignment boundary.

Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=47216&r1=47215&r2=47216&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Sat Feb 16 13:51:27 2008
@@ -1835,8 +1835,8 @@
   Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
   llvm::APSInt vecSize(32);
   if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) {
-    Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int,
-         sizeExpr->getSourceRange());
+    Diag(rawAttr->getAttributeLoc(), diag::err_attribute_argument_not_int,
+         "ocu_vector_type", sizeExpr->getSourceRange());
     return;
   }
   // unlike gcc's vector_size attribute, we do not allow vectors to be defined
@@ -1873,8 +1873,8 @@
   Expr *sizeExpr = static_cast<Expr *>(rawAttr->getArg(0));
   llvm::APSInt vecSize(32);
   if (!sizeExpr->isIntegerConstantExpr(vecSize, Context)) {
-    Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int,
-         sizeExpr->getSourceRange());
+    Diag(rawAttr->getAttributeLoc(), diag::err_attribute_argument_not_int,
+         "vector_size", sizeExpr->getSourceRange());
     return QualType();
   }
   // navigate to the base type - we need to provide for vector pointers, 
@@ -1959,15 +1959,24 @@
     return;
   }
 
-  // TODO: We probably need to actually do something with aligned attribute.
-  if (rawAttr->getNumArgs() == 0)
-    return;
+  unsigned Align = 0;
+  
+  if (rawAttr->getNumArgs() == 0) {
+    // FIXME: This should be the target specific maximum alignment.
+    // (For now we just use 128 bits which is the maximum on X86.
+    Align = 128;
+    return;
+  } else {
+    Expr *alignmentExpr = static_cast<Expr *>(rawAttr->getArg(0));
+    llvm::APSInt alignment(32);
+    if (!alignmentExpr->isIntegerConstantExpr(alignment, Context)) {
+      Diag(rawAttr->getAttributeLoc(), diag::err_attribute_argument_not_int,
+           "aligned", alignmentExpr->getSourceRange());
+      return;
+    }
+    
+    Align = alignment.getZExtValue() * 8;
+  }
 
-  Expr *alignmentExpr = static_cast<Expr *>(rawAttr->getArg(0));
-  llvm::APSInt alignment(32);
-  if (!alignmentExpr->isIntegerConstantExpr(alignment, Context)) {
-    Diag(rawAttr->getAttributeLoc(), diag::err_attribute_vector_size_not_int,
-         alignmentExpr->getSourceRange());
-    return;
-  }    
+  d->addAttr(new AlignedAttr(Align));
 }

Modified: cfe/trunk/include/clang/AST/Attr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Attr.h?rev=47216&r1=47215&r2=47216&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Attr.h (original)
+++ cfe/trunk/include/clang/AST/Attr.h Sat Feb 16 13:51:27 2008
@@ -65,6 +65,20 @@
   }
   static bool classof(const PackedAttr *A) { return true; }
 };
+  
+class AlignedAttr : public Attr {
+  unsigned Alignment;
+public:
+  AlignedAttr(unsigned alignment) : Attr(Aligned), Alignment(alignment) {}
+  
+  unsigned getAlignment() const { return Alignment; }
+  
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Attr *A) {
+    return A->getKind() == Aligned;
+  }
+  static bool classof(const AlignedAttr *A) { return true; }
+};
 
 }  // end namespace clang
 

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=47216&r1=47215&r2=47216&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Sat Feb 16 13:51:27 2008
@@ -561,8 +561,8 @@
      "attribute requires %0 argument(s)")
 DIAG(err_attribute_invalid_vector_type, ERROR,
      "invalid vector type '%0'")
-DIAG(err_attribute_vector_size_not_int, ERROR,
-     "vector_size requires integer constant")
+DIAG(err_attribute_argument_not_int, ERROR,
+     "'%0' attribute requires integer constant")
 DIAG(err_attribute_invalid_size, ERROR,
      "vector size not an integral multiple of component size")
 DIAG(err_attribute_zero_size, ERROR,





More information about the cfe-commits mailing list