diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -38,9 +38,7 @@ // Basic function tests. //===----------------------------------------------------------------------===// -TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { - EXPECT_EQ(";", format(";")); -} +TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { verifyFormat(";"); } TEST_F(FormatTest, FormatsGlobalStatementsAt0) { EXPECT_EQ("int i;", format(" int i;")); @@ -5159,7 +5157,7 @@ } TEST_F(FormatTest, LayoutUnknownPPDirective) { - EXPECT_EQ("#;", format("#;")); + verifyFormat("#;"); verifyFormat("#\n;\n;\n;"); } @@ -5492,7 +5490,7 @@ getLLVMStyleWithColumns(13))); } -TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } +TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); } TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { EXPECT_EQ("#define A \\\n" @@ -5504,7 +5502,7 @@ getLLVMStyleWithColumns(14))); } -TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } +TEST_F(FormatTest, LayoutRemainingTokens) { verifyFormat("{}"); } TEST_F(FormatTest, MacroDefinitionInsideStatement) { EXPECT_EQ("int x,\n" @@ -5514,14 +5512,14 @@ } TEST_F(FormatTest, HashInMacroDefinition) { - EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); - EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle())); - EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle())); - EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle())); - EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle())); - EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle())); - EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle())); - EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle())); + verifyFormat("#define A(c) L#c", getLLVMStyle()); + verifyFormat("#define A(c) u#c", getLLVMStyle()); + verifyFormat("#define A(c) U#c", getLLVMStyle()); + verifyFormat("#define A(c) u8#c", getLLVMStyle()); + verifyFormat("#define A(c) LR#c", getLLVMStyle()); + verifyFormat("#define A(c) uR#c", getLLVMStyle()); + verifyFormat("#define A(c) UR#c", getLLVMStyle()); + verifyFormat("#define A(c) u8R#c", getLLVMStyle()); verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); verifyFormat("#define A \\\n" " { \\\n" @@ -5541,8 +5539,8 @@ } TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { - EXPECT_EQ("#define A (x)", format("#define A (x)")); - EXPECT_EQ("#define A(x)", format("#define A(x)")); + verifyFormat("#define A (x)"); + verifyFormat("#define A(x)"); FormatStyle Style = getLLVMStyle(); Style.SpaceBeforeParens = FormatStyle::SBPO_Never; @@ -5669,7 +5667,7 @@ "} // namespace")); // Only if the identifier contains at least 5 characters. EXPECT_EQ("HTTP f();", format("HTTP\nf();")); - EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); + verifyNoChange("MACRO\nf();"); // Only if everything is upper case. EXPECT_EQ("class A : public QObject {\n" " Q_Object A() {}\n" @@ -6099,7 +6097,7 @@ "# define C 0\n" "#endif"; EXPECT_EQ(Expected, format(ToFormat, Style)); - EXPECT_EQ(Expected, format(Expected, Style)); + verifyFormat(Expected, Style); } // Keep block quotes aligned. { @@ -6126,7 +6124,7 @@ "# define C 0\n" "#endif"; EXPECT_EQ(Expected, format(ToFormat, Style)); - EXPECT_EQ(Expected, format(Expected, Style)); + verifyNoChange(Expected, Style); } // Keep comments aligned with un-indented directives. { @@ -6149,7 +6147,7 @@ " // Code. Not aligned with #\n" "#define C 0\n"; EXPECT_EQ(Expected, format(ToFormat, Style)); - EXPECT_EQ(Expected, format(Expected, Style)); + verifyFormat(Expected, Style); } // Test AfterHash with tabs. { @@ -6236,7 +6234,7 @@ "#endif\n" "}"; EXPECT_EQ(Expected, format(ToFormat, Style)); - EXPECT_EQ(Expected, format(Expected, Style)); + verifyFormat(Expected, Style); } { const char *Expected = "void f() {\n" @@ -6266,7 +6264,7 @@ "#endif\n" "}"; EXPECT_EQ(Expected, format(ToFormat, Style)); - EXPECT_EQ(Expected, format(Expected, Style)); + verifyNoChange(Expected, Style); } // Test single comment before preprocessor @@ -6432,7 +6430,7 @@ EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); EXPECT_EQ("template f();", format("\\\ntemplate f();")); EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); - EXPECT_EQ("", format("")); + verifyNoChange(""); FormatStyle AlignLeft = getLLVMStyle(); AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; @@ -6450,7 +6448,7 @@ EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); EXPECT_EQ("template f();", format("\\\ntemplate f();")); EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); - EXPECT_EQ("", format("")); + verifyNoChange(""); EXPECT_EQ("#define MACRO(x) \\\r\n" "private: \\\r\n" " int x(int a);\r\n", @@ -6791,7 +6789,7 @@ } TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { - EXPECT_EQ("{}", format("{}")); + verifyFormat("{}"); verifyFormat("enum E {};"); verifyFormat("enum E {}"); FormatStyle Style = getLLVMStyle(); @@ -12358,14 +12356,6 @@ "};\n", Style); verifyFormat("struct foo { /* comment */\n" - "private:\n" - " int i;\n" - " // comment\n" - "\n" - "private:\n" - " int j;\n" - "};\n", - "struct foo { /* comment */\n" "private:\n" " int i;\n" " // comment\n" @@ -14504,8 +14494,7 @@ } TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { - EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", - format("#error Leave all white!!!!! space* alone!\n")); + verifyNoChange("#error Leave all white!!!!! space* alone!\n"); EXPECT_EQ( "#warning Leave all white!!!!! space* alone!\n", format("#warning Leave all white!!!!! space* alone!\n")); @@ -14656,8 +14645,7 @@ format("-(NSInteger)Method4:(id)anObject;")); EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); - EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", - format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); + verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"); EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " "forAllCells:(BOOL)flag;", format("- (void)sendAction:(SEL)aSelector to:(id)anObject " @@ -14732,10 +14720,8 @@ " \"other\";", format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); - EXPECT_EQ("\"some text\"", - format("\"some text\"", getLLVMStyleWithColumns(1))); - EXPECT_EQ("\"some text\"", - format("\"some text\"", getLLVMStyleWithColumns(11))); + verifyFormat("\"some text\"", getLLVMStyleWithColumns(1)); + verifyFormat("\"some text\"", getLLVMStyleWithColumns(11)); EXPECT_EQ("\"some \"\n" "\"text\"", format("\"some text\"", getLLVMStyleWithColumns(10))); @@ -14861,7 +14847,7 @@ FormatStyle Style = getLLVMStyleWithColumns(12); Style.BreakStringLiterals = false; - EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); + verifyFormat("\"some text other\";", Style); FormatStyle AlignLeft = getLLVMStyleWithColumns(12); AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; @@ -14925,12 +14911,11 @@ TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { FormatStyle Style = getGoogleStyleWithColumns(15); - EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); - EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); - EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); - EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); - EXPECT_EQ("u8R\"x(raw literal)x\";", - format("u8R\"x(raw literal)x\";", Style)); + verifyFormat("R\"x(raw literal)x\";", Style); + verifyFormat("uR\"x(raw literal)x\";", Style); + verifyFormat("LR\"x(raw literal)x\";", Style); + verifyFormat("UR\"x(raw literal)x\";", Style); + verifyFormat("u8R\"x(raw literal)x\";", Style); } TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { @@ -15210,8 +15195,8 @@ } TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { - EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); - EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); + verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3)); + verifyFormat("\"\\\"", getLLVMStyleWithColumns(2)); EXPECT_EQ("\"test\"\n" "\"\\n\"", format("\"test\\n\"", getLLVMStyleWithColumns(7))); @@ -15221,19 +15206,16 @@ EXPECT_EQ("\"\\\\\\\\\"\n" "\"\\n\"", format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); - EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); + verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7)); EXPECT_EQ("\"\\uff01\"\n" "\"test\"", format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); - EXPECT_EQ("\"\\Uff01ff02\"", - format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); + verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)); EXPECT_EQ("\"\\x000000000001\"\n" "\"next\"", format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); - EXPECT_EQ("\"\\x000000000001next\"", - format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); - EXPECT_EQ("\"\\x000000000001\"", - format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); + verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)); + verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7)); EXPECT_EQ("\"test\"\n" "\"\\000000\"\n" "\"000001\"", @@ -21054,11 +21036,9 @@ } TEST_F(FormatTest, HandlesUTF8BOM) { - EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); - EXPECT_EQ("\xef\xbb\xbf#include ", - format("\xef\xbb\xbf#include ")); - EXPECT_EQ("\xef\xbb\xbf\n#include ", - format("\xef\xbb\xbf\n#include ")); + verifyFormat("\xef\xbb\xbf"); + verifyFormat("\xef\xbb\xbf#include "); + verifyFormat("\xef\xbb\xbf\n#include "); } // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. @@ -21127,8 +21107,7 @@ } TEST_F(FormatTest, SplitsUTF8LineComments) { - EXPECT_EQ("// aaaaÄ\xc2\x8d", - format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); + verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)); EXPECT_EQ("// Я из лесу\n" "// вышел; был\n" "// сильный\n" @@ -21985,12 +21964,9 @@ " return b;\n" "};", "auto c = []() { return b; };", MergeInline); - verifyFormat("function([]() { return b; })", "function([]() { return b; })", - MergeInline); - verifyFormat("function([]() { return b; }, a)", - "function([]() { return b; }, a)", MergeInline); - verifyFormat("function(a, []() { return b; })", - "function(a, []() { return b; })", MergeInline); + verifyFormat("function([]() { return b; })", MergeInline); + verifyFormat("function([]() { return b; }, a)", MergeInline); + verifyFormat("function(a, []() { return b; })", MergeInline); // Check option "BraceWrapping.BeforeLambdaBody" and different state of // AllowShortLambdasOnASingleLine @@ -22925,7 +22901,7 @@ "#if C\n" "#else\n" "#endif\n"; - EXPECT_EQ(code, format(code)); + verifyFormat(code); } TEST_F(FormatTest, HandleConflictMarkers) { @@ -23143,12 +23119,12 @@ TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers // all modes, including C++11, C++14 and C++17 - EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); + verifyFormat("auto c = u8'a';"); } TEST_F(FormatTest, DoNotFormatLikelyXml) { - EXPECT_EQ("", format("", getGoogleStyle())); - EXPECT_EQ(" ", format(" ", getGoogleStyle())); + verifyFormat("", getGoogleStyle()); + verifyNoChange(" ", getGoogleStyle()); } TEST_F(FormatTest, StructuredBindings) { @@ -23771,13 +23747,11 @@ // Don't use the helpers here, since 'mess up' will change the whitespace // and these are all whitespace sensitive by definition - EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);", - format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style)); + verifyFormat("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style); EXPECT_EQ( "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); - EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);", - format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style)); + verifyFormat("FOO(String-ized&Messy+But,: :Still=Intentional);", Style); EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" " Still=Intentional);", format("FOO(String-ized&Messy+But,: :\n" @@ -23791,8 +23765,7 @@ Style)); Style.ColumnLimit = 21; - EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);", - format("FOO(String-ized&Messy+But: :Still=Intentional);", Style)); + verifyFormat("FOO(String-ized&Messy+But: :Still=Intentional);", Style); } TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { @@ -24914,7 +24887,7 @@ " Q_EMIT signal(MyChar);\n" "}"; - EXPECT_EQ(Source, format(Source, Style)); + verifyFormat(Source, Style); Style.AlignConsecutiveDeclarations.Enabled = true; EXPECT_EQ("void Foo::slot() {\n" @@ -24925,7 +24898,7 @@ format(Source, Style)); Style.StatementAttributeLikeMacros.push_back("emit"); - EXPECT_EQ(Source, format(Source, Style)); + verifyFormat(Source, Style); Style.StatementAttributeLikeMacros = {}; EXPECT_EQ("void Foo::slot() {\n" @@ -25020,7 +24993,7 @@ " // Small line comment\n" " return String.size() > SmallString.size();\n" "}"; - EXPECT_EQ(Code, format(Code, Style)); + verifyNoChange(Code, Style); } TEST_F(FormatTest, FormatDecayCopy) { @@ -25717,7 +25690,7 @@ Code, Style); Style.BreakAfterAttributes = FormatStyle::ABS_Leave; - EXPECT_EQ(Code, format(Code, Style)); + verifyNoChange(Code, Style); } TEST_F(FormatTest, InsertNewlineAtEOF) { @@ -25733,7 +25706,7 @@ Style.KeepEmptyLinesAtEOF = true; const StringRef Code{"int i;\n\n"}; - verifyFormat(Code, Code, Style); + verifyNoChange(Code, Style); verifyFormat(Code, "int i;\n\n\n", Style); } diff --git a/clang/unittests/Format/FormatTestBase.h b/clang/unittests/Format/FormatTestBase.h --- a/clang/unittests/Format/FormatTestBase.h +++ b/clang/unittests/Format/FormatTestBase.h @@ -120,6 +120,11 @@ Style); } + void _verifyNoChange(const char *File, int Line, llvm::StringRef Code, + const std::optional &Style = {}) { + _verifyFormat(File, Line, Code, Code, Style); + } + /// \brief Verify that clang-format does not crash on the given input. void verifyNoCrash(llvm::StringRef Code, const std::optional &Style = {}) { @@ -135,6 +140,7 @@ _verifyIndependentOfContext(__FILE__, __LINE__, __VA_ARGS__) #define verifyIncompleteFormat(...) \ _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__) +#define verifyNoChange(...) _verifyNoChange(__FILE__, __LINE__, __VA_ARGS__) #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) #define verifyGoogleFormat(Code) verifyFormat(Code, getGoogleStyle()) diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -554,9 +554,8 @@ " // one line", format("if (true) // A comment that doesn't fit on one line ", getLLVMStyleWithColumns(30))); - EXPECT_EQ("// Don't_touch_leading_whitespace", - format("// Don't_touch_leading_whitespace", - getLLVMStyleWithColumns(20))); + verifyNoChange("// Don't_touch_leading_whitespace", + getLLVMStyleWithColumns(20)); EXPECT_EQ("// Add leading\n" "// whitespace", format("//Add leading whitespace", getLLVMStyleWithColumns(20))); @@ -571,7 +570,7 @@ "// limit", format("//Even if it makes the line exceed the column limit", getLLVMStyleWithColumns(51))); - EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle())); + verifyFormat("//--But not here", getLLVMStyle()); EXPECT_EQ("/// line 1\n" "// add leading whitespace", format("/// line 1\n" @@ -614,9 +613,8 @@ " int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n" " int c, int d, int e) {}", getLLVMStyleWithColumns(40))); - EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - getLLVMStyleWithColumns(20))); + verifyFormat("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + getLLVMStyleWithColumns(20)); EXPECT_EQ( "#define XXX // a b c d\n" " // e f g h", @@ -1065,7 +1063,7 @@ " #define KV(value) #value, value\n" " // clang-format on\n" "}"); - EXPECT_EQ(Code, format(Code)); + verifyNoChange(Code); } TEST_F(FormatTestComments, SplitsLongLinesInComments) { @@ -1228,9 +1226,8 @@ // This reproduces a crashing bug where both adaptStartOfLine and // getCommentSplit were trying to wrap after the "/**". - EXPECT_EQ("/** multilineblockcommentwithnowrapopportunity */", - format("/** multilineblockcommentwithnowrapopportunity */", - getLLVMStyleWithColumns(20))); + verifyFormat("/** multilineblockcommentwithnowrapopportunity */", + getLLVMStyleWithColumns(20)); EXPECT_EQ("/*\n" "\n" @@ -1651,8 +1648,7 @@ getLLVMStyleWithColumns(20))); // Don't shrink leading whitespace. - EXPECT_EQ("int i; /// a", - format("int i; /// a", getLLVMStyleWithColumns(20))); + verifyNoChange("int i; /// a", getLLVMStyleWithColumns(20)); // Shrink trailing whitespace if there is no postfix and reflow. EXPECT_EQ("// long long long\n" @@ -2363,7 +2359,7 @@ format("#define A\n" "/* */someCall(parameter);", getLLVMStyleWithColumns(15))); - EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/")); + verifyNoChange("/*\n**\n*/"); EXPECT_EQ("/*\n" " *\n" " * aaaaaa\n" @@ -3523,7 +3519,7 @@ format(NoTextInComment, Style)); Style.SpacesInLineCommentPrefix.Minimum = 0; - EXPECT_EQ("//#comment", format("//#comment", Style)); + verifyFormat("//#comment", Style); EXPECT_EQ("//\n" "\n" "void foo() { //\n" diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -116,12 +116,10 @@ " */", getGoogleJSStyleWithColumns(20))); // Don't break the first line of a short single line jsdoc comment. - EXPECT_EQ("/** jsdoc line 1 */", - format("/** jsdoc line 1 */", getGoogleJSStyleWithColumns(20))); + verifyFormat("/** jsdoc line 1 */", getGoogleJSStyleWithColumns(20)); // Don't break the first line of a single line jsdoc comment if it just fits // the column limit. - EXPECT_EQ("/** jsdoc line 12 */", - format("/** jsdoc line 12 */", getGoogleJSStyleWithColumns(20))); + verifyFormat("/** jsdoc line 12 */", getGoogleJSStyleWithColumns(20)); // Don't break after '/**' and before '*/' if there is no space between // '/**' and the content. EXPECT_EQ( @@ -183,8 +181,7 @@ getGoogleJSStyleWithColumns(20))); // Don't break the first line of a single line short jsdoc comment pragma. - EXPECT_EQ("/** @returns j */", - format("/** @returns j */", getGoogleJSStyleWithColumns(20))); + verifyFormat("/** @returns j */", getGoogleJSStyleWithColumns(20)); // Break a single line long jsdoc comment pragma. EXPECT_EQ("/**\n" diff --git a/clang/unittests/Format/FormatTestJava.cpp b/clang/unittests/Format/FormatTestJava.cpp --- a/clang/unittests/Format/FormatTestJava.cpp +++ b/clang/unittests/Format/FormatTestJava.cpp @@ -567,8 +567,7 @@ TEST_F(FormatTestJava, BreaksStringLiterals) { // FIXME: String literal breaking is currently disabled for Java and JS, as it // requires strings to be merged using "+" which we don't support. - EXPECT_EQ("\"some text other\";", - format("\"some text other\";", getStyleWithColumns(14))); + verifyFormat("\"some text other\";", getStyleWithColumns(14)); } TEST_F(FormatTestJava, AlignsBlockComments) { diff --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp --- a/clang/unittests/Format/QualifierFixerTest.cpp +++ b/clang/unittests/Format/QualifierFixerTest.cpp @@ -116,7 +116,7 @@ Style.QualifierOrder = {"friend", "inline", "constexpr", "static", "const", "volatile", "type"}; - verifyFormat("const volatile int a;", "const volatile int a;", Style); + verifyFormat("const volatile int a;", Style); verifyFormat("const volatile int a;", "volatile const int a;", Style); verifyFormat("const volatile int a;", "int const volatile a;", Style); verifyFormat("const volatile int a;", "int volatile const a;", Style); @@ -240,7 +240,7 @@ verifyFormat("int const *a;", "const int *a;", Style); verifyFormat("int const &a;", "const int &a;", Style); verifyFormat("foo(int const &a)", "foo(const int &a)", Style); - verifyFormat("unsigned char *a;", "unsigned char *a;", Style); + verifyFormat("unsigned char *a;", Style); verifyFormat("unsigned char const *a;", "const unsigned char *a;", Style); verifyFormat("vector args1", "vector args1", Style); @@ -283,7 +283,7 @@ Style); verifyFormat("static int const bat;", "static const int bat;", Style); - verifyFormat("static int const bat;", "static int const bat;", Style); + verifyFormat("static int const bat;", Style); // static is not configured, unchanged on the left of the right hand // qualifiers. @@ -303,7 +303,7 @@ verifyFormat("Foo inline static const;", "const Foo inline static;", Style); verifyFormat("Foo inline static const;", "Foo const inline static;", Style); verifyFormat("Foo inline static const;", "Foo inline const static;", Style); - verifyFormat("Foo inline static const;", "Foo inline static const;", Style); + verifyFormat("Foo inline static const;", Style); verifyFormat("Foo::Bar const volatile A::*;", "volatile const Foo::Bar A::*;", @@ -311,11 +311,9 @@ verifyFormat("int const Foo::bat = 0;", "const int Foo::bat = 0;", Style); - verifyFormat("int const Foo::bat = 0;", "int const Foo::bat = 0;", - Style); + verifyFormat("int const Foo::bat = 0;", Style); verifyFormat("void fn(Foo const &i);", "void fn(const Foo &i);", Style); - verifyFormat("int const Foo::fn() {", "int const Foo::fn() {", - Style); + verifyFormat("int const Foo::fn() {", Style); verifyFormat("Foo> const *p;", "const Foo> *p;", Style); verifyFormat( "Foo> const *p = const_cast> const *>(&ffi);", @@ -384,36 +382,29 @@ Style); // Don't move past C-style struct/class. - verifyFormat("void foo(const struct A a);", "void foo(const struct A a);", - Style); - verifyFormat("void foo(const class A a);", "void foo(const class A a);", - Style); + verifyFormat("void foo(const struct A a);", Style); + verifyFormat("void foo(const class A a);", Style); // Don't move past struct/class combined declaration and variable // definition. - verifyFormat("const struct {\n} var;", "const struct {\n} var;", Style); - verifyFormat("struct {\n} const var;", "struct {\n} const var;", Style); - verifyFormat("const class {\n} var;", "const class {\n} var;", Style); - verifyFormat("class {\n} const var;", "class {\n} const var;", Style); + verifyFormat("const struct {\n} var;", Style); + verifyFormat("struct {\n} const var;", Style); + verifyFormat("const class {\n} var;", Style); + verifyFormat("class {\n} const var;", Style); // Leave left qualifers unchanged for combined declaration and variable // definition. - verifyFormat("volatile const class {\n} var;", - "volatile const class {\n} var;", Style); - verifyFormat("const volatile class {\n} var;", - "const volatile class {\n} var;", Style); + verifyFormat("volatile const class {\n} var;", Style); + verifyFormat("const volatile class {\n} var;", Style); // Also do no sorting with respect to not-configured tokens. - verifyFormat("const static volatile class {\n} var;", - "const static volatile class {\n} var;", Style); + verifyFormat("const static volatile class {\n} var;", Style); // Sort right qualifiers for combined declaration and variable definition. - verifyFormat("class {\n} const volatile var;", - "class {\n} const volatile var;", Style); + verifyFormat("class {\n} const volatile var;", Style); verifyFormat("class {\n} const volatile var;", "class {\n} volatile const var;", Style); // Static keyword is not configured, should end up on the left of the right // side. - verifyFormat("class {\n} static const volatile var;", - "class {\n} static const volatile var;", Style); + verifyFormat("class {\n} static const volatile var;", Style); verifyFormat("class {\n} static const volatile var;", "class {\n} volatile static const var;", Style); @@ -442,9 +433,9 @@ Style); // Don't move past decltype, typeof, or _Atomic. - verifyFormat("const decltype(foo)", "const decltype(foo)", Style); - verifyFormat("const typeof(foo)", "const typeof(foo)", Style); - verifyFormat("const _Atomic(foo)", "const _Atomic(foo)", Style); + verifyFormat("const decltype(foo)", Style); + verifyFormat("const typeof(foo)", Style); + verifyFormat("const _Atomic(foo)", Style); // Comments const int ColumnLimit = Style.ColumnLimit; @@ -510,22 +501,19 @@ "const unsigned /*c*/ long /*c*/ long a;", Style); // Not changed - verifyFormat("foo() /*c*/ const", "foo() /*c*/ const", Style); - verifyFormat("const /*c*/ struct a;", "const /*c*/ struct a;", Style); - verifyFormat("const /*c*/ class a;", "const /*c*/ class a;", Style); - verifyFormat("const /*c*/ decltype(v) a;", "const /*c*/ decltype(v) a;", - Style); - verifyFormat("const /*c*/ typeof(v) a;", "const /*c*/ typeof(v) a;", Style); - verifyFormat("const /*c*/ _Atomic(v) a;", "const /*c*/ _Atomic(v) a;", Style); - verifyFormat("const decltype /*c*/ (v) a;", "const decltype /*c*/ (v) a;", - Style); - verifyFormat("const /*c*/ class {\n} volatile /*c*/ foo = {};", - "const /*c*/ class {\n} volatile /*c*/ foo = {};", Style); + verifyFormat("foo() /*c*/ const", Style); + verifyFormat("const /*c*/ struct a;", Style); + verifyFormat("const /*c*/ class a;", Style); + verifyFormat("const /*c*/ decltype(v) a;", Style); + verifyFormat("const /*c*/ typeof(v) a;", Style); + verifyFormat("const /*c*/ _Atomic(v) a;", Style); + verifyFormat("const decltype /*c*/ (v) a;", Style); + verifyFormat("const /*c*/ class {\n} volatile /*c*/ foo = {};", Style); Style.ColumnLimit = ColumnLimit; // Don't adjust macros - verifyFormat("const INTPTR a;", "const INTPTR a;", Style); + verifyFormat("const INTPTR a;", Style); // Pointers to members verifyFormat("int S::*a;", Style); @@ -583,18 +571,17 @@ verifyFormat("const int *a;", "int const *a;", Style); verifyFormat("const int &a;", "int const &a;", Style); verifyFormat("foo(const int &a)", "foo(int const &a)", Style); - verifyFormat("unsigned char *a;", "unsigned char *a;", Style); + verifyFormat("unsigned char *a;", Style); verifyFormat("const unsigned int &get_nu() const", "unsigned int const &get_nu() const", Style); verifyFormat("const volatile int;", "volatile const int;", Style); - verifyFormat("const volatile int;", "const volatile int;", Style); + verifyFormat("const volatile int;", Style); verifyFormat("const volatile int;", "const int volatile;", Style); verifyFormat("const volatile int *restrict;", "volatile const int *restrict;", Style); - verifyFormat("const volatile int *restrict;", "const volatile int *restrict;", - Style); + verifyFormat("const volatile int *restrict;", Style); verifyFormat("const volatile int *restrict;", "const int volatile *restrict;", Style); @@ -626,18 +613,16 @@ "template explicit Action(Action const &action);", Style); - verifyFormat("static const int bat;", "static const int bat;", Style); + verifyFormat("static const int bat;", Style); verifyFormat("static const int bat;", "static int const bat;", Style); - verifyFormat("static const int Foo::bat = 0;", - "static const int Foo::bat = 0;", Style); + verifyFormat("static const int Foo::bat = 0;", Style); verifyFormat("static const int Foo::bat = 0;", "static int const Foo::bat = 0;", Style); verifyFormat("void fn(const Foo &i);"); - verifyFormat("const int Foo::bat = 0;", "const int Foo::bat = 0;", - Style); + verifyFormat("const int Foo::bat = 0;", Style); verifyFormat("const int Foo::bat = 0;", "int const Foo::bat = 0;", Style); verifyFormat("void fn(const Foo &i);", "void fn( Foo const &i);", @@ -685,10 +670,10 @@ verifyFormat("const long long unsigned a;", "long const long unsigned a;", Style); - verifyFormat("const std::Foo", "const std::Foo", Style); - verifyFormat("const std::Foo<>", "const std::Foo<>", Style); + verifyFormat("const std::Foo", Style); + verifyFormat("const std::Foo<>", Style); verifyFormat("const std::Foo < int", "const std::Foo", "const std::Foo", Style); + verifyFormat("const std::Foo", Style); // Multiple template parameters. verifyFormat("Bar;", "Bar;", Style); @@ -701,30 +686,25 @@ Style); // Don't move past C-style struct/class. - verifyFormat("void foo(struct A const a);", "void foo(struct A const a);", - Style); - verifyFormat("void foo(class A const a);", "void foo(class A const a);", - Style); + verifyFormat("void foo(struct A const a);", Style); + verifyFormat("void foo(class A const a);", Style); // Don't move past struct/class combined declaration and variable // definition. - verifyFormat("const struct {\n} var;", "const struct {\n} var;", Style); - verifyFormat("struct {\n} const var;", "struct {\n} const var;", Style); - verifyFormat("const class {\n} var;", "const class {\n} var;", Style); - verifyFormat("class {\n} const var;", "class {\n} const var;", Style); + verifyFormat("const struct {\n} var;", Style); + verifyFormat("struct {\n} const var;", Style); + verifyFormat("const class {\n} var;", Style); + verifyFormat("class {\n} const var;", Style); // Sort left qualifiers for struct/class combined declaration and variable // definition. - verifyFormat("const volatile class {\n} var;", - "const volatile class {\n} var;", Style); + verifyFormat("const volatile class {\n} var;", Style); verifyFormat("const volatile class {\n} var;", "volatile const class {\n} var;", Style); // Leave right qualifers unchanged for struct/class combined declaration and // variable definition. - verifyFormat("class {\n} const volatile var;", - "class {\n} const volatile var;", Style); - verifyFormat("class {\n} volatile const var;", - "class {\n} volatile const var;", Style); + verifyFormat("class {\n} const volatile var;", Style); + verifyFormat("class {\n} volatile const var;", Style); verifyFormat("foo>>();", "foo> const>();", Style); verifyFormat("foo>>();", "foo > const>();", @@ -735,9 +715,9 @@ Style); // Don't move past decltype, typeof, or _Atomic. - verifyFormat("decltype(foo) const", "decltype(foo) const", Style); - verifyFormat("typeof(foo) const", "typeof(foo) const", Style); - verifyFormat("_Atomic(foo) const", "_Atomic(foo) const", Style); + verifyFormat("decltype(foo) const", Style); + verifyFormat("typeof(foo) const", Style); + verifyFormat("_Atomic(foo) const", Style); // ::template for dependent names verifyFormat("const volatile ::template Foo var;", @@ -820,22 +800,19 @@ "unsigned /*c*/ long /*c*/ long const a;", Style); // Not changed - verifyFormat("foo() /*c*/ const", "foo() /*c*/ const", Style); - verifyFormat("struct /*c*/ const a;", "struct /*c*/ const a;", Style); - verifyFormat("class /*c*/ const a;", "class /*c*/ const a;", Style); - verifyFormat("decltype(v) /*c*/ const a;", "decltype(v) /*c*/ const a;", - Style); - verifyFormat("typeof(v) /*c*/ const a;", "typeof(v) /*c*/ const a;", Style); - verifyFormat("_Atomic(v) /*c*/ const a;", "_Atomic(v) /*c*/ const a;", Style); - verifyFormat("decltype /*c*/ (v) const a;", "decltype /*c*/ (v) const a;", - Style); - verifyFormat("const /*c*/ class {\n} /*c*/ volatile /*c*/ foo = {};", - "const /*c*/ class {\n} /*c*/ volatile /*c*/ foo = {};", Style); + verifyFormat("foo() /*c*/ const", Style); + verifyFormat("struct /*c*/ const a;", Style); + verifyFormat("class /*c*/ const a;", Style); + verifyFormat("decltype(v) /*c*/ const a;", Style); + verifyFormat("typeof(v) /*c*/ const a;", Style); + verifyFormat("_Atomic(v) /*c*/ const a;", Style); + verifyFormat("decltype /*c*/ (v) const a;", Style); + verifyFormat("const /*c*/ class {\n} /*c*/ volatile /*c*/ foo = {};", Style); Style.ColumnLimit = ColumnLimit; // Don't adjust macros - verifyFormat("INTPTR const a;", "INTPTR const a;", Style); + verifyFormat("INTPTR const a;", Style); // Pointers to members verifyFormat("int S::*a;", Style); @@ -856,13 +833,13 @@ // The Default EXPECT_EQ(Style.QualifierOrder.size(), (size_t)5); - verifyFormat("const volatile int a;", "const volatile int a;", Style); + verifyFormat("const volatile int a;", Style); verifyFormat("const volatile int a;", "volatile const int a;", Style); verifyFormat("const volatile int a;", "int const volatile a;", Style); verifyFormat("const volatile int a;", "int volatile const a;", Style); verifyFormat("const volatile int a;", "const int volatile a;", Style); - verifyFormat("const volatile Foo a;", "const volatile Foo a;", Style); + verifyFormat("const volatile Foo a;", Style); verifyFormat("const volatile Foo a;", "volatile const Foo a;", Style); verifyFormat("const volatile Foo a;", "Foo const volatile a;", Style); verifyFormat("const volatile Foo a;", "Foo volatile const a;", Style); @@ -873,13 +850,13 @@ verifyFormat("int const volatile a;", "const volatile int a;", Style); verifyFormat("int const volatile a;", "volatile const int a;", Style); - verifyFormat("int const volatile a;", "int const volatile a;", Style); + verifyFormat("int const volatile a;", Style); verifyFormat("int const volatile a;", "int volatile const a;", Style); verifyFormat("int const volatile a;", "const int volatile a;", Style); verifyFormat("Foo const volatile a;", "const volatile Foo a;", Style); verifyFormat("Foo const volatile a;", "volatile const Foo a;", Style); - verifyFormat("Foo const volatile a;", "Foo const volatile a;", Style); + verifyFormat("Foo const volatile a;", Style); verifyFormat("Foo const volatile a;", "Foo volatile const a;", Style); verifyFormat("Foo const volatile a;", "const Foo volatile a;", Style); @@ -887,13 +864,13 @@ Style.QualifierOrder = {"volatile", "const", "type"}; verifyFormat("volatile const int a;", "const volatile int a;", Style); - verifyFormat("volatile const int a;", "volatile const int a;", Style); + verifyFormat("volatile const int a;", Style); verifyFormat("volatile const int a;", "int const volatile a;", Style); verifyFormat("volatile const int a;", "int volatile const a;", Style); verifyFormat("volatile const int a;", "const int volatile a;", Style); verifyFormat("volatile const Foo a;", "const volatile Foo a;", Style); - verifyFormat("volatile const Foo a;", "volatile const Foo a;", Style); + verifyFormat("volatile const Foo a;", Style); verifyFormat("volatile const Foo a;", "Foo const volatile a;", Style); verifyFormat("volatile const Foo a;", "Foo volatile const a;", Style); verifyFormat("volatile const Foo a;", "const Foo volatile a;", Style); @@ -904,13 +881,13 @@ verifyFormat("int volatile const a;", "const volatile int a;", Style); verifyFormat("int volatile const a;", "volatile const int a;", Style); verifyFormat("int volatile const a;", "int const volatile a;", Style); - verifyFormat("int volatile const a;", "int volatile const a;", Style); + verifyFormat("int volatile const a;", Style); verifyFormat("int volatile const a;", "const int volatile a;", Style); verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style); verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style); verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style); - verifyFormat("Foo volatile const a;", "Foo volatile const a;", Style); + verifyFormat("Foo volatile const a;", Style); verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style); Style.QualifierAlignment = FormatStyle::QAS_Custom; @@ -919,13 +896,13 @@ verifyFormat("int volatile const a;", "const volatile int a;", Style); verifyFormat("int volatile const a;", "volatile const int a;", Style); verifyFormat("int volatile const a;", "int const volatile a;", Style); - verifyFormat("int volatile const a;", "int volatile const a;", Style); + verifyFormat("int volatile const a;", Style); verifyFormat("int volatile const a;", "const int volatile a;", Style); verifyFormat("Foo volatile const a;", "const volatile Foo a;", Style); verifyFormat("Foo volatile const a;", "volatile const Foo a;", Style); verifyFormat("Foo volatile const a;", "Foo const volatile a;", Style); - verifyFormat("Foo volatile const a;", "Foo volatile const a;", Style); + verifyFormat("Foo volatile const a;", Style); verifyFormat("Foo volatile const a;", "const Foo volatile a;", Style); } @@ -1155,8 +1132,7 @@ Style.QualifierOrder = {"static", "const", "type"}; verifyFormat("inline static const int a;", Style); - verifyFormat("static inline const int a;", "static inline const int a;", - Style); + verifyFormat("static inline const int a;", Style); verifyFormat("static const int a;", "const static int a;", Style); @@ -1176,11 +1152,11 @@ Style); verifyFormat("inline static const Foo;", "inline static Foo const;", Style); - verifyFormat("inline static const Foo;", "inline static const Foo;", Style); + verifyFormat("inline static const Foo;", Style); // Don't move qualifiers to the right for aestethics only. - verifyFormat("inline const static Foo;", "inline const static Foo;", Style); - verifyFormat("const inline static Foo;", "const inline static Foo;", Style); + verifyFormat("inline const static Foo;", Style); + verifyFormat("const inline static Foo;", Style); } TEST_F(QualifierFixerTest, UnsignedQualifier) {