[cfe-commits] r64380 - in /cfe/trunk: include/clang/AST/Attr.h include/clang/Parse/AttributeList.h lib/CodeGen/CodeGenModule.cpp lib/Parse/AttributeList.cpp lib/Sema/SemaDeclAttr.cpp test/CodeGen/attributes.c

Daniel Dunbar daniel at zuster.org
Thu Feb 12 09:28:23 PST 2009


Author: ddunbar
Date: Thu Feb 12 11:28:23 2009
New Revision: 64380

URL: http://llvm.org/viewvc/llvm-project?rev=64380&view=rev
Log:
Support __attribute__(section(<name>))

Modified:
    cfe/trunk/include/clang/AST/Attr.h
    cfe/trunk/include/clang/Parse/AttributeList.h
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/Parse/AttributeList.cpp
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp
    cfe/trunk/test/CodeGen/attributes.c

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

==============================================================================
--- cfe/trunk/include/clang/AST/Attr.h (original)
+++ cfe/trunk/include/clang/AST/Attr.h Thu Feb 12 11:28:23 2009
@@ -45,6 +45,7 @@
     ObjCNSObject,
     Overloadable, // Clang-specific
     Packed,
+    Section,
     StdCall,
     TransparentUnion,
     Unavailable,
@@ -227,6 +228,20 @@
   static bool classof(const DeprecatedAttr *A) { return true; }
 };
 
+class SectionAttr : public Attr {
+  std::string Name;
+public:
+  SectionAttr(const std::string &N) : Attr(Section), Name(N) {}
+  
+  const std::string& getName() const { return Name; }
+  
+  // Implement isa/cast/dyncast/etc.
+  static bool classof(const Attr *A) {
+    return A->getKind() == Section;
+  }
+  static bool classof(const SectionAttr *A) { return true; }
+};
+
 class UnavailableAttr : public Attr {
 public:
   UnavailableAttr() : Attr(Unavailable) {}

Modified: cfe/trunk/include/clang/Parse/AttributeList.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/AttributeList.h?rev=64380&r1=64379&r2=64380&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/AttributeList.h (original)
+++ cfe/trunk/include/clang/Parse/AttributeList.h Thu Feb 12 11:28:23 2009
@@ -65,6 +65,7 @@
     AT_overloadable,      // Clang-specific
     AT_packed,
     AT_pure,
+    AT_section,
     AT_stdcall,
     AT_transparent_union,
     AT_unavailable,

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=64380&r1=64379&r2=64380&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Thu Feb 12 11:28:23 2009
@@ -259,6 +259,9 @@
     // should not be munged.
     GV->setName("\01" + ALA->getLabel());
   }
+
+  if (const SectionAttr *SA = D->getAttr<SectionAttr>())
+    GV->setSection(SA->getName());
 }
 
 void CodeGenModule::SetFunctionAttributes(const Decl *D,
@@ -653,6 +656,9 @@
     }
   }
 
+  if (const SectionAttr *SA = D->getAttr<SectionAttr>())
+    GV->setSection(SA->getName());
+
   // Emit global variable debug information.
   CGDebugInfo *DI = getDebugInfo();
   if(DI) {

Modified: cfe/trunk/lib/Parse/AttributeList.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/AttributeList.cpp?rev=64380&r1=64379&r2=64380&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/AttributeList.cpp (original)
+++ cfe/trunk/lib/Parse/AttributeList.cpp Thu Feb 12 11:28:23 2009
@@ -69,11 +69,12 @@
     break;
   case 7:
     if (!memcmp(Str, "aligned", 7)) return AT_aligned;
-    if (!memcmp(Str, "nothrow", 7)) return AT_nothrow;
+    if (!memcmp(Str, "cleanup", 7)) return AT_cleanup;
     if (!memcmp(Str, "nonnull", 7)) return AT_nonnull;
+    if (!memcmp(Str, "nothrow", 7)) return AT_nothrow;
     if (!memcmp(Str, "objc_gc", 7)) return AT_objc_gc;
+    if (!memcmp(Str, "section", 7)) return AT_section;
     if (!memcmp(Str, "stdcall", 7)) return AT_stdcall;
-    if (!memcmp(Str, "cleanup", 7)) return AT_cleanup;
     break;
   case 8:
     if (!memcmp(Str, "annotate", 8)) return AT_annotate;

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=64380&r1=64379&r2=64380&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Thu Feb 12 11:28:23 2009
@@ -784,6 +784,26 @@
   d->addAttr(new DLLExportAttr());
 }
 
+static void HandleSectionAttr(Decl *d, const AttributeList &Attr, Sema &S) {
+  // Attribute has no arguments.
+  if (Attr.getNumArgs() != 1) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
+    return;
+  }
+
+  // Make sure that there is a string literal as the sections's single
+  // argument.
+  StringLiteral *SE = 
+    dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
+  if (!SE) {
+    // FIXME
+    S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
+    return;
+  }
+  d->addAttr(new SectionAttr(std::string(SE->getStrData(),
+                                         SE->getByteLength())));
+}
+
 static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
   // Attribute has no arguments.
   if (Attr.getNumArgs() != 0) {
@@ -1306,6 +1326,7 @@
   case AttributeList::AT_noreturn:    HandleNoReturnAttr  (D, Attr, S); break;
   case AttributeList::AT_nothrow:     HandleNothrowAttr   (D, Attr, S); break;
   case AttributeList::AT_packed:      HandlePackedAttr    (D, Attr, S); break;
+  case AttributeList::AT_section:     HandleSectionAttr   (D, Attr, S); break;
   case AttributeList::AT_stdcall:     HandleStdCallAttr   (D, Attr, S); break;
   case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
   case AttributeList::AT_unused:      HandleUnusedAttr    (D, Attr, S); break;

Modified: cfe/trunk/test/CodeGen/attributes.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/attributes.c?rev=64380&r1=64379&r2=64380&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/attributes.c (original)
+++ cfe/trunk/test/CodeGen/attributes.c Thu Feb 12 11:28:23 2009
@@ -7,7 +7,11 @@
 // RUN: grep 't6.*protected' %t &&
 // RUN: grep 't7.*noreturn' %t &&
 // RUN: grep 't7.*nounwind' %t &&
-// RUN: grep 't9.*alias.*weak.*t8' %t
+// RUN: grep 't9.*alias.*weak.*t8' %t &&
+// RUN: grep '@t10().*section "SECT"' %t &&
+// RUN: grep '@t11().*section "SECT"' %t &&
+// RUN: grep '@t12 =.*section "SECT"' %t &&
+// RUN: grep '@t13 =.*section "SECT"' %t
 
 void t1() __attribute__((noreturn));
 void t1() {}
@@ -30,3 +34,11 @@
 
 void __t8() {}
 void t9() __attribute__((weak, alias("__t8")));
+
+void t10(void) __attribute__((section("SECT")));
+void t10(void) {}
+void __attribute__((section("SECT"))) t11(void) {}
+
+int t12 __attribute__((section("SECT")));
+struct s0 { int x; };
+struct s0 t13 __attribute__ ((section ("SECT"))) = { 0 };





More information about the cfe-commits mailing list