This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Add an option to format integer literal separators
ClosedPublic

Authored by owenpan on Dec 22 2022, 3:05 AM.

Diff Detail

Event Timeline

owenpan created this revision.Dec 22 2022, 3:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 22 2022, 3:05 AM
owenpan requested review of this revision.Dec 22 2022, 3:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 22 2022, 3:05 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
owenpan planned changes to this revision.Dec 22 2022, 3:06 AM

Will add unit tests.

owenpan updated this revision to Diff 484801.Dec 22 2022, 5:00 AM

Fixes a couple of bugs.

does it have any unit tests?

clang/include/clang/Format/Format.h
2395

Also add Octal?

4122

Below Insert, same above.

clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
21

Other is Octal.

owenpan updated this revision to Diff 484841.Dec 22 2022, 7:54 AM

Fixed another bug and added some unit tests. Also updated ReleaseNotes.rst and addressed some review comments.

owenpan marked an inline comment as done.Dec 22 2022, 8:07 AM
owenpan added inline comments.
clang/include/clang/Format/Format.h
2395

I don't know if anyone would want to group octal digits. I can add it in the future if it's really useful.

clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
21

Other includes octal and other ill-formed integers (if any).

HazardyKnusperkeks added inline comments.
clang/unittests/Format/FormatTest.cpp
25124 ↗(On Diff #484841)

I'd like to see FormatTest.cpp shrink, maybe add this in a new file?

25166 ↗(On Diff #484841)

Add a test that we really don't touch floating points?

This revision is now accepted and ready to land.Dec 22 2022, 1:37 PM
owenpan planned changes to this revision.Dec 22 2022, 11:17 PM

Will extend this option to support C#, Java, and JavaScript using the underscore character _ as the separator.

clang/unittests/Format/FormatTest.cpp
25124 ↗(On Diff #484841)

If this test will grow to, say, 100+ lines, it’ll be worth the overhead. Meanwhile, we can move quit a few other much large tests out into their own files.

25166 ↗(On Diff #484841)

Will do.

owenpan updated this revision to Diff 485180.Dec 23 2022, 8:10 PM
  • Adds support for C#, Java, and JavaScript using _.
  • Adds support for only formatting affected ranges.
  • Simplifies `IntegerLiteralSeparatorFixer::format()`.
  • Adds more assertions and makes minor efficiency improvements.
This revision is now accepted and ready to land.Dec 23 2022, 8:10 PM
owenpan planned changes to this revision.Dec 23 2022, 8:13 PM

Will add more unit tests and perhaps move them to their own file.

owenpan updated this revision to Diff 485188.Dec 24 2022, 1:24 AM
  • Fixed an assertion failure.
  • Added unit tests .
  • Moved all unit tests to another file.
This revision is now accepted and ready to land.Dec 24 2022, 1:24 AM
owenpan requested review of this revision.Dec 24 2022, 1:26 AM
owenpan marked 2 inline comments as done.
This revision is now accepted and ready to land.Dec 24 2022, 4:35 AM
owenpan updated this revision to Diff 485210.Dec 24 2022, 3:32 PM

Cleaned up the unit tests a little bit:

  • Made them a little more varied.
  • Removed some overlapping test cases.
  • Removed integer literal suffixes for JavaScript.
  • Added an octal BigInt test case for JavaScript.
This revision was landed with ongoing or failed builds.Dec 24 2022, 3:35 PM
This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.Dec 24 2022, 4:46 PM

This seems to break tests everywhere, eg http://45.33.8.238/linux/95289/step_7.txt

Please take a look and revert for now if it takes a while to fix.

This seems to break tests everywhere, eg http://45.33.8.238/linux/95289/step_7.txt

Please take a look and revert for now if it takes a while to fix.

I had run FormatTests on Windows and macOS without any problems and don't understand why the build bots failed. I will disable the FixRanges test as a workaround.

This seems to break tests everywhere, eg http://45.33.8.238/linux/95289/step_7.txt

Please take a look and revert for now if it takes a while to fix.

I had run FormatTests on Windows and macOS without any problems and don't understand why the build bots failed. I will disable the FixRanges test as a workaround.

FWIW it also fails on Windows and macOS on my bots: http://45.33.8.238/macm1/51645/step_7.txt http://45.33.8.238/win/72399/step_7.txt

If the test is failing, why not revert the commit for now instead of disabling the test? That's what we usually do.

This revision is now accepted and ready to land.Dec 25 2022, 12:46 PM
owenpan updated this revision to Diff 485600.Dec 29 2022, 3:20 AM

Fix a memory bug, which doesn't surface in Debug build.

owenpan requested review of this revision.Dec 29 2022, 3:43 AM

This seems to break tests everywhere, eg http://45.33.8.238/linux/95289/step_7.txt

Please take a look and revert for now if it takes a while to fix.

I had run FormatTests on Windows and macOS without any problems and don't understand why the build bots failed. I will disable the FixRanges test as a workaround.

FWIW it also fails on Windows and macOS on my bots: http://45.33.8.238/macm1/51645/step_7.txt http://45.33.8.238/win/72399/step_7.txt

If the test is failing, why not revert the commit for now instead of disabling the test? That's what we usually do.

I couldn't reproduce the failure until I ran the unit tests in the Release build. You are right though that I should have just reverted the commit.

clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
82–83

I should allocate memory for the Token object as shown but instead had Token Tok; before.

HazardyKnusperkeks added inline comments.
clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
82–83

But why? What is different?

This revision is now accepted and ready to land.Dec 29 2022, 10:35 AM
owenpan updated this revision to Diff 485658.Dec 29 2022, 7:02 PM

It turns out that the memory bug had nothing to do with whether to use the stack or heap memory for the Token object. The culprit was passing getFormattingLangOpts(Style) directly to the Lexer constructor. I suppose in the Release build the copying of the returned LangOptions object is optimized out.

owenpan requested review of this revision.Dec 29 2022, 7:07 PM
owenpan added inline comments.
clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
82–83

Thanks for asking! It made me find the real cause.

HazardyKnusperkeks added inline comments.
clang/lib/Format/IntegerLiteralSeparatorFixer.cpp
82–83

No problem! ;)

This revision is now accepted and ready to land.Dec 31 2022, 12:45 PM
This revision was landed with ongoing or failed builds.Dec 31 2022, 5:58 PM
This revision was automatically updated to reflect the committed changes.
aaron.ballman added inline comments.
clang/docs/ClangFormatStyleOptions.rst
3168–3171

This is causing the Sphinx build to fail:

Warning, treated as error:
/home/buildbot/llvm-build-dir/clang-sphinx-docs/llvm/build/tools/clang/docs/ClangFormatStyleOptions.rst:3170:Unexpected indentation.

https://lab.llvm.org/buildbot/#/builders/92/builds/38032

I think you need to remove one leading whitespace from line 3170 so that 0 aligns with < from the line above.

owenpan marked an inline comment as done.Jan 1 2023, 3:37 PM