[clang] 8aa3b8d - [clang-format] Handle typename macros inside cast expressions

Alex Richardson via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 7 02:09:34 PDT 2020


Author: Alex Richardson
Date: 2020-09-07T10:09:17+01:00
New Revision: 8aa3b8da5db2ae73bf536b630915eb9f0ddc15cb

URL: https://github.com/llvm/llvm-project/commit/8aa3b8da5db2ae73bf536b630915eb9f0ddc15cb
DIFF: https://github.com/llvm/llvm-project/commit/8aa3b8da5db2ae73bf536b630915eb9f0ddc15cb.diff

LOG: [clang-format] Handle typename macros inside cast expressions

Before: x = (STACK_OF(uint64_t)) & a;
After:  x = (STACK_OF(uint64_t))&a;

Reviewed By: MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D86930

Added: 
    

Modified: 
    clang/lib/Format/FormatToken.h
    clang/lib/Format/TokenAnnotator.cpp
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 795c26889629..a9aeef5e9e52 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -102,6 +102,7 @@ namespace format {
   TYPE(TrailingReturnArrow)                                                    \
   TYPE(TrailingUnaryOperator)                                                  \
   TYPE(TypenameMacro)                                                          \
+  TYPE(TypenameMacroParen)                                                     \
   TYPE(UnaryOperator)                                                          \
   TYPE(UntouchableMacroFunc)                                                   \
   TYPE(CSharpStringLiteral)                                                    \

diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index fc6a226dc4a1..097843bdca84 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -244,6 +244,8 @@ class AnnotatingParser {
       Contexts.back().IsExpression = false;
     } else if (Left->Previous && Left->Previous->is(tok::kw___attribute)) {
       Left->setType(TT_AttributeParen);
+    } else if (Left->Previous && Left->Previous->is(TT_TypenameMacro)) {
+      Left->setType(TT_TypenameMacroParen);
     } else if (Left->Previous && Left->Previous->is(TT_ForEachMacro)) {
       // The first argument to a foreach macro is a declaration.
       Contexts.back().IsForEachMacro = true;
@@ -335,6 +337,8 @@ class AnnotatingParser {
 
         if (Left->is(TT_AttributeParen))
           CurrentToken->setType(TT_AttributeParen);
+        if (Left->is(TT_TypenameMacroParen))
+          CurrentToken->setType(TT_TypenameMacroParen);
         if (Left->Previous && Left->Previous->is(TT_JavaAnnotation))
           CurrentToken->setType(TT_JavaAnnotation);
         if (Left->Previous && Left->Previous->is(TT_LeadingJavaAnnotation))
@@ -1855,9 +1859,11 @@ class AnnotatingParser {
       }
       return T && T->is(TT_PointerOrReference);
     };
-    bool ParensAreType = !Tok.Previous || Tok.Previous->is(TT_TemplateCloser) ||
-                         Tok.Previous->isSimpleTypeSpecifier() ||
-                         IsQualifiedPointerOrReference(Tok.Previous);
+    bool ParensAreType =
+        !Tok.Previous ||
+        Tok.Previous->isOneOf(TT_TemplateCloser, TT_TypenameMacroParen) ||
+        Tok.Previous->isSimpleTypeSpecifier() ||
+        IsQualifiedPointerOrReference(Tok.Previous);
     bool ParensCouldEndDecl =
         Tok.Next->isOneOf(tok::equal, tok::semi, tok::l_brace, tok::greater);
     if (ParensAreType && !ParensCouldEndDecl)

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index f224ab03271d..be68da6f2ef6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16557,6 +16557,8 @@ TEST_F(FormatTest, TypenameMacros) {
   Macros.PointerAlignment = FormatStyle::PAS_Left;
   verifyFormat("STACK_OF(int)* a;", Macros);
   verifyFormat("STACK_OF(int*)* a;", Macros);
+  verifyFormat("x = (STACK_OF(uint64_t))*a;", Macros);
+  verifyFormat("x = (STACK_OF(uint64_t))&a;", Macros);
 }
 
 TEST_F(FormatTest, AmbersandInLamda) {


        


More information about the cfe-commits mailing list