Skip to content

Commit a35b2d4

Browse files
committed
[code-completion] Sends compile notifications even if argument parsing fails
For now we don't get a diagnostic, but at least there is a will-compile containing the arguments.
1 parent b823db8 commit a35b2d4

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: not %sourcekitd-test -req=track-compiles == -req=complete %s -offset=0 -- %s -no-such-arg | %FileCheck %s -check-prefix=ARG_PARSE_1
2+
// ARG_PARSE_1: {
3+
// ARG_PARSE_1: key.notification: source.notification.compile-will-start
4+
// ARG_PARSE_1: key.compileid: [[CID1:".*"]]
5+
// ARG_PARSE_1: key.compilerargs-string: "{{.*}}.swift -no-such-arg"
6+
// ARG_PARSE_1: }
7+
// ARG_PARSE_1: {
8+
// ARG_PARSE_1: key.notification: source.notification.compile-did-finish
9+
// ARG_PARSE_1: key.diagnostics: [
10+
// FIXME: we should pass through the error from parsing the arguments
11+
// ARG_PARSE_1-NEXT: ]
12+
// ARG_PARSE_1: key.compileid: [[CID1]]
13+
// ARG_PARSE_1: }
14+
// ARG_PARSE_1-NOT: compile-will-start
15+
// ARG_PARSE_1-NOT: compile-did-finish

tools/SourceKit/lib/SwiftLang/SwiftCompletion.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ static bool swiftCodeCompleteImpl(SwiftLangSupport &Lang,
119119
UnresolvedInputFile->getBuffer(),
120120
Lang.resolvePathSymlinks(UnresolvedInputFile->getBufferIdentifier()));
121121

122+
auto origBuffSize = InputFile->getBufferSize();
123+
unsigned CodeCompletionOffset = Offset;
124+
if (CodeCompletionOffset > origBuffSize) {
125+
CodeCompletionOffset = origBuffSize;
126+
}
127+
122128
CompilerInstance CI;
123129
// Display diagnostics to stderr.
124130
PrintingDiagnosticConsumer PrintDiags;
@@ -128,6 +134,12 @@ static bool swiftCodeCompleteImpl(SwiftLangSupport &Lang,
128134
trace::TracedOperation TracedOp(trace::OperationKind::CodeCompletion);
129135
if (TracedOp.enabled()) {
130136
CI.addDiagnosticConsumer(&TraceDiags);
137+
trace::SwiftInvocation SwiftArgs;
138+
trace::initTraceInfo(SwiftArgs, InputFile->getBufferIdentifier(), Args);
139+
TracedOp.start(SwiftArgs,
140+
{std::make_pair("OriginalOffset", std::to_string(Offset)),
141+
std::make_pair("Offset",
142+
std::to_string(CodeCompletionOffset))});
131143
}
132144

133145
CompilerInvocation Invocation;
@@ -141,12 +153,6 @@ static bool swiftCodeCompleteImpl(SwiftLangSupport &Lang,
141153
return false;
142154
}
143155

144-
auto origBuffSize = InputFile->getBufferSize();
145-
unsigned CodeCompletionOffset = Offset;
146-
if (CodeCompletionOffset > origBuffSize) {
147-
CodeCompletionOffset = origBuffSize;
148-
}
149-
150156
const char *Position = InputFile->getBufferStart() + CodeCompletionOffset;
151157
std::unique_ptr<llvm::WritableMemoryBuffer> NewBuffer =
152158
llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
@@ -178,15 +184,6 @@ static bool swiftCodeCompleteImpl(SwiftLangSupport &Lang,
178184
return true;
179185
}
180186

181-
if (TracedOp.enabled()) {
182-
trace::SwiftInvocation SwiftArgs;
183-
trace::initTraceInfo(SwiftArgs, InputFile->getBufferIdentifier(), Args);
184-
TracedOp.start(SwiftArgs,
185-
{std::make_pair("OriginalOffset", std::to_string(Offset)),
186-
std::make_pair("Offset",
187-
std::to_string(CodeCompletionOffset))});
188-
}
189-
190187
CloseClangModuleFiles scopedCloseFiles(
191188
*CI.getASTContext().getClangModuleLoader());
192189
SwiftConsumer.setContext(&CI.getASTContext(), &Invocation,

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ struct NotificationBuffer {
139139
};
140140
static NotificationBuffer notificationBuffer;
141141

142+
static void printBufferedNotifications() {
143+
notificationBuffer.handleNotifications([](sourcekitd_response_t note) {
144+
sourcekitd_response_description_dump_filedesc(note, STDOUT_FILENO);
145+
});
146+
}
147+
142148
static int skt_main(int argc, const char **argv);
143149

144150
int main(int argc, const char **argv) {
@@ -173,12 +179,6 @@ static int skt_main(int argc, const char **argv) {
173179
#define REFACTORING(KIND, NAME, ID) Kind##Refactoring##KIND = sourcekitd_uid_get_from_cstr("source.refactoring.kind."#ID);
174180
#include "swift/IDE/RefactoringKinds.def"
175181

176-
auto printBufferedNotifications = []{
177-
notificationBuffer.handleNotifications([](sourcekitd_response_t note) {
178-
sourcekitd_response_description_dump_filedesc(note, STDOUT_FILENO);
179-
});
180-
};
181-
182182
// A test invocation may initialize the options to be used for subsequent
183183
// invocations.
184184
TestOptions InitOpts;
@@ -196,7 +196,6 @@ static int skt_main(int argc, const char **argv) {
196196
sourcekitd_shutdown();
197197
return ret;
198198
}
199-
printBufferedNotifications();
200199
Args = Args.slice(i+1);
201200
}
202201

@@ -343,7 +342,9 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
343342

344343
assert(Opts.repeatRequest >= 1);
345344
for (unsigned i = 0; i < Opts.repeatRequest; ++i) {
346-
if (int ret = handleTestInvocation(Opts, InitOpts)) {
345+
int ret = handleTestInvocation(Opts, InitOpts);
346+
printBufferedNotifications();
347+
if (ret) {
347348
return ret;
348349
}
349350
}

0 commit comments

Comments
 (0)