This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Do not treat C# attribute targets as labels
ClosedPublic

Authored by jbcoe on Feb 5 2020, 5:11 AM.

Details

Summary

Merge '[', 'target' , ':' into a single token for C# attributes to prevent the target from being seen as a label.

Diff Detail

Event Timeline

jbcoe created this revision.Feb 5 2020, 5:11 AM

This seems OK, but it feels a bit messy that the text [target: becomes the opening square bracket. I think this might be just good enough for these attributes, so I'm ok with this for now. But a more principled solution (for later, if we hit any dead ends with this) would be to introduce a new AnnotateToken type, like TT_CSharpAttributeTarget and make sure these are annotated properly.

clang/lib/Format/FormatTokenLexer.cpp
286

Here Target is unconstrained. Can it be anything? Maybe we should check if it's an tok::identifier?

Even better, seems we can even enumerate the possibilities:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/attributes/#attribute-targets

jbcoe marked an inline comment as done.Feb 5 2020, 7:45 AM

This definitely feels good enough rather than ideal. We should investigate adding a new token type if this 'fix' proves to have shortcomings.

krasimir accepted this revision.Feb 5 2020, 7:58 AM
krasimir added inline comments.
clang/lib/Format/FormatTokenLexer.cpp
299
This revision is now accepted and ready to land.Feb 5 2020, 7:58 AM
jbcoe updated this revision to Diff 242655.Feb 5 2020, 9:25 AM

Use StringSet to list valid C# attribute targets.

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 5 2020, 9:36 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

LGTM, thanks for the patch (I've seen this in the wild where the AssemblyInfo.cs gets a bit messed up.

clang/lib/Format/FormatTokenLexer.cpp
287

+1 for just supporting what it can be.