[cfe-commits] r156727 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td include/clang/Sema/Sema.h lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclAttr.cpp test/Sema/attr-section.c

Rafael Espindola rafael.espindola at gmail.com
Sat May 12 19:42:43 PDT 2012


Author: rafael
Date: Sat May 12 21:42:42 2012
New Revision: 156727

URL: http://llvm.org/viewvc/llvm-project?rev=156727&view=rev
Log:
Produce a warning for mismatched section attributes. Completest pr9356.

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

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=156727&r1=156726&r2=156727&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Sat May 12 21:42:42 2012
@@ -25,6 +25,7 @@
 def : DiagGroup<"attributes">;
 def : DiagGroup<"bad-function-cast">;
 def Availability : DiagGroup<"availability">;
+def Section : DiagGroup<"section">;
 def AutoImport : DiagGroup<"auto-import">;
 def ConstantConversion : DiagGroup<"constant-conversion">;
 def LiteralConversion : DiagGroup<"literal-conversion">;

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=156727&r1=156726&r2=156727&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Sat May 12 21:42:42 2012
@@ -1567,10 +1567,14 @@
 
 def err_only_annotate_after_access_spec : Error<
   "access specifier can only have annotation attributes">;
+
 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<Section>;
+
 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<

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=156727&r1=156726&r2=156727&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Sat May 12 21:42:42 2012
@@ -1572,6 +1572,8 @@
   bool mergeDLLExportAttr(Decl *D, SourceRange Range, bool Inherited);
   bool mergeFormatAttr(Decl *D, SourceRange Range, bool Inherited,
                        StringRef Format, int FormatIdx, int FirstArg);
+  bool mergeSectionAttr(Decl *D, SourceRange Range, bool Inherited,
+                        StringRef Name);
   bool mergeDeclAttribute(Decl *New, InheritableAttr *Attr);
 
   void mergeDeclAttributes(Decl *New, Decl *Old, bool MergeDeprecation = true);

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=156727&r1=156726&r2=156727&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat May 12 21:42:42 2012
@@ -1678,6 +1678,9 @@
     return mergeFormatAttr(D, FA->getRange(), true, FA->getType(),
                            FA->getFormatIdx(), FA->getFirstArg());
 
+  if (SectionAttr *SA = dyn_cast<SectionAttr>(Attr))
+    return mergeSectionAttr(D, SA->getRange(), true, SA->getName());
+
   if (!DeclHasAttr(D, Attr)) {
     InheritableAttr *NewAttr = cast<InheritableAttr>(Attr->clone(Context));
     NewAttr->setInherited(true);

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=156727&r1=156726&r2=156727&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Sat May 12 21:42:42 2012
@@ -2286,6 +2286,22 @@
                                                      WGSize[2]));
 }
 
+bool Sema::mergeSectionAttr(Decl *D, SourceRange Range, bool Inherited,
+                            StringRef Name) {
+  if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
+    if (ExistingAttr->getName() == Name)
+      return false;
+    Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section);
+    Diag(Range.getBegin(), diag::note_previous_attribute);
+    return false;
+  }
+  SectionAttr *Attr = ::new (Context) SectionAttr(Range, Context, Name);
+  if (Inherited)
+    Attr->setInherited(true);
+  D->addAttr(Attr);
+  return true;
+}
+
 static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
   // Attribute has no arguments.
   if (!checkAttributeNumArgs(S, Attr, 1))
@@ -2313,9 +2329,7 @@
     S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
     return;
   }
-  
-  D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
-                                           SE->getString()));
+  S.mergeSectionAttr(D, Attr.getRange(), false, SE->getString());
 }
 
 

Modified: cfe/trunk/test/Sema/attr-section.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-section.c?rev=156727&r1=156726&r2=156727&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-section.c (original)
+++ cfe/trunk/test/Sema/attr-section.c Sat May 12 21:42:42 2012
@@ -13,3 +13,7 @@
   __attribute__((section("NEAR,x"))) int n1; // expected-error {{'section' attribute is not valid on local 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}}





More information about the cfe-commits mailing list