[cfe-commits] r167873 - in /cfe/trunk: lib/Sema/SemaDecl.cpp test/SemaCXX/warn-unused-result.cpp

Kaelyn Uhrain rikka at google.com
Tue Nov 13 13:52:06 PST 2012


I just used the assignment operator as an example of a method in a class
that would return an instance of the class. If there is some member
function that returns an instance of the class that really should be used,
the function can still be explicitly annotated. The idea being to not
surprise users with bizarre warnings such as not using the return value of
an operator= or operator<<, and to allow the attribute to be applied to
existing classes that shouldn't be ignored as return values but can/will be
assigned, copied, etc without having to go through and annotate all of the
member functions that return an instance of the class with an attribute to
disable unused result checking.

On Tue, Nov 13, 2012 at 1:40 PM, Jordan Rose <jordan_rose at apple.com> wrote:

> That doesn't seem right. Factory methods? operator-? I think assignment
> operators are the special case, not 'returning an instance'.
>
> …except for operator<< on streams. Hm.
>
> Jordan
>
>
> On Nov 13, 2012, at 13:23 , Kaelyn Uhrain <rikka at google.com> wrote:
>
> > Author: rikka
> > Date: Tue Nov 13 15:23:31 2012
> > New Revision: 167873
> >
> > URL: http://llvm.org/viewvc/llvm-project?rev=167873&view=rev
> > Log:
> > For classes that have the warn_unused_result attribute, don't apply the
> > attribute to the class' methods even when they return an instance of the
> > class (e.g. assignment operators).
> >
> > Modified:
> >    cfe/trunk/lib/Sema/SemaDecl.cpp
> >    cfe/trunk/test/SemaCXX/warn-unused-result.cpp
> >
> > Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
> > URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=167873&r1=167872&r2=167873&view=diff
> >
> ==============================================================================
> > --- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
> > +++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Nov 13 15:23:31 2012
> > @@ -5696,7 +5696,11 @@
> >       RetType->getAsCXXRecordDecl() : RetType->getPointeeCXXRecordDecl();
> >   if (!NewFD->isInvalidDecl() && !NewFD->hasAttr<WarnUnusedResultAttr>()
> &&
> >       Ret && Ret->hasAttr<WarnUnusedResultAttr>()) {
> > -    NewFD->addAttr(new (Context) WarnUnusedResultAttr(SourceRange(),
> Context));
> > +    const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewFD);
> > +    if (!(MD && MD->getCorrespondingMethodInClass(Ret, true))) {
> > +      NewFD->addAttr(new (Context) WarnUnusedResultAttr(SourceRange(),
> > +                                                        Context));
> > +    }
> >   }
> >
> >   if (!getLangOpts().CPlusPlus) {
> >
> > Modified: cfe/trunk/test/SemaCXX/warn-unused-result.cpp
> > URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unused-result.cpp?rev=167873&r1=167872&r2=167873&view=diff
> >
> ==============================================================================
> > --- cfe/trunk/test/SemaCXX/warn-unused-result.cpp (original)
> > +++ cfe/trunk/test/SemaCXX/warn-unused-result.cpp Tue Nov 13 15:23:31
> 2012
> > @@ -46,6 +46,12 @@
> > namespace warn_unused_CXX11 {
> > struct [[clang::warn_unused_result]] Status {
> >   bool ok() const;
> > +  Status& operator=(const Status& x);
> > +  inline void Update(const Status& new_status) {
> > +    if (ok()) {
> > +      *this = new_status; //no-warning
> > +    }
> > +  }
> > };
> > Status DoSomething();
> > Status& DoSomethingElse();
> >
> >
> > _______________________________________________
> > cfe-commits mailing list
> > cfe-commits at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20121113/9c5f6e55/attachment.html>


More information about the cfe-commits mailing list