From: Aaron Ballman Date: Thu, 12 Dec 2013 01:34:39 +0000 (+0000) Subject: Added a Subjects clause to the section attribute and made its diagnostics more consis... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7825d70d225444d63aa4770e12d332404678f1d2;p=clang Added a Subjects clause to the section attribute and made its diagnostics more consistent with other attributes. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@197104 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/Attr.td b/include/clang/Basic/Attr.td index 5a8739b393..e29f762d8d 100644 --- a/include/clang/Basic/Attr.td +++ b/include/clang/Basic/Attr.td @@ -53,6 +53,9 @@ def TLSVar : SubsetSubjecthasGlobalStorage() && !S->getTLSKind()}]>; +def GlobalVar : SubsetSubjecthasLocalStorage()}]>; + // A single argument to an attribute class Argument { string Name = name; @@ -787,6 +790,8 @@ def InitPriority : InheritableAttr { def Section : InheritableAttr { let Spellings = [GNU<"section">, CXX11<"gnu", "section">]; let Args = [StringArgument<"Name">]; + let Subjects = SubjectList<[Function, GlobalVar], ErrorDiag, + "ExpectedFunctionOrGlobalVar">; } def Sentinel : InheritableAttr { diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 4c710d53d5..031454337b 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1941,8 +1941,6 @@ def err_only_annotate_after_access_spec : Error< 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 warn_mismatched_section : Warning< "section does not match previous declaration">, InGroup
; @@ -2038,7 +2036,8 @@ def warn_attribute_wrong_decl_type : Warning< "types and namespaces|Objective-C interfaces|methods and properties|" "struct or union|struct, union or class|types|" "Objective-C instance methods|init methods of interface declarations|" - "variables, functions and classes|Objective-C protocols}1">, + "variables, functions and classes|Objective-C protocols|" + "functions and global variables}1">, InGroup; def err_attribute_wrong_decl_type : Error< "%0 attribute only applies to %select{functions|unions|" @@ -2051,7 +2050,8 @@ def err_attribute_wrong_decl_type : Error< "types and namespaces|Objective-C interfaces|methods and properties|" "struct or union|struct, union or class|types|" "Objective-C instance methods|init methods of interface declarations|" - "variables, functions and classes|Objective-C protocols}1">; + "variables, functions and classes|Objective-C protocols|" + "functions and global variables}1">; def warn_type_attribute_wrong_type : Warning< "'%0' only applies to %select{function|pointer|" "Objective-C object or block pointer}1 types; type here is %2">, diff --git a/include/clang/Sema/AttributeList.h b/include/clang/Sema/AttributeList.h index 9d870a22c2..f31435546a 100644 --- a/include/clang/Sema/AttributeList.h +++ b/include/clang/Sema/AttributeList.h @@ -911,7 +911,8 @@ enum AttributeDeclKind { ExpectedObjCInstanceMethod, ExpectedObjCInterfaceDeclInitMethod, ExpectedFunctionVariableOrClass, - ExpectedObjectiveCProtocol + ExpectedObjectiveCProtocol, + ExpectedFunctionOrGlobalVar }; } // end namespace clang diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 0edef9e52a..f96add1482 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -2349,12 +2349,6 @@ static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { return; } - // This attribute cannot be applied to local variables. - if (isa(D) && cast(D)->hasLocalStorage()) { - S.Diag(LiteralLoc, diag::err_attribute_section_local_variable); - return; - } - unsigned Index = Attr.getAttributeSpellingListIndex(); SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); if (NewAttr) diff --git a/test/Sema/attr-section.c b/test/Sema/attr-section.c index 396892a1c5..812de06718 100644 --- a/test/Sema/attr-section.c +++ b/test/Sema/attr-section.c @@ -10,10 +10,12 @@ int y __attribute__((section( // PR6007 void test() { - __attribute__((section("NEAR,x"))) int n1; // expected-error {{'section' attribute is not valid on local variables}} + __attribute__((section("NEAR,x"))) int n1; // expected-error {{'section' attribute only applies to functions and global variables}} __attribute__((section("NEAR,x"))) static int n2; // ok. } // pr9356 void __attribute__((section("foo,zed"))) test2(void); // expected-note {{previous attribute is here}} 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 and global variables}}