r314262 - Emit section information for extern variables.

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 26 16:42:34 PDT 2017


Author: erichkeane
Date: Tue Sep 26 16:42:34 2017
New Revision: 314262

URL: http://llvm.org/viewvc/llvm-project?rev=314262&view=rev
Log:
Emit section information for extern variables. 

Currently, if _attribute_((section())) is used for extern variables, 
section information is not emitted in generated IR when the variables are used. 
This is expected since sections are not generated for external linkage objects. 
However NiosII requires this information as it uses special GP-relative accesses 
for any objects that use attribute section (.sdata). GCC keeps this attribute in 
  middle-end.

This change emits the section information for all targets.

Patch By: Elizabeth Andrews

Differential Revision:https://reviews.llvm.org/D36487


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/attr-section.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=314262&r1=314261&r2=314262&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Sep 26 16:42:34 2017
@@ -2620,6 +2620,8 @@ def err_attribute_section_invalid_for_ta
   "argument to 'section' attribute is not valid for this target: %0">;
 def warn_mismatched_section : Warning<
   "section does not match previous declaration">, InGroup<Section>;
+def warn_attribute_section_on_redeclaration : Warning<
+  "section attribute is specified on redeclared variable">, InGroup<Section>;
 
 def err_anonymous_property: Error<
   "anonymous property is not supported">;

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=314262&r1=314261&r2=314262&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Tue Sep 26 16:42:34 2017
@@ -2432,6 +2432,12 @@ CodeGenModule::GetOrCreateLLVMGlobal(Str
       EmitGlobalVarDefinition(D);
     }
 
+    // Emit section information for extern variables.
+    if (D->hasExternalStorage()) {
+      if (const SectionAttr *SA = D->getAttr<SectionAttr>())
+        GV->setSection(SA->getName());
+    }
+
     // Handle XCore specific ABI requirements.
     if (getTriple().getArch() == llvm::Triple::xcore &&
         D->getLanguageLinkage() == CLanguageLinkage &&

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=314262&r1=314261&r2=314262&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Sep 26 16:42:34 2017
@@ -2607,6 +2607,16 @@ void Sema::mergeDeclAttributes(NamedDecl
     }
   }
 
+  // This redeclaration adds a section attribute.
+  if (New->hasAttr<SectionAttr>() && !Old->hasAttr<SectionAttr>()) {
+    if (auto *VD = dyn_cast<VarDecl>(New)) {
+      if (VD->isThisDeclarationADefinition() != VarDecl::Definition) {
+        Diag(New->getLocation(), diag::warn_attribute_section_on_redeclaration);
+        Diag(Old->getLocation(), diag::note_previous_declaration);
+      }
+    }
+  }
+
   if (!Old->hasAttrs())
     return;
 

Modified: cfe/trunk/test/Sema/attr-section.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-section.c?rev=314262&r1=314261&r2=314262&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-section.c (original)
+++ cfe/trunk/test/Sema/attr-section.c Tue Sep 26 16:42:34 2017
@@ -19,3 +19,7 @@ void __attribute__((section("foo,zed")))
 void __attribute__((section("bar,zed"))) test2(void) {} // expected-warning {{section does not match previous declaration}}
 
 enum __attribute__((section("NEAR,x"))) e { one }; // expected-error {{'section' attribute only applies to functions, methods, properties, and global variables}}
+
+extern int a; // expected-note {{previous declaration is here}}
+int *b = &a;
+extern int a __attribute__((section("foo,zed"))); // expected-warning {{section attribute is specified on redeclared variable}}




More information about the cfe-commits mailing list