From dc293d3fd18c8dc7b89f1b6615745ef078e6be71 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 5 Nov 2014 10:48:04 +0000 Subject: [PATCH] clang-format: Improve free-standing macro detection. Before: SOME_WEIRD_LOG_MACRO << "Something long enough to cause a line break"; After: SOME_WEIRD_LOG_MACRO << "Something long enough to cause a line break"; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@221338 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineParser.cpp | 17 +++++++---------- unittests/Format/FormatTest.cpp | 5 +++++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 0b37acbebf..10b4aeaca3 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -812,17 +812,14 @@ void UnwrappedLineParser::parseStructuralElement() { parseLabel(); return; } - // Recognize function-like macro usages without trailing semicolon. - if (FormatTok->Tok.is(tok::l_paren)) { + // Recognize function-like macro usages without trailing semicolon as + // well as free-standing macrose like Q_OBJECT. + bool FunctionLike = FormatTok->is(tok::l_paren); + if (FunctionLike) parseParens(); - if (FormatTok->NewlinesBefore > 0 && - tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { - addUnwrappedLine(); - return; - } - } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 && - Text == Text.upper()) { - // Recognize free-standing macros like Q_OBJECT. + if (FormatTok->NewlinesBefore > 0 && + (Text.size() >= 5 || FunctionLike) && + tokenCanStartNewLine(FormatTok->Tok) && Text == Text.upper()) { addUnwrappedLine(); return; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index cd83265106..9e63bd11bb 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -2569,6 +2569,11 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { " Q_Object\n" " A() {\n}\n" "} ;")); + + // Only if the next line can actually start an unwrapped line. + EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", + format("SOME_WEIRD_LOG_MACRO\n" + "<< SomeThing;")); } TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { -- 2.40.0