This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Handle NullCoalescing and NullConditional operators in C#
ClosedPublic

Authored by jbcoe on Feb 28 2020, 10:19 AM.

Details

Summary

Disable merging of Type? into a single token.

Merge ?? ?. and ?[ into a single token.

Diff Detail

Event Timeline

jbcoe created this revision.Feb 28 2020, 10:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 28 2020, 10:19 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
jbcoe planned changes to this revision.Feb 28 2020, 10:21 AM

There's a weird test failure I'm unable to reproduce outside of the tests with real source code where

public static void Main(string[] args)
{ 
  string dirPath = args?[0];
}

is (mis)-formatted as

public static void Main(string[] args)
{ string dirPath = args?[0];
}
jbcoe updated this revision to Diff 247399.Feb 28 2020, 6:07 PM

Fix failing test by parsing tokens after '?[' in the same way as tokens after '['.

krasimir added inline comments.Mar 2 2020, 3:50 AM
clang/include/clang/Basic/TokenKinds.def
237 ↗(On Diff #247399)

I think this is not a good place to add these.
I think these definitions are generally for languages clang itself supports (compiling)
This could have unexpected ripple effects in unrelated parts of the clang.
Could you try to handle these somehow in lib/Format/FormatTokenLexer?
That's where the clang-format has some glue / heuristics for handling other languages syntax.

jbcoe planned changes to this revision.Mar 2 2020, 3:56 AM

Handle ??, ?. and ?[ in lib/Format/FormatTokenLexer.

jbcoe updated this revision to Diff 247610.Mar 2 2020, 5:16 AM
jbcoe edited the summary of this revision. (Show Details)

Do not add new punctuators for ??, ?. and ?[

jbcoe retitled this revision from [clang-format] Handle ?. ?? and ?[ as C# tokens to [clang-format] Handle NullCoalescing and NullConditional operators in C#.Mar 2 2020, 5:19 AM
krasimir accepted this revision.Mar 2 2020, 5:26 AM
krasimir added inline comments.
clang/lib/Format/FormatToken.h
109

nit: consider renaming to CSharpNullConditionalLSquare for consistency with other similar token types.

clang/lib/Format/TokenAnnotator.cpp
2955

please add a test case about this where Style.SpacesInSquareBrakets == true.

This revision is now accepted and ready to land.Mar 2 2020, 5:26 AM
This revision was automatically updated to reflect the committed changes.

Sorry, did the last round of comments not make sense?