This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Use a macro for non-C keywords
AbandonedPublic

Authored by sstwcw on Mar 15 2022, 4:24 PM.

Details

Summary

We had to add a bunch of keywords for a new language. Using the old
method, we had to add each keyword in the declaration, initialization,
and maybe in several lists, which turned out to be tiring and
error-prone.

Diff Detail

Event Timeline

sstwcw created this revision.Mar 15 2022, 4:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 15 2022, 4:24 PM
sstwcw requested review of this revision.Mar 15 2022, 4:24 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 15 2022, 4:24 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
sstwcw edited the summary of this revision. (Show Details)Mar 16 2022, 12:36 AM
HazardyKnusperkeks edited reviewers, added: MyDeveloperDay, HazardyKnusperkeks, curdeius, owenpan; removed: Restricted Project.Mar 16 2022, 1:35 PM
clang/lib/Format/FormatToken.h
907

Doubled

911

Doubled

924

doubled

947

Why not CSharp?

991–999

No fan of DEFINE_LIKE_NAMES. More a fan of scoped::enums. ;)

1149

Keep the comments.

clang/lib/Format/UnwrappedLineFormatter.cpp
115

I can live with that, but the former was nicer.

sstwcw updated this revision to Diff 416051.Mar 16 2022, 8:01 PM
sstwcw marked 5 inline comments as done.Mar 16 2022, 8:09 PM
sstwcw added inline comments.
clang/lib/Format/FormatToken.h
947

It was not in the original set for C# keywords. But since MyDeveloperDay also pointed it out I guess he simply forgot.

991–999

It looks like I would have to use the KeywordLanguage:: prefix throughout the macro list and also define an operator to combine categories for keywords that several languages have. Do you know of a simpler way? With the current enum, the DEFINE_LIKE_NAMES can't be used directly outside AdditionalKeywords.

sstwcw marked an inline comment as done.Mar 16 2022, 8:09 PM
clang/lib/Format/FormatToken.h
991–999

You can always drop the class and are back at integers.

991–999

With the current enum, the DEFINE_LIKE_NAMES can't be used directly outside AdditionalKeywords.

This one I don't understand? They are public and can be used, can't they? And the type I can get with decltype.

MyDeveloperDay requested changes to this revision.Mar 18 2022, 1:38 AM
MyDeveloperDay added inline comments.
clang/lib/Format/FormatToken.h
884

I didn't see this before... for me this is a hard no, I think macro code is ugly, it causes clang-format itself a whole host of issues, and I don't want to proliferate it. plus it begins a path of you using flags and I think that is unstructure C way of coding.

I know you want to add other keywords for Verilog but this to me isn't nice I don't understand why you couldn't have added the Verilog keywords like we did for JavaScript and C#

This revision now requires changes to proceed.Mar 18 2022, 1:38 AM

Allow me another attempt in justifying this patch.

Using the macros, it is easier to see when a keyword is not classified as some language's keyword. The author and reviewers didn't notice final and dollar being in only one place previously. With the old way, when someone adds a new keyword he is also more likely to forget to include it in the sets.

In the commit message when I said the old way turned out to be error-prone, I did debug why some code doesn't get formatted right only to find I forgot to add a keyword in one of the places.

With the isIdentifier and isKeyword functions matching the sets separately, it is more likely one of them will get it wrong. In the original isCSharpKeyword function, the check is wrong, it should be !=.

CSharpExtraKeywords.find(Tok.Tok.getIdentifierInfo()) == CSharpExtraKeywords.end()
sstwcw abandoned this revision.Mar 29 2022, 4:17 PM