Skip to content

[nfc][cxx-interop] Add some pretty stack traces for various things that crash a lot. #41876

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
Mar 18, 2022
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
13 changes: 13 additions & 0 deletions include/swift/AST/PrettyStackTrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ class PrettyStackTraceDecl : public llvm::PrettyStackTraceEntry {
virtual void print(llvm::raw_ostream &OS) const override;
};

/// PrettyStackTraceDecl - Observe that we are processing a specific
/// declaration with a given substitution map.
class PrettyStackTraceDeclAndSubst : public llvm::PrettyStackTraceEntry {
const Decl *decl;
SubstitutionMap subst;
const char *action;
public:
PrettyStackTraceDeclAndSubst(const char *action, SubstitutionMap subst,
const Decl *decl)
: decl(decl), subst(subst), action(action) {}
virtual void print(llvm::raw_ostream &OS) const override;
};

/// PrettyStackTraceAnyFunctionRef - Observe that we are processing a specific
/// function or closure literal.
class PrettyStackTraceAnyFunctionRef : public llvm::PrettyStackTraceEntry {
Expand Down
13 changes: 13 additions & 0 deletions lib/AST/PrettyStackTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ void PrettyStackTraceDecl::print(llvm::raw_ostream &out) const {
printDeclDescription(out, TheDecl, TheDecl->getASTContext());
}

void PrettyStackTraceDeclAndSubst::print(llvm::raw_ostream &out) const {
out << "While " << action << ' ';
if (!decl) {
out << "NULL declaration!\n";
return;
}
printDeclDescription(out, decl, decl->getASTContext());

out << "with substitution map: ";
subst.dump(out);
}


void swift::printDeclDescription(llvm::raw_ostream &out, const Decl *D,
const ASTContext &Context, bool addNewline) {
SourceLoc loc = D->getStartLoc();
Expand Down
3 changes: 3 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "swift/AST/Module.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/Types.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Platform.h"
Expand Down Expand Up @@ -5582,6 +5583,8 @@ static ValueDecl *generateThunkForExtraMetatypes(SubstitutionMap subst,
ConcreteDeclRef
ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
ValueDecl *decl) {
PrettyStackTraceDeclAndSubst trace("specializing", subst, decl);

assert(isa<clang::FunctionTemplateDecl>(decl->getClangDecl()) &&
"This API should only be used with function templates.");

Expand Down
5 changes: 5 additions & 0 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "swift/AST/Module.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/Type.h"
#include "swift/AST/Types.h"
#include "swift/AST/TypeVisitor.h"
Expand Down Expand Up @@ -211,6 +212,10 @@ namespace {

using TypeVisitor::Visit;
ImportResult Visit(clang::QualType type) {
PrettyStackTraceClangType trace(Impl.getClangASTContext(),
"importing a clang type",
type.getTypePtr());

auto IR = Visit(type.getTypePtr());
return IR;
}
Expand Down