This is an archive of the discontinued LLVM Phabricator instance.

[clang][TypePrinter] Support expression template arguments when checking defaultedness
ClosedPublic

Authored by Michael137 on Jan 26 2023, 7:35 AM.

Details

Summary

This patch adds support for TemplateArguments of kind
TemplateArgument::Expression to clang::isSubstitutedDefaultArgument.
We do so by evaluating both the Pattern and Arg expression to an
APInt, if we can, and comparing the results.

This will be useful in an upcoming change where
clang::isSubstitutedDefaultArgument gets called from clang::Sema
where the TemplateArguments are instantiated as expressions (without
being evaluted to APInt beforehand).

Testing

  • Added unit-tests

Diff Detail

Event Timeline

Michael137 created this revision.Jan 26 2023, 7:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 26 2023, 7:35 AM
Michael137 requested review of this revision.Jan 26 2023, 7:35 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 26 2023, 7:35 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

I think this looks correct, but 1 thing I found.

clang/lib/AST/TypePrinter.cpp
2020

this left over from debugging?

Michael137 added inline comments.Jan 26 2023, 8:07 AM
clang/lib/AST/TypePrinter.cpp
2020

Yes! Good catch!

  • Remove dump() leftover from debugging
Michael137 marked an inline comment as done.Jan 26 2023, 9:34 AM
aprantl accepted this revision.Jan 26 2023, 9:36 AM
aprantl added inline comments.
clang/lib/AST/TypePrinter.cpp
2018

nit: . at the end.

2019

I assume you checked that this is always non-null?

2031

Just for my own education: what's an example for a value-dependent constant integer expression?

This revision is now accepted and ready to land.Jan 26 2023, 9:36 AM
Michael137 added inline comments.Jan 26 2023, 11:03 AM
clang/lib/AST/TypePrinter.cpp
2031

There is no such case, we're checking it here because it's a pre-condition for isIntegerConstantExpr. But we can have value-dependent expressions here like sizeof(T) we so can't unconditionally call isIntegerConstantExpr

Michael137 added inline comments.Jan 26 2023, 11:11 AM
clang/lib/AST/TypePrinter.cpp
2019

I didn't add a null-check because no other caller around clang does it (including isSubstitutedTemplateArgument which precedes this call here). So we've already been doing this on top-of-tree for a while in this codepath. But may be worth a followup audit. Though I suspect the assumption is that we never construct TemplateArguments with nullptr Exprs. @erichkeane may know some more about this

This revision was landed with ongoing or failed builds.Jan 26 2023, 6:35 PM
This revision was automatically updated to reflect the committed changes.