Skip to content
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

fix(cxx_indexer): avoid crashing in dependent specialization w/o written arguments #5903

Merged
merged 2 commits into from
Oct 17, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 14 additions & 14 deletions kythe/cxx/indexer/cxx/IndexerASTHooks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3058,7 +3058,8 @@ bool IndexerASTVisitor::TraverseFunctionDecl(clang::FunctionDecl* FD) {
// };
// TODO(shahms): Fix this upstream by getting TraverseFunctionHelper to
// do the right thing.
if (auto* DFTSI = FD->getDependentSpecializationInfo()) {
if (auto* DFTSI = FD->getDependentSpecializationInfo();
DFTSI && DFTSI->TemplateArgumentsAsWritten) {
for (const auto arg : DFTSI->TemplateArgumentsAsWritten->arguments()) {
if (!TraverseTemplateArgumentLoc(arg)) {
return false;
Expand Down Expand Up @@ -3323,8 +3324,7 @@ bool IndexerASTVisitor::VisitFunctionDecl(clang::FunctionDecl* Decl) {
Observer.MakeNodeId(Observer.getDefaultClaimToken(), "");
// There are five flavors of function (see TemplateOrSpecialization in
// FunctionDecl).
const clang::TemplateArgumentLoc* ArgsAsWritten = nullptr;
unsigned NumArgsAsWritten = 0;
llvm::ArrayRef<clang::TemplateArgumentLoc> ArgsAsWritten;
const clang::TemplateArgumentList* Args = nullptr;
std::vector<std::pair<clang::TemplateName, SourceLocation>> TNs;
bool TNsAreSpeculative = false;
Expand Down Expand Up @@ -3354,8 +3354,7 @@ bool IndexerASTVisitor::VisitFunctionDecl(clang::FunctionDecl* Decl) {
GraphObserver::Confidence::NonSpeculative);
} else if (auto* FTSI = Decl->getTemplateSpecializationInfo()) {
if (FTSI->TemplateArgumentsAsWritten) {
ArgsAsWritten = FTSI->TemplateArgumentsAsWritten->getTemplateArgs();
NumArgsAsWritten = FTSI->TemplateArgumentsAsWritten->NumTemplateArgs;
ArgsAsWritten = FTSI->TemplateArgumentsAsWritten->arguments();
}
Args = FTSI->TemplateArguments;
TNs.emplace_back(clang::TemplateName(FTSI->getTemplate()),
Expand All @@ -3381,8 +3380,9 @@ bool IndexerASTVisitor::VisitFunctionDecl(clang::FunctionDecl* Decl) {
// specialize primary template f applied to no arguments. If instead the
// code read `friend void f<T>(T t)`, we would record that it specializes
// the primary template with type variable T.
ArgsAsWritten = DFTSI->TemplateArgumentsAsWritten->getTemplateArgs();
NumArgsAsWritten = DFTSI->TemplateArgumentsAsWritten->NumTemplateArgs;
if (DFTSI->TemplateArgumentsAsWritten) {
ArgsAsWritten = DFTSI->TemplateArgumentsAsWritten->arguments();
}
for (clang::FunctionTemplateDecl* FTD : DFTSI->getCandidates()) {
TNs.emplace_back(
clang::TemplateName(FTD->getTemplatedDecl()->getDescribedTemplate()),
Expand All @@ -3395,13 +3395,13 @@ bool IndexerASTVisitor::VisitFunctionDecl(clang::FunctionDecl* Decl) {
}
Marks.set_implicit(Job->UnderneathImplicitTemplateInstantiation ||
IsImplicit);
if (ArgsAsWritten || Args) {
if (!ArgsAsWritten.empty() || Args) {
bool CouldGetAllTypes = true;
std::vector<GraphObserver::NodeId> NIDS;
if (ArgsAsWritten) {
NIDS.reserve(NumArgsAsWritten);
for (unsigned I = 0; I < NumArgsAsWritten; ++I) {
if (auto ArgId = BuildNodeIdForTemplateArgument(ArgsAsWritten[I])) {
if (!ArgsAsWritten.empty()) {
NIDS.reserve(ArgsAsWritten.size());
for (const auto& Arg : ArgsAsWritten) {
if (auto ArgId = BuildNodeIdForTemplateArgument(Arg)) {
NIDS.push_back(ArgId.value());
} else {
CouldGetAllTypes = false;
Expand Down Expand Up @@ -3431,12 +3431,12 @@ bool IndexerASTVisitor::VisitFunctionDecl(clang::FunctionDecl* Decl) {
Observer.recordInstEdge(
DeclNode,
Observer.recordTappNode(SpecializedNode.value(), NIDS,
NumArgsAsWritten),
ArgsAsWritten.size()),
Confidence);
Observer.recordSpecEdge(
DeclNode,
Observer.recordTappNode(SpecializedNode.value(), NIDS,
NumArgsAsWritten),
ArgsAsWritten.size()),
Confidence);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ template <typename T> struct S {
//- @int ref BuiltinInt
//- @short ref BuiltinShort
long f<int, short>(T t) { return 1; }

//- @thing defines/binding AbsThing
template<typename U=void> U thing();
// TODO(shahms): Actually support this properly.
//- //@thing defines/binding DepSpecNT
//- //DepSpecNT specializes/speculative TAppAbsNT
//- //TAppAbsNT param.0 AbsThing
template<> void thing() {}
};