From: Daniel Jasper Date: Wed, 29 May 2013 13:16:10 +0000 (+0000) Subject: Leave some macros on their own line X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7e70f4c09c5e10c2690eac04d9d812fb8e4a0c0d;p=clang Leave some macros on their own line If an identifier is on its own line and it is all upper case, it is highly likely that this is a macro that is meant to stand on a line by itself. Before: class A : public QObject { Q_OBJECT A() {} }; Ater: class A : public QObject { Q_OBJECT A() {} }; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182855 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 341952bb72..316d324f7a 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -581,7 +581,8 @@ void UnwrappedLineParser::parseStructuralElement() { // Otherwise this was a braced init list, and the structural // element continues. break; - case tok::identifier: + case tok::identifier: { + StringRef Text = FormatTok->TokenText; nextToken(); if (Line->Tokens.size() == 1) { if (FormatTok->Tok.is(tok::colon)) { @@ -596,9 +597,14 @@ void UnwrappedLineParser::parseStructuralElement() { addUnwrappedLine(); return; } + } else if (FormatTok->HasUnescapedNewline && Text.size() >= 5 && + Text == Text.upper()) { + // Recognize free-standing macros like Q_OBJECT. + addUnwrappedLine(); } } break; + } case tok::equal: nextToken(); if (FormatTok->Tok.is(tok::l_brace)) { diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 0d3f2807a9..eacd189840 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -1690,6 +1690,34 @@ TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { "f(STR(this_is_a_string_literal{));"); } +TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { + verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. + EXPECT_EQ("class A : public QObject {\n" + " Q_OBJECT\n" + "\n" + " A() {}\n" + "};", + format("class A : public QObject {\n" + " Q_OBJECT\n" + "\n" + " A() {\n}\n" + "} ;")); + // Only if the identifier contains at least 5 characters. + EXPECT_EQ("HTTP f();", + format("HTTP\nf();")); + EXPECT_EQ("MACRO\nf();", + format("MACRO\nf();")); + // Only if everything is upper case. + EXPECT_EQ("class A : public QObject {\n" + " Q_Object A() {}\n" + "};", + format("class A : public QObject {\n" + " Q_Object\n" + "\n" + " A() {\n}\n" + "} ;")); +} + TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"