[cfe-commits] r133045 - in /cfe/trunk: lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclAttr.cpp test/Sema/format-strings.c

Douglas Gregor dgregor at apple.com
Tue Jun 14 22:45:11 PDT 2011


Author: dgregor
Date: Wed Jun 15 00:45:11 2011
New Revision: 133045

URL: http://llvm.org/viewvc/llvm-project?rev=133045&view=rev
Log:
Don't add redundant FormatAttr, ConstAttr, or NoThrowAttr attributes,
either imlicitly (for builtins) or explicitly (due to multiple
specification of the same attributes). Fixes <rdar://problem/9612060>.

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

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=133045&r1=133044&r2=133045&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Jun 15 00:45:11 2011
@@ -6471,6 +6471,9 @@
 /// These attributes can apply both to implicitly-declared builtins
 /// (like __builtin___printf_chk) or to library-declared functions
 /// like NSLog or printf.
+///
+/// We need to check for duplicate attributes both here and where user-written
+/// attributes are applied to declarations.
 void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) {
   if (FD->isInvalidDecl())
     return;
@@ -6504,9 +6507,9 @@
         FD->addAttr(::new (Context) ConstAttr(FD->getLocation(), Context));
     }
 
-    if (Context.BuiltinInfo.isNoThrow(BuiltinID))
+    if (Context.BuiltinInfo.isNoThrow(BuiltinID) && !FD->getAttr<NoThrowAttr>())
       FD->addAttr(::new (Context) NoThrowAttr(FD->getLocation(), Context));
-    if (Context.BuiltinInfo.isConst(BuiltinID))
+    if (Context.BuiltinInfo.isConst(BuiltinID) && !FD->getAttr<ConstAttr>())
       FD->addAttr(::new (Context) ConstAttr(FD->getLocation(), Context));
   }
 

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=133045&r1=133044&r2=133045&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Wed Jun 15 00:45:11 2011
@@ -1525,8 +1525,13 @@
     S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
     return;
   }
-
-  d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
+  
+  if (NoThrowAttr *Existing = d->getAttr<NoThrowAttr>()) {
+    if (Existing->getLocation().isInvalid())
+      Existing->setLocation(Attr.getLoc());
+  } else {
+    d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
+  }
 }
 
 static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
@@ -1536,7 +1541,12 @@
     return;
   }
 
-  d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
+  if (ConstAttr *Existing = d->getAttr<ConstAttr>()) {
+   if (Existing->getLocation().isInvalid())
+     Existing->setLocation(Attr.getLoc());
+  } else {
+    d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
+  }
 }
 
 static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
@@ -1904,6 +1914,23 @@
     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->setLocation(Attr.getLoc());
+      return;
+    }
+  }
+  
   d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
                                           Idx.getZExtValue(),
                                           FirstArg.getZExtValue()));

Modified: cfe/trunk/test/Sema/format-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=133045&r1=133044&r2=133045&view=diff
==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Wed Jun 15 00:45:11 2011
@@ -358,3 +358,8 @@
   printf(__func__); // no-warning
 }
 
+int printf(const char * restrict, ...) __attribute__((__format__ (__printf__, 1, 2)));
+
+void rdar9612060(void) {
+  printf("%s", 2); // expected-warning{{conversion specifies type 'char *' but the argument has type 'int'}}
+}





More information about the cfe-commits mailing list