]> granicus.if.org Git - clang/commitdiff
implement PR6007, diagnosing invalid attribute((section))
authorChris Lattner <sabre@nondot.org>
Tue, 12 Jan 2010 20:58:53 +0000 (20:58 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 12 Jan 2010 20:58:53 +0000 (20:58 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@93255 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclAttr.cpp
test/Sema/attr-section.c

index 645045e84d70e1c545b1ecd657093c4f5201486f..4cd8a952231c379a30bf6db60059264d330cb8c7 100644 (file)
@@ -683,6 +683,8 @@ def err_attribute_not_string : Error<
   "argument to %0 attribute was not a string literal">;
 def err_attribute_section_invalid_for_target : Error<
   "argument to 'section' attribute is not valid for this target: %0">;
+def err_attribute_section_local_variable : Error<
+  "'section' attribute is not valid on local variables">;
 def err_attribute_aligned_not_power_of_two : Error<
   "requested alignment is not a power of 2">;
 def warn_redeclaration_without_attribute_prev_attribute_ignored : Warning<
index ceab525db128c2f0819b6dd538989ea44a060bb3..1a12208e5a94155f7b01d23f84d20ae274722cc6 100644 (file)
@@ -926,14 +926,19 @@ static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
 
   // If the target wants to validate the section specifier, make it happen.
   std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
-  if (Error.empty()) {
-    D->addAttr(::new (S.Context) SectionAttr(SE->getString()));
+  if (!Error.empty()) {
+    S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
+    << Error;
     return;
   }
 
-  S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
-    << Error;
-
+  // This attribute cannot be applied to local variables.
+  if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
+    S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
+    return;
+  }
+  
+  D->addAttr(::new (S.Context) SectionAttr(SE->getString()));
 }
 
 static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
index 20ae2e3547b941fe3d63a3c3bae4d45a8d71d540..614f294d237e5d3560b23e52de74d417516d56e5 100644 (file)
@@ -8,3 +8,8 @@ int x __attribute__((section(
 int y __attribute__((section(
    "sadf"))); // expected-error {{mach-o section specifier requires a segment and section separated by a comma}}
 
+// PR6007
+void test() {
+  __attribute__((section("NEAR,x"))) int n1; // expected-error {{'section' attribute is not valid on local variables}}
+  __attribute__((section("NEAR,x"))) static int n2; // ok.
+}
\ No newline at end of file