This is an archive of the discontinued LLVM Phabricator instance.

[clang][NFC] Generate the {Type,ArrayType,UnaryExprOrType,Expression}Traits enumerations from TokenKinds.def...
ClosedPublic

Authored by riccibruno on Jun 9 2020, 3:20 AM.

Details

Summary

... and use the new macros from TokenKinds.def to remove the hard-coded lists of traits.

All the information needed to generate these enumerations is already present in TokenKinds.def.
The motivation here is to be able to dump the trait spelling without hard-coding the list in yet another
place.

Note that this change the order of the enumerators in the enumerations (except that in the TypeTrait
enumeration all unary type traits are before all binary type traits, and all binary type traits are before all
n-ary type traits).

Apart from the aforementioned ordering which is relied upon, after this patch no code in clang or in the
various clang tools depend on the specific ordering of the enumerators.

No functional changes intended.

Diff Detail

Event Timeline

riccibruno created this revision.Jun 9 2020, 3:20 AM
riccibruno marked an inline comment as done.Jun 9 2020, 3:23 AM
riccibruno added inline comments.
clang/include/clang/Basic/TokenKinds.def
65

It is slightly unfortunate to have to use both UNARY_EXPR_OR_TYPE_TRAIT and CXX11_UNARY_EXPR_OR_TYPE_TRAIT since users have to define both. I have no better idea unfortunately.

riccibruno marked 2 inline comments as done.Jun 9 2020, 3:36 AM
riccibruno added inline comments.
clang/include/clang/Basic/DiagnosticSemaKinds.td
6008

These two can merged in a further patch.

6014

Same.

riccibruno edited the summary of this revision. (Show Details)Jun 10 2020, 12:11 PM
riccibruno added a reviewer: eli.friedman.
aaron.ballman added inline comments.Jun 10 2020, 12:25 PM
clang/lib/Basic/ExpressionTraits.cpp
29–34

Isn't ET_Last -1?

clang/lib/Basic/TypeTraits.cpp
64

ATT_Last is also -1.

69

UETT_Last is also -1.

clang/lib/Sema/SemaExpr.cpp
3974

I think the original code was a bit more clear; would it make sense to make the diagnostic engine aware of trait kinds so that it does this dance for you? (It may be overkill given that we don't pass UnaryExprOrTypeTrait objects to the diagnostic engine THAT often, but I'm curious what you think.)

riccibruno marked 2 inline comments as done.Jun 10 2020, 1:21 PM

Thanks for the review Aaron! I have added some comments inline.

clang/lib/Basic/ExpressionTraits.cpp
29–34

Nope :) It's -1 plus 1 per element in the enumeration. I have added the enumerators ET_Last, ATT_Last and UETT_Last for consistency with UTT_Last, BTT_Last and TT_Last which are needed.

In this patch ET_Last, ATT_Last and UETT_Last are only used here in this assertion and could be replaced by the equivalent T < XX_Last where XX_Last is just added as the last element in the enumeration. However mixing XX_Last = XX_LastElement and XX_Last = LastElement + 1 would be very error-prone.

clang/lib/Sema/SemaExpr.cpp
3974

I don't think it is worthwhile since as you say UnaryExprOrTypeTrait objects are not frequently passed to the diagnostic engine.

Moreover I personally finds the explicit getTraitSpelling(TraitKind) clearer for two reasons:

  1. Frequently a non-class enumerator is used as an index to a select in a diagnostic, relying on the implicit integer conversion. Special casing UnaryExprOrTypeTrait would be surprising.
  1. (weaker) << TraitKind could mean something else than the trait's spelling; for example this could print the trait's name or some user-visible version thereof.
aaron.ballman accepted this revision.Jun 11 2020, 4:31 AM

LGTM, your choice on hiding in TokenKinds.def.

clang/lib/Basic/ExpressionTraits.cpp
29–34

Oh! I see now what's going on and that's clever; I was misunderstanding the second macro usage (which makes me wonder if it would make more sense to hide the Last fields at the bottom of TokenKinds.def rather than use the current approach, but I don't insist). Thank you for the clarification.

clang/lib/Sema/SemaExpr.cpp
3974

Sold! Thank you. :-)

This revision is now accepted and ready to land.Jun 11 2020, 4:31 AM
riccibruno marked 6 inline comments as done.Jun 11 2020, 5:02 AM

LGTM, your choice on hiding in TokenKinds.def.

Oh! I see now what's going on and that's clever; I was misunderstanding the second macro usage (which makes me wonder if it would make more sense to hide the Last fields at the bottom of TokenKinds.def rather than use the current approach, but I don't insist). Thank you for the clarification.

Ah, I admit I was grinning for a few minutes when I came up with this :) Yay for macros... I think I will leave the XX_Lasts in the enumeration since they are not really a trait but a structural feature of the enumerations. I will however add a comment next to each XX_Last explaining this trick.

Thanks for the review!

This revision was automatically updated to reflect the committed changes.