tmatheson updated this revision to Diff 312232. tmatheson added a comment. Herald added a subscriber: jdoerfert.
Extended to Objective-C methods and properties. Added suggested C tests, C++ template function test and Objective-C tests. I had to removed PSF_Implicit flag so that cases where the function was declared before the global variable were caught. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D93102/new/ https://reviews.llvm.org/D93102 Files: clang/include/clang/AST/ASTContext.h clang/include/clang/Sema/Sema.h clang/lib/Sema/SemaAttr.cpp clang/lib/Sema/SemaDeclAttr.cpp clang/test/Sema/attr-section.c clang/test/SemaCXX/attr-section.cpp clang/test/SemaObjC/method-attributes.m
Index: clang/test/SemaObjC/method-attributes.m =================================================================== --- clang/test/SemaObjC/method-attributes.m +++ clang/test/SemaObjC/method-attributes.m @@ -99,3 +99,18 @@ + (void) CMethod : (id) Obj __attribute__((section("__TEXT,fee"))); @end + +// Section type conflicts between methods/properties and global variables +const int global1 __attribute__((section("seg1,sec1"))) = 10; // expected-note {{declared here}} expected-note {{declared here}} expected-note {{declared here}} +int global2 __attribute__((section("seg2,sec2"))) = 10; // expected-note {{declared here}} expected-note {{declared here}} expected-note {{declared here}} + +@interface section_conflicts : NSObject +@property int p1 __attribute__((section("seg1,sec1"))); // expected-error {{'p1' causes a section type conflict with 'global1'}} +@property int p2 __attribute__((section("seg2,sec2"))); // expected-error {{'p2' causes a section type conflict with 'global2'}} + +- (void)imethod1 __attribute__((section("seg1,sec1"))); // expected-error {{'imethod1' causes a section type conflict with 'global1'}} +- (void)imethod2 __attribute__((section("seg2,sec2"))); // expected-error {{'imethod2' causes a section type conflict with 'global2'}} + ++ (void)cmethod1:(id)Obj __attribute__((section("seg1,sec1"))); // expected-error {{'cmethod1:' causes a section type conflict with 'global1'}} ++ (void)cmethod2:(id)Obj __attribute__((section("seg2,sec2"))); // expected-error {{'cmethod2:' causes a section type conflict with 'global2'}} +@end Index: clang/test/SemaCXX/attr-section.cpp =================================================================== --- clang/test/SemaCXX/attr-section.cpp +++ clang/test/SemaCXX/attr-section.cpp @@ -42,3 +42,9 @@ __attribute__((section("baz"))) // expected-warning {{section does not match}} void C::f() {} } + +// Check for section type conflicts between global variables and function templates +template <typename> __attribute__((section("template_fn1"))) void template_fn1() {} // expected-note {{declared here}} +const int const_global_var __attribute__((section("template_fn1"))) = 42; // expected-error {{'const_global_var' causes a section type conflict with 'template_fn1'}} +int mut_global_var __attribute__((section("template_fn2"))) = 42; // expected-note {{declared here}} +template <typename> __attribute__((section("template_fn2"))) void template_fn2() {} // expected-error {{'template_fn2' causes a section type conflict with 'mut_global_var'}} Index: clang/test/Sema/attr-section.c =================================================================== --- clang/test/Sema/attr-section.c +++ clang/test/Sema/attr-section.c @@ -26,9 +26,27 @@ // Not a warning. int c; -int c __attribute__((section("foo,zed"))); +int c __attribute__((section("seg1,sec1"))); // Also OK. struct r_debug {}; extern struct r_debug _r_debug; struct r_debug _r_debug __attribute__((nocommon, section(".r_debug,bar"))); + +// Section type conflicts between functions and variables +void test3(void) __attribute__((section("seg3,sec3"))); // expected-note {{declared here}} +void test3(void) {} +const int const_global_var __attribute__((section("seg3,sec3"))) = 10; // expected-error {{'const_global_var' causes a section type conflict with 'test3'}} + +void test4(void) __attribute__((section("seg4,sec4"))); // expected-note {{declared here}} +void test4(void) {} +int mut_global_var __attribute__((section("seg4,sec4"))) = 10; // expected-error {{'mut_global_var' causes a section type conflict with 'test4'}} + +const int global_seg5sec5 __attribute__((section("seg5,sec5"))) = 10; // expected-note {{declared here}} +void test5(void) __attribute__((section("seg5,sec5"))); // expected-error {{'test5' causes a section type conflict with 'global_seg5sec5'}} +void test5(void) {} + +void test6(void); +const int global_seg6sec6 __attribute__((section("seg6,sec6"))) = 10; // expected-note {{declared here}} +void test6(void) __attribute__((section("seg6,sec6"))); // expected-error {{'test6' causes a section type conflict with 'global_seg6sec6'}} +void test6(void) {} Index: clang/lib/Sema/SemaDeclAttr.cpp =================================================================== --- clang/lib/Sema/SemaDeclAttr.cpp +++ clang/lib/Sema/SemaDeclAttr.cpp @@ -3081,8 +3081,14 @@ } SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str); - if (NewAttr) + if (NewAttr) { D->addAttr(NewAttr); + if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D) || + isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D)) + S.UnifySection(NewAttr->getName(), + ASTContext::PSF_Execute | ASTContext::PSF_Read, + cast<NamedDecl>(D)); + } } // This is used for `__declspec(code_seg("segname"))` on a decl. Index: clang/lib/Sema/SemaAttr.cpp =================================================================== --- clang/lib/Sema/SemaAttr.cpp +++ clang/lib/Sema/SemaAttr.cpp @@ -481,9 +481,8 @@ VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode); } -bool Sema::UnifySection(StringRef SectionName, - int SectionFlags, - DeclaratorDecl *Decl) { +bool Sema::UnifySection(StringRef SectionName, int SectionFlags, + NamedDecl *Decl) { SourceLocation PragmaLocation; if (auto A = Decl->getAttr<SectionAttr>()) if (A->isImplicit()) Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -9757,9 +9757,8 @@ PSK_CodeSeg, }; - bool UnifySection(StringRef SectionName, - int SectionFlags, - DeclaratorDecl *TheDecl); + bool UnifySection(StringRef SectionName, int SectionFlags, + NamedDecl *TheDecl); bool UnifySection(StringRef SectionName, int SectionFlags, SourceLocation PragmaSectionLocation); Index: clang/include/clang/AST/ASTContext.h =================================================================== --- clang/include/clang/AST/ASTContext.h +++ clang/include/clang/AST/ASTContext.h @@ -3086,13 +3086,12 @@ }; struct SectionInfo { - DeclaratorDecl *Decl; + NamedDecl *Decl; SourceLocation PragmaSectionLocation; int SectionFlags; SectionInfo() = default; - SectionInfo(DeclaratorDecl *Decl, - SourceLocation PragmaSectionLocation, + SectionInfo(NamedDecl *Decl, SourceLocation PragmaSectionLocation, int SectionFlags) : Decl(Decl), PragmaSectionLocation(PragmaSectionLocation), SectionFlags(SectionFlags) {}
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits