From eed64b5fc6c0d619b2cb2df2eac1dc6cdf282a19 Mon Sep 17 00:00:00 2001 From: Krystian Kuzniarek Date: Thu, 8 Aug 2019 07:49:00 +0200 Subject: replace autogenerated TypesX classes by variadic ones --- googletest/include/gtest/gtest-typed-test.h | 18 +- googletest/include/gtest/internal/gtest-internal.h | 4 +- .../include/gtest/internal/gtest-type-util.h | 1590 +------------------- .../include/gtest/internal/gtest-type-util.h.pump | 132 +- googletest/test/gtest-unittest-api_test.cc | 9 +- 5 files changed, 102 insertions(+), 1651 deletions(-) diff --git a/googletest/include/gtest/gtest-typed-test.h b/googletest/include/gtest/gtest-typed-test.h index 6b7c9c8..3848044 100644 --- a/googletest/include/gtest/gtest-typed-test.h +++ b/googletest/include/gtest/gtest-typed-test.h @@ -27,7 +27,6 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - // GOOGLETEST_CM0001 DO NOT DELETE #ifndef GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_ @@ -188,13 +187,13 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes); #define GTEST_NAME_GENERATOR_(TestSuiteName) \ gtest_type_params_##TestSuiteName##_NameGenerator -#define TYPED_TEST_SUITE(CaseName, Types, ...) \ - typedef ::testing::internal::TypeList::type GTEST_TYPE_PARAMS_( \ - CaseName); \ - typedef ::testing::internal::NameGeneratorSelector<__VA_ARGS__>::type \ +#define TYPED_TEST_SUITE(CaseName, Types, ...) \ + typedef ::testing::internal::GenerateTypeList::type \ + GTEST_TYPE_PARAMS_(CaseName); \ + typedef ::testing::internal::NameGeneratorSelector<__VA_ARGS__>::type \ GTEST_NAME_GENERATOR_(CaseName) -# define TYPED_TEST(CaseName, TestName) \ +#define TYPED_TEST(CaseName, TestName) \ template \ class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \ : public CaseName { \ @@ -204,8 +203,7 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes); void TestBody() override; \ }; \ static bool gtest_##CaseName##_##TestName##_registered_ \ - GTEST_ATTRIBUTE_UNUSED_ = \ - ::testing::internal::TypeParameterizedTest< \ + GTEST_ATTRIBUTE_UNUSED_ = ::testing::internal::TypeParameterizedTest< \ CaseName, \ ::testing::internal::TemplateSel, \ @@ -307,7 +305,7 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes); static bool gtest_##Prefix##_##SuiteName GTEST_ATTRIBUTE_UNUSED_ = \ ::testing::internal::TypeParameterizedTestSuite< \ SuiteName, GTEST_SUITE_NAMESPACE_(SuiteName)::gtest_AllTests_, \ - ::testing::internal::TypeList::type>:: \ + ::testing::internal::GenerateTypeList::type>:: \ Register(#Prefix, \ ::testing::internal::CodeLocation(__FILE__, __LINE__), \ >EST_TYPED_TEST_SUITE_P_STATE_(SuiteName), #SuiteName, \ @@ -315,7 +313,7 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes); ::testing::internal::GenerateNames< \ ::testing::internal::NameGeneratorSelector< \ __VA_ARGS__>::type, \ - ::testing::internal::TypeList::type>()) + ::testing::internal::GenerateTypeList::type>()) // Legacy API is deprecated but still available #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index 9524223..2056561 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -662,7 +662,7 @@ struct NameGeneratorSelector { }; template -void GenerateNamesRecursively(Types0, std::vector*, int) {} +void GenerateNamesRecursively(internal::None, std::vector*, int) {} template void GenerateNamesRecursively(Types, std::vector* result, int i) { @@ -729,7 +729,7 @@ class TypeParameterizedTest { // The base case for the compile time recursion. template -class TypeParameterizedTest { +class TypeParameterizedTest { public: static bool Register(const char* /*prefix*/, const CodeLocation&, const char* /*case_name*/, const char* /*test_names*/, diff --git a/googletest/include/gtest/internal/gtest-type-util.h b/googletest/include/gtest/internal/gtest-type-util.h index 3d7542d..001fadb 100644 --- a/googletest/include/gtest/internal/gtest-type-util.h +++ b/googletest/include/gtest/internal/gtest-type-util.h @@ -31,11 +31,12 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + // Type utilities needed for implementing typed and type-parameterized // tests. This file is generated by a SCRIPT. DO NOT EDIT BY HAND! // -// Currently we support at most 50 types in a list, and at most 50 -// type-parameterized tests in one type-parameterized test suite. +// Currently we support at most 50 type-parameterized tests +// in one type-parameterized test suite. // Please contact googletestframework@googlegroups.com if you need // more. @@ -105,1524 +106,6 @@ std::string GetTypeName() { #if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P -// A unique type used as the default value for the arguments of class -// template Types. This allows us to simulate variadic templates -// (e.g. Types, Type, and etc), which C++ doesn't -// support directly. -struct None {}; - -// The following family of struct and struct templates are used to -// represent type lists. In particular, TypesN -// represents a type list with N types (T1, T2, ..., and TN) in it. -// Except for Types0, every struct in the family has two member types: -// Head for the first type in the list, and Tail for the rest of the -// list. - -// The empty type list. -struct Types0 {}; - -// Type lists of length 1, 2, 3, and so on. - -template -struct Types1 { - typedef T1 Head; - typedef Types0 Tail; -}; -template -struct Types2 { - typedef T1 Head; - typedef Types1 Tail; -}; - -template -struct Types3 { - typedef T1 Head; - typedef Types2 Tail; -}; - -template -struct Types4 { - typedef T1 Head; - typedef Types3 Tail; -}; - -template -struct Types5 { - typedef T1 Head; - typedef Types4 Tail; -}; - -template -struct Types6 { - typedef T1 Head; - typedef Types5 Tail; -}; - -template -struct Types7 { - typedef T1 Head; - typedef Types6 Tail; -}; - -template -struct Types8 { - typedef T1 Head; - typedef Types7 Tail; -}; - -template -struct Types9 { - typedef T1 Head; - typedef Types8 Tail; -}; - -template -struct Types10 { - typedef T1 Head; - typedef Types9 Tail; -}; - -template -struct Types11 { - typedef T1 Head; - typedef Types10 Tail; -}; - -template -struct Types12 { - typedef T1 Head; - typedef Types11 Tail; -}; - -template -struct Types13 { - typedef T1 Head; - typedef Types12 Tail; -}; - -template -struct Types14 { - typedef T1 Head; - typedef Types13 Tail; -}; - -template -struct Types15 { - typedef T1 Head; - typedef Types14 Tail; -}; - -template -struct Types16 { - typedef T1 Head; - typedef Types15 Tail; -}; - -template -struct Types17 { - typedef T1 Head; - typedef Types16 Tail; -}; - -template -struct Types18 { - typedef T1 Head; - typedef Types17 Tail; -}; - -template -struct Types19 { - typedef T1 Head; - typedef Types18 Tail; -}; - -template -struct Types20 { - typedef T1 Head; - typedef Types19 Tail; -}; - -template -struct Types21 { - typedef T1 Head; - typedef Types20 Tail; -}; - -template -struct Types22 { - typedef T1 Head; - typedef Types21 Tail; -}; - -template -struct Types23 { - typedef T1 Head; - typedef Types22 Tail; -}; - -template -struct Types24 { - typedef T1 Head; - typedef Types23 Tail; -}; - -template -struct Types25 { - typedef T1 Head; - typedef Types24 Tail; -}; - -template -struct Types26 { - typedef T1 Head; - typedef Types25 Tail; -}; - -template -struct Types27 { - typedef T1 Head; - typedef Types26 Tail; -}; - -template -struct Types28 { - typedef T1 Head; - typedef Types27 Tail; -}; - -template -struct Types29 { - typedef T1 Head; - typedef Types28 Tail; -}; - -template -struct Types30 { - typedef T1 Head; - typedef Types29 Tail; -}; - -template -struct Types31 { - typedef T1 Head; - typedef Types30 Tail; -}; - -template -struct Types32 { - typedef T1 Head; - typedef Types31 Tail; -}; - -template -struct Types33 { - typedef T1 Head; - typedef Types32 Tail; -}; - -template -struct Types34 { - typedef T1 Head; - typedef Types33 Tail; -}; - -template -struct Types35 { - typedef T1 Head; - typedef Types34 Tail; -}; - -template -struct Types36 { - typedef T1 Head; - typedef Types35 Tail; -}; - -template -struct Types37 { - typedef T1 Head; - typedef Types36 Tail; -}; - -template -struct Types38 { - typedef T1 Head; - typedef Types37 Tail; -}; - -template -struct Types39 { - typedef T1 Head; - typedef Types38 Tail; -}; - -template -struct Types40 { - typedef T1 Head; - typedef Types39 Tail; -}; - -template -struct Types41 { - typedef T1 Head; - typedef Types40 Tail; -}; - -template -struct Types42 { - typedef T1 Head; - typedef Types41 Tail; -}; - -template -struct Types43 { - typedef T1 Head; - typedef Types42 Tail; -}; - -template -struct Types44 { - typedef T1 Head; - typedef Types43 Tail; -}; - -template -struct Types45 { - typedef T1 Head; - typedef Types44 Tail; -}; - -template -struct Types46 { - typedef T1 Head; - typedef Types45 Tail; -}; - -template -struct Types47 { - typedef T1 Head; - typedef Types46 Tail; -}; - -template -struct Types48 { - typedef T1 Head; - typedef Types47 Tail; -}; - -template -struct Types49 { - typedef T1 Head; - typedef Types48 Tail; -}; - -template -struct Types50 { - typedef T1 Head; - typedef Types49 Tail; -}; - - -} // namespace internal - -// We don't want to require the users to write TypesN<...> directly, -// as that would require them to count the length. Types<...> is much -// easier to write, but generates horrible messages when there is a -// compiler error, as gcc insists on printing out each template -// argument, even if it has the default value (this means Types -// will appear as Types in the compiler -// errors). -// -// Our solution is to combine the best part of the two approaches: a -// user would write Types, and Google Test will translate -// that to TypesN internally to make error messages -// readable. The translation is done by the 'type' member of the -// Types template. -template -struct Types { - typedef internal::Types50 type; -}; - -template <> -struct Types { - typedef internal::Types0 type; -}; -template -struct Types { - typedef internal::Types1 type; -}; -template -struct Types { - typedef internal::Types2 type; -}; -template -struct Types { - typedef internal::Types3 type; -}; -template -struct Types { - typedef internal::Types4 type; -}; -template -struct Types { - typedef internal::Types5 type; -}; -template -struct Types { - typedef internal::Types6 type; -}; -template -struct Types { - typedef internal::Types7 type; -}; -template -struct Types { - typedef internal::Types8 type; -}; -template -struct Types { - typedef internal::Types9 type; -}; -template -struct Types { - typedef internal::Types10 type; -}; -template -struct Types { - typedef internal::Types11 type; -}; -template -struct Types { - typedef internal::Types12 type; -}; -template -struct Types { - typedef internal::Types13 type; -}; -template -struct Types { - typedef internal::Types14 type; -}; -template -struct Types { - typedef internal::Types15 type; -}; -template -struct Types { - typedef internal::Types16 type; -}; -template -struct Types { - typedef internal::Types17 type; -}; -template -struct Types { - typedef internal::Types18 type; -}; -template -struct Types { - typedef internal::Types19 type; -}; -template -struct Types { - typedef internal::Types20 type; -}; -template -struct Types { - typedef internal::Types21 type; -}; -template -struct Types { - typedef internal::Types22 type; -}; -template -struct Types { - typedef internal::Types23 type; -}; -template -struct Types { - typedef internal::Types24 type; -}; -template -struct Types { - typedef internal::Types25 type; -}; -template -struct Types { - typedef internal::Types26 type; -}; -template -struct Types { - typedef internal::Types27 type; -}; -template -struct Types { - typedef internal::Types28 type; -}; -template -struct Types { - typedef internal::Types29 type; -}; -template -struct Types { - typedef internal::Types30 type; -}; -template -struct Types { - typedef internal::Types31 type; -}; -template -struct Types { - typedef internal::Types32 type; -}; -template -struct Types { - typedef internal::Types33 type; -}; -template -struct Types { - typedef internal::Types34 type; -}; -template -struct Types { - typedef internal::Types35 type; -}; -template -struct Types { - typedef internal::Types36 type; -}; -template -struct Types { - typedef internal::Types37 type; -}; -template -struct Types { - typedef internal::Types38 type; -}; -template -struct Types { - typedef internal::Types39 type; -}; -template -struct Types { - typedef internal::Types40 type; -}; -template -struct Types { - typedef internal::Types41 type; -}; -template -struct Types { - typedef internal::Types42 type; -}; -template -struct Types { - typedef internal::Types43 type; -}; -template -struct Types { - typedef internal::Types44 type; -}; -template -struct Types { - typedef internal::Types45 type; -}; -template -struct Types { - typedef internal::Types46 type; -}; -template -struct Types { - typedef internal::Types47 type; -}; -template -struct Types { - typedef internal::Types48 type; -}; -template -struct Types { - typedef internal::Types49 type; -}; - -namespace internal { - # define GTEST_TEMPLATE_ template class // The template "selector" struct TemplateSel is used to @@ -3298,38 +1781,55 @@ struct Templates type; }; -// The TypeList template makes it possible to use either a single type -// or a Types<...> list in TYPED_TEST_SUITE() and -// INSTANTIATE_TYPED_TEST_SUITE_P(). +// A unique type indicating an empty node +struct None {}; + +// Tuple-like type lists +template +struct Types { + using Head = Head_; + using Tail = Types; +}; + +template +struct Types { + using Head = Head_; + using Tail = None; +}; + +// Helper metafunctions to tell apart a single type from types +// generated by ::testing::Types +template +struct ProxyTypeList { + typedef Types type; +}; + +template +struct is_proxy_type_list : std::false_type {}; + +template +struct is_proxy_type_list> : std::true_type {}; +// Generator which conditionally creates type lists. +// It recognizes if a requested type list should be created +// and prevents creating a new type list nested within another one. template -struct TypeList { - typedef Types1 type; -}; - -template -struct TypeList > { - typedef typename Types::type type; +struct GenerateTypeList { + private: + using proxy = typename std::conditional::value, T, + ProxyTypeList>::type; + + public: + using type = typename proxy::type; }; #endif // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P } // namespace internal + +template +using Types = internal::ProxyTypeList; + } // namespace testing #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ diff --git a/googletest/include/gtest/internal/gtest-type-util.h.pump b/googletest/include/gtest/internal/gtest-type-util.h.pump index 5e31b7b..605b78f 100644 --- a/googletest/include/gtest/internal/gtest-type-util.h.pump +++ b/googletest/include/gtest/internal/gtest-type-util.h.pump @@ -33,8 +33,8 @@ $var n = 50 $$ Maximum length of type lists we want to support. // Type utilities needed for implementing typed and type-parameterized // tests. This file is generated by a SCRIPT. DO NOT EDIT BY HAND! // -// Currently we support at most $n types in a list, and at most $n -// type-parameterized tests in one type-parameterized test suite. +// Currently we support at most $n type-parameterized tests +// in one type-parameterized test suite. // Please contact googletestframework@googlegroups.com if you need // more. @@ -104,84 +104,6 @@ std::string GetTypeName() { #if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P -// A unique type used as the default value for the arguments of class -// template Types. This allows us to simulate variadic templates -// (e.g. Types, Type, and etc), which C++ doesn't -// support directly. -struct None {}; - -// The following family of struct and struct templates are used to -// represent type lists. In particular, TypesN -// represents a type list with N types (T1, T2, ..., and TN) in it. -// Except for Types0, every struct in the family has two member types: -// Head for the first type in the list, and Tail for the rest of the -// list. - -// The empty type list. -struct Types0 {}; - -// Type lists of length 1, 2, 3, and so on. - -template -struct Types1 { - typedef T1 Head; - typedef Types0 Tail; -}; - -$range i 2..n - -$for i [[ -$range j 1..i -$range k 2..i -template <$for j, [[typename T$j]]> -struct Types$i { - typedef T1 Head; - typedef Types$(i-1)<$for k, [[T$k]]> Tail; -}; - - -]] - -} // namespace internal - -// We don't want to require the users to write TypesN<...> directly, -// as that would require them to count the length. Types<...> is much -// easier to write, but generates horrible messages when there is a -// compiler error, as gcc insists on printing out each template -// argument, even if it has the default value (this means Types -// will appear as Types in the compiler -// errors). -// -// Our solution is to combine the best part of the two approaches: a -// user would write Types, and Google Test will translate -// that to TypesN internally to make error messages -// readable. The translation is done by the 'type' member of the -// Types template. - -$range i 1..n -template <$for i, [[typename T$i = internal::None]]> -struct Types { - typedef internal::Types$n<$for i, [[T$i]]> type; -}; - -template <> -struct Types<$for i, [[internal::None]]> { - typedef internal::Types0 type; -}; - -$range i 1..n-1 -$for i [[ -$range j 1..i -$range k i+1..n -template <$for j, [[typename T$j]]> -struct Types<$for j, [[T$j]]$for k[[, internal::None]]> { - typedef internal::Types$i<$for j, [[T$j]]> type; -}; - -]] - -namespace internal { - # define GTEST_TEMPLATE_ template class // The template "selector" struct TemplateSel is used to @@ -278,25 +200,55 @@ struct Templates<$for j, [[T$j]]$for k[[, NoneT]]> { ]] -// The TypeList template makes it possible to use either a single type -// or a Types<...> list in TYPED_TEST_SUITE() and -// INSTANTIATE_TYPED_TEST_SUITE_P(). +// A unique type indicating an empty node +struct None {}; -template -struct TypeList { - typedef Types1 type; +// Tuple-like type lists +template +struct Types { + using Head = Head_; + using Tail = Types; }; +template +struct Types { + using Head = Head_; + using Tail = None; +}; -$range i 1..n -template <$for i, [[typename T$i]]> -struct TypeList > { - typedef typename Types<$for i, [[T$i]]>::type type; +// Helper metafunctions to tell apart a single type from types +// generated by ::testing::Types +template +struct ProxyTypeList { + typedef Types type; +}; + +template +struct is_proxy_type_list : std::false_type {}; + +template +struct is_proxy_type_list> : std::true_type {}; + +// Generator which conditionally creates type lists. +// It recognizes if a requested type list should be created +// and prevents creating a new type list nested within another one. +template +struct GenerateTypeList { + private: + using proxy = typename std::conditional::value, T, + ProxyTypeList>::type; + + public: + using type = typename proxy::type; }; #endif // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P } // namespace internal + +template +using Types = internal::ProxyTypeList; + } // namespace testing #endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ diff --git a/googletest/test/gtest-unittest-api_test.cc b/googletest/test/gtest-unittest-api_test.cc index 480a41f..7d2d8cc 100644 --- a/googletest/test/gtest-unittest-api_test.cc +++ b/googletest/test/gtest-unittest-api_test.cc @@ -188,7 +188,7 @@ TEST(ApiTest, TestSuiteImmutableAccessorsWork) { ASSERT_TRUE(test_suite != nullptr); EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suite->name()); - EXPECT_STREQ(GetTypeName().c_str(), test_suite->type_param()); + EXPECT_STREQ(GetTypeName>().c_str(), test_suite->type_param()); EXPECT_TRUE(test_suite->should_run()); EXPECT_EQ(0, test_suite->disabled_test_count()); EXPECT_EQ(1, test_suite->test_to_run_count()); @@ -199,7 +199,7 @@ TEST(ApiTest, TestSuiteImmutableAccessorsWork) { EXPECT_STREQ("Dummy", tests[0]->name()); EXPECT_STREQ("TestSuiteWithCommentTest/0", tests[0]->test_suite_name()); EXPECT_TRUE(IsNull(tests[0]->value_param())); - EXPECT_STREQ(GetTypeName().c_str(), tests[0]->type_param()); + EXPECT_STREQ(GetTypeName>().c_str(), tests[0]->type_param()); EXPECT_TRUE(tests[0]->should_run()); delete[] tests; @@ -265,7 +265,8 @@ class FinalSuccessChecker : public Environment { #if GTEST_HAS_TYPED_TEST EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites[2]->name()); - EXPECT_STREQ(GetTypeName().c_str(), test_suites[2]->type_param()); + EXPECT_STREQ(GetTypeName>().c_str(), + test_suites[2]->type_param()); EXPECT_TRUE(test_suites[2]->should_run()); EXPECT_EQ(0, test_suites[2]->disabled_test_count()); ASSERT_EQ(1, test_suites[2]->total_test_count()); @@ -317,7 +318,7 @@ class FinalSuccessChecker : public Environment { EXPECT_STREQ("Dummy", tests[0]->name()); EXPECT_STREQ("TestSuiteWithCommentTest/0", tests[0]->test_suite_name()); EXPECT_TRUE(IsNull(tests[0]->value_param())); - EXPECT_STREQ(GetTypeName().c_str(), tests[0]->type_param()); + EXPECT_STREQ(GetTypeName>().c_str(), tests[0]->type_param()); EXPECT_TRUE(tests[0]->should_run()); EXPECT_TRUE(tests[0]->result()->Passed()); EXPECT_EQ(0, tests[0]->result()->test_property_count()); -- cgit v0.12 From e3a9a567d8264965054cbf715c7ccf1f82c13b17 Mon Sep 17 00:00:00 2001 From: Krystian Kuzniarek Date: Thu, 8 Aug 2019 20:23:58 +0200 Subject: replace autogenerated TemplatesX classes by variadic ones --- googletest/include/gtest/gtest-typed-test.h | 14 +- googletest/include/gtest/internal/gtest-internal.h | 2 +- .../include/gtest/internal/gtest-type-util.h | 1667 +------------------- .../include/gtest/internal/gtest-type-util.h.pump | 88 +- 4 files changed, 28 insertions(+), 1743 deletions(-) diff --git a/googletest/include/gtest/gtest-typed-test.h b/googletest/include/gtest/gtest-typed-test.h index 3848044..151fc8f 100644 --- a/googletest/include/gtest/gtest-typed-test.h +++ b/googletest/include/gtest/gtest-typed-test.h @@ -284,13 +284,13 @@ INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes); void GTEST_SUITE_NAMESPACE_( \ SuiteName)::TestName::TestBody() -#define REGISTER_TYPED_TEST_SUITE_P(SuiteName, ...) \ - namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \ - typedef ::testing::internal::Templates<__VA_ARGS__>::type gtest_AllTests_; \ - } \ - static const char* const GTEST_REGISTERED_TEST_NAMES_( \ - SuiteName) GTEST_ATTRIBUTE_UNUSED_ = \ - GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).VerifyRegisteredTestNames( \ +#define REGISTER_TYPED_TEST_SUITE_P(SuiteName, ...) \ + namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \ + typedef ::testing::internal::Templates<__VA_ARGS__> gtest_AllTests_; \ + } \ + static const char* const GTEST_REGISTERED_TEST_NAMES_( \ + SuiteName) GTEST_ATTRIBUTE_UNUSED_ = \ + GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).VerifyRegisteredTestNames( \ __FILE__, __LINE__, #__VA_ARGS__) // Legacy API is deprecated but still available diff --git a/googletest/include/gtest/internal/gtest-internal.h b/googletest/include/gtest/internal/gtest-internal.h index 2056561..edf0afa 100644 --- a/googletest/include/gtest/internal/gtest-internal.h +++ b/googletest/include/gtest/internal/gtest-internal.h @@ -781,7 +781,7 @@ class TypeParameterizedTestSuite { // The base case for the compile time recursion. template -class TypeParameterizedTestSuite { +class TypeParameterizedTestSuite { public: static bool Register(const char* /*prefix*/, const CodeLocation&, const TypedTestSuitePState* /*state*/, diff --git a/googletest/include/gtest/internal/gtest-type-util.h b/googletest/include/gtest/internal/gtest-type-util.h index 001fadb..3f4c82a 100644 --- a/googletest/include/gtest/internal/gtest-type-util.h +++ b/googletest/include/gtest/internal/gtest-type-util.h @@ -34,11 +34,6 @@ // Type utilities needed for implementing typed and type-parameterized // tests. This file is generated by a SCRIPT. DO NOT EDIT BY HAND! -// -// Currently we support at most 50 type-parameterized tests -// in one type-parameterized test suite. -// Please contact googletestframework@googlegroups.com if you need -// more. // GOOGLETEST_CM0001 DO NOT DELETE @@ -106,6 +101,9 @@ std::string GetTypeName() { #if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P +// A unique type indicating an empty node +struct None {}; + # define GTEST_TEMPLATE_ template class // The template "selector" struct TemplateSel is used to @@ -127,1662 +125,17 @@ struct TemplateSel { # define GTEST_BIND_(TmplSel, T) \ TmplSel::template Bind::type -// A unique struct template used as the default value for the -// arguments of class template Templates. This allows us to simulate -// variadic templates (e.g. Templates, Templates, -// and etc), which C++ doesn't support directly. -template -struct NoneT {}; - -// The following family of struct and struct templates are used to -// represent template lists. In particular, TemplatesN represents a list of N templates (T1, T2, ..., and TN). Except -// for Templates0, every struct in the family has two member types: -// Head for the selector of the first template in the list, and Tail -// for the rest of the list. - -// The empty template list. -struct Templates0 {}; - -// Template lists of length 1, 2, 3, and so on. - -template -struct Templates1 { - typedef TemplateSel Head; - typedef Templates0 Tail; -}; -template -struct Templates2 { - typedef TemplateSel Head; - typedef Templates1 Tail; -}; - -template -struct Templates3 { - typedef TemplateSel Head; - typedef Templates2 Tail; -}; - -template -struct Templates4 { - typedef TemplateSel Head; - typedef Templates3 Tail; -}; - -template -struct Templates5 { - typedef TemplateSel Head; - typedef Templates4 Tail; -}; - -template -struct Templates6 { - typedef TemplateSel Head; - typedef Templates5 Tail; -}; - -template -struct Templates7 { - typedef TemplateSel Head; - typedef Templates6 Tail; -}; - -template -struct Templates8 { - typedef TemplateSel Head; - typedef Templates7 Tail; -}; - -template -struct Templates9 { - typedef TemplateSel Head; - typedef Templates8 Tail; -}; - -template -struct Templates10 { - typedef TemplateSel Head; - typedef Templates9 Tail; -}; - -template -struct Templates11 { - typedef TemplateSel Head; - typedef Templates10 Tail; -}; - -template -struct Templates12 { - typedef TemplateSel Head; - typedef Templates11 Tail; -}; - -template -struct Templates13 { - typedef TemplateSel Head; - typedef Templates12 Tail; -}; - -template -struct Templates14 { - typedef TemplateSel Head; - typedef Templates13 Tail; -}; - -template -struct Templates15 { - typedef TemplateSel Head; - typedef Templates14 Tail; -}; - -template -struct Templates16 { - typedef TemplateSel Head; - typedef Templates15 Tail; -}; - -template -struct Templates17 { - typedef TemplateSel Head; - typedef Templates16 Tail; -}; - -template -struct Templates18 { - typedef TemplateSel Head; - typedef Templates17 Tail; -}; - -template -struct Templates19 { - typedef TemplateSel Head; - typedef Templates18 Tail; -}; - -template -struct Templates20 { - typedef TemplateSel Head; - typedef Templates19 Tail; -}; - -template -struct Templates21 { - typedef TemplateSel Head; - typedef Templates20 Tail; -}; - -template -struct Templates22 { - typedef TemplateSel Head; - typedef Templates21 Tail; -}; - -template -struct Templates23 { - typedef TemplateSel Head; - typedef Templates22 Tail; -}; - -template -struct Templates24 { - typedef TemplateSel Head; - typedef Templates23 Tail; -}; - -template -struct Templates25 { - typedef TemplateSel Head; - typedef Templates24 Tail; -}; - -template -struct Templates26 { - typedef TemplateSel Head; - typedef Templates25 Tail; -}; - -template -struct Templates27 { - typedef TemplateSel Head; - typedef Templates26 Tail; -}; - -template -struct Templates28 { - typedef TemplateSel Head; - typedef Templates27 Tail; -}; - -template -struct Templates29 { - typedef TemplateSel Head; - typedef Templates28 Tail; -}; - -template -struct Templates30 { - typedef TemplateSel Head; - typedef Templates29 Tail; -}; - -template -struct Templates31 { - typedef TemplateSel Head; - typedef Templates30 Tail; -}; - -template -struct Templates32 { - typedef TemplateSel Head; - typedef Templates31 Tail; -}; - -template -struct Templates33 { - typedef TemplateSel Head; - typedef Templates32 Tail; -}; - -template -struct Templates34 { - typedef TemplateSel Head; - typedef Templates33 Tail; -}; - -template -struct Templates35 { - typedef TemplateSel Head; - typedef Templates34 Tail; -}; - -template -struct Templates36 { - typedef TemplateSel Head; - typedef Templates35 Tail; -}; - -template -struct Templates37 { - typedef TemplateSel Head; - typedef Templates36 Tail; -}; - -template -struct Templates38 { - typedef TemplateSel Head; - typedef Templates37 Tail; -}; - -template -struct Templates39 { - typedef TemplateSel Head; - typedef Templates38 Tail; -}; - -template -struct Templates40 { - typedef TemplateSel Head; - typedef Templates39 Tail; -}; - -template -struct Templates41 { - typedef TemplateSel Head; - typedef Templates40 Tail; -}; - -template -struct Templates42 { - typedef TemplateSel Head; - typedef Templates41 Tail; -}; - -template -struct Templates43 { - typedef TemplateSel Head; - typedef Templates42 Tail; -}; - -template -struct Templates44 { - typedef TemplateSel Head; - typedef Templates43 Tail; -}; - -template -struct Templates45 { - typedef TemplateSel Head; - typedef Templates44 Tail; -}; - -template -struct Templates46 { - typedef TemplateSel Head; - typedef Templates45 Tail; -}; - -template -struct Templates47 { - typedef TemplateSel Head; - typedef Templates46 Tail; -}; - -template -struct Templates48 { - typedef TemplateSel Head; - typedef Templates47 Tail; -}; - -template -struct Templates49 { - typedef TemplateSel Head; - typedef Templates48 Tail; -}; - -template -struct Templates50 { - typedef TemplateSel Head; - typedef Templates49 Tail; -}; - - -// We don't want to require the users to write TemplatesN<...> directly, -// as that would require them to count the length. Templates<...> is much -// easier to write, but generates horrible messages when there is a -// compiler error, as gcc insists on printing out each template -// argument, even if it has the default value (this means Templates -// will appear as Templates in the compiler -// errors). -// -// Our solution is to combine the best part of the two approaches: a -// user would write Templates, and Google Test will translate -// that to TemplatesN internally to make error messages -// readable. The translation is done by the 'type' member of the -// Templates template. -template +template struct Templates { - typedef Templates50 type; + using Head = TemplateSel; + using Tail = Templates; }; -template <> -struct Templates { - typedef Templates0 type; -}; -template -struct Templates { - typedef Templates1 type; -}; -template -struct Templates { - typedef Templates2 type; -}; -template -struct Templates { - typedef Templates3 type; -}; -template -struct Templates { - typedef Templates4 type; -}; -template -struct Templates { - typedef Templates5 type; -}; -template -struct Templates { - typedef Templates6 type; -}; -template -struct Templates { - typedef Templates7 type; -}; -template -struct Templates { - typedef Templates8 type; -}; -template -struct Templates { - typedef Templates9 type; -}; -template -struct Templates { - typedef Templates10 type; -}; -template -struct Templates { - typedef Templates11 type; -}; -template -struct Templates { - typedef Templates12 type; +template +struct Templates { + typedef TemplateSel Head; + typedef None Tail; }; -template -struct Templates { - typedef Templates13 type; -}; -template -struct Templates { - typedef Templates14 type; -}; -template -struct Templates { - typedef Templates15 type; -}; -template -struct Templates { - typedef Templates16 type; -}; -template -struct Templates { - typedef Templates17 type; -}; -template -struct Templates { - typedef Templates18 type; -}; -template -struct Templates { - typedef Templates19 type; -}; -template -struct Templates { - typedef Templates20 type; -}; -template -struct Templates { - typedef Templates21 type; -}; -template -struct Templates { - typedef Templates22 type; -}; -template -struct Templates { - typedef Templates23 type; -}; -template -struct Templates { - typedef Templates24 type; -}; -template -struct Templates { - typedef Templates25 type; -}; -template -struct Templates { - typedef Templates26 type; -}; -template -struct Templates { - typedef Templates27 type; -}; -template -struct Templates { - typedef Templates28 type; -}; -template -struct Templates { - typedef Templates29 type; -}; -template -struct Templates { - typedef Templates30 type; -}; -template -struct Templates { - typedef Templates31 type; -}; -template -struct Templates { - typedef Templates32 type; -}; -template -struct Templates { - typedef Templates33 type; -}; -template -struct Templates { - typedef Templates34 type; -}; -template -struct Templates { - typedef Templates35 type; -}; -template -struct Templates { - typedef Templates36 type; -}; -template -struct Templates { - typedef Templates37 type; -}; -template -struct Templates { - typedef Templates38 type; -}; -template -struct Templates { - typedef Templates39 type; -}; -template -struct Templates { - typedef Templates40 type; -}; -template -struct Templates { - typedef Templates41 type; -}; -template -struct Templates { - typedef Templates42 type; -}; -template -struct Templates { - typedef Templates43 type; -}; -template -struct Templates { - typedef Templates44 type; -}; -template -struct Templates { - typedef Templates45 type; -}; -template -struct Templates { - typedef Templates46 type; -}; -template -struct Templates { - typedef Templates47 type; -}; -template -struct Templates { - typedef Templates48 type; -}; -template -struct Templates { - typedef Templates49 type; -}; - -// A unique type indicating an empty node -struct None {}; // Tuple-like type lists template diff --git a/googletest/include/gtest/internal/gtest-type-util.h.pump b/googletest/include/gtest/internal/gtest-type-util.h.pump index 605b78f..dcfde18 100644 --- a/googletest/include/gtest/internal/gtest-type-util.h.pump +++ b/googletest/include/gtest/internal/gtest-type-util.h.pump @@ -32,11 +32,6 @@ $var n = 50 $$ Maximum length of type lists we want to support. // Type utilities needed for implementing typed and type-parameterized // tests. This file is generated by a SCRIPT. DO NOT EDIT BY HAND! -// -// Currently we support at most $n type-parameterized tests -// in one type-parameterized test suite. -// Please contact googletestframework@googlegroups.com if you need -// more. // GOOGLETEST_CM0001 DO NOT DELETE @@ -104,6 +99,9 @@ std::string GetTypeName() { #if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P +// A unique type indicating an empty node +struct None {}; + # define GTEST_TEMPLATE_ template class // The template "selector" struct TemplateSel is used to @@ -125,84 +123,18 @@ struct TemplateSel { # define GTEST_BIND_(TmplSel, T) \ TmplSel::template Bind::type -// A unique struct template used as the default value for the -// arguments of class template Templates. This allows us to simulate -// variadic templates (e.g. Templates, Templates, -// and etc), which C++ doesn't support directly. -template -struct NoneT {}; - -// The following family of struct and struct templates are used to -// represent template lists. In particular, TemplatesN represents a list of N templates (T1, T2, ..., and TN). Except -// for Templates0, every struct in the family has two member types: -// Head for the selector of the first template in the list, and Tail -// for the rest of the list. - -// The empty template list. -struct Templates0 {}; - -// Template lists of length 1, 2, 3, and so on. - -template -struct Templates1 { - typedef TemplateSel Head; - typedef Templates0 Tail; -}; - -$range i 2..n - -$for i [[ -$range j 1..i -$range k 2..i -template <$for j, [[GTEST_TEMPLATE_ T$j]]> -struct Templates$i { - typedef TemplateSel Head; - typedef Templates$(i-1)<$for k, [[T$k]]> Tail; -}; - - -]] - -// We don't want to require the users to write TemplatesN<...> directly, -// as that would require them to count the length. Templates<...> is much -// easier to write, but generates horrible messages when there is a -// compiler error, as gcc insists on printing out each template -// argument, even if it has the default value (this means Templates -// will appear as Templates in the compiler -// errors). -// -// Our solution is to combine the best part of the two approaches: a -// user would write Templates, and Google Test will translate -// that to TemplatesN internally to make error messages -// readable. The translation is done by the 'type' member of the -// Templates template. - -$range i 1..n -template <$for i, [[GTEST_TEMPLATE_ T$i = NoneT]]> +template struct Templates { - typedef Templates$n<$for i, [[T$i]]> type; + using Head = TemplateSel; + using Tail = Templates; }; -template <> -struct Templates<$for i, [[NoneT]]> { - typedef Templates0 type; +template +struct Templates { + typedef TemplateSel Head; + typedef None Tail; }; -$range i 1..n-1 -$for i [[ -$range j 1..i -$range k i+1..n -template <$for j, [[GTEST_TEMPLATE_ T$j]]> -struct Templates<$for j, [[T$j]]$for k[[, NoneT]]> { - typedef Templates$i<$for j, [[T$j]]> type; -}; - -]] - -// A unique type indicating an empty node -struct None {}; - // Tuple-like type lists template struct Types { -- cgit v0.12 From a7083564d55078e7bed7255a6aa2e52cdb96b557 Mon Sep 17 00:00:00 2001 From: Krystian Kuzniarek Date: Thu, 8 Aug 2019 20:33:52 +0200 Subject: remove gtest-type-util.h.pump --- CONTRIBUTING.md | 4 +- .../include/gtest/internal/gtest-type-util.h | 7 +- .../include/gtest/internal/gtest-type-util.h.pump | 186 --------------------- 3 files changed, 3 insertions(+), 194 deletions(-) delete mode 100644 googletest/include/gtest/internal/gtest-type-util.h.pump diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 30c8d89..b9c14e9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -133,8 +133,8 @@ All tests should pass. Some of Google Test's source files are generated from templates (not in the C++ sense) using a script. For example, the file -include/gtest/internal/gtest-type-util.h.pump is used to generate -gtest-type-util.h in the same directory. +*googlemock/include/gmock/gmock-generated-actions.h.pump* is used to generate +*gmock-generated-actions.h* in the same directory. You don't need to worry about regenerating the source files unless you need to modify them. You would then modify the corresponding `.pump` files and run the diff --git a/googletest/include/gtest/internal/gtest-type-util.h b/googletest/include/gtest/internal/gtest-type-util.h index 3f4c82a..848c756 100644 --- a/googletest/include/gtest/internal/gtest-type-util.h +++ b/googletest/include/gtest/internal/gtest-type-util.h @@ -1,7 +1,3 @@ -// This file was GENERATED by command: -// pump.py gtest-type-util.h.pump -// DO NOT EDIT BY HAND!!! - // Copyright 2008 Google Inc. // All Rights Reserved. // @@ -31,9 +27,8 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - // Type utilities needed for implementing typed and type-parameterized -// tests. This file is generated by a SCRIPT. DO NOT EDIT BY HAND! +// tests. // GOOGLETEST_CM0001 DO NOT DELETE diff --git a/googletest/include/gtest/internal/gtest-type-util.h.pump b/googletest/include/gtest/internal/gtest-type-util.h.pump deleted file mode 100644 index dcfde18..0000000 --- a/googletest/include/gtest/internal/gtest-type-util.h.pump +++ /dev/null @@ -1,186 +0,0 @@ -$$ -*- mode: c++; -*- -$var n = 50 $$ Maximum length of type lists we want to support. -// Copyright 2008 Google Inc. -// All Rights Reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - -// Type utilities needed for implementing typed and type-parameterized -// tests. This file is generated by a SCRIPT. DO NOT EDIT BY HAND! - -// GOOGLETEST_CM0001 DO NOT DELETE - -#ifndef GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ -#define GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ - -#include "gtest/internal/gtest-port.h" - -// #ifdef __GNUC__ is too general here. It is possible to use gcc without using -// libstdc++ (which is where cxxabi.h comes from). -# if GTEST_HAS_CXXABI_H_ -# include -# elif defined(__HP_aCC) -# include -# endif // GTEST_HASH_CXXABI_H_ - -namespace testing { -namespace internal { - -// Canonicalizes a given name with respect to the Standard C++ Library. -// This handles removing the inline namespace within `std` that is -// used by various standard libraries (e.g., `std::__1`). Names outside -// of namespace std are returned unmodified. -inline std::string CanonicalizeForStdLibVersioning(std::string s) { - static const char prefix[] = "std::__"; - if (s.compare(0, strlen(prefix), prefix) == 0) { - std::string::size_type end = s.find("::", strlen(prefix)); - if (end != s.npos) { - // Erase everything between the initial `std` and the second `::`. - s.erase(strlen("std"), end - strlen("std")); - } - } - return s; -} - -// GetTypeName() returns a human-readable name of type T. -// NB: This function is also used in Google Mock, so don't move it inside of -// the typed-test-only section below. -template -std::string GetTypeName() { -# if GTEST_HAS_RTTI - - const char* const name = typeid(T).name(); -# if GTEST_HAS_CXXABI_H_ || defined(__HP_aCC) - int status = 0; - // gcc's implementation of typeid(T).name() mangles the type name, - // so we have to demangle it. -# if GTEST_HAS_CXXABI_H_ - using abi::__cxa_demangle; -# endif // GTEST_HAS_CXXABI_H_ - char* const readable_name = __cxa_demangle(name, nullptr, nullptr, &status); - const std::string name_str(status == 0 ? readable_name : name); - free(readable_name); - return CanonicalizeForStdLibVersioning(name_str); -# else - return name; -# endif // GTEST_HAS_CXXABI_H_ || __HP_aCC - -# else - - return ""; - -# endif // GTEST_HAS_RTTI -} - -#if GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P - -// A unique type indicating an empty node -struct None {}; - -# define GTEST_TEMPLATE_ template class - -// The template "selector" struct TemplateSel is used to -// represent Tmpl, which must be a class template with one type -// parameter, as a type. TemplateSel::Bind::type is defined -// as the type Tmpl. This allows us to actually instantiate the -// template "selected" by TemplateSel. -// -// This trick is necessary for simulating typedef for class templates, -// which C++ doesn't support directly. -template -struct TemplateSel { - template - struct Bind { - typedef Tmpl type; - }; -}; - -# define GTEST_BIND_(TmplSel, T) \ - TmplSel::template Bind::type - -template -struct Templates { - using Head = TemplateSel; - using Tail = Templates; -}; - -template -struct Templates { - typedef TemplateSel Head; - typedef None Tail; -}; - -// Tuple-like type lists -template -struct Types { - using Head = Head_; - using Tail = Types; -}; - -template -struct Types { - using Head = Head_; - using Tail = None; -}; - -// Helper metafunctions to tell apart a single type from types -// generated by ::testing::Types -template -struct ProxyTypeList { - typedef Types type; -}; - -template -struct is_proxy_type_list : std::false_type {}; - -template -struct is_proxy_type_list> : std::true_type {}; - -// Generator which conditionally creates type lists. -// It recognizes if a requested type list should be created -// and prevents creating a new type list nested within another one. -template -struct GenerateTypeList { - private: - using proxy = typename std::conditional::value, T, - ProxyTypeList>::type; - - public: - using type = typename proxy::type; -}; - -#endif // GTEST_HAS_TYPED_TEST || GTEST_HAS_TYPED_TEST_P - -} // namespace internal - -template -using Types = internal::ProxyTypeList; - -} // namespace testing - -#endif // GTEST_INCLUDE_GTEST_INTERNAL_GTEST_TYPE_UTIL_H_ -- cgit v0.12 From 002905f29fc910c7d9756ae70edeffb96a21585a Mon Sep 17 00:00:00 2001 From: Krystian Kuzniarek Date: Thu, 8 Aug 2019 20:39:33 +0200 Subject: move the pumping script to googlemock --- CONTRIBUTING.md | 4 +- googlemock/docs/pump_manual.md | 190 +++++++++ googlemock/scripts/pump.py | 855 +++++++++++++++++++++++++++++++++++++++++ googletest/docs/pump_manual.md | 190 --------- googletest/scripts/pump.py | 855 ----------------------------------------- 5 files changed, 1047 insertions(+), 1047 deletions(-) create mode 100644 googlemock/docs/pump_manual.md create mode 100755 googlemock/scripts/pump.py delete mode 100644 googletest/docs/pump_manual.md delete mode 100755 googletest/scripts/pump.py diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b9c14e9..4c18499 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -138,5 +138,5 @@ sense) using a script. For example, the file You don't need to worry about regenerating the source files unless you need to modify them. You would then modify the corresponding `.pump` files and run the -'[pump.py](googletest/scripts/pump.py)' generator script. See the -[Pump Manual](googletest/docs/pump_manual.md). +'[pump.py](googlemock/scripts/pump.py)' generator script. See the +[Pump Manual](googlemock/docs/pump_manual.md). diff --git a/googlemock/docs/pump_manual.md b/googlemock/docs/pump_manual.md new file mode 100644 index 0000000..10b3c5f --- /dev/null +++ b/googlemock/docs/pump_manual.md @@ -0,0 +1,190 @@ +Pump is Useful for Meta Programming. + +# The Problem + +Template and macro libraries often need to define many classes, functions, or +macros that vary only (or almost only) in the number of arguments they take. +It's a lot of repetitive, mechanical, and error-prone work. + +Variadic templates and variadic macros can alleviate the problem. However, while +both are being considered by the C++ committee, neither is in the standard yet +or widely supported by compilers. Thus they are often not a good choice, +especially when your code needs to be portable. And their capabilities are still +limited. + +As a result, authors of such libraries often have to write scripts to generate +their implementation. However, our experience is that it's tedious to write such +scripts, which tend to reflect the structure of the generated code poorly and +are often hard to read and edit. For example, a small change needed in the +generated code may require some non-intuitive, non-trivial changes in the +script. This is especially painful when experimenting with the code. + +# Our Solution + +Pump (for Pump is Useful for Meta Programming, Pretty Useful for Meta +Programming, or Practical Utility for Meta Programming, whichever you prefer) is +a simple meta-programming tool for C++. The idea is that a programmer writes a +`foo.pump` file which contains C++ code plus meta code that manipulates the C++ +code. The meta code can handle iterations over a range, nested iterations, local +meta variable definitions, simple arithmetic, and conditional expressions. You +can view it as a small Domain-Specific Language. The meta language is designed +to be non-intrusive (s.t. it won't confuse Emacs' C++ mode, for example) and +concise, making Pump code intuitive and easy to maintain. + +## Highlights + +* The implementation is in a single Python script and thus ultra portable: no + build or installation is needed and it works cross platforms. +* Pump tries to be smart with respect to + [Google's style guide](https://github.com/google/styleguide): it breaks long + lines (easy to have when they are generated) at acceptable places to fit + within 80 columns and indent the continuation lines correctly. +* The format is human-readable and more concise than XML. +* The format works relatively well with Emacs' C++ mode. + +## Examples + +The following Pump code (where meta keywords start with `$`, `[[` and `]]` are +meta brackets, and `$$` starts a meta comment that ends with the line): + +``` +$var n = 3 $$ Defines a meta variable n. +$range i 0..n $$ Declares the range of meta iterator i (inclusive). +$for i [[ + $$ Meta loop. +// Foo$i does blah for $i-ary predicates. +$range j 1..i +template +class Foo$i { +$if i == 0 [[ + blah a; +]] $elif i <= 2 [[ + blah b; +]] $else [[ + blah c; +]] +}; + +]] +``` + +will be translated by the Pump compiler to: + +```cpp +// Foo0 does blah for 0-ary predicates. +template +class Foo0 { + blah a; +}; + +// Foo1 does blah for 1-ary predicates. +template +class Foo1 { + blah b; +}; + +// Foo2 does blah for 2-ary predicates. +template +class Foo2 { + blah b; +}; + +// Foo3 does blah for 3-ary predicates. +template +class Foo3 { + blah c; +}; +``` + +In another example, + +``` +$range i 1..n +Func($for i + [[a$i]]); +$$ The text between i and [[ is the separator between iterations. +``` + +will generate one of the following lines (without the comments), depending on +the value of `n`: + +```cpp +Func(); // If n is 0. +Func(a1); // If n is 1. +Func(a1 + a2); // If n is 2. +Func(a1 + a2 + a3); // If n is 3. +// And so on... +``` + +## Constructs + +We support the following meta programming constructs: + +| `$var id = exp` | Defines a named constant value. `$id` is | +: : valid util the end of the current meta : +: : lexical block. : +| :------------------------------- | :--------------------------------------- | +| `$range id exp..exp` | Sets the range of an iteration variable, | +: : which can be reused in multiple loops : +: : later. : +| `$for id sep [[ code ]]` | Iteration. The range of `id` must have | +: : been defined earlier. `$id` is valid in : +: : `code`. : +| `$($)` | Generates a single `$` character. | +| `$id` | Value of the named constant or iteration | +: : variable. : +| `$(exp)` | Value of the expression. | +| `$if exp [[ code ]] else_branch` | Conditional. | +| `[[ code ]]` | Meta lexical block. | +| `cpp_code` | Raw C++ code. | +| `$$ comment` | Meta comment. | + +**Note:** To give the user some freedom in formatting the Pump source code, Pump +ignores a new-line character if it's right after `$for foo` or next to `[[` or +`]]`. Without this rule you'll often be forced to write very long lines to get +the desired output. Therefore sometimes you may need to insert an extra new-line +in such places for a new-line to show up in your output. + +## Grammar + +```ebnf +code ::= atomic_code* +atomic_code ::= $var id = exp + | $var id = [[ code ]] + | $range id exp..exp + | $for id sep [[ code ]] + | $($) + | $id + | $(exp) + | $if exp [[ code ]] else_branch + | [[ code ]] + | cpp_code +sep ::= cpp_code | empty_string +else_branch ::= $else [[ code ]] + | $elif exp [[ code ]] else_branch + | empty_string +exp ::= simple_expression_in_Python_syntax +``` + +## Code + +You can find the source code of Pump in [scripts/pump.py](../scripts/pump.py). +It is still very unpolished and lacks automated tests, although it has been +successfully used many times. If you find a chance to use it in your project, +please let us know what you think! We also welcome help on improving Pump. + +## Real Examples + +You can find real-world applications of Pump in +[Google Test](https://github.com/google/googletest/tree/master/googletest) and +[Google Mock](https://github.com/google/googletest/tree/master/googlemock). The +source file `foo.h.pump` generates `foo.h`. + +## Tips + +* If a meta variable is followed by a letter or digit, you can separate them + using `[[]]`, which inserts an empty string. For example `Foo$j[[]]Helper` + generate `Foo1Helper` when `j` is 1. +* To avoid extra-long Pump source lines, you can break a line anywhere you + want by inserting `[[]]` followed by a new line. Since any new-line + character next to `[[` or `]]` is ignored, the generated code won't contain + this new line. diff --git a/googlemock/scripts/pump.py b/googlemock/scripts/pump.py new file mode 100755 index 0000000..66e3217 --- /dev/null +++ b/googlemock/scripts/pump.py @@ -0,0 +1,855 @@ +#!/usr/bin/env python2.7 +# +# Copyright 2008, Google Inc. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following disclaimer +# in the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Google Inc. nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +"""pump v0.2.0 - Pretty Useful for Meta Programming. + +A tool for preprocessor meta programming. Useful for generating +repetitive boilerplate code. Especially useful for writing C++ +classes, functions, macros, and templates that need to work with +various number of arguments. + +USAGE: + pump.py SOURCE_FILE + +EXAMPLES: + pump.py foo.cc.pump + Converts foo.cc.pump to foo.cc. + +GRAMMAR: + CODE ::= ATOMIC_CODE* + ATOMIC_CODE ::= $var ID = EXPRESSION + | $var ID = [[ CODE ]] + | $range ID EXPRESSION..EXPRESSION + | $for ID SEPARATOR [[ CODE ]] + | $($) + | $ID + | $(EXPRESSION) + | $if EXPRESSION [[ CODE ]] ELSE_BRANCH + | [[ CODE ]] + | RAW_CODE + SEPARATOR ::= RAW_CODE | EMPTY + ELSE_BRANCH ::= $else [[ CODE ]] + | $elif EXPRESSION [[ CODE ]] ELSE_BRANCH + | EMPTY + EXPRESSION has Python syntax. +""" + +from __future__ import print_function + +import os +import re +import sys + + +TOKEN_TABLE = [ + (re.compile(r'\$var\s+'), '$var'), + (re.compile(r'\$elif\s+'), '$elif'), + (re.compile(r'\$else\s+'), '$else'), + (re.compile(r'\$for\s+'), '$for'), + (re.compile(r'\$if\s+'), '$if'), + (re.compile(r'\$range\s+'), '$range'), + (re.compile(r'\$[_A-Za-z]\w*'), '$id'), + (re.compile(r'\$\(\$\)'), '$($)'), + (re.compile(r'\$'), '$'), + (re.compile(r'\[\[\n?'), '[['), + (re.compile(r'\]\]\n?'), ']]'), + ] + + +class Cursor: + """Represents a position (line and column) in a text file.""" + + def __init__(self, line=-1, column=-1): + self.line = line + self.column = column + + def __eq__(self, rhs): + return self.line == rhs.line and self.column == rhs.column + + def __ne__(self, rhs): + return not self == rhs + + def __lt__(self, rhs): + return self.line < rhs.line or ( + self.line == rhs.line and self.column < rhs.column) + + def __le__(self, rhs): + return self < rhs or self == rhs + + def __gt__(self, rhs): + return rhs < self + + def __ge__(self, rhs): + return rhs <= self + + def __str__(self): + if self == Eof(): + return 'EOF' + else: + return '%s(%s)' % (self.line + 1, self.column) + + def __add__(self, offset): + return Cursor(self.line, self.column + offset) + + def __sub__(self, offset): + return Cursor(self.line, self.column - offset) + + def Clone(self): + """Returns a copy of self.""" + + return Cursor(self.line, self.column) + + +# Special cursor to indicate the end-of-file. +def Eof(): + """Returns the special cursor to denote the end-of-file.""" + return Cursor(-1, -1) + + +class Token: + """Represents a token in a Pump source file.""" + + def __init__(self, start=None, end=None, value=None, token_type=None): + if start is None: + self.start = Eof() + else: + self.start = start + if end is None: + self.end = Eof() + else: + self.end = end + self.value = value + self.token_type = token_type + + def __str__(self): + return 'Token @%s: \'%s\' type=%s' % ( + self.start, self.value, self.token_type) + + def Clone(self): + """Returns a copy of self.""" + + return Token(self.start.Clone(), self.end.Clone(), self.value, + self.token_type) + + +def StartsWith(lines, pos, string): + """Returns True iff the given position in lines starts with 'string'.""" + + return lines[pos.line][pos.column:].startswith(string) + + +def FindFirstInLine(line, token_table): + best_match_start = -1 + for (regex, token_type) in token_table: + m = regex.search(line) + if m: + # We found regex in lines + if best_match_start < 0 or m.start() < best_match_start: + best_match_start = m.start() + best_match_length = m.end() - m.start() + best_match_token_type = token_type + + if best_match_start < 0: + return None + + return (best_match_start, best_match_length, best_match_token_type) + + +def FindFirst(lines, token_table, cursor): + """Finds the first occurrence of any string in strings in lines.""" + + start = cursor.Clone() + cur_line_number = cursor.line + for line in lines[start.line:]: + if cur_line_number == start.line: + line = line[start.column:] + m = FindFirstInLine(line, token_table) + if m: + # We found a regex in line. + (start_column, length, token_type) = m + if cur_line_number == start.line: + start_column += start.column + found_start = Cursor(cur_line_number, start_column) + found_end = found_start + length + return MakeToken(lines, found_start, found_end, token_type) + cur_line_number += 1 + # We failed to find str in lines + return None + + +def SubString(lines, start, end): + """Returns a substring in lines.""" + + if end == Eof(): + end = Cursor(len(lines) - 1, len(lines[-1])) + + if start >= end: + return '' + + if start.line == end.line: + return lines[start.line][start.column:end.column] + + result_lines = ([lines[start.line][start.column:]] + + lines[start.line + 1:end.line] + + [lines[end.line][:end.column]]) + return ''.join(result_lines) + + +def StripMetaComments(str): + """Strip meta comments from each line in the given string.""" + + # First, completely remove lines containing nothing but a meta + # comment, including the trailing \n. + str = re.sub(r'^\s*\$\$.*\n', '', str) + + # Then, remove meta comments from contentful lines. + return re.sub(r'\s*\$\$.*', '', str) + + +def MakeToken(lines, start, end, token_type): + """Creates a new instance of Token.""" + + return Token(start, end, SubString(lines, start, end), token_type) + + +def ParseToken(lines, pos, regex, token_type): + line = lines[pos.line][pos.column:] + m = regex.search(line) + if m and not m.start(): + return MakeToken(lines, pos, pos + m.end(), token_type) + else: + print('ERROR: %s expected at %s.' % (token_type, pos)) + sys.exit(1) + + +ID_REGEX = re.compile(r'[_A-Za-z]\w*') +EQ_REGEX = re.compile(r'=') +REST_OF_LINE_REGEX = re.compile(r'.*?(?=$|\$\$)') +OPTIONAL_WHITE_SPACES_REGEX = re.compile(r'\s*') +WHITE_SPACE_REGEX = re.compile(r'\s') +DOT_DOT_REGEX = re.compile(r'\.\.') + + +def Skip(lines, pos, regex): + line = lines[pos.line][pos.column:] + m = re.search(regex, line) + if m and not m.start(): + return pos + m.end() + else: + return pos + + +def SkipUntil(lines, pos, regex, token_type): + line = lines[pos.line][pos.column:] + m = re.search(regex, line) + if m: + return pos + m.start() + else: + print ('ERROR: %s expected on line %s after column %s.' % + (token_type, pos.line + 1, pos.column)) + sys.exit(1) + + +def ParseExpTokenInParens(lines, pos): + def ParseInParens(pos): + pos = Skip(lines, pos, OPTIONAL_WHITE_SPACES_REGEX) + pos = Skip(lines, pos, r'\(') + pos = Parse(pos) + pos = Skip(lines, pos, r'\)') + return pos + + def Parse(pos): + pos = SkipUntil(lines, pos, r'\(|\)', ')') + if SubString(lines, pos, pos + 1) == '(': + pos = Parse(pos + 1) + pos = Skip(lines, pos, r'\)') + return Parse(pos) + else: + return pos + + start = pos.Clone() + pos = ParseInParens(pos) + return MakeToken(lines, start, pos, 'exp') + + +def RStripNewLineFromToken(token): + if token.value.endswith('\n'): + return Token(token.start, token.end, token.value[:-1], token.token_type) + else: + return token + + +def TokenizeLines(lines, pos): + while True: + found = FindFirst(lines, TOKEN_TABLE, pos) + if not found: + yield MakeToken(lines, pos, Eof(), 'code') + return + + if found.start == pos: + prev_token = None + prev_token_rstripped = None + else: + prev_token = MakeToken(lines, pos, found.start, 'code') + prev_token_rstripped = RStripNewLineFromToken(prev_token) + + if found.token_type == '$var': + if prev_token_rstripped: + yield prev_token_rstripped + yield found + id_token = ParseToken(lines, found.end, ID_REGEX, 'id') + yield id_token + pos = Skip(lines, id_token.end, OPTIONAL_WHITE_SPACES_REGEX) + + eq_token = ParseToken(lines, pos, EQ_REGEX, '=') + yield eq_token + pos = Skip(lines, eq_token.end, r'\s*') + + if SubString(lines, pos, pos + 2) != '[[': + exp_token = ParseToken(lines, pos, REST_OF_LINE_REGEX, 'exp') + yield exp_token + pos = Cursor(exp_token.end.line + 1, 0) + elif found.token_type == '$for': + if prev_token_rstripped: + yield prev_token_rstripped + yield found + id_token = ParseToken(lines, found.end, ID_REGEX, 'id') + yield id_token + pos = Skip(lines, id_token.end, WHITE_SPACE_REGEX) + elif found.token_type == '$range': + if prev_token_rstripped: + yield prev_token_rstripped + yield found + id_token = ParseToken(lines, found.end, ID_REGEX, 'id') + yield id_token + pos = Skip(lines, id_token.end, OPTIONAL_WHITE_SPACES_REGEX) + + dots_pos = SkipUntil(lines, pos, DOT_DOT_REGEX, '..') + yield MakeToken(lines, pos, dots_pos, 'exp') + yield MakeToken(lines, dots_pos, dots_pos + 2, '..') + pos = dots_pos + 2 + new_pos = Cursor(pos.line + 1, 0) + yield MakeToken(lines, pos, new_pos, 'exp') + pos = new_pos + elif found.token_type == '$': + if prev_token: + yield prev_token + yield found + exp_token = ParseExpTokenInParens(lines, found.end) + yield exp_token + pos = exp_token.end + elif (found.token_type == ']]' or found.token_type == '$if' or + found.token_type == '$elif' or found.token_type == '$else'): + if prev_token_rstripped: + yield prev_token_rstripped + yield found + pos = found.end + else: + if prev_token: + yield prev_token + yield found + pos = found.end + + +def Tokenize(s): + """A generator that yields the tokens in the given string.""" + if s != '': + lines = s.splitlines(True) + for token in TokenizeLines(lines, Cursor(0, 0)): + yield token + + +class CodeNode: + def __init__(self, atomic_code_list=None): + self.atomic_code = atomic_code_list + + +class VarNode: + def __init__(self, identifier=None, atomic_code=None): + self.identifier = identifier + self.atomic_code = atomic_code + + +class RangeNode: + def __init__(self, identifier=None, exp1=None, exp2=None): + self.identifier = identifier + self.exp1 = exp1 + self.exp2 = exp2 + + +class ForNode: + def __init__(self, identifier=None, sep=None, code=None): + self.identifier = identifier + self.sep = sep + self.code = code + + +class ElseNode: + def __init__(self, else_branch=None): + self.else_branch = else_branch + + +class IfNode: + def __init__(self, exp=None, then_branch=None, else_branch=None): + self.exp = exp + self.then_branch = then_branch + self.else_branch = else_branch + + +class RawCodeNode: + def __init__(self, token=None): + self.raw_code = token + + +class LiteralDollarNode: + def __init__(self, token): + self.token = token + + +class ExpNode: + def __init__(self, token, python_exp): + self.token = token + self.python_exp = python_exp + + +def PopFront(a_list): + head = a_list[0] + a_list[:1] = [] + return head + + +def PushFront(a_list, elem): + a_list[:0] = [elem] + + +def PopToken(a_list, token_type=None): + token = PopFront(a_list) + if token_type is not None and token.token_type != token_type: + print('ERROR: %s expected at %s' % (token_type, token.start)) + print('ERROR: %s found instead' % (token,)) + sys.exit(1) + + return token + + +def PeekToken(a_list): + if not a_list: + return None + + return a_list[0] + + +def ParseExpNode(token): + python_exp = re.sub(r'([_A-Za-z]\w*)', r'self.GetValue("\1")', token.value) + return ExpNode(token, python_exp) + + +def ParseElseNode(tokens): + def Pop(token_type=None): + return PopToken(tokens, token_type) + + next = PeekToken(tokens) + if not next: + return None + if next.token_type == '$else': + Pop('$else') + Pop('[[') + code_node = ParseCodeNode(tokens) + Pop(']]') + return code_node + elif next.token_type == '$elif': + Pop('$elif') + exp = Pop('code') + Pop('[[') + code_node = ParseCodeNode(tokens) + Pop(']]') + inner_else_node = ParseElseNode(tokens) + return CodeNode([IfNode(ParseExpNode(exp), code_node, inner_else_node)]) + elif not next.value.strip(): + Pop('code') + return ParseElseNode(tokens) + else: + return None + + +def ParseAtomicCodeNode(tokens): + def Pop(token_type=None): + return PopToken(tokens, token_type) + + head = PopFront(tokens) + t = head.token_type + if t == 'code': + return RawCodeNode(head) + elif t == '$var': + id_token = Pop('id') + Pop('=') + next = PeekToken(tokens) + if next.token_type == 'exp': + exp_token = Pop() + return VarNode(id_token, ParseExpNode(exp_token)) + Pop('[[') + code_node = ParseCodeNode(tokens) + Pop(']]') + return VarNode(id_token, code_node) + elif t == '$for': + id_token = Pop('id') + next_token = PeekToken(tokens) + if next_token.token_type == 'code': + sep_token = next_token + Pop('code') + else: + sep_token = None + Pop('[[') + code_node = ParseCodeNode(tokens) + Pop(']]') + return ForNode(id_token, sep_token, code_node) + elif t == '$if': + exp_token = Pop('code') + Pop('[[') + code_node = ParseCodeNode(tokens) + Pop(']]') + else_node = ParseElseNode(tokens) + return IfNode(ParseExpNode(exp_token), code_node, else_node) + elif t == '$range': + id_token = Pop('id') + exp1_token = Pop('exp') + Pop('..') + exp2_token = Pop('exp') + return RangeNode(id_token, ParseExpNode(exp1_token), + ParseExpNode(exp2_token)) + elif t == '$id': + return ParseExpNode(Token(head.start + 1, head.end, head.value[1:], 'id')) + elif t == '$($)': + return LiteralDollarNode(head) + elif t == '$': + exp_token = Pop('exp') + return ParseExpNode(exp_token) + elif t == '[[': + code_node = ParseCodeNode(tokens) + Pop(']]') + return code_node + else: + PushFront(tokens, head) + return None + + +def ParseCodeNode(tokens): + atomic_code_list = [] + while True: + if not tokens: + break + atomic_code_node = ParseAtomicCodeNode(tokens) + if atomic_code_node: + atomic_code_list.append(atomic_code_node) + else: + break + return CodeNode(atomic_code_list) + + +def ParseToAST(pump_src_text): + """Convert the given Pump source text into an AST.""" + tokens = list(Tokenize(pump_src_text)) + code_node = ParseCodeNode(tokens) + return code_node + + +class Env: + def __init__(self): + self.variables = [] + self.ranges = [] + + def Clone(self): + clone = Env() + clone.variables = self.variables[:] + clone.ranges = self.ranges[:] + return clone + + def PushVariable(self, var, value): + # If value looks like an int, store it as an int. + try: + int_value = int(value) + if ('%s' % int_value) == value: + value = int_value + except Exception: + pass + self.variables[:0] = [(var, value)] + + def PopVariable(self): + self.variables[:1] = [] + + def PushRange(self, var, lower, upper): + self.ranges[:0] = [(var, lower, upper)] + + def PopRange(self): + self.ranges[:1] = [] + + def GetValue(self, identifier): + for (var, value) in self.variables: + if identifier == var: + return value + + print('ERROR: meta variable %s is undefined.' % (identifier,)) + sys.exit(1) + + def EvalExp(self, exp): + try: + result = eval(exp.python_exp) + except Exception as e: # pylint: disable=broad-except + print('ERROR: caught exception %s: %s' % (e.__class__.__name__, e)) + print('ERROR: failed to evaluate meta expression %s at %s' % + (exp.python_exp, exp.token.start)) + sys.exit(1) + return result + + def GetRange(self, identifier): + for (var, lower, upper) in self.ranges: + if identifier == var: + return (lower, upper) + + print('ERROR: range %s is undefined.' % (identifier,)) + sys.exit(1) + + +class Output: + def __init__(self): + self.string = '' + + def GetLastLine(self): + index = self.string.rfind('\n') + if index < 0: + return '' + + return self.string[index + 1:] + + def Append(self, s): + self.string += s + + +def RunAtomicCode(env, node, output): + if isinstance(node, VarNode): + identifier = node.identifier.value.strip() + result = Output() + RunAtomicCode(env.Clone(), node.atomic_code, result) + value = result.string + env.PushVariable(identifier, value) + elif isinstance(node, RangeNode): + identifier = node.identifier.value.strip() + lower = int(env.EvalExp(node.exp1)) + upper = int(env.EvalExp(node.exp2)) + env.PushRange(identifier, lower, upper) + elif isinstance(node, ForNode): + identifier = node.identifier.value.strip() + if node.sep is None: + sep = '' + else: + sep = node.sep.value + (lower, upper) = env.GetRange(identifier) + for i in range(lower, upper + 1): + new_env = env.Clone() + new_env.PushVariable(identifier, i) + RunCode(new_env, node.code, output) + if i != upper: + output.Append(sep) + elif isinstance(node, RawCodeNode): + output.Append(node.raw_code.value) + elif isinstance(node, IfNode): + cond = env.EvalExp(node.exp) + if cond: + RunCode(env.Clone(), node.then_branch, output) + elif node.else_branch is not None: + RunCode(env.Clone(), node.else_branch, output) + elif isinstance(node, ExpNode): + value = env.EvalExp(node) + output.Append('%s' % (value,)) + elif isinstance(node, LiteralDollarNode): + output.Append('$') + elif isinstance(node, CodeNode): + RunCode(env.Clone(), node, output) + else: + print('BAD') + print(node) + sys.exit(1) + + +def RunCode(env, code_node, output): + for atomic_code in code_node.atomic_code: + RunAtomicCode(env, atomic_code, output) + + +def IsSingleLineComment(cur_line): + return '//' in cur_line + + +def IsInPreprocessorDirective(prev_lines, cur_line): + if cur_line.lstrip().startswith('#'): + return True + return prev_lines and prev_lines[-1].endswith('\\') + + +def WrapComment(line, output): + loc = line.find('//') + before_comment = line[:loc].rstrip() + if before_comment == '': + indent = loc + else: + output.append(before_comment) + indent = len(before_comment) - len(before_comment.lstrip()) + prefix = indent*' ' + '// ' + max_len = 80 - len(prefix) + comment = line[loc + 2:].strip() + segs = [seg for seg in re.split(r'(\w+\W*)', comment) if seg != ''] + cur_line = '' + for seg in segs: + if len((cur_line + seg).rstrip()) < max_len: + cur_line += seg + else: + if cur_line.strip() != '': + output.append(prefix + cur_line.rstrip()) + cur_line = seg.lstrip() + if cur_line.strip() != '': + output.append(prefix + cur_line.strip()) + + +def WrapCode(line, line_concat, output): + indent = len(line) - len(line.lstrip()) + prefix = indent*' ' # Prefix of the current line + max_len = 80 - indent - len(line_concat) # Maximum length of the current line + new_prefix = prefix + 4*' ' # Prefix of a continuation line + new_max_len = max_len - 4 # Maximum length of a continuation line + # Prefers to wrap a line after a ',' or ';'. + segs = [seg for seg in re.split(r'([^,;]+[,;]?)', line.strip()) if seg != ''] + cur_line = '' # The current line without leading spaces. + for seg in segs: + # If the line is still too long, wrap at a space. + while cur_line == '' and len(seg.strip()) > max_len: + seg = seg.lstrip() + split_at = seg.rfind(' ', 0, max_len) + output.append(prefix + seg[:split_at].strip() + line_concat) + seg = seg[split_at + 1:] + prefix = new_prefix + max_len = new_max_len + + if len((cur_line + seg).rstrip()) < max_len: + cur_line = (cur_line + seg).lstrip() + else: + output.append(prefix + cur_line.rstrip() + line_concat) + prefix = new_prefix + max_len = new_max_len + cur_line = seg.lstrip() + if cur_line.strip() != '': + output.append(prefix + cur_line.strip()) + + +def WrapPreprocessorDirective(line, output): + WrapCode(line, ' \\', output) + + +def WrapPlainCode(line, output): + WrapCode(line, '', output) + + +def IsMultiLineIWYUPragma(line): + return re.search(r'/\* IWYU pragma: ', line) + + +def IsHeaderGuardIncludeOrOneLineIWYUPragma(line): + return (re.match(r'^#(ifndef|define|endif\s*//)\s*[\w_]+\s*$', line) or + re.match(r'^#include\s', line) or + # Don't break IWYU pragmas, either; that causes iwyu.py problems. + re.search(r'// IWYU pragma: ', line)) + + +def WrapLongLine(line, output): + line = line.rstrip() + if len(line) <= 80: + output.append(line) + elif IsSingleLineComment(line): + if IsHeaderGuardIncludeOrOneLineIWYUPragma(line): + # The style guide made an exception to allow long header guard lines, + # includes and IWYU pragmas. + output.append(line) + else: + WrapComment(line, output) + elif IsInPreprocessorDirective(output, line): + if IsHeaderGuardIncludeOrOneLineIWYUPragma(line): + # The style guide made an exception to allow long header guard lines, + # includes and IWYU pragmas. + output.append(line) + else: + WrapPreprocessorDirective(line, output) + elif IsMultiLineIWYUPragma(line): + output.append(line) + else: + WrapPlainCode(line, output) + + +def BeautifyCode(string): + lines = string.splitlines() + output = [] + for line in lines: + WrapLongLine(line, output) + output2 = [line.rstrip() for line in output] + return '\n'.join(output2) + '\n' + + +def ConvertFromPumpSource(src_text): + """Return the text generated from the given Pump source text.""" + ast = ParseToAST(StripMetaComments(src_text)) + output = Output() + RunCode(Env(), ast, output) + return BeautifyCode(output.string) + + +def main(argv): + if len(argv) == 1: + print(__doc__) + sys.exit(1) + + file_path = argv[-1] + output_str = ConvertFromPumpSource(file(file_path, 'r').read()) + if file_path.endswith('.pump'): + output_file_path = file_path[:-5] + else: + output_file_path = '-' + if output_file_path == '-': + print(output_str,) + else: + output_file = file(output_file_path, 'w') + output_file.write('// This file was GENERATED by command:\n') + output_file.write('// %s %s\n' % + (os.path.basename(__file__), os.path.basename(file_path))) + output_file.write('// DO NOT EDIT BY HAND!!!\n\n') + output_file.write(output_str) + output_file.close() + + +if __name__ == '__main__': + main(sys.argv) diff --git a/googletest/docs/pump_manual.md b/googletest/docs/pump_manual.md deleted file mode 100644 index 10b3c5f..0000000 --- a/googletest/docs/pump_manual.md +++ /dev/null @@ -1,190 +0,0 @@ -Pump is Useful for Meta Programming. - -# The Problem - -Template and macro libraries often need to define many classes, functions, or -macros that vary only (or almost only) in the number of arguments they take. -It's a lot of repetitive, mechanical, and error-prone work. - -Variadic templates and variadic macros can alleviate the problem. However, while -both are being considered by the C++ committee, neither is in the standard yet -or widely supported by compilers. Thus they are often not a good choice, -especially when your code needs to be portable. And their capabilities are still -limited. - -As a result, authors of such libraries often have to write scripts to generate -their implementation. However, our experience is that it's tedious to write such -scripts, which tend to reflect the structure of the generated code poorly and -are often hard to read and edit. For example, a small change needed in the -generated code may require some non-intuitive, non-trivial changes in the -script. This is especially painful when experimenting with the code. - -# Our Solution - -Pump (for Pump is Useful for Meta Programming, Pretty Useful for Meta -Programming, or Practical Utility for Meta Programming, whichever you prefer) is -a simple meta-programming tool for C++. The idea is that a programmer writes a -`foo.pump` file which contains C++ code plus meta code that manipulates the C++ -code. The meta code can handle iterations over a range, nested iterations, local -meta variable definitions, simple arithmetic, and conditional expressions. You -can view it as a small Domain-Specific Language. The meta language is designed -to be non-intrusive (s.t. it won't confuse Emacs' C++ mode, for example) and -concise, making Pump code intuitive and easy to maintain. - -## Highlights - -* The implementation is in a single Python script and thus ultra portable: no - build or installation is needed and it works cross platforms. -* Pump tries to be smart with respect to - [Google's style guide](https://github.com/google/styleguide): it breaks long - lines (easy to have when they are generated) at acceptable places to fit - within 80 columns and indent the continuation lines correctly. -* The format is human-readable and more concise than XML. -* The format works relatively well with Emacs' C++ mode. - -## Examples - -The following Pump code (where meta keywords start with `$`, `[[` and `]]` are -meta brackets, and `$$` starts a meta comment that ends with the line): - -``` -$var n = 3 $$ Defines a meta variable n. -$range i 0..n $$ Declares the range of meta iterator i (inclusive). -$for i [[ - $$ Meta loop. -// Foo$i does blah for $i-ary predicates. -$range j 1..i -template -class Foo$i { -$if i == 0 [[ - blah a; -]] $elif i <= 2 [[ - blah b; -]] $else [[ - blah c; -]] -}; - -]] -``` - -will be translated by the Pump compiler to: - -```cpp -// Foo0 does blah for 0-ary predicates. -template -class Foo0 { - blah a; -}; - -// Foo1 does blah for 1-ary predicates. -template -class Foo1 { - blah b; -}; - -// Foo2 does blah for 2-ary predicates. -template -class Foo2 { - blah b; -}; - -// Foo3 does blah for 3-ary predicates. -template -class Foo3 { - blah c; -}; -``` - -In another example, - -``` -$range i 1..n -Func($for i + [[a$i]]); -$$ The text between i and [[ is the separator between iterations. -``` - -will generate one of the following lines (without the comments), depending on -the value of `n`: - -```cpp -Func(); // If n is 0. -Func(a1); // If n is 1. -Func(a1 + a2); // If n is 2. -Func(a1 + a2 + a3); // If n is 3. -// And so on... -``` - -## Constructs - -We support the following meta programming constructs: - -| `$var id = exp` | Defines a named constant value. `$id` is | -: : valid util the end of the current meta : -: : lexical block. : -| :------------------------------- | :--------------------------------------- | -| `$range id exp..exp` | Sets the range of an iteration variable, | -: : which can be reused in multiple loops : -: : later. : -| `$for id sep [[ code ]]` | Iteration. The range of `id` must have | -: : been defined earlier. `$id` is valid in : -: : `code`. : -| `$($)` | Generates a single `$` character. | -| `$id` | Value of the named constant or iteration | -: : variable. : -| `$(exp)` | Value of the expression. | -| `$if exp [[ code ]] else_branch` | Conditional. | -| `[[ code ]]` | Meta lexical block. | -| `cpp_code` | Raw C++ code. | -| `$$ comment` | Meta comment. | - -**Note:** To give the user some freedom in formatting the Pump source code, Pump -ignores a new-line character if it's right after `$for foo` or next to `[[` or -`]]`. Without this rule you'll often be forced to write very long lines to get -the desired output. Therefore sometimes you may need to insert an extra new-line -in such places for a new-line to show up in your output. - -## Grammar - -```ebnf -code ::= atomic_code* -atomic_code ::= $var id = exp - | $var id = [[ code ]] - | $range id exp..exp - | $for id sep [[ code ]] - | $($) - | $id - | $(exp) - | $if exp [[ code ]] else_branch - | [[ code ]] - | cpp_code -sep ::= cpp_code | empty_string -else_branch ::= $else [[ code ]] - | $elif exp [[ code ]] else_branch - | empty_string -exp ::= simple_expression_in_Python_syntax -``` - -## Code - -You can find the source code of Pump in [scripts/pump.py](../scripts/pump.py). -It is still very unpolished and lacks automated tests, although it has been -successfully used many times. If you find a chance to use it in your project, -please let us know what you think! We also welcome help on improving Pump. - -## Real Examples - -You can find real-world applications of Pump in -[Google Test](https://github.com/google/googletest/tree/master/googletest) and -[Google Mock](https://github.com/google/googletest/tree/master/googlemock). The -source file `foo.h.pump` generates `foo.h`. - -## Tips - -* If a meta variable is followed by a letter or digit, you can separate them - using `[[]]`, which inserts an empty string. For example `Foo$j[[]]Helper` - generate `Foo1Helper` when `j` is 1. -* To avoid extra-long Pump source lines, you can break a line anywhere you - want by inserting `[[]]` followed by a new line. Since any new-line - character next to `[[` or `]]` is ignored, the generated code won't contain - this new line. diff --git a/googletest/scripts/pump.py b/googletest/scripts/pump.py deleted file mode 100755 index 66e3217..0000000 --- a/googletest/scripts/pump.py +++ /dev/null @@ -1,855 +0,0 @@ -#!/usr/bin/env python2.7 -# -# Copyright 2008, Google Inc. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are -# met: -# -# * Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above -# copyright notice, this list of conditions and the following disclaimer -# in the documentation and/or other materials provided with the -# distribution. -# * Neither the name of Google Inc. nor the names of its -# contributors may be used to endorse or promote products derived from -# this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -"""pump v0.2.0 - Pretty Useful for Meta Programming. - -A tool for preprocessor meta programming. Useful for generating -repetitive boilerplate code. Especially useful for writing C++ -classes, functions, macros, and templates that need to work with -various number of arguments. - -USAGE: - pump.py SOURCE_FILE - -EXAMPLES: - pump.py foo.cc.pump - Converts foo.cc.pump to foo.cc. - -GRAMMAR: - CODE ::= ATOMIC_CODE* - ATOMIC_CODE ::= $var ID = EXPRESSION - | $var ID = [[ CODE ]] - | $range ID EXPRESSION..EXPRESSION - | $for ID SEPARATOR [[ CODE ]] - | $($) - | $ID - | $(EXPRESSION) - | $if EXPRESSION [[ CODE ]] ELSE_BRANCH - | [[ CODE ]] - | RAW_CODE - SEPARATOR ::= RAW_CODE | EMPTY - ELSE_BRANCH ::= $else [[ CODE ]] - | $elif EXPRESSION [[ CODE ]] ELSE_BRANCH - | EMPTY - EXPRESSION has Python syntax. -""" - -from __future__ import print_function - -import os -import re -import sys - - -TOKEN_TABLE = [ - (re.compile(r'\$var\s+'), '$var'), - (re.compile(r'\$elif\s+'), '$elif'), - (re.compile(r'\$else\s+'), '$else'), - (re.compile(r'\$for\s+'), '$for'), - (re.compile(r'\$if\s+'), '$if'), - (re.compile(r'\$range\s+'), '$range'), - (re.compile(r'\$[_A-Za-z]\w*'), '$id'), - (re.compile(r'\$\(\$\)'), '$($)'), - (re.compile(r'\$'), '$'), - (re.compile(r'\[\[\n?'), '[['), - (re.compile(r'\]\]\n?'), ']]'), - ] - - -class Cursor: - """Represents a position (line and column) in a text file.""" - - def __init__(self, line=-1, column=-1): - self.line = line - self.column = column - - def __eq__(self, rhs): - return self.line == rhs.line and self.column == rhs.column - - def __ne__(self, rhs): - return not self == rhs - - def __lt__(self, rhs): - return self.line < rhs.line or ( - self.line == rhs.line and self.column < rhs.column) - - def __le__(self, rhs): - return self < rhs or self == rhs - - def __gt__(self, rhs): - return rhs < self - - def __ge__(self, rhs): - return rhs <= self - - def __str__(self): - if self == Eof(): - return 'EOF' - else: - return '%s(%s)' % (self.line + 1, self.column) - - def __add__(self, offset): - return Cursor(self.line, self.column + offset) - - def __sub__(self, offset): - return Cursor(self.line, self.column - offset) - - def Clone(self): - """Returns a copy of self.""" - - return Cursor(self.line, self.column) - - -# Special cursor to indicate the end-of-file. -def Eof(): - """Returns the special cursor to denote the end-of-file.""" - return Cursor(-1, -1) - - -class Token: - """Represents a token in a Pump source file.""" - - def __init__(self, start=None, end=None, value=None, token_type=None): - if start is None: - self.start = Eof() - else: - self.start = start - if end is None: - self.end = Eof() - else: - self.end = end - self.value = value - self.token_type = token_type - - def __str__(self): - return 'Token @%s: \'%s\' type=%s' % ( - self.start, self.value, self.token_type) - - def Clone(self): - """Returns a copy of self.""" - - return Token(self.start.Clone(), self.end.Clone(), self.value, - self.token_type) - - -def StartsWith(lines, pos, string): - """Returns True iff the given position in lines starts with 'string'.""" - - return lines[pos.line][pos.column:].startswith(string) - - -def FindFirstInLine(line, token_table): - best_match_start = -1 - for (regex, token_type) in token_table: - m = regex.search(line) - if m: - # We found regex in lines - if best_match_start < 0 or m.start() < best_match_start: - best_match_start = m.start() - best_match_length = m.end() - m.start() - best_match_token_type = token_type - - if best_match_start < 0: - return None - - return (best_match_start, best_match_length, best_match_token_type) - - -def FindFirst(lines, token_table, cursor): - """Finds the first occurrence of any string in strings in lines.""" - - start = cursor.Clone() - cur_line_number = cursor.line - for line in lines[start.line:]: - if cur_line_number == start.line: - line = line[start.column:] - m = FindFirstInLine(line, token_table) - if m: - # We found a regex in line. - (start_column, length, token_type) = m - if cur_line_number == start.line: - start_column += start.column - found_start = Cursor(cur_line_number, start_column) - found_end = found_start + length - return MakeToken(lines, found_start, found_end, token_type) - cur_line_number += 1 - # We failed to find str in lines - return None - - -def SubString(lines, start, end): - """Returns a substring in lines.""" - - if end == Eof(): - end = Cursor(len(lines) - 1, len(lines[-1])) - - if start >= end: - return '' - - if start.line == end.line: - return lines[start.line][start.column:end.column] - - result_lines = ([lines[start.line][start.column:]] + - lines[start.line + 1:end.line] + - [lines[end.line][:end.column]]) - return ''.join(result_lines) - - -def StripMetaComments(str): - """Strip meta comments from each line in the given string.""" - - # First, completely remove lines containing nothing but a meta - # comment, including the trailing \n. - str = re.sub(r'^\s*\$\$.*\n', '', str) - - # Then, remove meta comments from contentful lines. - return re.sub(r'\s*\$\$.*', '', str) - - -def MakeToken(lines, start, end, token_type): - """Creates a new instance of Token.""" - - return Token(start, end, SubString(lines, start, end), token_type) - - -def ParseToken(lines, pos, regex, token_type): - line = lines[pos.line][pos.column:] - m = regex.search(line) - if m and not m.start(): - return MakeToken(lines, pos, pos + m.end(), token_type) - else: - print('ERROR: %s expected at %s.' % (token_type, pos)) - sys.exit(1) - - -ID_REGEX = re.compile(r'[_A-Za-z]\w*') -EQ_REGEX = re.compile(r'=') -REST_OF_LINE_REGEX = re.compile(r'.*?(?=$|\$\$)') -OPTIONAL_WHITE_SPACES_REGEX = re.compile(r'\s*') -WHITE_SPACE_REGEX = re.compile(r'\s') -DOT_DOT_REGEX = re.compile(r'\.\.') - - -def Skip(lines, pos, regex): - line = lines[pos.line][pos.column:] - m = re.search(regex, line) - if m and not m.start(): - return pos + m.end() - else: - return pos - - -def SkipUntil(lines, pos, regex, token_type): - line = lines[pos.line][pos.column:] - m = re.search(regex, line) - if m: - return pos + m.start() - else: - print ('ERROR: %s expected on line %s after column %s.' % - (token_type, pos.line + 1, pos.column)) - sys.exit(1) - - -def ParseExpTokenInParens(lines, pos): - def ParseInParens(pos): - pos = Skip(lines, pos, OPTIONAL_WHITE_SPACES_REGEX) - pos = Skip(lines, pos, r'\(') - pos = Parse(pos) - pos = Skip(lines, pos, r'\)') - return pos - - def Parse(pos): - pos = SkipUntil(lines, pos, r'\(|\)', ')') - if SubString(lines, pos, pos + 1) == '(': - pos = Parse(pos + 1) - pos = Skip(lines, pos, r'\)') - return Parse(pos) - else: - return pos - - start = pos.Clone() - pos = ParseInParens(pos) - return MakeToken(lines, start, pos, 'exp') - - -def RStripNewLineFromToken(token): - if token.value.endswith('\n'): - return Token(token.start, token.end, token.value[:-1], token.token_type) - else: - return token - - -def TokenizeLines(lines, pos): - while True: - found = FindFirst(lines, TOKEN_TABLE, pos) - if not found: - yield MakeToken(lines, pos, Eof(), 'code') - return - - if found.start == pos: - prev_token = None - prev_token_rstripped = None - else: - prev_token = MakeToken(lines, pos, found.start, 'code') - prev_token_rstripped = RStripNewLineFromToken(prev_token) - - if found.token_type == '$var': - if prev_token_rstripped: - yield prev_token_rstripped - yield found - id_token = ParseToken(lines, found.end, ID_REGEX, 'id') - yield id_token - pos = Skip(lines, id_token.end, OPTIONAL_WHITE_SPACES_REGEX) - - eq_token = ParseToken(lines, pos, EQ_REGEX, '=') - yield eq_token - pos = Skip(lines, eq_token.end, r'\s*') - - if SubString(lines, pos, pos + 2) != '[[': - exp_token = ParseToken(lines, pos, REST_OF_LINE_REGEX, 'exp') - yield exp_token - pos = Cursor(exp_token.end.line + 1, 0) - elif found.token_type == '$for': - if prev_token_rstripped: - yield prev_token_rstripped - yield found - id_token = ParseToken(lines, found.end, ID_REGEX, 'id') - yield id_token - pos = Skip(lines, id_token.end, WHITE_SPACE_REGEX) - elif found.token_type == '$range': - if prev_token_rstripped: - yield prev_token_rstripped - yield found - id_token = ParseToken(lines, found.end, ID_REGEX, 'id') - yield id_token - pos = Skip(lines, id_token.end, OPTIONAL_WHITE_SPACES_REGEX) - - dots_pos = SkipUntil(lines, pos, DOT_DOT_REGEX, '..') - yield MakeToken(lines, pos, dots_pos, 'exp') - yield MakeToken(lines, dots_pos, dots_pos + 2, '..') - pos = dots_pos + 2 - new_pos = Cursor(pos.line + 1, 0) - yield MakeToken(lines, pos, new_pos, 'exp') - pos = new_pos - elif found.token_type == '$': - if prev_token: - yield prev_token - yield found - exp_token = ParseExpTokenInParens(lines, found.end) - yield exp_token - pos = exp_token.end - elif (found.token_type == ']]' or found.token_type == '$if' or - found.token_type == '$elif' or found.token_type == '$else'): - if prev_token_rstripped: - yield prev_token_rstripped - yield found - pos = found.end - else: - if prev_token: - yield prev_token - yield found - pos = found.end - - -def Tokenize(s): - """A generator that yields the tokens in the given string.""" - if s != '': - lines = s.splitlines(True) - for token in TokenizeLines(lines, Cursor(0, 0)): - yield token - - -class CodeNode: - def __init__(self, atomic_code_list=None): - self.atomic_code = atomic_code_list - - -class VarNode: - def __init__(self, identifier=None, atomic_code=None): - self.identifier = identifier - self.atomic_code = atomic_code - - -class RangeNode: - def __init__(self, identifier=None, exp1=None, exp2=None): - self.identifier = identifier - self.exp1 = exp1 - self.exp2 = exp2 - - -class ForNode: - def __init__(self, identifier=None, sep=None, code=None): - self.identifier = identifier - self.sep = sep - self.code = code - - -class ElseNode: - def __init__(self, else_branch=None): - self.else_branch = else_branch - - -class IfNode: - def __init__(self, exp=None, then_branch=None, else_branch=None): - self.exp = exp - self.then_branch = then_branch - self.else_branch = else_branch - - -class RawCodeNode: - def __init__(self, token=None): - self.raw_code = token - - -class LiteralDollarNode: - def __init__(self, token): - self.token = token - - -class ExpNode: - def __init__(self, token, python_exp): - self.token = token - self.python_exp = python_exp - - -def PopFront(a_list): - head = a_list[0] - a_list[:1] = [] - return head - - -def PushFront(a_list, elem): - a_list[:0] = [elem] - - -def PopToken(a_list, token_type=None): - token = PopFront(a_list) - if token_type is not None and token.token_type != token_type: - print('ERROR: %s expected at %s' % (token_type, token.start)) - print('ERROR: %s found instead' % (token,)) - sys.exit(1) - - return token - - -def PeekToken(a_list): - if not a_list: - return None - - return a_list[0] - - -def ParseExpNode(token): - python_exp = re.sub(r'([_A-Za-z]\w*)', r'self.GetValue("\1")', token.value) - return ExpNode(token, python_exp) - - -def ParseElseNode(tokens): - def Pop(token_type=None): - return PopToken(tokens, token_type) - - next = PeekToken(tokens) - if not next: - return None - if next.token_type == '$else': - Pop('$else') - Pop('[[') - code_node = ParseCodeNode(tokens) - Pop(']]') - return code_node - elif next.token_type == '$elif': - Pop('$elif') - exp = Pop('code') - Pop('[[') - code_node = ParseCodeNode(tokens) - Pop(']]') - inner_else_node = ParseElseNode(tokens) - return CodeNode([IfNode(ParseExpNode(exp), code_node, inner_else_node)]) - elif not next.value.strip(): - Pop('code') - return ParseElseNode(tokens) - else: - return None - - -def ParseAtomicCodeNode(tokens): - def Pop(token_type=None): - return PopToken(tokens, token_type) - - head = PopFront(tokens) - t = head.token_type - if t == 'code': - return RawCodeNode(head) - elif t == '$var': - id_token = Pop('id') - Pop('=') - next = PeekToken(tokens) - if next.token_type == 'exp': - exp_token = Pop() - return VarNode(id_token, ParseExpNode(exp_token)) - Pop('[[') - code_node = ParseCodeNode(tokens) - Pop(']]') - return VarNode(id_token, code_node) - elif t == '$for': - id_token = Pop('id') - next_token = PeekToken(tokens) - if next_token.token_type == 'code': - sep_token = next_token - Pop('code') - else: - sep_token = None - Pop('[[') - code_node = ParseCodeNode(tokens) - Pop(']]') - return ForNode(id_token, sep_token, code_node) - elif t == '$if': - exp_token = Pop('code') - Pop('[[') - code_node = ParseCodeNode(tokens) - Pop(']]') - else_node = ParseElseNode(tokens) - return IfNode(ParseExpNode(exp_token), code_node, else_node) - elif t == '$range': - id_token = Pop('id') - exp1_token = Pop('exp') - Pop('..') - exp2_token = Pop('exp') - return RangeNode(id_token, ParseExpNode(exp1_token), - ParseExpNode(exp2_token)) - elif t == '$id': - return ParseExpNode(Token(head.start + 1, head.end, head.value[1:], 'id')) - elif t == '$($)': - return LiteralDollarNode(head) - elif t == '$': - exp_token = Pop('exp') - return ParseExpNode(exp_token) - elif t == '[[': - code_node = ParseCodeNode(tokens) - Pop(']]') - return code_node - else: - PushFront(tokens, head) - return None - - -def ParseCodeNode(tokens): - atomic_code_list = [] - while True: - if not tokens: - break - atomic_code_node = ParseAtomicCodeNode(tokens) - if atomic_code_node: - atomic_code_list.append(atomic_code_node) - else: - break - return CodeNode(atomic_code_list) - - -def ParseToAST(pump_src_text): - """Convert the given Pump source text into an AST.""" - tokens = list(Tokenize(pump_src_text)) - code_node = ParseCodeNode(tokens) - return code_node - - -class Env: - def __init__(self): - self.variables = [] - self.ranges = [] - - def Clone(self): - clone = Env() - clone.variables = self.variables[:] - clone.ranges = self.ranges[:] - return clone - - def PushVariable(self, var, value): - # If value looks like an int, store it as an int. - try: - int_value = int(value) - if ('%s' % int_value) == value: - value = int_value - except Exception: - pass - self.variables[:0] = [(var, value)] - - def PopVariable(self): - self.variables[:1] = [] - - def PushRange(self, var, lower, upper): - self.ranges[:0] = [(var, lower, upper)] - - def PopRange(self): - self.ranges[:1] = [] - - def GetValue(self, identifier): - for (var, value) in self.variables: - if identifier == var: - return value - - print('ERROR: meta variable %s is undefined.' % (identifier,)) - sys.exit(1) - - def EvalExp(self, exp): - try: - result = eval(exp.python_exp) - except Exception as e: # pylint: disable=broad-except - print('ERROR: caught exception %s: %s' % (e.__class__.__name__, e)) - print('ERROR: failed to evaluate meta expression %s at %s' % - (exp.python_exp, exp.token.start)) - sys.exit(1) - return result - - def GetRange(self, identifier): - for (var, lower, upper) in self.ranges: - if identifier == var: - return (lower, upper) - - print('ERROR: range %s is undefined.' % (identifier,)) - sys.exit(1) - - -class Output: - def __init__(self): - self.string = '' - - def GetLastLine(self): - index = self.string.rfind('\n') - if index < 0: - return '' - - return self.string[index + 1:] - - def Append(self, s): - self.string += s - - -def RunAtomicCode(env, node, output): - if isinstance(node, VarNode): - identifier = node.identifier.value.strip() - result = Output() - RunAtomicCode(env.Clone(), node.atomic_code, result) - value = result.string - env.PushVariable(identifier, value) - elif isinstance(node, RangeNode): - identifier = node.identifier.value.strip() - lower = int(env.EvalExp(node.exp1)) - upper = int(env.EvalExp(node.exp2)) - env.PushRange(identifier, lower, upper) - elif isinstance(node, ForNode): - identifier = node.identifier.value.strip() - if node.sep is None: - sep = '' - else: - sep = node.sep.value - (lower, upper) = env.GetRange(identifier) - for i in range(lower, upper + 1): - new_env = env.Clone() - new_env.PushVariable(identifier, i) - RunCode(new_env, node.code, output) - if i != upper: - output.Append(sep) - elif isinstance(node, RawCodeNode): - output.Append(node.raw_code.value) - elif isinstance(node, IfNode): - cond = env.EvalExp(node.exp) - if cond: - RunCode(env.Clone(), node.then_branch, output) - elif node.else_branch is not None: - RunCode(env.Clone(), node.else_branch, output) - elif isinstance(node, ExpNode): - value = env.EvalExp(node) - output.Append('%s' % (value,)) - elif isinstance(node, LiteralDollarNode): - output.Append('$') - elif isinstance(node, CodeNode): - RunCode(env.Clone(), node, output) - else: - print('BAD') - print(node) - sys.exit(1) - - -def RunCode(env, code_node, output): - for atomic_code in code_node.atomic_code: - RunAtomicCode(env, atomic_code, output) - - -def IsSingleLineComment(cur_line): - return '//' in cur_line - - -def IsInPreprocessorDirective(prev_lines, cur_line): - if cur_line.lstrip().startswith('#'): - return True - return prev_lines and prev_lines[-1].endswith('\\') - - -def WrapComment(line, output): - loc = line.find('//') - before_comment = line[:loc].rstrip() - if before_comment == '': - indent = loc - else: - output.append(before_comment) - indent = len(before_comment) - len(before_comment.lstrip()) - prefix = indent*' ' + '// ' - max_len = 80 - len(prefix) - comment = line[loc + 2:].strip() - segs = [seg for seg in re.split(r'(\w+\W*)', comment) if seg != ''] - cur_line = '' - for seg in segs: - if len((cur_line + seg).rstrip()) < max_len: - cur_line += seg - else: - if cur_line.strip() != '': - output.append(prefix + cur_line.rstrip()) - cur_line = seg.lstrip() - if cur_line.strip() != '': - output.append(prefix + cur_line.strip()) - - -def WrapCode(line, line_concat, output): - indent = len(line) - len(line.lstrip()) - prefix = indent*' ' # Prefix of the current line - max_len = 80 - indent - len(line_concat) # Maximum length of the current line - new_prefix = prefix + 4*' ' # Prefix of a continuation line - new_max_len = max_len - 4 # Maximum length of a continuation line - # Prefers to wrap a line after a ',' or ';'. - segs = [seg for seg in re.split(r'([^,;]+[,;]?)', line.strip()) if seg != ''] - cur_line = '' # The current line without leading spaces. - for seg in segs: - # If the line is still too long, wrap at a space. - while cur_line == '' and len(seg.strip()) > max_len: - seg = seg.lstrip() - split_at = seg.rfind(' ', 0, max_len) - output.append(prefix + seg[:split_at].strip() + line_concat) - seg = seg[split_at + 1:] - prefix = new_prefix - max_len = new_max_len - - if len((cur_line + seg).rstrip()) < max_len: - cur_line = (cur_line + seg).lstrip() - else: - output.append(prefix + cur_line.rstrip() + line_concat) - prefix = new_prefix - max_len = new_max_len - cur_line = seg.lstrip() - if cur_line.strip() != '': - output.append(prefix + cur_line.strip()) - - -def WrapPreprocessorDirective(line, output): - WrapCode(line, ' \\', output) - - -def WrapPlainCode(line, output): - WrapCode(line, '', output) - - -def IsMultiLineIWYUPragma(line): - return re.search(r'/\* IWYU pragma: ', line) - - -def IsHeaderGuardIncludeOrOneLineIWYUPragma(line): - return (re.match(r'^#(ifndef|define|endif\s*//)\s*[\w_]+\s*$', line) or - re.match(r'^#include\s', line) or - # Don't break IWYU pragmas, either; that causes iwyu.py problems. - re.search(r'// IWYU pragma: ', line)) - - -def WrapLongLine(line, output): - line = line.rstrip() - if len(line) <= 80: - output.append(line) - elif IsSingleLineComment(line): - if IsHeaderGuardIncludeOrOneLineIWYUPragma(line): - # The style guide made an exception to allow long header guard lines, - # includes and IWYU pragmas. - output.append(line) - else: - WrapComment(line, output) - elif IsInPreprocessorDirective(output, line): - if IsHeaderGuardIncludeOrOneLineIWYUPragma(line): - # The style guide made an exception to allow long header guard lines, - # includes and IWYU pragmas. - output.append(line) - else: - WrapPreprocessorDirective(line, output) - elif IsMultiLineIWYUPragma(line): - output.append(line) - else: - WrapPlainCode(line, output) - - -def BeautifyCode(string): - lines = string.splitlines() - output = [] - for line in lines: - WrapLongLine(line, output) - output2 = [line.rstrip() for line in output] - return '\n'.join(output2) + '\n' - - -def ConvertFromPumpSource(src_text): - """Return the text generated from the given Pump source text.""" - ast = ParseToAST(StripMetaComments(src_text)) - output = Output() - RunCode(Env(), ast, output) - return BeautifyCode(output.string) - - -def main(argv): - if len(argv) == 1: - print(__doc__) - sys.exit(1) - - file_path = argv[-1] - output_str = ConvertFromPumpSource(file(file_path, 'r').read()) - if file_path.endswith('.pump'): - output_file_path = file_path[:-5] - else: - output_file_path = '-' - if output_file_path == '-': - print(output_str,) - else: - output_file = file(output_file_path, 'w') - output_file.write('// This file was GENERATED by command:\n') - output_file.write('// %s %s\n' % - (os.path.basename(__file__), os.path.basename(file_path))) - output_file.write('// DO NOT EDIT BY HAND!!!\n\n') - output_file.write(output_str) - output_file.close() - - -if __name__ == '__main__': - main(sys.argv) -- cgit v0.12 From 1a49b67aebe9e5178d5614ff1359bbbca04d2eed Mon Sep 17 00:00:00 2001 From: Krystian Kuzniarek Date: Tue, 3 Sep 2019 18:15:09 +0200 Subject: update CONTRIBUTORS --- googletest/CONTRIBUTORS | 1 + 1 file changed, 1 insertion(+) diff --git a/googletest/CONTRIBUTORS b/googletest/CONTRIBUTORS index feae2fc..1e4afe2 100644 --- a/googletest/CONTRIBUTORS +++ b/googletest/CONTRIBUTORS @@ -17,6 +17,7 @@ Jói Sigurðsson Keir Mierle Keith Ray Kenton Varda +Krystian Kuzniarek Manuel Klimek Markus Heule Mika Raento -- cgit v0.12