Skip to content

[CodeCompletion] Add pretty stacktrace for completion operation #30954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/swift/AST/PrettyStackTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ void printDifferentiabilityWitnessDescription(
llvm::raw_ostream &out, const SILDifferentiabilityWitnessKey key,
bool addNewline = true);

/// PrettyStackTraceDeclContext - Observe that we are processing a
/// specific decl context.
class PrettyStackTraceDeclContext : public llvm::PrettyStackTraceEntry {
const DeclContext *DC;
const char *Action;
public:
PrettyStackTraceDeclContext(const char *action, const DeclContext *DC)
: DC(DC), Action(action) {}
virtual void print(llvm::raw_ostream &OS) const;
};

} // end namespace swift

#endif
7 changes: 7 additions & 0 deletions lib/AST/PrettyStackTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,10 @@ void swift::printDifferentiabilityWitnessDescription(
if (addNewline)
out << '\n';
}

void PrettyStackTraceDeclContext::print(llvm::raw_ostream &out) const {
out << "While " << Action << " in decl context:\n";
out << " ---\n";
DC->printContext(out, /*indent=*/4);
out << " ---\n";
}
23 changes: 17 additions & 6 deletions lib/IDE/CompletionInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/Module.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/SourceFile.h"
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Driver/FrontendUtil.h"
#include "swift/Frontend/Frontend.h"
Expand Down Expand Up @@ -167,6 +169,8 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
llvm::MemoryBuffer *completionBuffer, unsigned int Offset,
DiagnosticConsumer *DiagC,
llvm::function_ref<void(CompilerInstance &, bool)> Callback) {
llvm::PrettyStackTraceString trace(
"While performing cached completion if possible");

if (!CachedCI)
return false;
Expand Down Expand Up @@ -218,7 +222,7 @@ bool CompletionInstance::performCachedOperaitonIfPossible(

auto &newInfo = newState->getCodeCompletionDelayedDeclState();
unsigned newBufferID;

DeclContext *traceDC = nullptr;
switch (newInfo.Kind) {
case CodeCompletionDelayedDeclKind::FunctionBody: {
// If the interface has changed, AST must be refreshed.
Expand Down Expand Up @@ -285,6 +289,7 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
if (AFD->isBodySkipped())
AFD->setBodyDelayed(AFD->getBodySourceRange());

traceDC = AFD;
break;
}
case CodeCompletionDelayedDeclKind::Decl:
Expand Down Expand Up @@ -329,6 +334,7 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
performImportResolution(*newSF);
bindExtensions(*newSF);

traceDC = newM;
#ifndef NDEBUG
const auto *reparsedState = newSF->getDelayedParserState();
assert(reparsedState->hasCodeCompletionDelayedDeclState() &&
Expand All @@ -341,13 +347,17 @@ bool CompletionInstance::performCachedOperaitonIfPossible(
}
}

if (DiagC)
CI.addDiagnosticConsumer(DiagC);
{
PrettyStackTraceDeclContext trace("performing cached completion", traceDC);

Callback(CI, /*reusingASTContext=*/true);
if (DiagC)
CI.addDiagnosticConsumer(DiagC);

if (DiagC)
CI.removeDiagnosticConsumer(DiagC);
Callback(CI, /*reusingASTContext=*/true);

if (DiagC)
CI.removeDiagnosticConsumer(DiagC);
}

CachedReuseCount += 1;

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

auto TheInstance = std::make_unique<CompilerInstance>();
{
Expand Down