Skip to content

Commit ebe1b91

Browse files
committed
Add tests for CollectVariableType request
- Add CollectVariableType test for function/closure parameters - Test ranged CollectVariableType request - Add separate test for ranged CollectVariableType - Use line:col positions in sourcekitd-test for var types - Add test case for CollectVariableType and inout params - Update ranged CollectVariableType test ...as per the PR suggestions. - Fix style issue
1 parent 2703d08 commit ebe1b91

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
let x: Int = 3
2+
let y = "abc"
3+
4+
var foo = ["abc" + "def"]
5+
6+
struct A {
7+
let x: String = ""
8+
let y = ""
9+
}
10+
11+
class B {
12+
var x = 4.0
13+
var y = [A]()
14+
var z: [Int: Int] = [:]
15+
var w: (Int) -> Int { { $0 * 2 } }
16+
}
17+
18+
func foo() {
19+
var local = 5
20+
}
21+
22+
// RUN: %sourcekitd-test -req=collect-var-type %s -- %s | %FileCheck %s
23+
// CHECK: (1:5, 1:6): Int (explicit type: 1)
24+
// CHECK: (2:5, 2:6): String (explicit type: 0)
25+
// CHECK: (4:5, 4:8): [String] (explicit type: 0)
26+
// CHECK: (7:7, 7:8): String (explicit type: 1)
27+
// CHECK: (8:7, 8:8): String (explicit type: 0)
28+
// CHECK: (12:7, 12:8): Double (explicit type: 0)
29+
// CHECK: (13:7, 13:8): [A] (explicit type: 0)
30+
// CHECK: (14:7, 14:8): [Int : Int] (explicit type: 1)
31+
// CHECK: (15:7, 15:8): (Int) -> Int (explicit type: 1)
32+
// CHECK: (19:7, 19:12): Int (explicit type: 0)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func x(_ param: inout Int) -> Int {
2+
param = 4
3+
}
4+
5+
let z = { (param: inout String) in }
6+
7+
// RUN: %sourcekitd-test -req=collect-var-type %s -- %s | %FileCheck %s
8+
// CHECK: (1:10, 1:15): Int (explicit type: 1)
9+
// CHECK: (5:5, 5:6): (inout String) -> () (explicit type: 0)
10+
// CHECK: (5:12, 5:17): String (explicit type: 1)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func x(_ param: Int) -> Int {
2+
param
3+
}
4+
5+
let y: (String) -> Void = { param in }
6+
7+
let z = { (param: String) in
8+
param.count
9+
}
10+
11+
// RUN: %sourcekitd-test -req=collect-var-type %s -- %s | %FileCheck %s
12+
// CHECK: (1:10, 1:15): Int (explicit type: 1)
13+
// CHECK: (5:5, 5:6): (String) -> Void (explicit type: 1)
14+
// CHECK: (5:29, 5:34): String (explicit type: 0)
15+
// CHECK: (7:5, 7:6): (String) -> Int (explicit type: 0)
16+
// CHECK: (7:12, 7:17): String (explicit type: 1)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let x = 1
2+
let y = "abc"
3+
let z: String = "def"
4+
var w = 4
5+
6+
// RUN: %sourcekitd-test -req=collect-var-type -pos=2:1 -end-pos=4:1 %s -- %s | %FileCheck %s
7+
// CHECK-NOT: (1:5, 1:6)
8+
// CHECK: (2:5, 2:6): String (explicit type: 0)
9+
// CHECK: (3:5, 3:6): String (explicit type: 1)
10+
// CHECK-NOT: (4:5, 4:6)

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ static void printNameTranslationInfo(sourcekitd_variant_t Info, llvm::raw_ostrea
6767
static void printRangeInfo(sourcekitd_variant_t Info, StringRef Filename,
6868
llvm::raw_ostream &OS);
6969
static void printExpressionType(sourcekitd_variant_t Info, llvm::raw_ostream &OS);
70-
static void printVariableType(sourcekitd_variant_t Info, llvm::raw_ostream &OS);
70+
static void printVariableType(sourcekitd_variant_t Info,
71+
llvm::MemoryBuffer *SourceBuf,
72+
llvm::raw_ostream &OS);
7173
static void printDocInfo(sourcekitd_variant_t Info, StringRef Filename);
7274
static void printInterfaceGen(sourcekitd_variant_t Info, bool CheckASCII);
7375
static void printSemanticInfo();
@@ -1267,7 +1269,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
12671269
break;
12681270

12691271
case SourceKitRequest::CollectVariableType:
1270-
printVariableType(Info, llvm::outs());
1272+
printVariableType(Info, SourceBuf.get(), llvm::outs());
12711273
break;
12721274

12731275
case SourceKitRequest::DocInfo:
@@ -1961,7 +1963,9 @@ static void printExpressionType(sourcekitd_variant_t Info, llvm::raw_ostream &OS
19611963
OS << "</ExpressionTypes>\n";
19621964
}
19631965

1964-
static void printVariableType(sourcekitd_variant_t Info, llvm::raw_ostream &OS) {
1966+
static void printVariableType(sourcekitd_variant_t Info,
1967+
llvm::MemoryBuffer *SourceBuf,
1968+
llvm::raw_ostream &OS) {
19651969
auto TypeBuffer =
19661970
sourcekitd_variant_dictionary_get_value(Info, KeyVariableTypeList);
19671971
unsigned Count = sourcekitd_variant_array_get_count(TypeBuffer);
@@ -1974,9 +1978,16 @@ static void printVariableType(sourcekitd_variant_t Info, llvm::raw_ostream &OS)
19741978
sourcekitd_variant_t Item = sourcekitd_variant_array_get_value(TypeBuffer, i);
19751979
unsigned Offset = sourcekitd_variant_dictionary_get_int64(Item, KeyVariableOffset);
19761980
unsigned Length = sourcekitd_variant_dictionary_get_int64(Item, KeyVariableLength);
1981+
auto Start = resolveToLineCol(Offset, SourceBuf);
1982+
auto End = resolveToLineCol(Offset + Length, SourceBuf);
19771983
bool HasExplicitType = sourcekitd_variant_dictionary_get_bool(Item, KeyVariableTypeExplicit);
1978-
OS << "(" << Offset << ", " << Offset + Length << "): "
1979-
<< sourcekitd_variant_dictionary_get_string(Item, KeyVariableType)
1984+
auto PrintedType = sourcekitd_variant_dictionary_get_string(Item, KeyVariableType);
1985+
OS << "("
1986+
<< Start.first << ":" << Start.second
1987+
<< ", "
1988+
<< End.first << ":" << End.second
1989+
<< "): "
1990+
<< PrintedType
19801991
<< " (explicit type: " << HasExplicitType << ")\n";
19811992
}
19821993
OS << "</VariableTypes>\n";

0 commit comments

Comments
 (0)