This is an archive of the discontinued LLVM Phabricator instance.

Fix for crash due to g++.old-deja/g++.other/using3.C
ClosedPublic

Authored by dinesh.d on Mar 12 2014, 12:37 AM.

Details

Reviewers
rjmccall
rsmith
Summary

Following patch fixes crash in clang with attached test case [taken from g++.old-deja/g++.other/using3.C].

Issue:

Test is trying to make using declaration for a non-existing member in base class in derived class and expect error
while compiling. Current clang code handles normal cases properly.

Problem arises when base class is typedefed to a type constructed in-place. As using declaration is referring to a
non-existing member, normal lookup fails and while trying typo-correction, clang returns typedef declaration as
possible alternative, which is not a record type and can not be type casted to EnumDecl/ RecordDecl/ CXXRecordDecl.

But Sema::CheckUsingShadowDecl() assumes otherwise and tries to typecast it to CXXRecordDecl without any check
while checking if referred type is from base class or not. This results in crash in clang.

Solution:

I looked for ways to get CXXRecordDecl from TypedefDecl but did not find any. I found function like getUnderlyingDecl()
but they just work with UsingDecls and ObjCCompatibleAliasDecls. Moreover, for any existing member, clang find proper
declaration and everything works fine.

So I have added a check in Sema::CheckUsingShadowDecl() to bypass base class check if referred type in using declaration
is not in a record type. A proper fix may be if we can get proper RecordDecl from TypedefDecl and use it for this check
or handle TypedefDecl case separately.

Diff Detail

Event Timeline

Submitted bug report [http://llvm.org/bugs/show_bug.cgi?id=19171]

Should we update typo-correction code, to suggest only base class member if using declaration is inside derivied class.

  • {F53369, layout=link}
dinesh.d updated this revision to Unknown Object (????).Apr 21 2014, 8:24 AM

Hi Richard,

Thanks for reviewing.

I have updated patch to handle this issue in typo-correction. This can be further improved is we can check if suggested typo belong to base class or not.

I have merged test case in using-decl-1.cpp as suggested.

Please have a look.

Thanks, this looks good. Please also add test cases (with FIXMEs) for the
two cases mentioned in your FIXME comment, then this looks fine to commit.

dinesh.d updated this revision to Unknown Object (????).Apr 22 2014, 5:08 AM

Added test cases with fix-me as per review comments.

rsmith accepted this revision.Apr 28 2014, 11:30 AM
rsmith edited edge metadata.

LGTM

lib/Sema/SemaDeclCXX.cpp
7338–7340

Is this still necessary?

This revision is now accepted and ready to land.Apr 28 2014, 11:30 AM
dinesh.d updated this revision to Diff 8904.Apr 29 2014, 3:06 AM
dinesh.d edited edge metadata.

Updated test-cases

updated test case fails if we comment lines 7335:7337.

using declaration for class member can not use templates id, scoped enumerators etc.

dinesh.d added inline comments.Apr 29 2014, 3:11 AM
lib/Sema/SemaDeclCXX.cpp
7338–7340

yes, I have added one test case which fails if we comment these lines. I have kept these lines so that my change just filter out non class member if RequiredMember is true but should not add any other type of class member which it was not considering before my changes.

Please commit the patch as-is, I'd like to get the crash fix in first before worrying about getting better typo correction. Let me know if you need me to commit this for you.

lib/Sema/SemaDeclCXX.cpp
7338–7340

I think your testcase shows that these lines are in fact incorrect -- if the function template were in a base class, rather than in an unrelated class, typo correction *should* find it.

dinesh.d added inline comments.Apr 29 2014, 10:26 PM
lib/Sema/SemaDeclCXX.cpp
7338–7340

No, in added test case, function template is not in base class.

It will be great if you can commit this for me.

dinesh.d closed this revision.Apr 30 2014, 2:49 PM

Thanks for committing this patch (r207677).

Committed as r207677.

FIXME for members of base classes fixed in r207680.
Support for typo-correcting to member templates added in r207681.

lib/Sema/SemaDeclCXX.cpp
7338–7340

Right, that's my point -- the reason we shouldn't find the function template is because it's not in a base class, *not* because it's a template. If it were in a base class but mistyped, typo correction *should* find it, but won't, because of this incorrect check.