[cfe-commits] r156597 - in /cfe/trunk: include/clang/Sema/Sema.h lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclAttr.cpp test/Sema/format-strings.c

Rafael Espindola rafael.espindola at gmail.com
Thu May 10 17:36:07 PDT 2012


Author: rafael
Date: Thu May 10 19:36:07 2012
New Revision: 156597

URL: http://llvm.org/viewvc/llvm-project?rev=156597&view=rev
Log:
Fix a recent regression with the merging of format attributes.

Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp
    cfe/trunk/test/Sema/format-strings.c

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=156597&r1=156596&r2=156597&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Thu May 10 19:36:07 2012
@@ -1570,6 +1570,8 @@
                            bool Inherited, VisibilityAttr::VisibilityType Vis);
   bool mergeDLLImportAttr(Decl *D, SourceRange Range, bool Inherited);
   bool mergeDLLExportAttr(Decl *D, SourceRange Range, bool Inherited);
+  bool mergeFormatAttr(Decl *D, SourceRange Range, bool Inherited,
+                       StringRef Format, int FormatIdx, int FirstArg);
   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=156597&r1=156596&r2=156597&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu May 10 19:36:07 2012
@@ -1674,6 +1674,10 @@
   if (DLLExportAttr *ExportA = dyn_cast<DLLExportAttr>(Attr))
     return mergeDLLExportAttr(D, ExportA->getRange(), true);
 
+  if (FormatAttr *FA = dyn_cast<FormatAttr>(Attr))
+    return mergeFormatAttr(D, FA->getRange(), true, FA->getType(),
+                           FA->getFormatIdx(), FA->getFirstArg());
+
   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=156597&r1=156596&r2=156597&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Thu May 10 19:36:07 2012
@@ -2535,6 +2535,31 @@
                                                 prioritynum));
 }
 
+bool Sema::mergeFormatAttr(Decl *D, SourceRange Range, bool Inherited,
+                           StringRef Format, int FormatIdx, int FirstArg) {
+  // Check whether we already have an equivalent format attribute.
+  for (specific_attr_iterator<FormatAttr>
+         i = D->specific_attr_begin<FormatAttr>(),
+         e = D->specific_attr_end<FormatAttr>();
+       i != e ; ++i) {
+    FormatAttr *f = *i;
+    if (f->getType() == Format &&
+        f->getFormatIdx() == FormatIdx &&
+        f->getFirstArg() == FirstArg) {
+      // If we don't have a valid location for this attribute, adopt the
+      // location.
+      if (f->getLocation().isInvalid())
+        f->setRange(Range);
+      return false;
+    }
+  }
+
+  FormatAttr *Attr = ::new (Context) FormatAttr(Range, Context, Format,
+                                               FormatIdx, FirstArg);
+  D->addAttr(Attr);
+  return true;
+}
+
 /// Handle __attribute__((format(type,idx,firstarg))) attributes based on
 /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
 static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
@@ -2670,26 +2695,8 @@
     return;
   }
 
-  // Check whether we already have an equivalent format attribute.
-  for (specific_attr_iterator<FormatAttr>
-         i = D->specific_attr_begin<FormatAttr>(),
-         e = D->specific_attr_end<FormatAttr>();
-       i != e ; ++i) {
-    FormatAttr *f = *i;
-    if (f->getType() == Format &&
-        f->getFormatIdx() == (int)Idx.getZExtValue() &&
-        f->getFirstArg() == (int)FirstArg.getZExtValue()) {
-      // If we don't have a valid location for this attribute, adopt the
-      // location.
-      if (f->getLocation().isInvalid())
-        f->setRange(Attr.getRange());
-      return;
-    }
-  }
-  
-  D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
-                                          Idx.getZExtValue(),
-                                          FirstArg.getZExtValue()));
+  S.mergeFormatAttr(D, Attr.getRange(), false, Format, Idx.getZExtValue(),
+                    FirstArg.getZExtValue());
 }
 
 static void handleTransparentUnionAttr(Sema &S, Decl *D,

Modified: cfe/trunk/test/Sema/format-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=156597&r1=156596&r2=156597&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Thu May 10 19:36:07 2012
@@ -536,3 +536,20 @@
   // This should not warn even with -fno-signed-char.
   printf("%hhx", c);
 }
+
+
+// Test that we correctly merge the format in both orders.
+extern void test14_foo(const char *, const char *, ...)
+     __attribute__((__format__(__printf__, 1, 3)));
+extern void test14_foo(const char *, const char *, ...)
+     __attribute__((__format__(__scanf__, 2, 3)));
+
+extern void test14_bar(const char *, const char *, ...)
+     __attribute__((__format__(__scanf__, 2, 3)));
+extern void test14_bar(const char *, const char *, ...)
+     __attribute__((__format__(__printf__, 1, 3)));
+
+void test14_zed(int *p) {
+  test14_foo("%", "%d", p); // expected-warning{{incomplete format specifier}}
+  test14_bar("%", "%d", p); // expected-warning{{incomplete format specifier}}
+}





More information about the cfe-commits mailing list