This is an archive of the discontinued LLVM Phabricator instance.

PR31469: Don't add friend template class decls to redecl chain in dependent contexts.
ClosedPublic

Authored by v.g.vassilev on Jan 6 2017, 7:11 AM.

Details

Reviewers
rsmith
Summary

Fixes a crash in modules where the template class decl becomes the most recent decl in the redeclaration chain and forcing the template instantiator try to instantiate the friend declaration, rather than the template definition.

In practice, A::list<int> produces a TemplateSpecializationType A::__1::list<int, allocator<type-parameter-0-0> >' failing to replace to subsitute the default argument to allocator<int>.

Diff Detail

Event Timeline

v.g.vassilev retitled this revision from to PR31469: Don't add friend template class decls to redecl chain in dependent contexts..
v.g.vassilev updated this object.
v.g.vassilev added a reviewer: rsmith.
v.g.vassilev set the repository for this revision to rL LLVM.
v.g.vassilev added a subscriber: cfe-commits.
kimgr added a subscriber: kimgr.Jan 6 2017, 9:41 AM

I think we're seeing similar new behavior in IWYU, I've been meaning to bring it up on cfe-dev. But since it might be relevant here, I'll add our observations.

To us, it looks like friends in templates form their own redecl chain. An example:

// template.h
void foo();

template<class T>
struct Template {
  friend void foo();
};

// foo.h
inline void foo() {
}

// main.cpp
#include "template.h"
#include "foo.h"

Template<int> i;

int main() {
  foo();  // attributed to Template<T>::foo
}

This behavior changed in r283207.

We never got to the bottom of whether the redecl shuffling is correct or not, so we decided to just work around it, our code here:
https://github.com/include-what-you-use/include-what-you-use/blob/master/iwyu_ast_util.cc#L842

Relevant to this patch, I wonder if you're also about to work around a more central problem?

@kimgr, here is the discussion about the change in r283207 https://reviews.llvm.org/D16989

rsmith added inline comments.Jan 9 2017, 2:06 PM
lib/Sema/SemaTemplate.cpp
1237–1238

You should keep the class out of its redeclaration chain too.

v.g.vassilev removed rL LLVM as the repository for this revision.
v.g.vassilev marked an inline comment as done.

Address comments.

@yaron.keren, it seems that http://llvm.org/pr30994 concerns friend function declarations. My current patch focuses on friend class templates. Perhaps we should open another review item for a fix of http://llvm.org/pr30994.

rsmith accepted this revision.Jan 11 2017, 1:47 PM
rsmith edited edge metadata.
This revision is now accepted and ready to land.Jan 11 2017, 1:47 PM

@yaron.keren, it seems that http://llvm.org/pr30994 concerns friend function declarations. My current patch focuses on friend class templates. Perhaps we should open another review item for a fix of http://llvm.org/pr30994.

yes, of course.

v.g.vassilev closed this revision.Jan 12 2017, 1:27 AM

Landed in r291753.