[PATCH] D35847: clang-format: Fix left pointer alignment after delctype/typeof

Erik Uhlmann via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 3 12:17:02 PDT 2017


euhlmann updated this revision to Diff 109613.
euhlmann added a comment.

This uses `FormatToken::getPreviousNonComment` and adds a test. This also fixes a bug in token annotation that was breaking the test (by also using `getPreviousNonComment` instead of `Previous`)


https://reviews.llvm.org/D35847

Files:
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTest.cpp


Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -5422,6 +5422,10 @@
   verifyFormat("for (;; *a = b) {\n}", Left);
   verifyFormat("return *this += 1;", Left);
   verifyFormat("throw *x;", Left);
+  verifyFormat("delete *x;", Left);
+  verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
+  verifyFormat("[](const decltype(*a)* ptr) {}", Left);
+  verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
 
   verifyIndependentOfContext("a = *(x + y);");
   verifyIndependentOfContext("a = &(x + y);");
@@ -5468,9 +5472,6 @@
   verifyGoogleFormat("T** t = new T*;");
   verifyGoogleFormat("T** t = new T*();");
 
-  FormatStyle PointerLeft = getLLVMStyle();
-  PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
-  verifyFormat("delete *x;", PointerLeft);
   verifyFormat("STATIC_ASSERT((a & b) == 0);");
   verifyFormat("STATIC_ASSERT(0 == (a & b));");
   verifyFormat("template <bool a, bool b> "
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -1398,9 +1398,10 @@
       return TT_PointerOrReference;
 
     if (PrevToken->is(tok::r_paren) && PrevToken->MatchingParen &&
-        PrevToken->MatchingParen->Previous &&
-        PrevToken->MatchingParen->Previous->isOneOf(tok::kw_typeof,
-                                                    tok::kw_decltype))
+        PrevToken->MatchingParen->getPreviousNonComment() &&
+        PrevToken->MatchingParen->getPreviousNonComment()->isOneOf(
+          tok::kw_typeof,
+          tok::kw_decltype))
       return TT_PointerOrReference;
 
     if (PrevToken->Tok.isLiteral() ||
@@ -2206,7 +2207,12 @@
   if (Left.is(tok::l_square) && Right.is(tok::amp))
     return false;
   if (Right.is(TT_PointerOrReference))
-    return (Left.is(tok::r_paren) && Line.MightBeFunctionDecl) ||
+    return (Left.is(tok::r_paren) && Line.MightBeFunctionDecl &&
+            !(Left.MatchingParen &&
+              Left.MatchingParen->getPreviousNonComment() &&
+              Left.MatchingParen->getPreviousNonComment()->isOneOf(
+                tok::kw_typeof,
+                tok::kw_decltype))) ||
            (Left.Tok.isLiteral() || (Left.is(tok::kw_const) && Left.Previous &&
                                      Left.Previous->is(tok::r_paren)) ||
             (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35847.109613.patch
Type: text/x-patch
Size: 2559 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170803/93d8d86b/attachment.bin>


More information about the cfe-commits mailing list