This is an archive of the discontinued LLVM Phabricator instance.

Fix a crash when an error occurs in Template and the initializer is a nullptr for C++17
ClosedPublic

Authored by bviyer on Jul 24 2018, 4:10 PM.

Details

Summary

When the error has occurred for one of the variables inside a template, the Loc function will try to find the end-location of the final item. If this variable is default initialized to nullptr, then the templateArgumentLoc function is invoked. It has an assert to see if the Argument's kind is Expression. If the default initializer is a nullptr, then the argument's kind will be NullPtr, not Expression. This will cause a compiler crash. This patch will allow NullPtr as a possibility for expression's kind.

Diff Detail

Repository
rL LLVM

Event Timeline

bviyer created this revision.Jul 24 2018, 4:10 PM

Hi Balaji,

I reduced your testcase a bit more, this looks like the can be a crash on valid. Can you use the more minimal version in the testcase?

template <typename a, int* = nullptr>
struct e {
    e(a) {}
};
e c(0);

Also: you should add cfe-commits as a subscriber when creating new phab revisions. IIRC there was some issue with adding cfe-commits after the fact.

include/clang/AST/TemplateBase.h
469 ↗(On Diff #157156)

Please use // comments in C++ files!

I think this comment is pretty superfluous though. If a future reader came across this then they would probably not care too much about the details of this bug.

474–475 ↗(On Diff #157156)

I think we should let this constructor work with any non type template argument. If you agree, could you add the extra cases? (Should just be TemplateArgument::Declaration and TemplateArgument::Integral)

bviyer updated this revision to Diff 157721.Jul 27 2018, 11:24 AM

I have fixed all the changes requested by Erik along with shortening the test case.

bviyer marked 2 inline comments as done.Jul 27 2018, 11:24 AM
erik.pilkington accepted this revision.Jul 27 2018, 11:35 AM

LGTM, thanks! Do you have your commit rights yet?

include/clang/AST/TemplateBase.h
469 ↗(On Diff #157721)

This comment sounds pretty strange, how about "Permit any kind of template argument that can be represented with an expression"?

This revision is now accepted and ready to land.Jul 27 2018, 11:35 AM
bviyer updated this revision to Diff 157730.Jul 27 2018, 11:56 AM

Fixed the comment as you suggested.

I do not have check in rights yet. Can you please check it in for me?

bviyer marked an inline comment as done.Jul 27 2018, 11:57 AM
This revision was automatically updated to reflect the committed changes.