From bf9da1f8292bb66720ada94a050ede9dca17f60a Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 11 May 2012 00:36:07 +0000 Subject: [PATCH] Fix a recent regression with the merging of format attributes. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@156597 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 2 ++ lib/Sema/SemaDecl.cpp | 4 ++++ lib/Sema/SemaDeclAttr.cpp | 47 ++++++++++++++++++++++---------------- test/Sema/format-strings.c | 17 ++++++++++++++ 4 files changed, 50 insertions(+), 20 deletions(-) diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index e0ef1cf50a..b43a69929f 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -1570,6 +1570,8 @@ public: 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); diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 691c27cc25..64caf2bacc 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1674,6 +1674,10 @@ bool Sema::mergeDeclAttribute(Decl *D, InheritableAttr *Attr) { if (DLLExportAttr *ExportA = dyn_cast(Attr)) return mergeDLLExportAttr(D, ExportA->getRange(), true); + if (FormatAttr *FA = dyn_cast(Attr)) + return mergeFormatAttr(D, FA->getRange(), true, FA->getType(), + FA->getFormatIdx(), FA->getFirstArg()); + if (!DeclHasAttr(D, Attr)) { InheritableAttr *NewAttr = cast(Attr->clone(Context)); NewAttr->setInherited(true); diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 9cd72b2e1e..93516501aa 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -2535,6 +2535,31 @@ static void handleInitPriorityAttr(Sema &S, Decl *D, 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 + i = D->specific_attr_begin(), + e = D->specific_attr_end(); + 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 @@ static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { return; } - // Check whether we already have an equivalent format attribute. - for (specific_attr_iterator - i = D->specific_attr_begin(), - e = D->specific_attr_end(); - 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, diff --git a/test/Sema/format-strings.c b/test/Sema/format-strings.c index 1f9acd4ef8..a9a040fdc0 100644 --- a/test/Sema/format-strings.c +++ b/test/Sema/format-strings.c @@ -536,3 +536,20 @@ void pr12761(char c) { // 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}} +} -- 2.40.0