forked from OSchip/llvm-project
[clang-format] Fix SeparateDefinitionBlocks issues
Fixes https://github.com/llvm/llvm-project/issues/52976. - Make no formatting for macros - Attach comment with definition headers - Make no change on use of empty lines at block start/end - Fix misrecognition of keyword namespace Differential Revision: https://reviews.llvm.org/D116663 Reviewed By: MyDeveloperDay, HazardyKnusperkeks, curdeius
This commit is contained in:
parent
8f553da492
commit
ee25a327aa
|
@ -31,15 +31,16 @@ std::pair<tooling::Replacements, unsigned> DefinitionBlockSeparator::analyze(
|
||||||
|
|
||||||
void DefinitionBlockSeparator::separateBlocks(
|
void DefinitionBlockSeparator::separateBlocks(
|
||||||
SmallVectorImpl<AnnotatedLine *> &Lines, tooling::Replacements &Result) {
|
SmallVectorImpl<AnnotatedLine *> &Lines, tooling::Replacements &Result) {
|
||||||
|
const bool IsNeverStyle =
|
||||||
|
Style.SeparateDefinitionBlocks == FormatStyle::SDS_Never;
|
||||||
auto LikelyDefinition = [this](const AnnotatedLine *Line) {
|
auto LikelyDefinition = [this](const AnnotatedLine *Line) {
|
||||||
if (Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition())
|
if ((Line->MightBeFunctionDecl && Line->mightBeFunctionDefinition()) ||
|
||||||
|
Line->startsWithNamespace())
|
||||||
return true;
|
return true;
|
||||||
FormatToken *CurrentToken = Line->First;
|
FormatToken *CurrentToken = Line->First;
|
||||||
while (CurrentToken) {
|
while (CurrentToken) {
|
||||||
if (CurrentToken->isOneOf(tok::kw_class, tok::kw_struct,
|
if (CurrentToken->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_enum) ||
|
||||||
tok::kw_namespace, tok::kw_enum) ||
|
(Style.isJavaScript() && CurrentToken->TokenText == "function"))
|
||||||
(Style.Language == FormatStyle::LK_JavaScript &&
|
|
||||||
CurrentToken->TokenText == "function"))
|
|
||||||
return true;
|
return true;
|
||||||
CurrentToken = CurrentToken->Next;
|
CurrentToken = CurrentToken->Next;
|
||||||
}
|
}
|
||||||
|
@ -54,11 +55,14 @@ void DefinitionBlockSeparator::separateBlocks(
|
||||||
Env.getSourceManager().getBufferData(Env.getFileID()),
|
Env.getSourceManager().getBufferData(Env.getFileID()),
|
||||||
Style.UseCRLF)
|
Style.UseCRLF)
|
||||||
: Style.UseCRLF);
|
: Style.UseCRLF);
|
||||||
for (unsigned I = 0; I < Lines.size(); I++) {
|
for (unsigned I = 0; I < Lines.size(); ++I) {
|
||||||
const auto &CurrentLine = Lines[I];
|
const auto &CurrentLine = Lines[I];
|
||||||
|
if (CurrentLine->InPPDirective)
|
||||||
|
continue;
|
||||||
FormatToken *TargetToken = nullptr;
|
FormatToken *TargetToken = nullptr;
|
||||||
AnnotatedLine *TargetLine;
|
AnnotatedLine *TargetLine;
|
||||||
auto OpeningLineIndex = CurrentLine->MatchingOpeningBlockLineIndex;
|
auto OpeningLineIndex = CurrentLine->MatchingOpeningBlockLineIndex;
|
||||||
|
AnnotatedLine *OpeningLine = nullptr;
|
||||||
const auto InsertReplacement = [&](const int NewlineToInsert) {
|
const auto InsertReplacement = [&](const int NewlineToInsert) {
|
||||||
assert(TargetLine);
|
assert(TargetLine);
|
||||||
assert(TargetToken);
|
assert(TargetToken);
|
||||||
|
@ -72,9 +76,18 @@ void DefinitionBlockSeparator::separateBlocks(
|
||||||
TargetToken->SpacesRequiredBefore - 1,
|
TargetToken->SpacesRequiredBefore - 1,
|
||||||
TargetToken->StartsColumn);
|
TargetToken->StartsColumn);
|
||||||
};
|
};
|
||||||
|
const auto IsPPConditional = [&](const size_t LineIndex) {
|
||||||
|
const auto &Line = Lines[LineIndex];
|
||||||
|
return Line->First->is(tok::hash) && Line->First->Next &&
|
||||||
|
Line->First->Next->isOneOf(tok::pp_if, tok::pp_ifdef, tok::pp_else,
|
||||||
|
tok::pp_ifndef, tok::pp_elifndef,
|
||||||
|
tok::pp_elifdef, tok::pp_elif,
|
||||||
|
tok::pp_endif);
|
||||||
|
};
|
||||||
const auto FollowingOtherOpening = [&]() {
|
const auto FollowingOtherOpening = [&]() {
|
||||||
return OpeningLineIndex == 0 ||
|
return OpeningLineIndex == 0 ||
|
||||||
Lines[OpeningLineIndex - 1]->Last->opensScope();
|
Lines[OpeningLineIndex - 1]->Last->opensScope() ||
|
||||||
|
IsPPConditional(OpeningLineIndex - 1);
|
||||||
};
|
};
|
||||||
const auto HasEnumOnLine = [CurrentLine]() {
|
const auto HasEnumOnLine = [CurrentLine]() {
|
||||||
FormatToken *CurrentToken = CurrentLine->First;
|
FormatToken *CurrentToken = CurrentLine->First;
|
||||||
|
@ -87,17 +100,29 @@ void DefinitionBlockSeparator::separateBlocks(
|
||||||
};
|
};
|
||||||
|
|
||||||
bool IsDefBlock = false;
|
bool IsDefBlock = false;
|
||||||
|
const auto MayPrecedeDefinition = [&](const int Direction = -1) {
|
||||||
|
const size_t OperateIndex = OpeningLineIndex + Direction;
|
||||||
|
assert(OperateIndex < Lines.size());
|
||||||
|
const auto &OperateLine = Lines[OperateIndex];
|
||||||
|
return (Style.isCSharp() && OperateLine->First->is(TT_AttributeSquare)) ||
|
||||||
|
OperateLine->First->is(tok::comment);
|
||||||
|
};
|
||||||
|
|
||||||
if (HasEnumOnLine()) {
|
if (HasEnumOnLine()) {
|
||||||
// We have no scope opening/closing information for enum.
|
// We have no scope opening/closing information for enum.
|
||||||
IsDefBlock = true;
|
IsDefBlock = true;
|
||||||
OpeningLineIndex = I;
|
OpeningLineIndex = I;
|
||||||
TargetLine = CurrentLine;
|
while (OpeningLineIndex > 0 && MayPrecedeDefinition())
|
||||||
TargetToken = CurrentLine->First;
|
--OpeningLineIndex;
|
||||||
|
OpeningLine = Lines[OpeningLineIndex];
|
||||||
|
TargetLine = OpeningLine;
|
||||||
|
TargetToken = TargetLine->First;
|
||||||
if (!FollowingOtherOpening())
|
if (!FollowingOtherOpening())
|
||||||
InsertReplacement(NewlineCount);
|
InsertReplacement(NewlineCount);
|
||||||
else
|
else if (IsNeverStyle)
|
||||||
InsertReplacement(OpeningLineIndex != 0);
|
InsertReplacement(OpeningLineIndex != 0);
|
||||||
|
TargetLine = CurrentLine;
|
||||||
|
TargetToken = TargetLine->First;
|
||||||
while (TargetToken && !TargetToken->is(tok::r_brace))
|
while (TargetToken && !TargetToken->is(tok::r_brace))
|
||||||
TargetToken = TargetToken->Next;
|
TargetToken = TargetToken->Next;
|
||||||
if (!TargetToken) {
|
if (!TargetToken) {
|
||||||
|
@ -108,40 +133,48 @@ void DefinitionBlockSeparator::separateBlocks(
|
||||||
if (OpeningLineIndex > Lines.size())
|
if (OpeningLineIndex > Lines.size())
|
||||||
continue;
|
continue;
|
||||||
// Handling the case that opening bracket has its own line.
|
// Handling the case that opening bracket has its own line.
|
||||||
OpeningLineIndex -= Lines[OpeningLineIndex]->First->TokenText == "{";
|
OpeningLineIndex -= Lines[OpeningLineIndex]->First->is(tok::l_brace);
|
||||||
AnnotatedLine *OpeningLine = Lines[OpeningLineIndex];
|
OpeningLine = Lines[OpeningLineIndex];
|
||||||
// Closing a function definition.
|
// Closing a function definition.
|
||||||
if (LikelyDefinition(OpeningLine)) {
|
if (LikelyDefinition(OpeningLine)) {
|
||||||
IsDefBlock = true;
|
IsDefBlock = true;
|
||||||
if (OpeningLineIndex > 0) {
|
while (OpeningLineIndex > 0 && MayPrecedeDefinition())
|
||||||
OpeningLineIndex -=
|
--OpeningLineIndex;
|
||||||
Style.Language == FormatStyle::LK_CSharp &&
|
OpeningLine = Lines[OpeningLineIndex];
|
||||||
Lines[OpeningLineIndex - 1]->First->is(tok::l_square);
|
|
||||||
OpeningLine = Lines[OpeningLineIndex];
|
|
||||||
}
|
|
||||||
TargetLine = OpeningLine;
|
TargetLine = OpeningLine;
|
||||||
TargetToken = TargetLine->First;
|
TargetToken = TargetLine->First;
|
||||||
if (!FollowingOtherOpening()) {
|
if (!FollowingOtherOpening()) {
|
||||||
// Avoid duplicated replacement.
|
// Avoid duplicated replacement.
|
||||||
if (!TargetToken->opensScope())
|
if (TargetToken->isNot(tok::l_brace))
|
||||||
InsertReplacement(NewlineCount);
|
InsertReplacement(NewlineCount);
|
||||||
} else
|
} else if (IsNeverStyle)
|
||||||
InsertReplacement(OpeningLineIndex != 0);
|
InsertReplacement(OpeningLineIndex != 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not the last token.
|
// Not the last token.
|
||||||
if (IsDefBlock && I + 1 < Lines.size()) {
|
if (IsDefBlock && I + 1 < Lines.size()) {
|
||||||
TargetLine = Lines[I + 1];
|
OpeningLineIndex = I + 1;
|
||||||
|
TargetLine = Lines[OpeningLineIndex];
|
||||||
TargetToken = TargetLine->First;
|
TargetToken = TargetLine->First;
|
||||||
|
|
||||||
// No empty line for continuously closing scopes. The token will be
|
// No empty line for continuously closing scopes. The token will be
|
||||||
// handled in another case if the line following is opening a
|
// handled in another case if the line following is opening a
|
||||||
// definition.
|
// definition.
|
||||||
if (!TargetToken->closesScope()) {
|
if (!TargetToken->closesScope() && !IsPPConditional(OpeningLineIndex)) {
|
||||||
if (!LikelyDefinition(TargetLine))
|
// Check whether current line may be precedings of a definition line.
|
||||||
|
while (OpeningLineIndex + 1 < Lines.size() &&
|
||||||
|
MayPrecedeDefinition(/*Direction=*/0))
|
||||||
|
++OpeningLineIndex;
|
||||||
|
TargetLine = Lines[OpeningLineIndex];
|
||||||
|
if (!LikelyDefinition(TargetLine)) {
|
||||||
|
TargetLine = Lines[I + 1];
|
||||||
|
TargetToken = TargetLine->First;
|
||||||
InsertReplacement(NewlineCount);
|
InsertReplacement(NewlineCount);
|
||||||
} else {
|
}
|
||||||
|
} else if (IsNeverStyle) {
|
||||||
|
TargetLine = Lines[I + 1];
|
||||||
|
TargetToken = TargetLine->First;
|
||||||
InsertReplacement(OpeningLineIndex != 0);
|
InsertReplacement(OpeningLineIndex != 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,8 +57,9 @@ protected:
|
||||||
InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
|
InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
|
||||||
EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
|
EXPECT_EQ(ExpectedCode.str(), separateDefinitionBlocks(ExpectedCode, Style))
|
||||||
<< "Expected code is not stable";
|
<< "Expected code is not stable";
|
||||||
std::string InverseResult = separateDefinitionBlocks(Code, InverseStyle);
|
std::string InverseResult =
|
||||||
EXPECT_NE(Code.str(), InverseResult)
|
separateDefinitionBlocks(ExpectedCode, InverseStyle);
|
||||||
|
EXPECT_NE(ExpectedCode.str(), InverseResult)
|
||||||
<< "Inverse formatting makes no difference";
|
<< "Inverse formatting makes no difference";
|
||||||
std::string CodeToFormat =
|
std::string CodeToFormat =
|
||||||
HasOriginalCode ? Code.str() : removeEmptyLines(Code);
|
HasOriginalCode ? Code.str() : removeEmptyLines(Code);
|
||||||
|
@ -129,49 +130,166 @@ TEST_F(DefinitionBlockSeparatorTest, Basic) {
|
||||||
Style);
|
Style);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(DefinitionBlockSeparatorTest, UntouchBlockStartStyle) {
|
||||||
|
// Returns a std::pair of two strings, with the first one for passing into
|
||||||
|
// Always test and the second one be the expected result of the first string.
|
||||||
|
auto MakeUntouchTest = [&](std::string BlockHeader, std::string BlockChanger,
|
||||||
|
std::string BlockFooter, bool BlockEndNewLine) {
|
||||||
|
std::string CodePart1 = "enum Foo { FOO, BAR };\n"
|
||||||
|
"\n"
|
||||||
|
"/*\n"
|
||||||
|
"test1\n"
|
||||||
|
"test2\n"
|
||||||
|
"*/\n"
|
||||||
|
"int foo(int i, int j) {\n"
|
||||||
|
" int r = i + j;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n";
|
||||||
|
std::string CodePart2 = "/* Comment block in one line*/\n"
|
||||||
|
"enum Bar { FOOBAR, BARFOO };\n"
|
||||||
|
"\n"
|
||||||
|
"int bar3(int j, int k) {\n"
|
||||||
|
" // A comment\n"
|
||||||
|
" int r = j % k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n";
|
||||||
|
std::string CodePart3 = "int bar2(int j, int k) {\n"
|
||||||
|
" int r = j / k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n";
|
||||||
|
std::string ConcatAll = BlockHeader + CodePart1 + BlockChanger + CodePart2 +
|
||||||
|
BlockFooter + (BlockEndNewLine ? "\n" : "") +
|
||||||
|
CodePart3;
|
||||||
|
return std::make_pair(BlockHeader + removeEmptyLines(CodePart1) +
|
||||||
|
BlockChanger + removeEmptyLines(CodePart2) +
|
||||||
|
BlockFooter + removeEmptyLines(CodePart3),
|
||||||
|
ConcatAll);
|
||||||
|
};
|
||||||
|
|
||||||
|
FormatStyle AlwaysStyle = getLLVMStyle();
|
||||||
|
AlwaysStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
|
||||||
|
|
||||||
|
FormatStyle NeverStyle = getLLVMStyle();
|
||||||
|
NeverStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
|
||||||
|
|
||||||
|
auto TestKit = MakeUntouchTest("#ifdef FOO\n\n", "\n#elifndef BAR\n\n",
|
||||||
|
"\n#endif\n\n", false);
|
||||||
|
verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
|
||||||
|
verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
|
||||||
|
|
||||||
|
TestKit =
|
||||||
|
MakeUntouchTest("#ifdef FOO\n", "#elifndef BAR\n", "#endif\n", false);
|
||||||
|
verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
|
||||||
|
verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
|
||||||
|
|
||||||
|
TestKit = MakeUntouchTest("namespace Ns {\n\n",
|
||||||
|
"\n} // namespace Ns\n\n"
|
||||||
|
"namespace {\n\n",
|
||||||
|
"\n} // namespace\n", true);
|
||||||
|
verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
|
||||||
|
verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
|
||||||
|
|
||||||
|
TestKit = MakeUntouchTest("namespace Ns {\n",
|
||||||
|
"} // namespace Ns\n\n"
|
||||||
|
"namespace {\n",
|
||||||
|
"} // namespace\n", true);
|
||||||
|
verifyFormat(TestKit.first, AlwaysStyle, TestKit.second);
|
||||||
|
verifyFormat(TestKit.second, NeverStyle, removeEmptyLines(TestKit.second));
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(DefinitionBlockSeparatorTest, Always) {
|
TEST_F(DefinitionBlockSeparatorTest, Always) {
|
||||||
FormatStyle Style = getLLVMStyle();
|
FormatStyle Style = getLLVMStyle();
|
||||||
Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
|
Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
|
||||||
std::string Prefix = "namespace {\n";
|
std::string Prefix = "namespace {\n";
|
||||||
std::string Postfix = "enum Foo { FOO, BAR };\n"
|
std::string Infix = "\n"
|
||||||
"\n"
|
"// Enum test1\n"
|
||||||
"int foo(int i, int j) {\n"
|
"// Enum test2\n"
|
||||||
" int r = i + j;\n"
|
"enum Foo { FOO, BAR };\n"
|
||||||
" return r;\n"
|
"\n"
|
||||||
"}\n"
|
"/*\n"
|
||||||
|
"test1\n"
|
||||||
|
"test2\n"
|
||||||
|
"*/\n"
|
||||||
|
"int foo(int i, int j) {\n"
|
||||||
|
" int r = i + j;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"// Foobar\n"
|
||||||
|
"int i, j, k;\n"
|
||||||
|
"\n"
|
||||||
|
"// Comment for function\n"
|
||||||
|
"// Comment line 2\n"
|
||||||
|
"// Comment line 3\n"
|
||||||
|
"int bar(int j, int k) {\n"
|
||||||
|
" int r = j * k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"int bar2(int j, int k) {\n"
|
||||||
|
" int r = j / k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"/* Comment block in one line*/\n"
|
||||||
|
"enum Bar { FOOBAR, BARFOO };\n"
|
||||||
|
"\n"
|
||||||
|
"int bar3(int j, int k) {\n"
|
||||||
|
" // A comment\n"
|
||||||
|
" int r = j % k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n";
|
||||||
|
std::string Postfix = "\n"
|
||||||
|
"} // namespace\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"namespace T {\n"
|
||||||
"int i, j, k;\n"
|
"int i, j, k;\n"
|
||||||
"\n"
|
"} // namespace T";
|
||||||
"int bar(int j, int k) {\n"
|
verifyFormat(Prefix + removeEmptyLines(Infix) + removeEmptyLines(Postfix),
|
||||||
" int r = j * k;\n"
|
Style, Prefix + Infix + Postfix);
|
||||||
" return r;\n"
|
|
||||||
"}\n"
|
|
||||||
"\n"
|
|
||||||
"enum Bar { FOOBAR, BARFOO };\n"
|
|
||||||
"} // namespace";
|
|
||||||
verifyFormat(Prefix + "\n\n\n" + removeEmptyLines(Postfix), Style,
|
|
||||||
Prefix + Postfix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(DefinitionBlockSeparatorTest, Never) {
|
TEST_F(DefinitionBlockSeparatorTest, Never) {
|
||||||
FormatStyle Style = getLLVMStyle();
|
FormatStyle Style = getLLVMStyle();
|
||||||
Style.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
|
Style.SeparateDefinitionBlocks = FormatStyle::SDS_Never;
|
||||||
std::string Prefix = "namespace {\n";
|
std::string Prefix = "namespace {\n";
|
||||||
std::string Postfix = "enum Foo { FOO, BAR };\n"
|
std::string Postfix = "// Enum test1\n"
|
||||||
|
"// Enum test2\n"
|
||||||
|
"enum Foo { FOO, BAR };\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"/*\n"
|
||||||
|
"test1\n"
|
||||||
|
"test2\n"
|
||||||
|
"*/\n"
|
||||||
"int foo(int i, int j) {\n"
|
"int foo(int i, int j) {\n"
|
||||||
" int r = i + j;\n"
|
" int r = i + j;\n"
|
||||||
" return r;\n"
|
" return r;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"// Foobar\n"
|
||||||
"int i, j, k;\n"
|
"int i, j, k;\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"// Comment for function\n"
|
||||||
|
"// Comment line 2\n"
|
||||||
|
"// Comment line 3\n"
|
||||||
"int bar(int j, int k) {\n"
|
"int bar(int j, int k) {\n"
|
||||||
" int r = j * k;\n"
|
" int r = j * k;\n"
|
||||||
" return r;\n"
|
" return r;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"int bar2(int j, int k) {\n"
|
||||||
|
" int r = j / k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"/* Comment block in one line*/\n"
|
||||||
"enum Bar { FOOBAR, BARFOO };\n"
|
"enum Bar { FOOBAR, BARFOO };\n"
|
||||||
|
"\n"
|
||||||
|
"int bar3(int j, int k) {\n"
|
||||||
|
" // A comment\n"
|
||||||
|
" int r = j % k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n"
|
||||||
"} // namespace";
|
"} // namespace";
|
||||||
verifyFormat(Prefix + "\n\n\n" + Postfix, Style,
|
verifyFormat(Prefix + "\n\n\n" + Postfix, Style,
|
||||||
Prefix + removeEmptyLines(Postfix));
|
Prefix + removeEmptyLines(Postfix));
|
||||||
|
@ -181,31 +299,57 @@ TEST_F(DefinitionBlockSeparatorTest, OpeningBracketOwnsLine) {
|
||||||
FormatStyle Style = getLLVMStyle();
|
FormatStyle Style = getLLVMStyle();
|
||||||
Style.BreakBeforeBraces = FormatStyle::BS_Allman;
|
Style.BreakBeforeBraces = FormatStyle::BS_Allman;
|
||||||
Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
|
Style.SeparateDefinitionBlocks = FormatStyle::SDS_Always;
|
||||||
verifyFormat("enum Foo\n"
|
verifyFormat("namespace NS\n"
|
||||||
|
"{\n"
|
||||||
|
"// Enum test1\n"
|
||||||
|
"// Enum test2\n"
|
||||||
|
"enum Foo\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" FOO,\n"
|
" FOO,\n"
|
||||||
" BAR\n"
|
" BAR\n"
|
||||||
"};\n"
|
"};\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"/*\n"
|
||||||
|
"test1\n"
|
||||||
|
"test2\n"
|
||||||
|
"*/\n"
|
||||||
"int foo(int i, int j)\n"
|
"int foo(int i, int j)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int r = i + j;\n"
|
" int r = i + j;\n"
|
||||||
" return r;\n"
|
" return r;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"// Foobar\n"
|
||||||
"int i, j, k;\n"
|
"int i, j, k;\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"// Comment for function\n"
|
||||||
|
"// Comment line 2\n"
|
||||||
|
"// Comment line 3\n"
|
||||||
"int bar(int j, int k)\n"
|
"int bar(int j, int k)\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int r = j * k;\n"
|
" int r = j * k;\n"
|
||||||
" return r;\n"
|
" return r;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"int bar2(int j, int k)\n"
|
||||||
|
"{\n"
|
||||||
|
" int r = j / k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
"enum Bar\n"
|
"enum Bar\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" FOOBAR,\n"
|
" FOOBAR,\n"
|
||||||
" BARFOO\n"
|
" BARFOO\n"
|
||||||
"};",
|
"};\n"
|
||||||
|
"\n"
|
||||||
|
"int bar3(int j, int k)\n"
|
||||||
|
"{\n"
|
||||||
|
" // A comment\n"
|
||||||
|
" int r = j % k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n"
|
||||||
|
"} // namespace NS",
|
||||||
Style);
|
Style);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,21 +359,42 @@ TEST_F(DefinitionBlockSeparatorTest, Leave) {
|
||||||
Style.MaxEmptyLinesToKeep = 3;
|
Style.MaxEmptyLinesToKeep = 3;
|
||||||
std::string LeaveAs = "namespace {\n"
|
std::string LeaveAs = "namespace {\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"// Enum test1\n"
|
||||||
|
"// Enum test2\n"
|
||||||
"enum Foo { FOO, BAR };\n"
|
"enum Foo { FOO, BAR };\n"
|
||||||
"\n\n\n"
|
"\n\n\n"
|
||||||
|
"/*\n"
|
||||||
|
"test1\n"
|
||||||
|
"test2\n"
|
||||||
|
"*/\n"
|
||||||
"int foo(int i, int j) {\n"
|
"int foo(int i, int j) {\n"
|
||||||
" int r = i + j;\n"
|
" int r = i + j;\n"
|
||||||
" return r;\n"
|
" return r;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"// Foobar\n"
|
||||||
"int i, j, k;\n"
|
"int i, j, k;\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"// Comment for function\n"
|
||||||
|
"// Comment line 2\n"
|
||||||
|
"// Comment line 3\n"
|
||||||
"int bar(int j, int k) {\n"
|
"int bar(int j, int k) {\n"
|
||||||
" int r = j * k;\n"
|
" int r = j * k;\n"
|
||||||
" return r;\n"
|
" return r;\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"int bar2(int j, int k) {\n"
|
||||||
|
" int r = j / k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n"
|
||||||
|
"\n"
|
||||||
|
"// Comment for inline enum\n"
|
||||||
"enum Bar { FOOBAR, BARFOO };\n"
|
"enum Bar { FOOBAR, BARFOO };\n"
|
||||||
|
"int bar3(int j, int k) {\n"
|
||||||
|
" // A comment\n"
|
||||||
|
" int r = j % k;\n"
|
||||||
|
" return r;\n"
|
||||||
|
"}\n"
|
||||||
"} // namespace";
|
"} // namespace";
|
||||||
verifyFormat(LeaveAs, Style, LeaveAs);
|
verifyFormat(LeaveAs, Style, LeaveAs);
|
||||||
}
|
}
|
||||||
|
@ -251,6 +416,7 @@ TEST_F(DefinitionBlockSeparatorTest, CSharp) {
|
||||||
"internal static String toString() {\r\n"
|
"internal static String toString() {\r\n"
|
||||||
"}\r\n"
|
"}\r\n"
|
||||||
"\r\n"
|
"\r\n"
|
||||||
|
"// Comment for enum\r\n"
|
||||||
"public enum var {\r\n"
|
"public enum var {\r\n"
|
||||||
" none,\r\n"
|
" none,\r\n"
|
||||||
" @string,\r\n"
|
" @string,\r\n"
|
||||||
|
@ -258,6 +424,7 @@ TEST_F(DefinitionBlockSeparatorTest, CSharp) {
|
||||||
" @enum\r\n"
|
" @enum\r\n"
|
||||||
"}\r\n"
|
"}\r\n"
|
||||||
"\r\n"
|
"\r\n"
|
||||||
|
"// Test\r\n"
|
||||||
"[STAThread]\r\n"
|
"[STAThread]\r\n"
|
||||||
"static void Main(string[] args) {\r\n"
|
"static void Main(string[] args) {\r\n"
|
||||||
" Console.WriteLine(\"HelloWorld\");\r\n"
|
" Console.WriteLine(\"HelloWorld\");\r\n"
|
||||||
|
|
Loading…
Reference in New Issue