]> granicus.if.org Git - clang/commitdiff
Format */& as binary operator if followed by a unary operator.
authorDaniel Jasper <djasper@google.com>
Wed, 2 Jan 2013 17:21:36 +0000 (17:21 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 2 Jan 2013 17:21:36 +0000 (17:21 +0000)
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 :-)

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171396 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 8ea95c48621c4ca17f11cd04655e240eb069f264..3c337ec06314cb9c50beea53e99ad0450e834540 100644 (file)
@@ -835,19 +835,23 @@ private:
   }
 
   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 @@ private:
       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;
   }
index daae94bb13edc30cb85b43a73f35063331c6bfaf..574d685fbd8d62f4d214514af4f348faa6824d12 100644 (file)
@@ -28,6 +28,7 @@ protected:
         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 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) {
   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 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
   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;");