Skip to content

Commit cca6fea

Browse files
authored
Merge pull request swiftlang#22835 from nkcsgexi/custom-buffer
sourcekitd: define a new custom buffer for expression type response.
2 parents 9bd3935 + d006afc commit cca6fea

File tree

15 files changed

+273
-28
lines changed

15 files changed

+273
-28
lines changed

lib/IDE/IDETypeChecking.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,11 +638,13 @@ class ExpressionTypeCollector: public SourceEntityWalker {
638638
{
639639
llvm::raw_svector_ostream OS(Buffer);
640640
E->getType()->getRValueType()->reconstituteSugar(true)->print(OS);
641+
// Ensure the end user can directly use the char*
642+
OS << '\0';
641643
}
642644

643645
// Add the type information to the result list.
644646
Results.push_back({Offset, Length, getTypeOffsets(Buffer.str()),
645-
static_cast<uint32_t>(Buffer.size())});
647+
static_cast<uint32_t>(Buffer.size()) - 1});
646648

647649
// Keep track of that we have a type reported for this range.
648650
AllPrintedTypes[Offset].insert(Length);
Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
1-
func foo() { return 1 }
1+
func foo() -> Int { return 1 }
2+
3+
func bar(f: Float) -> Float { return bar(f: 1) }
4+
5+
protocol P {}
6+
7+
func fooP(_ p: P) { fooP(p) }
8+
9+
class C {}
10+
11+
func ArrayC(_ a: [C]) {
12+
_ = a.count
13+
_ = a.description.count.advanced(by: 1).description
14+
}
15+
16+
struct S {
17+
let val = 4
18+
}
19+
func DictS(_ a: [Int: S]) {
20+
_ = a[2]?.val.advanced(by: 1).byteSwapped
21+
}
222

323
// RUN: %sourcekitd-test -req=collect-type %s -- %s | %FileCheck %s
4-
// CHECK: (20, 21): Int
24+
// CHECK: (183, 202): Int
25+
// CHECK: (183, 196): String
26+
// CHECK: (183, 184): [C]
27+
// CHECK: (203, 211): (Int) -> (Int) -> Int
28+
// CHECK: (211, 218): (by: Int)
29+
// CHECK: (216, 217): Int
30+
// CHECK: (257, 258): Int
31+
// CHECK: (291, 332): ()
32+
// CHECK: (291, 292): Int?
33+
// CHECK: (295, 332): Int?
34+
// CHECK: (295, 320): Int

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ struct ExpressionType {
128128
unsigned ExprOffset;
129129
unsigned ExprLength;
130130
unsigned TypeOffset;
131-
unsigned TypeLength;
132131
};
133132

134133
struct ExpressionTypesInFile {

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,8 +2057,7 @@ void SwiftLangSupport::collectExpressionTypes(StringRef FileName,
20572057
llvm::raw_svector_ostream OS(TypeBuffer);
20582058
ExpressionTypesInFile Result;
20592059
for (auto Item: collectExpressionType(*SF, Scratch, OS)) {
2060-
Result.Results.push_back({Item.offset, Item.length, Item.typeOffset,
2061-
Item.typeLength});
2060+
Result.Results.push_back({Item.offset, Item.length, Item.typeOffset});
20622061
}
20632062
Result.TypeBuffer = OS.str();
20642063
Receiver(Result);

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,20 +1537,18 @@ static void printRangeInfo(sourcekitd_variant_t Info, StringRef FilenameIn,
15371537
}
15381538

15391539
static void printExpressionType(sourcekitd_variant_t Info, llvm::raw_ostream &OS) {
1540-
auto *TypeBuffer = sourcekitd_variant_dictionary_get_string(Info, KeyTypeBuffer);
1541-
sourcekitd_variant_t ExprList = sourcekitd_variant_dictionary_get_value(Info,
1542-
KeyExpressionTypeList);
1543-
unsigned Count = sourcekitd_variant_array_get_count(ExprList);
1540+
auto TypeBuffer = sourcekitd_variant_dictionary_get_value(Info, KeyExpressionTypeList);
1541+
unsigned Count = sourcekitd_variant_array_get_count(TypeBuffer);
1542+
if (!Count) {
1543+
OS << "cannot find expression types in the file\n";
1544+
return;
1545+
}
15441546
for (unsigned i = 0; i != Count; ++i) {
1545-
sourcekitd_variant_t Item = sourcekitd_variant_array_get_value(ExprList, i);
1547+
sourcekitd_variant_t Item = sourcekitd_variant_array_get_value(TypeBuffer, i);
15461548
unsigned Offset = sourcekitd_variant_dictionary_get_int64(Item, KeyExpressionOffset);
15471549
unsigned Length = sourcekitd_variant_dictionary_get_int64(Item, KeyExpressionLength);
1548-
StringRef PrintedType(TypeBuffer + sourcekitd_variant_dictionary_get_int64(Item,
1549-
KeyTypeOffset), sourcekitd_variant_dictionary_get_int64(Item, KeyTypeLength));
1550-
OS << "(" << Offset << ", " << Offset + Length << "): " << PrintedType << "\n";
1551-
}
1552-
if (!Count) {
1553-
OS << "cannot find expression types in the file\n";
1550+
OS << "(" << Offset << ", " << Offset + Length << "): " <<
1551+
sourcekitd_variant_dictionary_get_string(Item, KeyExpressionType) << "\n";
15541552
}
15551553
}
15561554

tools/SourceKit/tools/sourcekitd/include/sourcekitd/CompactArray.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CompactArrayBuilderImpl {
2222
public:
2323
std::unique_ptr<llvm::MemoryBuffer> createBuffer() const;
2424
void appendTo(llvm::SmallVectorImpl<char> &Buf) const;
25-
25+
unsigned copyInto(char *BufPtr) const;
2626
size_t sizeInBytes() const;
2727
bool empty() const;
2828

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===--- ExpressionTypeArray.h - --------------------------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_SOURCEKITD_EXPRESSION_TYPE_ARRAY_H
14+
#define LLVM_SOURCEKITD_EXPRESSION_TYPE_ARRAY_H
15+
16+
#include "sourcekitd/Internal.h"
17+
18+
namespace SourceKit {
19+
struct ExpressionType;
20+
}
21+
22+
namespace sourcekitd {
23+
VariantFunctions *getVariantFunctionsForExpressionTypeArray();
24+
25+
class ExpressionTypeArrayBuilder {
26+
public:
27+
ExpressionTypeArrayBuilder(llvm::StringRef PrintedType);
28+
~ExpressionTypeArrayBuilder();
29+
30+
void add(const SourceKit::ExpressionType &ExpType);
31+
std::unique_ptr<llvm::MemoryBuffer> createBuffer();
32+
static VariantFunctions Funcs;
33+
34+
private:
35+
struct Implementation;
36+
Implementation &Impl;
37+
};
38+
39+
}
40+
41+
#endif

tools/SourceKit/tools/sourcekitd/include/sourcekitd/Internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ enum class CustomBufferKind {
5959
InheritedTypesArray,
6060
DocStructureElementArray,
6161
AttributesArray,
62+
ExpressionTypeArray,
6263
RawData
6364
};
6465

tools/SourceKit/tools/sourcekitd/lib/API/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set(sourcekitdAPI_sources
88
Requests.cpp
99
sourcekitdAPI-Common.cpp
1010
TokenAnnotationsArray.cpp
11+
ExpressionTypeArray.cpp
1112
)
1213

1314
set(sourcekitdAPI_Darwin_sources

tools/SourceKit/tools/sourcekitd/lib/API/CompactArray.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ void CompactArrayBuilderImpl::copyInto(char *BufPtr, size_t Length) const {
9494
memcpy(BufPtr, StringBuffer.data(), StringBuffer.size());
9595
}
9696

97+
unsigned CompactArrayBuilderImpl::copyInto(char *BufPtr) const {
98+
size_t Length = sizeInBytes();
99+
copyInto(BufPtr, Length);
100+
return Length;
101+
}
102+
97103
bool CompactArrayBuilderImpl::empty() const {
98104
return EntriesBuffer.empty();
99105
}

0 commit comments

Comments
 (0)