[cfe-commits] r169500 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp
Daniel Jasper
djasper at google.com
Thu Dec 6 05:16:39 PST 2012
Author: djasper
Date: Thu Dec 6 07:16:39 2012
New Revision: 169500
URL: http://llvm.org/viewvc/llvm-project?rev=169500&view=rev
Log:
Improve clang-format's handling of unary operators.
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=169500&r1=169499&r2=169500&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Dec 6 07:16:39 2012
@@ -618,12 +618,24 @@
bool isUnaryOperator(unsigned Index) {
const Token &Tok = Line.Tokens[Index].Tok;
+
+ // '++', '--' and '!' are always unary operators.
+ if (Tok.is(tok::minusminus) || Tok.is(tok::plusplus) ||
+ Tok.is(tok::exclaim))
+ return true;
+
+ // The other possible unary operators are '+' and '-' as we
+ // determine the usage of '*' and '&' in determineStarAmpUsage().
if (Tok.isNot(tok::minus) && Tok.isNot(tok::plus))
return false;
+
+ // Use heuristics to recognize unary operators.
const Token &PreviousTok = Line.Tokens[Index - 1].Tok;
if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) ||
PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square))
return true;
+
+ // Fall back to marking the token as binary operator.
return Annotations[Index - 1].Type == TokenAnnotation::TT_BinaryOperator;
}
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=169500&r1=169499&r2=169500&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Dec 6 07:16:39 2012
@@ -364,6 +364,9 @@
verifyFormat("if (i != -1) {\n}");
verifyFormat("if (i > -1) {\n}");
verifyFormat("if (i < -1) {\n}");
+ verifyFormat("++(a->f());");
+ verifyFormat("--(a->f());");
+ verifyFormat("if (!(a->f())) {\n}");
}
TEST_F(FormatTest, UndestandsOverloadedOperators) {
More information about the cfe-commits
mailing list