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() {}
};

llvm-svn: 182855
This commit is contained in:
Daniel Jasper 2013-05-29 13:16:10 +00:00
parent 09886cd17a
commit 40e1921f2a
2 changed files with 35 additions and 1 deletions

View File

@ -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)) {

View File

@ -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"