Skip to content

Commit 579e662

Browse files
authored
Merge pull request #30954 from rintaro/ide-completion-prettystacktrace
[CodeCompletion] Add pretty stacktrace for completion operation
2 parents a61223a + de6a402 commit 579e662

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

include/swift/AST/PrettyStackTrace.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@ void printDifferentiabilityWitnessDescription(
220220
llvm::raw_ostream &out, const SILDifferentiabilityWitnessKey key,
221221
bool addNewline = true);
222222

223+
/// PrettyStackTraceDeclContext - Observe that we are processing a
224+
/// specific decl context.
225+
class PrettyStackTraceDeclContext : public llvm::PrettyStackTraceEntry {
226+
const DeclContext *DC;
227+
const char *Action;
228+
public:
229+
PrettyStackTraceDeclContext(const char *action, const DeclContext *DC)
230+
: DC(DC), Action(action) {}
231+
virtual void print(llvm::raw_ostream &OS) const;
232+
};
233+
223234
} // end namespace swift
224235

225236
#endif

lib/AST/PrettyStackTrace.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,10 @@ void swift::printDifferentiabilityWitnessDescription(
288288
if (addNewline)
289289
out << '\n';
290290
}
291+
292+
void PrettyStackTraceDeclContext::print(llvm::raw_ostream &out) const {
293+
out << "While " << Action << " in decl context:\n";
294+
out << " ---\n";
295+
DC->printContext(out, /*indent=*/4);
296+
out << " ---\n";
297+
}

lib/IDE/CompletionInstance.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
#include "swift/AST/DiagnosticEngine.h"
1717
#include "swift/AST/DiagnosticsFrontend.h"
1818
#include "swift/AST/Module.h"
19+
#include "swift/AST/PrettyStackTrace.h"
1920
#include "swift/AST/SourceFile.h"
2021
#include "swift/Basic/LangOptions.h"
22+
#include "swift/Basic/PrettyStackTrace.h"
2123
#include "swift/Basic/SourceManager.h"
2224
#include "swift/Driver/FrontendUtil.h"
2325
#include "swift/Frontend/Frontend.h"
@@ -167,6 +169,8 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
167169
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
168170
DiagnosticConsumer *DiagC,
169171
llvm::function_ref<void(CompilerInstance &, bool)> Callback) {
172+
llvm::PrettyStackTraceString trace(
173+
"While performing cached completion if possible");
170174

171175
if (!CachedCI)
172176
return false;
@@ -218,7 +222,7 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
218222

219223
auto &newInfo = newState->getCodeCompletionDelayedDeclState();
220224
unsigned newBufferID;
221-
225+
DeclContext *traceDC = nullptr;
222226
switch (newInfo.Kind) {
223227
case CodeCompletionDelayedDeclKind::FunctionBody: {
224228
// If the interface has changed, AST must be refreshed.
@@ -285,6 +289,7 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
285289
if (AFD->isBodySkipped())
286290
AFD->setBodyDelayed(AFD->getBodySourceRange());
287291

292+
traceDC = AFD;
288293
break;
289294
}
290295
case CodeCompletionDelayedDeclKind::Decl:
@@ -329,6 +334,7 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
329334
performImportResolution(*newSF);
330335
bindExtensions(*newSF);
331336

337+
traceDC = newM;
332338
#ifndef NDEBUG
333339
const auto *reparsedState = newSF->getDelayedParserState();
334340
assert(reparsedState->hasCodeCompletionDelayedDeclState() &&
@@ -341,13 +347,17 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
341347
}
342348
}
343349

344-
if (DiagC)
345-
CI.addDiagnosticConsumer(DiagC);
350+
{
351+
PrettyStackTraceDeclContext trace("performing cached completion", traceDC);
346352

347-
Callback(CI, /*reusingASTContext=*/true);
353+
if (DiagC)
354+
CI.addDiagnosticConsumer(DiagC);
348355

349-
if (DiagC)
350-
CI.removeDiagnosticConsumer(DiagC);
356+
Callback(CI, /*reusingASTContext=*/true);
357+
358+
if (DiagC)
359+
CI.removeDiagnosticConsumer(DiagC);
360+
}
351361

352362
CachedReuseCount += 1;
353363

@@ -360,6 +370,7 @@ bool CompletionInstance::performNewOperation(
360370
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
361371
std::string &Error, DiagnosticConsumer *DiagC,
362372
llvm::function_ref<void(CompilerInstance &, bool)> Callback) {
373+
llvm::PrettyStackTraceString trace("While performing new completion");
363374

364375
auto TheInstance = std::make_unique<CompilerInstance>();
365376
{

0 commit comments

Comments
 (0)