Skip to content
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
24 changes: 5 additions & 19 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3237,26 +3237,12 @@ namespace {

Decl *VisitClassTemplateSpecializationDecl(
const clang::ClassTemplateSpecializationDecl *decl) {
bool isPair = decl->getSpecializedTemplate()->isInStdNamespace() &&
decl->getSpecializedTemplate()->getName() == "pair";

// Before we go any further, check if we've already got tens of thousands
// of specializations. If so, it means we're likely instantiating a very
// deep/complex template, or we've run into an infinite loop. In either
// case, its not worth the compile time, so bail.
// TODO: this could be configurable at some point.
size_t specializationLimit = !isPair ? 1000 : 10000;
if (size_t(
llvm::size(decl->getSpecializedTemplate()->specializations())) >
specializationLimit) {
// Note: it would be nice to import a dummy unavailable struct,
// but we would then need to instantiate the template here,
// as we cannot import a struct without a definition. That would
// defeat the purpose. Also, we can't make the dummy
// struct simply unavailable, as that still makes the
// typelias that references it available.
// Importing std::conditional substantially increases compile times when
// building with libstdc++, i.e. on most Linux distros.
if (decl->isInStdNamespace() && decl->getIdentifier() &&
(decl->getName() == "conditional" || decl->getName() == "__or_" ||
decl->getName() == "_Expr"))
return nullptr;
}

// `decl->getDefinition()` can return nullptr before the call to sema and
// return its definition afterwards.
Expand Down
2 changes: 1 addition & 1 deletion test/Interop/Cxx/templates/many-specializations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
// CHECK-NEXT: struct TemplateStruct<T> {
// CHECK-NEXT: }

// CHECK-NOT: typealias
// CHECK: typealias T1000