From d60b3870985efd980894c42db7a6721c5ef04a07 Mon Sep 17 00:00:00 2001 From: Ilya Biryukov Date: Thu, 31 Jan 2019 20:20:32 +0000 Subject: [CodeComplete] Propagate preferred types through parser in more cases Preferred types are used by code completion for ranking. This commit considerably increases the number of points in code where those types are propagated. In order to avoid complicating signatures of Parser's methods, a preferred type is kept as a member variable in the parser and updated during parsing. Differential revision: https://reviews.llvm.org/D56723 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@352788 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/Sema/CodeCompleteTest.cpp | 99 +++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) (limited to 'unittests/Sema') diff --git a/unittests/Sema/CodeCompleteTest.cpp b/unittests/Sema/CodeCompleteTest.cpp index d44e0ece55..3bec79dc9a 100644 --- a/unittests/Sema/CodeCompleteTest.cpp +++ b/unittests/Sema/CodeCompleteTest.cpp @@ -339,4 +339,103 @@ TEST(PreferredTypeTest, BinaryExpr) { EXPECT_THAT(collectPreferredTypes(Code), Each("NULL TYPE")); } +TEST(PreferredTypeTest, Members) { + StringRef Code = R"cpp( + struct vector { + int *begin(); + vector clone(); + }; + + void test(int *a) { + a = ^vector().^clone().^begin(); + } + )cpp"; + EXPECT_THAT(collectPreferredTypes(Code), Each("int *")); +} + +TEST(PreferredTypeTest, Conditions) { + StringRef Code = R"cpp( + struct vector { + bool empty(); + }; + + void test() { + if (^vector().^empty()) {} + while (^vector().^empty()) {} + for (; ^vector().^empty();) {} + } + )cpp"; + EXPECT_THAT(collectPreferredTypes(Code), Each("_Bool")); +} + +TEST(PreferredTypeTest, InitAndAssignment) { + StringRef Code = R"cpp( + struct vector { + int* begin(); + }; + + void test() { + const int* x = ^vector().^begin(); + x = ^vector().^begin(); + + if (const int* y = ^vector().^begin()) {} + } + )cpp"; + EXPECT_THAT(collectPreferredTypes(Code), Each("const int *")); +} + +TEST(PreferredTypeTest, UnaryExprs) { + StringRef Code = R"cpp( + void test(long long a) { + a = +^a; + a = -^a + a = ++^a; + a = --^a; + } + )cpp"; + EXPECT_THAT(collectPreferredTypes(Code), Each("long long")); + + Code = R"cpp( + void test(int a, int *ptr) { + !^a; + !^ptr; + !!!^a; + + a = !^a; + a = !^ptr; + a = !!!^a; + } + )cpp"; + EXPECT_THAT(collectPreferredTypes(Code), Each("_Bool")); + + Code = R"cpp( + void test(int a) { + const int* x = &^a; + } + )cpp"; + EXPECT_THAT(collectPreferredTypes(Code), Each("const int")); + + Code = R"cpp( + void test(int *a) { + int x = *^a; + int &r = *^a; + } + )cpp"; + EXPECT_THAT(collectPreferredTypes(Code), Each("int *")); + + Code = R"cpp( + void test(int a) { + *^a; + &^a; + } + + )cpp"; +} + +TEST(PreferredTypeTest, ParenExpr) { + StringRef Code = R"cpp( + const int *i = ^(^(^(^10))); + )cpp"; + EXPECT_THAT(collectPreferredTypes(Code), Each("const int *")); +} } // namespace -- cgit v1.2.1