From a8fda85af8ddc91366b5522aee3a78a1fed206f7 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 15 May 2013 07:51:51 +0000 Subject: [PATCH] Improve formatting of function types. The function type detection in r181438 and r181764 detected function types too eagerly. This led to inconsistent formatting of inline assembly and (together with r181687) to an incorrect formatting of calls in macros. Before: #define DEREF_AND_CALL_F(parameter) f (*parameter) After: #define DEREF_AND_CALL_F(parameter) f(*parameter) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181870 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/TokenAnnotator.cpp | 7 ++++--- unittests/Format/FormatTest.cpp | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 11f81fd8ba..e0552de01d 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -155,6 +155,9 @@ private: } if (CurrentToken->is(tok::r_paren)) { + if (CurrentToken->Children.empty() || + !CurrentToken->Children[0].isOneOf(tok::l_paren, tok::l_square)) + Left->DefinesFunctionType = false; if (CurrentToken->Parent->closesScope()) CurrentToken->Parent->MatchingParen->NoMoreTokensOnLevel = true; Left->MatchingParen = CurrentToken; @@ -173,9 +176,7 @@ private: } if (CurrentToken->isOneOf(tok::r_square, tok::r_brace)) return false; - if (Left->Parent && - !Left->Parent->isOneOf(tok::kw_sizeof, tok::kw_alignof) && - CurrentToken->Parent->Type == TT_PointerOrReference && + if (CurrentToken->Parent->Type == TT_PointerOrReference && CurrentToken->Parent->Parent->isOneOf(tok::l_paren, tok::coloncolon)) Left->DefinesFunctionType = true; updateParameterCount(Left, CurrentToken); diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 206343bb69..081303af6a 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1110,7 +1110,7 @@ TEST_F(FormatTest, FormatsInlineASM) { "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" " \"cpuid\\n\\t\"\n" " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" - " : \"=a\" (*rEAX), \"=S\" (*rEBX), \"=c\" (*rECX), \"=d\" (*rEDX)\n" + " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" " : \"a\"(value));"); } @@ -2803,9 +2803,9 @@ TEST_F(FormatTest, FormatsFunctionTypes) { verifyGoogleFormat("A;"); verifyGoogleFormat("void* (*a)(int);"); - // Other constructs can look like function types: + // Other constructs can look somewhat like function types: verifyFormat("A a;"); - verifyFormat("A a;"); + verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)"); } TEST_F(FormatTest, BreaksLongDeclarations) { -- 2.40.0