[cfe-commits] r171396 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Daniel Jasper djasper at google.com
Wed Jan 2 09:58:53 PST 2013


The CL description is not precise enough (see patch). The unary operators
we use are: +, ++, -, --, !, ~ and alignof. Also there is a precedence in
the detection, so it actually formats:

int* a = *++i;

I am sure there are still cases we are missing, but this should be an
improvement. Feel free to file further bugs.


On Wed, Jan 2, 2013 at 6:47 PM, David Blaikie <dblaikie at gmail.com> wrote:

> On Wed, Jan 2, 2013 at 9:21 AM, Daniel Jasper <djasper at google.com> wrote:
> > Author: djasper
> > Date: Wed Jan  2 11:21:36 2013
> > New Revision: 171396
> >
> > URL: http://llvm.org/viewvc/llvm-project?rev=171396&view=rev
> > Log:
> > Format */& as binary operator if followed by a unary operator.
>
> That doesn't quite sound correct - what about cases where */& are
> actually unary operators even when they're followed by another unary
> operator?
>
> iter x;
> func(&*x);
>
> void func(int **x) {
>   **x = 3;
> }
>
> etc... (certainly *++x wouldn't be too uncommon, again with iterators,
> but any combination could be possible with arbitrary operator
> overloads, though I'm not sure how much you want/need to bother
> accounting for that kind of code)
>
> >
> > This fixes llvm.org/PR14687.
> > Also fixes segfault for lines starting with * or &.
> >
> > Before:
> > a *~b;
> > *a = 1;  // <- this segfaulted
> >
> > After:
> > a * ~b;
> > *a = 1;  // no segfault :-)
> >
> > Modified:
> >     cfe/trunk/lib/Format/Format.cpp
> >     cfe/trunk/unittests/Format/FormatTest.cpp
> >
> > Modified: cfe/trunk/lib/Format/Format.cpp
> > URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=171396&r1=171395&r2=171396&view=diff
> >
> ==============================================================================
> > --- cfe/trunk/lib/Format/Format.cpp (original)
> > +++ cfe/trunk/lib/Format/Format.cpp Wed Jan  2 11:21:36 2013
> > @@ -835,19 +835,23 @@
> >    }
> >
> >    TokenAnnotation::TokenType determineStarAmpUsage(unsigned Index, bool
> IsRHS) {
> > +    if (Index == 0)
> > +      return TokenAnnotation::TT_UnaryOperator;
> >      if (Index == Annotations.size())
> >        return TokenAnnotation::TT_Unknown;
> >      const FormatToken &PrevToken = Line.Tokens[Index - 1];
> >      const FormatToken &NextToken = Line.Tokens[Index + 1];
> >
> > -    if (Index == 0 || PrevToken.Tok.is(tok::l_paren) ||
> > -        PrevToken.Tok.is(tok::comma) || PrevToken.Tok.is(tok::kw_return)
> ||
> > -        PrevToken.Tok.is(tok::colon) ||
> > +    if (PrevToken.Tok.is(tok::l_paren) || PrevToken.Tok.is(tok::comma)
> ||
> > +        PrevToken.Tok.is(tok::kw_return) || PrevToken.Tok.is(tok::colon)
> ||
> >          Annotations[Index - 1].Type ==
> TokenAnnotation::TT_BinaryOperator)
> >        return TokenAnnotation::TT_UnaryOperator;
> >
> >      if (PrevToken.Tok.isLiteral() || NextToken.Tok.isLiteral() ||
> > -        NextToken.Tok.is(tok::kw_sizeof))
> > +        NextToken.Tok.is(tok::plus) || NextToken.Tok.is(tok::minus) ||
> > +        NextToken.Tok.is(tok::plusplus) || NextToken.Tok.is(tok::minusminus)
> ||
> > +        NextToken.Tok.is(tok::tilde) || NextToken.Tok.is(tok::exclaim)
> ||
> > +        NextToken.Tok.is(tok::kw_alignof) || NextToken.Tok.is
> (tok::kw_sizeof))
> >        return TokenAnnotation::TT_BinaryOperator;
> >
> >      if (NextToken.Tok.is(tok::comma) || NextToken.Tok.is(tok::r_paren)
> ||
> > @@ -931,7 +935,7 @@
> >        return Left.is(tok::kw_if) || Left.is(tok::kw_for) ||
> >               Left.is(tok::kw_while) || Left.is(tok::kw_switch) ||
> >               (Left.isNot(tok::identifier) && Left.isNot(tok::kw_sizeof)
> &&
> > -              Left.isNot(tok::kw_typeof));
> > +              Left.isNot(tok::kw_typeof) &&
> Left.isNot(tok::kw_alignof));
> >      }
> >      return true;
> >    }
> >
> > Modified: cfe/trunk/unittests/Format/FormatTest.cpp
> > URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=171396&r1=171395&r2=171396&view=diff
> >
> ==============================================================================
> > --- cfe/trunk/unittests/Format/FormatTest.cpp (original)
> > +++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan  2 11:21:36 2013
> > @@ -28,6 +28,7 @@
> >          CharSourceRange::getCharRange(Start,
> Start.getLocWithOffset(Length)));
> >      LangOptions LangOpts;
> >      LangOpts.CPlusPlus = 1;
> > +    LangOpts.CPlusPlus11 = 1;
> >      Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources,
> LangOpts);
> >      tooling::Replacements Replace =
> >          reformat(Style, Lex, Context.Sources, Ranges);
> > @@ -676,7 +677,9 @@
> >    verifyFormat("a-- > b;");
> >    verifyFormat("b ? -a : c;");
> >    verifyFormat("n * sizeof char16;");
> > +  verifyFormat("n * alignof char16;");
> >    verifyFormat("sizeof(char);");
> > +  verifyFormat("alignof(char);");
> >
> >    verifyFormat("return -1;");
> >    verifyFormat("switch (a) {\n"
> > @@ -724,6 +727,13 @@
> >    verifyFormat("return a & ~b;");
> >    verifyFormat("f(b ? *c : *d);");
> >    verifyFormat("int a = b ? *c : *d;");
> > +  verifyFormat("*b = a;");
> > +  verifyFormat("a * ~b;");
> > +  verifyFormat("a * !b;");
> > +  verifyFormat("a * +b;");
> > +  verifyFormat("a * -b;");
> > +  verifyFormat("a * ++b;");
> > +  verifyFormat("a * --b;");
> >
> >    // FIXME: Is this desired for LLVM? Fix if not.
> >    verifyFormat("A<int *> a;");
> >
> >
> > _______________________________________________
> > 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/20130102/bc4d415d/attachment.html>


More information about the cfe-commits mailing list