Skip to content

Commit 9869c3a

Browse files
committed
Produce a warning for mismatched section attributes. Completest pr9356.
llvm-svn: 156727
1 parent bac0fdb commit 9869c3a

File tree

6 files changed

+31
-3
lines changed

6 files changed

+31
-3
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def AmbigMemberTemplate : DiagGroup<"ambiguous-member-template">;
2525
def : DiagGroup<"attributes">;
2626
def : DiagGroup<"bad-function-cast">;
2727
def Availability : DiagGroup<"availability">;
28+
def Section : DiagGroup<"section">;
2829
def AutoImport : DiagGroup<"auto-import">;
2930
def ConstantConversion : DiagGroup<"constant-conversion">;
3031
def LiteralConversion : DiagGroup<"literal-conversion">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,10 +1567,14 @@ def warn_objc_redundant_literal_use : Warning<
15671567

15681568
def err_only_annotate_after_access_spec : Error<
15691569
"access specifier can only have annotation attributes">;
1570+
15701571
def err_attribute_section_invalid_for_target : Error<
15711572
"argument to 'section' attribute is not valid for this target: %0">;
15721573
def err_attribute_section_local_variable : Error<
15731574
"'section' attribute is not valid on local variables">;
1575+
def warn_mismatched_section : Warning<
1576+
"section does not match previous declaration">, InGroup<Section>;
1577+
15741578
def err_attribute_aligned_not_power_of_two : Error<
15751579
"requested alignment is not a power of 2">;
15761580
def warn_redeclaration_without_attribute_prev_attribute_ignored : Warning<

clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,8 @@ class Sema {
15721572
bool mergeDLLExportAttr(Decl *D, SourceRange Range, bool Inherited);
15731573
bool mergeFormatAttr(Decl *D, SourceRange Range, bool Inherited,
15741574
StringRef Format, int FormatIdx, int FirstArg);
1575+
bool mergeSectionAttr(Decl *D, SourceRange Range, bool Inherited,
1576+
StringRef Name);
15751577
bool mergeDeclAttribute(Decl *New, InheritableAttr *Attr);
15761578

15771579
void mergeDeclAttributes(Decl *New, Decl *Old, bool MergeDeprecation = true);

clang/lib/Sema/SemaDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,9 @@ bool Sema::mergeDeclAttribute(Decl *D, InheritableAttr *Attr) {
16781678
return mergeFormatAttr(D, FA->getRange(), true, FA->getType(),
16791679
FA->getFormatIdx(), FA->getFirstArg());
16801680

1681+
if (SectionAttr *SA = dyn_cast<SectionAttr>(Attr))
1682+
return mergeSectionAttr(D, SA->getRange(), true, SA->getName());
1683+
16811684
if (!DeclHasAttr(D, Attr)) {
16821685
InheritableAttr *NewAttr = cast<InheritableAttr>(Attr->clone(Context));
16831686
NewAttr->setInherited(true);

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2286,6 +2286,22 @@ static void handleReqdWorkGroupSize(Sema &S, Decl *D,
22862286
WGSize[2]));
22872287
}
22882288

2289+
bool Sema::mergeSectionAttr(Decl *D, SourceRange Range, bool Inherited,
2290+
StringRef Name) {
2291+
if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2292+
if (ExistingAttr->getName() == Name)
2293+
return false;
2294+
Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
2295+
Diag(Range.getBegin(), diag::note_previous_attribute);
2296+
return false;
2297+
}
2298+
SectionAttr *Attr = ::new (Context) SectionAttr(Range, Context, Name);
2299+
if (Inherited)
2300+
Attr->setInherited(true);
2301+
D->addAttr(Attr);
2302+
return true;
2303+
}
2304+
22892305
static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
22902306
// Attribute has no arguments.
22912307
if (!checkAttributeNumArgs(S, Attr, 1))
@@ -2313,9 +2329,7 @@ static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
23132329
S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
23142330
return;
23152331
}
2316-
2317-
D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
2318-
SE->getString()));
2332+
S.mergeSectionAttr(D, Attr.getRange(), false, SE->getString());
23192333
}
23202334

23212335

clang/test/Sema/attr-section.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ void test() {
1313
__attribute__((section("NEAR,x"))) int n1; // expected-error {{'section' attribute is not valid on local variables}}
1414
__attribute__((section("NEAR,x"))) static int n2; // ok.
1515
}
16+
17+
// pr9356
18+
void __attribute__((section("foo,zed"))) test2(void); // expected-note {{previous attribute is here}}
19+
void __attribute__((section("bar,zed"))) test2(void) {} // expected-warning {{section does not match previous declaration}}

0 commit comments

Comments
 (0)