aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests
diff options
context:
space:
mode:
authorowenca <owenpiano@gmail.com>2022-07-19 01:22:31 -0700
committerowenca <owenpiano@gmail.com>2022-07-20 21:56:48 -0700
commita4c62f66545d11253ef76384b78d9dc196fc8e0e (patch)
tree887fed28b27a55764215e671b5aa96abe361d3ac /clang/unittests
parent892a9968ec7753467a7663cb91ee1e5a299844cf (diff)
downloadllvm-a4c62f66545d11253ef76384b78d9dc196fc8e0e.zip
llvm-a4c62f66545d11253ef76384b78d9dc196fc8e0e.tar.gz
llvm-a4c62f66545d11253ef76384b78d9dc196fc8e0e.tar.bz2
[clang-format][NFC] Refactor RequiresDoesNotChangeParsingOfTheRest
Differential Revision: https://reviews.llvm.org/D129982
Diffstat (limited to 'clang/unittests')
-rw-r--r--clang/unittests/Format/TokenAnnotatorTest.cpp478
1 files changed, 163 insertions, 315 deletions
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index bdbfca7..4b26225 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -549,323 +549,171 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {
}
TEST_F(TokenAnnotatorTest, RequiresDoesNotChangeParsingOfTheRest) {
- auto NumberOfAdditionalRequiresClauseTokens = 5u;
- auto NumberOfTokensBeforeRequires = 5u;
-
- auto BaseTokens = annotate("template<typename T>\n"
- "T Pi = 3.14;");
- auto ConstrainedTokens = annotate("template<typename T>\n"
- " requires Foo<T>\n"
- "T Pi = 3.14;");
-
- auto NumberOfBaseTokens = 11u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
+ const char *BaseCode = nullptr;
+ const char *ConstrainedCode = nullptr;
+ auto BaseTokenCount = 0u;
+ auto RequiresTokenCount = 0u;
+ auto PrefixTokenCount = 0u;
+
+ auto TestRequires = [&](int Line) {
+ const auto BaseTokens = annotate(BaseCode);
+ const auto ConstrainedTokens = annotate(ConstrainedCode);
+
+#define LINE " (Line " << Line << ')'
+
+ ASSERT_EQ(BaseTokens.size(), BaseTokenCount) << BaseTokens << LINE;
+ ASSERT_EQ(ConstrainedTokens.size(), BaseTokenCount + RequiresTokenCount)
+ << LINE;
+
+ for (auto I = 0u; I < BaseTokenCount; ++I) {
+ EXPECT_EQ(
+ *BaseTokens[I],
+ *ConstrainedTokens[I < PrefixTokenCount ? I : I + RequiresTokenCount])
+ << I << LINE;
}
- }
- BaseTokens = annotate("template<typename T>\n"
- "struct Bar;");
- ConstrainedTokens = annotate("template<typename T>\n"
- " requires Foo<T>\n"
- "struct Bar;");
- NumberOfBaseTokens = 9u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
-
- BaseTokens = annotate("template<typename T>\n"
- "struct Bar {"
- " T foo();\n"
- " T bar();\n"
- "};");
- ConstrainedTokens = annotate("template<typename T>\n"
- " requires Foo<T>\n"
- "struct Bar {"
- " T foo();\n"
- " T bar();\n"
- "};");
- NumberOfBaseTokens = 21u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
-
- BaseTokens = annotate("template<typename T>\n"
- "Bar(T) -> Bar<T>;");
- ConstrainedTokens = annotate("template<typename T>\n"
- " requires Foo<T>\n"
- "Bar(T) -> Bar<T>;");
- NumberOfBaseTokens = 16u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
-
- BaseTokens = annotate("template<typename T>\n"
- "T foo();");
- ConstrainedTokens = annotate("template<typename T>\n"
- " requires Foo<T>\n"
- "T foo();");
- NumberOfBaseTokens = 11u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
-
- BaseTokens = annotate("template<typename T>\n"
- "T foo() {\n"
- " auto bar = baz();\n"
- " return bar + T{};\n"
- "}");
- ConstrainedTokens = annotate("template<typename T>\n"
- " requires Foo<T>\n"
- "T foo() {\n"
- " auto bar = baz();\n"
- " return bar + T{};\n"
- "}");
- NumberOfBaseTokens = 26u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
-
- BaseTokens = annotate("template<typename T>\n"
- "T foo();");
- ConstrainedTokens = annotate("template<typename T>\n"
- "T foo() requires Foo<T>;");
- NumberOfBaseTokens = 11u;
- NumberOfTokensBeforeRequires = 9u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
-
- BaseTokens = annotate("template<typename T>\n"
- "T foo() {\n"
- " auto bar = baz();\n"
- " return bar + T{};\n"
- "}");
- ConstrainedTokens = annotate("template<typename T>\n"
- "T foo() requires Foo<T> {\n"
- " auto bar = baz();\n"
- " return bar + T{};\n"
- "}");
- NumberOfBaseTokens = 26u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
-
- BaseTokens = annotate("template<typename T>\n"
- "Bar(T) -> Bar<typename T::I>;");
- ConstrainedTokens = annotate("template<typename T>\n"
- " requires requires(T &&t) {\n"
- " typename T::I;\n"
- " }\n"
- "Bar(T) -> Bar<typename T::I>;");
- NumberOfBaseTokens = 19u;
- NumberOfAdditionalRequiresClauseTokens = 14u;
- NumberOfTokensBeforeRequires = 5u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
-
- BaseTokens = annotate("struct [[nodiscard]] zero_t {\n"
- " template<class T>\n"
- " [[nodiscard]] constexpr operator T() const { "
- "return number_zero_v<T>; }\n"
- "};");
-
- ConstrainedTokens = annotate("struct [[nodiscard]] zero_t {\n"
- " template<class T>\n"
- " requires requires { number_zero_v<T>; }\n"
- " [[nodiscard]] constexpr operator T() const { "
- "return number_zero_v<T>; }\n"
- "};");
- NumberOfBaseTokens = 35u;
- NumberOfAdditionalRequiresClauseTokens = 9u;
- NumberOfTokensBeforeRequires = 13u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
-
- BaseTokens = annotate("constexpr Foo(Foo const &other)\n"
- " : value{other.value} {\n"
- " do_magic();\n"
- " do_more_magic();\n"
- "}");
-
- ConstrainedTokens = annotate("constexpr Foo(Foo const &other)\n"
- " requires std::is_copy_constructible<T>\n"
- " : value{other.value} {\n"
- " do_magic();\n"
- " do_more_magic();\n"
- "}");
-
- NumberOfBaseTokens = 26u;
- NumberOfAdditionalRequiresClauseTokens = 7u;
- NumberOfTokensBeforeRequires = 8u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
-
- BaseTokens = annotate("constexpr Foo(Foo const &other)\n"
- " : value{other.value} {\n"
- " do_magic();\n"
- " do_more_magic();\n"
- "}");
-
- ConstrainedTokens = annotate("constexpr Foo(Foo const &other)\n"
- " requires (std::is_copy_constructible<T>)\n"
- " : value{other.value} {\n"
- " do_magic();\n"
- " do_more_magic();\n"
- "}");
-
- NumberOfBaseTokens = 26u;
- NumberOfAdditionalRequiresClauseTokens = 9u;
- NumberOfTokensBeforeRequires = 8u;
-
- ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
- ASSERT_EQ(ConstrainedTokens.size(),
- NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
- << ConstrainedTokens;
-
- for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
- if (I < NumberOfTokensBeforeRequires) {
- EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
- } else {
- EXPECT_EQ(*BaseTokens[I],
- *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
- << I;
- }
- }
+#undef LINE
+ };
+
+ BaseCode = "template<typename T>\n"
+ "T Pi = 3.14;";
+ ConstrainedCode = "template<typename T>\n"
+ " requires Foo<T>\n"
+ "T Pi = 3.14;";
+ BaseTokenCount = 11;
+ RequiresTokenCount = 5;
+ PrefixTokenCount = 5;
+ TestRequires(__LINE__);
+
+ BaseCode = "template<typename T>\n"
+ "struct Bar;";
+ ConstrainedCode = "template<typename T>\n"
+ " requires Foo<T>\n"
+ "struct Bar;";
+ BaseTokenCount = 9;
+ TestRequires(__LINE__);
+
+ BaseCode = "template<typename T>\n"
+ "struct Bar {\n"
+ " T foo();\n"
+ " T bar();\n"
+ "};";
+ ConstrainedCode = "template<typename T>\n"
+ " requires Foo<T>\n"
+ "struct Bar {\n"
+ " T foo();\n"
+ " T bar();\n"
+ "};";
+ BaseTokenCount = 21;
+ TestRequires(__LINE__);
+
+ BaseCode = "template<typename T>\n"
+ "Bar(T) -> Bar<T>;";
+ ConstrainedCode = "template<typename T>\n"
+ " requires Foo<T>\n"
+ "Bar(T) -> Bar<T>;";
+ BaseTokenCount = 16;
+ TestRequires(__LINE__);
+
+ BaseCode = "template<typename T>\n"
+ "T foo();";
+ ConstrainedCode = "template<typename T>\n"
+ " requires Foo<T>\n"
+ "T foo();";
+ BaseTokenCount = 11;
+ TestRequires(__LINE__);
+
+ BaseCode = "template<typename T>\n"
+ "T foo() {\n"
+ " auto bar = baz();\n"
+ " return bar + T{};\n"
+ "}";
+ ConstrainedCode = "template<typename T>\n"
+ " requires Foo<T>\n"
+ "T foo() {\n"
+ " auto bar = baz();\n"
+ " return bar + T{};\n"
+ "}";
+ BaseTokenCount = 26;
+ TestRequires(__LINE__);
+
+ BaseCode = "template<typename T>\n"
+ "T foo();";
+ ConstrainedCode = "template<typename T>\n"
+ "T foo() requires Foo<T>;";
+ BaseTokenCount = 11;
+ PrefixTokenCount = 9;
+ TestRequires(__LINE__);
+
+ BaseCode = "template<typename T>\n"
+ "T foo() {\n"
+ " auto bar = baz();\n"
+ " return bar + T{};\n"
+ "}";
+ ConstrainedCode = "template<typename T>\n"
+ "T foo() requires Foo<T> {\n"
+ " auto bar = baz();\n"
+ " return bar + T{};\n"
+ "}";
+ BaseTokenCount = 26;
+ TestRequires(__LINE__);
+
+ BaseCode = "template<typename T>\n"
+ "Bar(T) -> Bar<typename T::I>;";
+ ConstrainedCode = "template<typename T>\n"
+ " requires requires(T &&t) {\n"
+ " typename T::I;\n"
+ " }\n"
+ "Bar(T) -> Bar<typename T::I>;";
+ BaseTokenCount = 19;
+ RequiresTokenCount = 14;
+ PrefixTokenCount = 5;
+ TestRequires(__LINE__);
+
+ BaseCode = "struct [[nodiscard]] zero_t {\n"
+ " template<class T>\n"
+ " [[nodiscard]] constexpr operator T() const { return v<T>; }\n"
+ "};";
+ ConstrainedCode =
+ "struct [[nodiscard]] zero_t {\n"
+ " template<class T>\n"
+ " requires requires { v<T>; }\n"
+ " [[nodiscard]] constexpr operator T() const { return v<T>; }\n"
+ "};";
+ BaseTokenCount = 35;
+ RequiresTokenCount = 9;
+ PrefixTokenCount = 13;
+ TestRequires(__LINE__);
+
+ BaseCode = "constexpr Foo(Foo const &other)\n"
+ " : value{other.value} {\n"
+ " do_magic();\n"
+ " do_more_magic();\n"
+ "}";
+ ConstrainedCode = "constexpr Foo(Foo const &other)\n"
+ " requires std::is_copy_constructible<T>\n"
+ " : value{other.value} {\n"
+ " do_magic();\n"
+ " do_more_magic();\n"
+ "}";
+ BaseTokenCount = 26;
+ RequiresTokenCount = 7;
+ PrefixTokenCount = 8;
+ TestRequires(__LINE__);
+
+ BaseCode = "constexpr Foo(Foo const &other)\n"
+ " : value{other.value} {\n"
+ " do_magic();\n"
+ " do_more_magic();\n"
+ "}";
+ ConstrainedCode = "constexpr Foo(Foo const &other)\n"
+ " requires (std::is_copy_constructible<T>)\n"
+ " : value{other.value} {\n"
+ " do_magic();\n"
+ " do_more_magic();\n"
+ "}";
+ RequiresTokenCount = 9;
+ TestRequires(__LINE__);
}
TEST_F(TokenAnnotatorTest, UnderstandsAsm) {