Imported Upstream version 6.10.0.49

Former-commit-id: 1d6753294b2993e1fbf92de9366bb9544db4189b
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2020-01-16 16:38:04 +00:00
parent d94e79959b
commit 468663ddbb
48518 changed files with 2789335 additions and 61176 deletions

View File

@@ -0,0 +1,29 @@
set(LLVM_LINK_COMPONENTS
Support
)
add_clang_unittest(FormatTests
CleanupTest.cpp
FormatTest.cpp
FormatTestComments.cpp
FormatTestJS.cpp
FormatTestJava.cpp
FormatTestObjC.cpp
FormatTestProto.cpp
FormatTestRawStrings.cpp
FormatTestSelective.cpp
FormatTestTextProto.cpp
NamespaceEndCommentsFixerTest.cpp
SortImportsTestJS.cpp
SortIncludesTest.cpp
UsingDeclarationsSorterTest.cpp
)
target_link_libraries(FormatTests
PRIVATE
clangBasic
clangFormat
clangFrontend
clangRewrite
clangToolingCore
)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1 @@
593caf0fdc9a43a1f347aad24c8ab635f49bdc76

View File

@@ -0,0 +1 @@
ed11fbdb1fc0da92c686f7f2bbab7de0be78be48

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,414 @@
//===- unittest/Format/FormatTestProto.cpp --------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "FormatTestUtils.h"
#include "clang/Format/Format.h"
#include "llvm/Support/Debug.h"
#include "gtest/gtest.h"
#define DEBUG_TYPE "format-test"
namespace clang {
namespace format {
class FormatTestProto : public ::testing::Test {
protected:
static std::string format(llvm::StringRef Code, unsigned Offset,
unsigned Length, const FormatStyle &Style) {
DEBUG(llvm::errs() << "---\n");
DEBUG(llvm::errs() << Code << "\n\n");
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
tooling::Replacements Replaces = reformat(Style, Code, Ranges);
auto Result = applyAllReplacements(Code, Replaces);
EXPECT_TRUE(static_cast<bool>(Result));
DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
return *Result;
}
static std::string format(llvm::StringRef Code) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto);
Style.ColumnLimit = 60; // To make writing tests easier.
return format(Code, 0, Code.size(), Style);
}
static void verifyFormat(llvm::StringRef Code) {
EXPECT_EQ(Code.str(), format(test::messUp(Code)));
}
};
TEST_F(FormatTestProto, FormatsMessages) {
verifyFormat("message SomeMessage {\n"
" required int32 field1 = 1;\n"
"}");
verifyFormat("message SomeMessage {\n"
" required .absolute.Reference field1 = 1;\n"
"}");
verifyFormat("message SomeMessage {\n"
" required int32 field1 = 1;\n"
" optional string field2 = 2 [default = \"2\"]\n"
"}");
verifyFormat("message SomeMessage {\n"
" optional really.really.long.qualified.type.aaa.aaaaaaa\n"
" fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n"
" optional\n"
" really.really.long.qualified.type.aaa.aaaaaaa.aaaaaaaa\n"
" another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n"
"}");
verifyFormat("message SomeMessage {\n"
" map<string, Project> projects = 1;\n"
" optional map<string, int32> size_projects = 2;\n"
" map<int, really.really.really.long.qualified.type.nameeee>\n"
" projects = 3;\n"
" map<int, really.really.really.really.long.qualified.type\n"
" .nameeee> projects = 4;\n"
" map<int,\n"
" reallyreallyreallyreallyreallyreallyreallylongname>\n"
" projects = 5;\n"
" map<int, Project>\n"
" longlonglonglonglonglonglonglonglonglongonglon = 6;\n"
" map<releleallyreallyreallyreallyreallyreallyreallylongname,\n"
" int> projects = 7;\n"
" map<releleallyreallyreallyreallyreallyreallyreallylongname,\n"
" releleallyreallyreallyreallyreallyreallyreallylongname>\n"
" releleallyreallyreallyreallyreallyreallyreallylongnam =\n"
" 8;\n"
" map<relele.llyreal.yreallyr.allyreally.eallyreal\n"
" .sauenirylongname,\n"
" really.really.really.really.long.qualified.type\n"
" .nameeee> projects = 9;\n"
"}");
}
TEST_F(FormatTestProto, KeywordsInOtherLanguages) {
verifyFormat("optional string operator = 1;");
}
TEST_F(FormatTestProto, FormatsEnums) {
verifyFormat("enum Type {\n"
" UNKNOWN = 0;\n"
" TYPE_A = 1;\n"
" TYPE_B = 2;\n"
"};");
verifyFormat("enum Type {\n"
" UNKNOWN = 0 [(some_options) = {a: aa, b: bb}];\n"
"};");
verifyFormat("enum Type {\n"
" UNKNOWN = 0 [(some_options) = {\n"
" a: aa, // wrap\n"
" b: bb\n"
" }];\n"
"};");
}
TEST_F(FormatTestProto, UnderstandsReturns) {
verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
}
TEST_F(FormatTestProto, MessageFieldAttributes) {
verifyFormat("optional string test = 1 [default = \"test\"];");
verifyFormat("optional bool a = 1 [default = true, deprecated = true];");
verifyFormat("optional LongMessageType long_proto_field = 1 [\n"
" default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
" deprecated = true\n"
"];");
verifyFormat("optional LongMessageType long_proto_field = 1\n"
" [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
verifyFormat("repeated double value = 1\n"
" [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];");
verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
" aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
" bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
"}];");
verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
" aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
" bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
"}];");
verifyFormat("repeated double value = 1 [\n"
" (aaaaaaa.aaaaaaaaa) = {\n"
" aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
" bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
" },\n"
" (bbbbbbb.bbbbbbbbb) = {\n"
" aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
" bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
" }\n"
"];");
verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
" type: \"AAAAAAAAAA\"\n"
" is: \"AAAAAAAAAA\"\n"
" or: \"BBBBBBBBBB\"\n"
"}];");
verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
" aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
" bbbbbbb: BBBB,\n"
" bbbb: BBB\n"
"}];");
verifyFormat("optional AAA aaa = 1 [\n"
" foo = {\n"
" key: 'a' //\n"
" },\n"
" bar = {\n"
" key: 'a' //\n"
" }\n"
"];");
verifyFormat("optional string test = 1 [default =\n"
" \"test\"\n"
" \"test\"];");
verifyFormat("optional Aaaaaaaa aaaaaaaa = 12 [\n"
" (aaa) = aaaa,\n"
" (bbbbbbbbbbbbbbbbbbbbbbbbbb) = {\n"
" aaaaaaaaaaaaaaaaa: true,\n"
" aaaaaaaaaaaaaaaa: true\n"
" }\n"
"];");
}
TEST_F(FormatTestProto, DoesntWrapFileOptions) {
EXPECT_EQ(
"option java_package = "
"\"some.really.long.package.that.exceeds.the.column.limit\";",
format("option java_package = "
"\"some.really.long.package.that.exceeds.the.column.limit\";"));
}
TEST_F(FormatTestProto, FormatsOptions) {
verifyFormat("option (MyProto.options) = {\n"
" field_a: OK\n"
" field_b: \"OK\"\n"
" field_c: \"OK\"\n"
" msg_field: {field_d: 123}\n"
"};");
verifyFormat("option (MyProto.options) = {\n"
" field_a: OK\n"
" field_b: \"OK\"\n"
" field_c: \"OK\"\n"
" msg_field: {field_d: 123 field_e: OK}\n"
"};");
verifyFormat("option (MyProto.options) = {\n"
" field_a: OK // Comment\n"
" field_b: \"OK\"\n"
" field_c: \"OK\"\n"
" msg_field: {field_d: 123}\n"
"};");
verifyFormat("option (MyProto.options) = {\n"
" field_c: \"OK\"\n"
" msg_field {field_d: 123}\n"
"};");
verifyFormat("option (MyProto.options) = {\n"
" field_a: OK\n"
" field_b {field_c: OK}\n"
" field_d: OKOKOK\n"
" field_e: OK\n"
"}");
// Support syntax with <> instead of {}.
verifyFormat("option (MyProto.options) = {\n"
" field_c: \"OK\",\n"
" msg_field: <field_d: 123>\n"
"};");
verifyFormat("option (MyProto.options) = {\n"
" field_a: OK\n"
" field_b <field_c: OK>\n"
" field_d: OKOKOK\n"
" field_e: OK\n"
"}");
verifyFormat("option (MyProto.options) = {\n"
" msg_field: <>\n"
" field_c: \"OK\",\n"
" msg_field: <field_d: 123>\n"
" field_e: OK\n"
" msg_field: <field_d: 12>\n"
"};");
verifyFormat("option (MyProto.options) = <\n"
" field_a: OK\n"
" field_b: \"OK\"\n"
" field_c: 1\n"
" field_d: 12.5\n"
" field_e: OK\n"
">;");
verifyFormat("option (MyProto.options) = <\n"
" field_a: OK,\n"
" field_b: \"OK\",\n"
" field_c: 1,\n"
" field_d: 12.5,\n"
" field_e: OK,\n"
">;");
verifyFormat("option (MyProto.options) = <\n"
" field_a: \"OK\"\n"
" msg_field: {field_b: OK}\n"
" field_g: OK\n"
" field_g: OK\n"
" field_g: OK\n"
">;");
verifyFormat("option (MyProto.options) = <\n"
" field_a: \"OK\"\n"
" msg_field <\n"
" field_b: OK\n"
" field_c: OK\n"
" field_d: OK\n"
" field_e: OK\n"
" field_f: OK\n"
" >\n"
" field_g: OK\n"
">;");
verifyFormat("option (MyProto.options) = <\n"
" field_a: \"OK\"\n"
" msg_field <\n"
" field_b: OK,\n"
" field_c: OK,\n"
" field_d: OK,\n"
" field_e: OK,\n"
" field_f: OK\n"
" >\n"
" field_g: OK\n"
">;");
verifyFormat("option (MyProto.options) = <\n"
" field_a: \"OK\"\n"
" msg_field: <\n"
" field_b: OK\n"
" field_c: OK\n"
" field_d: OK\n"
" field_e: OK\n"
" field_f: OK\n"
" >\n"
" field_g: OK\n"
">;");
verifyFormat("option (MyProto.options) = <\n"
" field_a: \"OK\"\n"
" msg_field: {\n"
" field_b: OK\n"
" field_c: OK\n"
" field_d: OK\n"
" field_e: OK\n"
" field_f: OK\n"
" }\n"
" field_g: OK\n"
">;");
verifyFormat("option (MyProto.options) = <\n"
" field_a: \"OK\"\n"
" msg_field {\n"
" field_b: OK\n"
" field_c: OK\n"
" field_d: OK\n"
" field_e: OK\n"
" field_f: OK\n"
" }\n"
" field_g: OK\n"
">;");
verifyFormat("option (MyProto.options) = {\n"
" field_a: \"OK\"\n"
" msg_field <\n"
" field_b: OK\n"
" field_c: OK\n"
" field_d: OK\n"
" field_e: OK\n"
" field_f: OK\n"
" >\n"
" field_g: OK\n"
"};");
verifyFormat("option (MyProto.options) = {\n"
" field_a: \"OK\"\n"
" msg_field: <\n"
" field_b: OK\n"
" field_c: OK\n"
" field_d: OK\n"
" field_e: OK\n"
" field_f: OK\n"
" >\n"
" field_g: OK\n"
"};");
verifyFormat("option (MyProto.options) = <\n"
" field_a: \"OK\"\n"
" msg_field {\n"
" field_b: OK\n"
" field_c: OK\n"
" field_d: OK\n"
" msg_field <\n"
" field_A: 1\n"
" field_B: 2\n"
" field_C: 3\n"
" field_D: 4\n"
" field_E: 5\n"
" >\n"
" msg_field <field_A: 1 field_B: 2 field_C: 3 field_D: 4>\n"
" field_e: OK\n"
" field_f: OK\n"
" }\n"
" field_g: OK\n"
">;");
verifyFormat("option (MyProto.options) = <\n"
" data1 <key1: value1>\n"
" data2 {key2: value2}\n"
">;");
verifyFormat("option (MyProto.options) = <\n"
" app_id: 'com.javax.swing.salsa.latino'\n"
" head_id: 1\n"
" data <key: value>\n"
">;");
verifyFormat("option (MyProto.options) = {\n"
" app_id: 'com.javax.swing.salsa.latino'\n"
" head_id: 1\n"
" headheadheadheadheadhead_id: 1\n"
" product_data {product {1}}\n"
"};");
}
TEST_F(FormatTestProto, FormatsService) {
verifyFormat("service SearchService {\n"
" rpc Search(SearchRequest) returns (SearchResponse) {\n"
" option foo = true;\n"
" }\n"
"};");
}
TEST_F(FormatTestProto, ExtendingMessage) {
verifyFormat("extend .foo.Bar {\n"
"}");
}
TEST_F(FormatTestProto, FormatsImports) {
verifyFormat("import \"a.proto\";\n"
"import \"b.proto\";\n"
"// comment\n"
"message A {\n"
"}");
verifyFormat("import public \"a.proto\";\n"
"import \"b.proto\";\n"
"// comment\n"
"message A {\n"
"}");
// Missing semicolons should not confuse clang-format.
verifyFormat("import \"a.proto\"\n"
"import \"b.proto\"\n"
"// comment\n"
"message A {\n"
"}");
}
} // end namespace tooling
} // end namespace clang

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,294 @@
//===- unittest/Format/FormatTestProto.cpp --------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "FormatTestUtils.h"
#include "clang/Format/Format.h"
#include "llvm/Support/Debug.h"
#include "gtest/gtest.h"
#define DEBUG_TYPE "format-test"
namespace clang {
namespace format {
class FormatTestTextProto : public ::testing::Test {
protected:
static std::string format(llvm::StringRef Code, unsigned Offset,
unsigned Length, const FormatStyle &Style) {
DEBUG(llvm::errs() << "---\n");
DEBUG(llvm::errs() << Code << "\n\n");
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
tooling::Replacements Replaces = reformat(Style, Code, Ranges);
auto Result = applyAllReplacements(Code, Replaces);
EXPECT_TRUE(static_cast<bool>(Result));
DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
return *Result;
}
static std::string format(llvm::StringRef Code) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
Style.ColumnLimit = 60; // To make writing tests easier.
return format(Code, 0, Code.size(), Style);
}
static void verifyFormat(llvm::StringRef Code) {
EXPECT_EQ(Code.str(), format(test::messUp(Code)));
}
};
TEST_F(FormatTestTextProto, KeepsTopLevelEntriesFittingALine) {
verifyFormat("field_a: OK field_b: OK field_c: OK field_d: OK field_e: OK");
}
TEST_F(FormatTestTextProto, SupportsMessageFields) {
verifyFormat("msg_field: {}");
verifyFormat("msg_field: {field_a: A}");
verifyFormat("msg_field: {field_a: \"OK\" field_b: 123}");
verifyFormat("msg_field: {\n"
" field_a: 1\n"
" field_b: OK\n"
" field_c: \"OK\"\n"
" field_d: 123\n"
" field_e: 23\n"
"}");
verifyFormat("msg_field {}");
verifyFormat("msg_field {field_a: A}");
verifyFormat("msg_field {field_a: \"OK\" field_b: 123}");
verifyFormat("msg_field {\n"
" field_a: 1\n"
" field_b: OK\n"
" field_c: \"OK\"\n"
" field_d: 123\n"
" field_e: 23.0\n"
" field_f: false\n"
" field_g: 'lala'\n"
" field_h: 1234.567e-89\n"
"}");
verifyFormat("msg_field: {msg_field {field_a: 1}}");
verifyFormat("id: \"ala.bala\"\n"
"item {type: ITEM_A rank: 1 score: 90.0}\n"
"item {type: ITEM_B rank: 2 score: 70.5}\n"
"item {\n"
" type: ITEM_A\n"
" rank: 3\n"
" score: 20.0\n"
" description: \"the third item has a description\"\n"
"}");
}
TEST_F(FormatTestTextProto, AvoidsTopLevelBinPacking) {
verifyFormat("field_a: OK\n"
"field_b: OK\n"
"field_c: OK\n"
"field_d: OK\n"
"field_e: OK\n"
"field_f: OK");
verifyFormat("field_a: OK\n"
"field_b: \"OK\"\n"
"field_c: \"OK\"\n"
"msg_field: {field_d: 123}\n"
"field_e: OK\n"
"field_f: OK");
verifyFormat("field_a: OK\n"
"field_b: \"OK\"\n"
"field_c: \"OK\"\n"
"msg_field: {field_d: 123 field_e: OK}");
verifyFormat("a: {\n"
" field_a: OK\n"
" field_b {field_c: OK}\n"
" field_d: OKOKOK\n"
" field_e: OK\n"
"}");
verifyFormat("field_a: OK,\n"
"field_b {field_c: OK},\n"
"field_d: OKOKOK,\n"
"field_e: OK");
}
TEST_F(FormatTestTextProto, AddsNewlinesAfterTrailingComments) {
verifyFormat("field_a: OK // Comment\n"
"field_b: 1");
verifyFormat("field_a: OK\n"
"msg_field: {\n"
" field_b: OK // Comment\n"
"}");
verifyFormat("field_a: OK\n"
"msg_field {\n"
" field_b: OK // Comment\n"
"}");
}
TEST_F(FormatTestTextProto, SupportsAngleBracketMessageFields) {
// Single-line tests
verifyFormat("msg_field <>");
verifyFormat("msg_field: <>");
verifyFormat("msg_field <field_a: OK>");
verifyFormat("msg_field: <field_a: 123>");
verifyFormat("msg_field <field_a <>>");
verifyFormat("msg_field <field_a <field_b <>>>");
verifyFormat("msg_field: <field_a <field_b: <>>>");
verifyFormat("msg_field <field_a: OK, field_b: \"OK\">");
verifyFormat("msg_field <field_a: OK field_b: <>, field_c: OK>");
verifyFormat("msg_field <field_a {field_b: 1}, field_c: <field_d: 2>>");
verifyFormat("msg_field: <field_a: OK, field_b: \"OK\">");
verifyFormat("msg_field: <field_a: OK field_b: <>, field_c: OK>");
verifyFormat("msg_field: <field_a {field_b: 1}, field_c: <field_d: 2>>");
verifyFormat("field_a: \"OK\", msg_field: <field_b: 123>, field_c: {}");
verifyFormat("field_a <field_b: 1>, msg_field: <field_b: 123>, field_c <>");
verifyFormat("field_a <field_b: 1> msg_field: <field_b: 123> field_c <>");
verifyFormat("field <field <field: <>>, field <>> field: <field: 1>");
// Multiple lines tests
verifyFormat("msg_field <\n"
" field_a: OK\n"
" field_b: \"OK\"\n"
" field_c: 1\n"
" field_d: 12.5\n"
" field_e: OK\n"
">");
verifyFormat("msg_field: <>\n"
"field_c: \"OK\",\n"
"msg_field: <field_d: 123>\n"
"field_e: OK\n"
"msg_field: <field_d: 12>");
verifyFormat("field_a: OK,\n"
"field_b <field_c: OK>,\n"
"field_d: <12.5>,\n"
"field_e: OK");
verifyFormat("field_a: OK\n"
"field_b <field_c: OK>\n"
"field_d: <12.5>\n"
"field_e: OKOKOK");
verifyFormat("msg_field <\n"
" field_a: OK,\n"
" field_b <field_c: OK>,\n"
" field_d: <12.5>,\n"
" field_e: OK\n"
">");
verifyFormat("msg_field <\n"
" field_a: <field: OK>,\n"
" field_b <field_c: OK>,\n"
" field_d: <12.5>,\n"
" field_e: OK,\n"
">");
verifyFormat("msg_field: <\n"
" field_a: \"OK\"\n"
" msg_field: {field_b: OK}\n"
" field_g: OK\n"
" field_g: OK\n"
" field_g: OK\n"
">");
verifyFormat("field_a {\n"
" field_d: ok\n"
" field_b: <field_c: 1>\n"
" field_d: ok\n"
" field_d: ok\n"
"}");
verifyFormat("field_a: {\n"
" field_d: ok\n"
" field_b: <field_c: 1>\n"
" field_d: ok\n"
" field_d: ok\n"
"}");
verifyFormat("field_a: <f1: 1, f2: <>>\n"
"field_b <\n"
" field_b1: <>\n"
" field_b2: ok,\n"
" field_b3: <\n"
" field_x {} // Comment\n"
" field_y: {field_z: 1}\n"
" field_w: ok\n"
" >\n"
" field {\n"
" field_x <> // Comment\n"
" field_y: <field_z: 1>\n"
" field_w: ok\n"
" msg_field: <\n"
" field: <>\n"
" field: <field: 1>\n"
" field: <field: 2>\n"
" field: <field: 3>\n"
" field: <field: 4>\n"
" field: ok\n"
" >\n"
" }\n"
">\n"
"field: OK,\n"
"field_c <field <field <>>>");
verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
"head_id: 1\n"
"data <key: value>");
verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
"head_id: 1\n"
"data <key: value>\n"
"tail_id: 2");
verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
"head_id: 1\n"
"data <key: value>\n"
"data {key: value}");
verifyFormat("app {\n"
" app_id: 'com.javax.swing.salsa.latino'\n"
" head_id: 1\n"
" data <key: value>\n"
"}");
verifyFormat("app: {\n"
" app_id: 'com.javax.swing.salsa.latino'\n"
" head_id: 1\n"
" data <key: value>\n"
"}");
verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
"headheadheadheadheadhead_id: 1\n"
"product_data {product {1}}");
verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
"headheadheadheadheadhead_id: 1\n"
"product_data <product {1}>");
verifyFormat("app_id: 'com.javax.swing.salsa.latino'\n"
"headheadheadheadheadhead_id: 1\n"
"product_data <product <1>>");
verifyFormat("app <\n"
" app_id: 'com.javax.swing.salsa.latino'\n"
" headheadheadheadheadhead_id: 1\n"
" product_data <product {1}>\n"
">");
}
} // end namespace tooling
} // end namespace clang

View File

@@ -0,0 +1,68 @@
//===- unittest/Format/FormatTestUtils.h - Formatting unit tests ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines utility functions for Clang-Format related tests.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H
#define LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H
#include "llvm/ADT/StringRef.h"
namespace clang {
namespace format {
namespace test {
inline std::string messUp(llvm::StringRef Code) {
std::string MessedUp(Code.str());
bool InComment = false;
bool InPreprocessorDirective = false;
bool JustReplacedNewline = false;
for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) {
if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') {
if (JustReplacedNewline)
MessedUp[i - 1] = '\n';
InComment = true;
} else if (MessedUp[i] == '#' &&
(JustReplacedNewline || i == 0 || MessedUp[i - 1] == '\n')) {
if (i != 0)
MessedUp[i - 1] = '\n';
InPreprocessorDirective = true;
} else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
MessedUp[i] = ' ';
MessedUp[i + 1] = ' ';
} else if (MessedUp[i] == '\n') {
if (InComment) {
InComment = false;
} else if (InPreprocessorDirective) {
InPreprocessorDirective = false;
} else {
JustReplacedNewline = true;
MessedUp[i] = ' ';
}
} else if (MessedUp[i] != ' ') {
JustReplacedNewline = false;
}
}
std::string WithoutWhitespace;
if (MessedUp[0] != ' ')
WithoutWhitespace.push_back(MessedUp[0]);
for (unsigned i = 1, e = MessedUp.size(); i != e; ++i) {
if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ')
WithoutWhitespace.push_back(MessedUp[i]);
}
return WithoutWhitespace;
}
} // end namespace test
} // end namespace format
} // end namespace clang
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,313 @@
//===- unittest/Format/SortImportsTestJS.cpp - JS import sort unit tests --===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "FormatTestUtils.h"
#include "clang/Format/Format.h"
#include "llvm/Support/Debug.h"
#include "gtest/gtest.h"
#define DEBUG_TYPE "format-test"
namespace clang {
namespace format {
namespace {
class SortImportsTestJS : public ::testing::Test {
protected:
std::string sort(StringRef Code, unsigned Offset = 0, unsigned Length = 0) {
StringRef FileName = "input.js";
if (Length == 0U)
Length = Code.size() - Offset;
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
auto Sorted =
applyAllReplacements(Code, sortIncludes(Style, Code, Ranges, FileName));
EXPECT_TRUE(static_cast<bool>(Sorted));
auto Formatted = applyAllReplacements(
*Sorted, reformat(Style, *Sorted, Ranges, FileName));
EXPECT_TRUE(static_cast<bool>(Formatted));
return *Formatted;
}
void verifySort(llvm::StringRef Expected, llvm::StringRef Code,
unsigned Offset = 0, unsigned Length = 0) {
std::string Result = sort(Code, Offset, Length);
EXPECT_EQ(Expected.str(), Result) << "Expected:\n"
<< Expected << "\nActual:\n"
<< Result;
}
FormatStyle Style = getGoogleStyle(FormatStyle::LK_JavaScript);
};
TEST_F(SortImportsTestJS, AlreadySorted) {
verifySort("import {sym} from 'a';\n"
"import {sym} from 'b';\n"
"import {sym} from 'c';\n"
"\n"
"let x = 1;",
"import {sym} from 'a';\n"
"import {sym} from 'b';\n"
"import {sym} from 'c';\n"
"\n"
"let x = 1;");
}
TEST_F(SortImportsTestJS, BasicSorting) {
verifySort("import {sym} from 'a';\n"
"import {sym} from 'b';\n"
"import {sym} from 'c';\n"
"\n"
"let x = 1;",
"import {sym} from 'a';\n"
"import {sym} from 'c';\n"
"import {sym} from 'b';\n"
"let x = 1;");
}
TEST_F(SortImportsTestJS, DefaultBinding) {
verifySort("import A from 'a';\n"
"import B from 'b';\n"
"\n"
"let x = 1;",
"import B from 'b';\n"
"import A from 'a';\n"
"let x = 1;");
}
TEST_F(SortImportsTestJS, DefaultAndNamedBinding) {
verifySort("import A, {a} from 'a';\n"
"import B, {b} from 'b';\n"
"\n"
"let x = 1;",
"import B, {b} from 'b';\n"
"import A, {a} from 'a';\n"
"let x = 1;");
}
TEST_F(SortImportsTestJS, WrappedImportStatements) {
verifySort("import {sym1, sym2} from 'a';\n"
"import {sym} from 'b';\n"
"\n"
"1;",
"import\n"
" {sym}\n"
" from 'b';\n"
"import {\n"
" sym1,\n"
" sym2\n"
"} from 'a';\n"
"1;");
}
TEST_F(SortImportsTestJS, SeparateMainCodeBody) {
verifySort("import {sym} from 'a';"
"\n"
"let x = 1;\n",
"import {sym} from 'a'; let x = 1;\n");
}
TEST_F(SortImportsTestJS, Comments) {
verifySort("/** @fileoverview This is a great file. */\n"
"// A very important import follows.\n"
"import {sym} from 'a'; /* more comments */\n"
"import {sym} from 'b'; // from //foo:bar\n",
"/** @fileoverview This is a great file. */\n"
"import {sym} from 'b'; // from //foo:bar\n"
"// A very important import follows.\n"
"import {sym} from 'a'; /* more comments */\n");
verifySort("import {sym} from 'a';\n"
"import {sym} from 'b';\n"
"\n"
"/** Comment on variable. */\n"
"const x = 1;\n",
"import {sym} from 'b';\n"
"import {sym} from 'a';\n"
"\n"
"/** Comment on variable. */\n"
"const x = 1;\n");
}
TEST_F(SortImportsTestJS, SortStar) {
verifySort("import * as foo from 'a';\n"
"import {sym} from 'a';\n"
"import * as bar from 'b';\n",
"import {sym} from 'a';\n"
"import * as foo from 'a';\n"
"import * as bar from 'b';\n");
}
TEST_F(SortImportsTestJS, AliasesSymbols) {
verifySort("import {sym1 as alias1} from 'b';\n"
"import {sym2 as alias2, sym3 as alias3} from 'c';\n",
"import {sym2 as alias2, sym3 as alias3} from 'c';\n"
"import {sym1 as alias1} from 'b';\n");
}
TEST_F(SortImportsTestJS, SortSymbols) {
verifySort("import {sym1, sym2 as a, sym3} from 'b';\n",
"import {sym2 as a, sym1, sym3} from 'b';\n");
verifySort("import {sym1 /* important! */, /*!*/ sym2 as a} from 'b';\n",
"import {/*!*/ sym2 as a, sym1 /* important! */} from 'b';\n");
verifySort("import {sym1, sym2} from 'b';\n", "import {\n"
" sym2 \n"
",\n"
" sym1 \n"
"} from 'b';\n");
}
TEST_F(SortImportsTestJS, GroupImports) {
verifySort("import {a} from 'absolute';\n"
"\n"
"import {b} from '../parent';\n"
"import {b} from '../parent/nested';\n"
"\n"
"import {b} from './relative/path';\n"
"import {b} from './relative/path/nested';\n"
"\n"
"let x = 1;\n",
"import {b} from './relative/path/nested';\n"
"import {b} from './relative/path';\n"
"import {b} from '../parent/nested';\n"
"import {b} from '../parent';\n"
"import {a} from 'absolute';\n"
"let x = 1;\n");
}
TEST_F(SortImportsTestJS, Exports) {
verifySort("import {S} from 'bpath';\n"
"\n"
"import {T} from './cpath';\n"
"\n"
"export {A, B} from 'apath';\n"
"export {P} from '../parent';\n"
"export {R} from './relative';\n"
"export {S};\n"
"\n"
"let x = 1;\n"
"export y = 1;\n",
"export {R} from './relative';\n"
"import {T} from './cpath';\n"
"export {S};\n"
"export {A, B} from 'apath';\n"
"import {S} from 'bpath';\n"
"export {P} from '../parent';\n"
"let x = 1;\n"
"export y = 1;\n");
verifySort("import {S} from 'bpath';\n"
"\n"
"export {T} from 'epath';\n",
"export {T} from 'epath';\n"
"import {S} from 'bpath';\n");
}
TEST_F(SortImportsTestJS, SideEffectImports) {
verifySort("import 'ZZside-effect';\n"
"import 'AAside-effect';\n"
"\n"
"import {A} from 'absolute';\n"
"\n"
"import {R} from './relative';\n",
"import {R} from './relative';\n"
"import 'ZZside-effect';\n"
"import {A} from 'absolute';\n"
"import 'AAside-effect';\n");
}
TEST_F(SortImportsTestJS, AffectedRange) {
// Affected range inside of import statements.
verifySort("import {sym} from 'a';\n"
"import {sym} from 'b';\n"
"import {sym} from 'c';\n"
"\n"
"let x = 1;",
"import {sym} from 'c';\n"
"import {sym} from 'b';\n"
"import {sym} from 'a';\n"
"let x = 1;",
0, 30);
// Affected range outside of import statements.
verifySort("import {sym} from 'c';\n"
"import {sym} from 'b';\n"
"import {sym} from 'a';\n"
"\n"
"let x = 1;",
"import {sym} from 'c';\n"
"import {sym} from 'b';\n"
"import {sym} from 'a';\n"
"\n"
"let x = 1;",
70, 1);
}
TEST_F(SortImportsTestJS, SortingCanShrink) {
// Sort excluding a suffix.
verifySort("import {B} from 'a';\n"
"import {A} from 'b';\n"
"\n"
"1;",
"import {A} from 'b';\n"
"\n"
"import {B} from 'a';\n"
"\n"
"1;");
}
TEST_F(SortImportsTestJS, TrailingComma) {
verifySort("import {A, B,} from 'aa';\n", "import {B, A,} from 'aa';\n");
}
TEST_F(SortImportsTestJS, SortCaseInsensitive) {
verifySort("import {A} from 'aa';\n"
"import {A} from 'Ab';\n"
"import {A} from 'b';\n"
"import {A} from 'Bc';\n"
"\n"
"1;",
"import {A} from 'b';\n"
"import {A} from 'Bc';\n"
"import {A} from 'Ab';\n"
"import {A} from 'aa';\n"
"\n"
"1;");
verifySort("import {aa, Ab, b, Bc} from 'x';\n"
"\n"
"1;",
"import {b, Bc, Ab, aa} from 'x';\n"
"\n"
"1;");
}
TEST_F(SortImportsTestJS, SortMultiLine) {
// Reproduces issue where multi-line import was not parsed correctly.
verifySort("import {A} from 'a';\n"
"import {A} from 'b';\n"
"\n"
"1;",
"import\n"
"{\n"
"A\n"
"}\n"
"from\n"
"'b';\n"
"import {A} from 'a';\n"
"\n"
"1;");
}
TEST_F(SortImportsTestJS, SortDefaultImports) {
// Reproduces issue where multi-line import was not parsed correctly.
verifySort("import {A} from 'a';\n"
"import {default as B} from 'b';\n",
"import {default as B} from 'b';\n"
"import {A} from 'a';\n");
}
} // end namespace
} // end namespace format
} // end namespace clang

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,375 @@
//===- UsingDeclarationsSorterTest.cpp - Formatting unit tests ------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Format/Format.h"
#include "llvm/Support/Debug.h"
#include "gtest/gtest.h"
#define DEBUG_TYPE "using-declarations-sorter-test"
namespace clang {
namespace format {
namespace {
class UsingDeclarationsSorterTest : public ::testing::Test {
protected:
std::string sortUsingDeclarations(llvm::StringRef Code,
const std::vector<tooling::Range> &Ranges,
const FormatStyle &Style = getLLVMStyle()) {
DEBUG(llvm::errs() << "---\n");
DEBUG(llvm::errs() << Code << "\n\n");
tooling::Replacements Replaces =
clang::format::sortUsingDeclarations(Style, Code, Ranges, "<stdin>");
auto Result = applyAllReplacements(Code, Replaces);
EXPECT_TRUE(static_cast<bool>(Result));
DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
return *Result;
}
std::string sortUsingDeclarations(llvm::StringRef Code,
const FormatStyle &Style = getLLVMStyle()) {
return sortUsingDeclarations(Code,
/*Ranges=*/{1, tooling::Range(0, Code.size())},
Style);
}
};
TEST_F(UsingDeclarationsSorterTest, SwapsTwoConsecutiveUsingDeclarations) {
EXPECT_EQ("using a;\n"
"using b;",
sortUsingDeclarations("using a;\n"
"using b;"));
EXPECT_EQ("using a;\n"
"using aa;",
sortUsingDeclarations("using aa;\n"
"using a;"));
EXPECT_EQ("using a;\n"
"using ::a;",
sortUsingDeclarations("using a;\n"
"using ::a;"));
EXPECT_EQ("using a::bcd;\n"
"using a::cd;",
sortUsingDeclarations("using a::cd;\n"
"using a::bcd;"));
EXPECT_EQ("using a;\n"
"using a::a;",
sortUsingDeclarations("using a::a;\n"
"using a;"));
EXPECT_EQ("using a::ba::aa;\n"
"using a::bb::ccc;",
sortUsingDeclarations("using a::bb::ccc;\n"
"using a::ba::aa;"));
EXPECT_EQ("using a;\n"
"using typename a;",
sortUsingDeclarations("using typename a;\n"
"using a;"));
EXPECT_EQ("using typename z;\n"
"using typenamea;",
sortUsingDeclarations("using typenamea;\n"
"using typename z;"));
EXPECT_EQ("using a, b;\n"
"using aa;",
sortUsingDeclarations("using aa;\n"
"using a, b;"));
}
TEST_F(UsingDeclarationsSorterTest, UsingDeclarationOrder) {
EXPECT_EQ("using A;\n"
"using a;",
sortUsingDeclarations("using A;\n"
"using a;"));
EXPECT_EQ("using a;\n"
"using A;",
sortUsingDeclarations("using a;\n"
"using A;"));
EXPECT_EQ("using a;\n"
"using B;",
sortUsingDeclarations("using B;\n"
"using a;"));
// Ignores leading '::'.
EXPECT_EQ("using ::a;\n"
"using A;",
sortUsingDeclarations("using ::a;\n"
"using A;"));
EXPECT_EQ("using ::A;\n"
"using a;",
sortUsingDeclarations("using ::A;\n"
"using a;"));
// Sorts '_' before 'a' and 'A'.
EXPECT_EQ("using _;\n"
"using A;",
sortUsingDeclarations("using A;\n"
"using _;"));
EXPECT_EQ("using _;\n"
"using a;",
sortUsingDeclarations("using a;\n"
"using _;"));
EXPECT_EQ("using a::_;\n"
"using a::a;",
sortUsingDeclarations("using a::a;\n"
"using a::_;"));
// Sorts non-namespace names before namespace names at the same level.
EXPECT_EQ("using ::testing::_;\n"
"using ::testing::Aardvark;\n"
"using ::testing::kMax;\n"
"using ::testing::Xylophone;\n"
"using ::testing::apple::Honeycrisp;\n"
"using ::testing::zebra::Stripes;",
sortUsingDeclarations("using ::testing::Aardvark;\n"
"using ::testing::Xylophone;\n"
"using ::testing::kMax;\n"
"using ::testing::_;\n"
"using ::testing::apple::Honeycrisp;\n"
"using ::testing::zebra::Stripes;"));
}
TEST_F(UsingDeclarationsSorterTest, SortsStably) {
EXPECT_EQ("using a;\n"
"using A;\n"
"using a;\n"
"using A;\n"
"using a;\n"
"using A;\n"
"using a;\n"
"using B;\n"
"using b;\n"
"using B;\n"
"using b;\n"
"using B;\n"
"using b;",
sortUsingDeclarations("using a;\n"
"using B;\n"
"using a;\n"
"using b;\n"
"using A;\n"
"using a;\n"
"using b;\n"
"using B;\n"
"using b;\n"
"using A;\n"
"using a;\n"
"using b;\n"
"using b;\n"
"using B;\n"
"using b;\n"
"using A;\n"
"using a;"));
}
TEST_F(UsingDeclarationsSorterTest, SortsMultipleTopLevelDeclarations) {
EXPECT_EQ("using a;\n"
"using b;\n"
"using c;\n"
"using d;\n"
"using e;",
sortUsingDeclarations("using d;\n"
"using b;\n"
"using e;\n"
"using a;\n"
"using c;"));
EXPECT_EQ("#include <iostream>\n"
"using std::cin;\n"
"using std::cout;\n"
"using ::std::endl;\n"
"int main();",
sortUsingDeclarations("#include <iostream>\n"
"using std::cout;\n"
"using ::std::endl;\n"
"using std::cin;\n"
"int main();"));
}
TEST_F(UsingDeclarationsSorterTest, BreaksOnEmptyLines) {
EXPECT_EQ("using b;\n"
"using c;\n"
"\n"
"using a;\n"
"using d;",
sortUsingDeclarations("using c;\n"
"using b;\n"
"\n"
"using d;\n"
"using a;"));
}
TEST_F(UsingDeclarationsSorterTest, BreaksOnUsingNamespace) {
EXPECT_EQ("using b;\n"
"using namespace std;\n"
"using a;",
sortUsingDeclarations("using b;\n"
"using namespace std;\n"
"using a;"));
}
TEST_F(UsingDeclarationsSorterTest, KeepsUsingDeclarationsInPPDirectives) {
EXPECT_EQ("#define A \\\n"
"using b;\\\n"
"using a;",
sortUsingDeclarations("#define A \\\n"
"using b;\\\n"
"using a;"));
}
TEST_F(UsingDeclarationsSorterTest, KeepsTypeAliases) {
auto Code = "struct C { struct B { struct A; }; };\n"
"using B = C::B;\n"
"using A = B::A;";
EXPECT_EQ(Code, sortUsingDeclarations(Code));
}
TEST_F(UsingDeclarationsSorterTest, MovesTrailingCommentsWithDeclarations) {
EXPECT_EQ("using a; // line a1\n"
"using b; /* line b1\n"
" * line b2\n"
" * line b3 */\n"
"using c; // line c1\n"
" // line c2",
sortUsingDeclarations("using c; // line c1\n"
" // line c2\n"
"using b; /* line b1\n"
" * line b2\n"
" * line b3 */\n"
"using a; // line a1"));
}
TEST_F(UsingDeclarationsSorterTest, SortsInStructScope) {
EXPECT_EQ("struct pt3 : pt2 {\n"
" using pt2::x;\n"
" using pt2::y;\n"
" float z;\n"
"};",
sortUsingDeclarations("struct pt3 : pt2 {\n"
" using pt2::y;\n"
" using pt2::x;\n"
" float z;\n"
"};"));
}
TEST_F(UsingDeclarationsSorterTest, KeepsOperators) {
EXPECT_EQ("using a::operator();\n"
"using a::operator-;\n"
"using a::operator+;",
sortUsingDeclarations("using a::operator();\n"
"using a::operator-;\n"
"using a::operator+;"));
}
TEST_F(UsingDeclarationsSorterTest, SortsUsingDeclarationsInsideNamespaces) {
EXPECT_EQ("namespace A {\n"
"struct B;\n"
"struct C;\n"
"}\n"
"namespace X {\n"
"using A::B;\n"
"using A::C;\n"
"}",
sortUsingDeclarations("namespace A {\n"
"struct B;\n"
"struct C;\n"
"}\n"
"namespace X {\n"
"using A::C;\n"
"using A::B;\n"
"}"));
}
TEST_F(UsingDeclarationsSorterTest, SupportsClangFormatOff) {
EXPECT_EQ("// clang-format off\n"
"using b;\n"
"using a;\n"
"// clang-format on\n"
"using c;\n"
"using d;",
sortUsingDeclarations("// clang-format off\n"
"using b;\n"
"using a;\n"
"// clang-format on\n"
"using d;\n"
"using c;"));
}
TEST_F(UsingDeclarationsSorterTest, SortsPartialRangeOfUsingDeclarations) {
// Sorts the whole block of using declarations surrounding the range.
EXPECT_EQ("using a;\n"
"using b;\n"
"using c;",
sortUsingDeclarations("using b;\n"
"using c;\n" // starts at offset 10
"using a;",
{tooling::Range(10, 15)}));
EXPECT_EQ("using a;\n"
"using b;\n"
"using c;\n"
"using A = b;",
sortUsingDeclarations("using b;\n"
"using c;\n" // starts at offset 10
"using a;\n"
"using A = b;",
{tooling::Range(10, 15)}));
EXPECT_EQ("using d;\n"
"using c;\n"
"\n"
"using a;\n"
"using b;\n"
"\n"
"using f;\n"
"using e;",
sortUsingDeclarations("using d;\n"
"using c;\n"
"\n"
"using b;\n" // starts at offset 19
"using a;\n"
"\n"
"using f;\n"
"using e;",
{tooling::Range(19, 1)}));
}
TEST_F(UsingDeclarationsSorterTest, SortsUsingDeclarationsWithLeadingkComments) {
EXPECT_EQ("/* comment */ using a;\n"
"/* comment */ using b;",
sortUsingDeclarations("/* comment */ using b;\n"
"/* comment */ using a;"));
}
TEST_F(UsingDeclarationsSorterTest, DeduplicatesUsingDeclarations) {
EXPECT_EQ("using a;\n"
"using b;\n"
"using c;\n"
"\n"
"using a;\n"
"using e;",
sortUsingDeclarations("using c;\n"
"using a;\n"
"using b;\n"
"using a;\n"
"using b;\n"
"\n"
"using e;\n"
"using a;\n"
"using e;"));
}
} // end namespace
} // end namespace format
} // end namespace clang