[PATCH] D69764: [clang-format] Add Left/Right Const fixer capability

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 11 09:08:10 PDT 2021


MyDeveloperDay updated this revision to Diff 365765.
MyDeveloperDay added a comment.

- Remove the "CV" from the options
- Add support for ordering static/inline/const/volatile around the type, allowing for mixing both left and right alignment at the same time

  QualifierAlignment: Custom
  QualifierOrder: [ "inline", "static", "volatile", "type", const ]



- Add "Custom" format to allow use of QualifierOrder  (may want to change the `Qualifier` name as its now both Qualifiers and Specifiers)
- Add Parse errors and test to check for unsupported specifiers/qualifiers, duplicates,missing 'type' field and empty Order lists
- Add Defaults for Left and Right Alignment (may need to discuss suitable defaults).
- Removal of excess braces (if only clang-format could do that for us!!)

NOTE: Known issue:

Despite the clang-format file being already format correctly, I seem to be getting replacements: (e.g.)

For the following example

  inline volatile int const c;

with the .clang-format file of:

  ---
  Language: Cpp
  BasedOnStyle: LLVM
  QualifierAlignment: Custom
  QualifierOrder: [ inline, static, volatile, type, const ]

I get the following:

  $ clang-format -n test1.cpp
  test1.cpp:1:1: warning: code should be clang-formatted [-Wclang-format-violations]
  inline volatile int const c;
  ^

I believe caused by the existence of replacements that effectively do nothing.

  $ clang-format test1.cpp --output-replacements-xml
  <?xml version='1.0'?>
  <replacements xml:space='preserve' incomplete_format='false'>
  <replacement offset='0' length='15'>inline volatile</replacement>
  </replacements>

Its likely I am switching the order from:

  inline volatile int const c;

to

  volatile inline int const c;

and then back to

  inline volatile int const c;

Which I think likely becomes  a single `inline volatile` replacement. This could in theory be a corner case for tooling::Replacements but I need to dig in more, its more likely an artefact of the reversing the `LeftOrder` rather keeping the Left order and not pushing specifiers though other specifiers that are more `LeftMost` combined with each specifier/qualifier being handled in its own pass.

I'll look into that next, but wanted to park the current implementation as functionally this is a significant step closer.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69764/new/

https://reviews.llvm.org/D69764

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/CMakeLists.txt
  clang/lib/Format/Format.cpp
  clang/lib/Format/QualifierAlignmentFixer.cpp
  clang/lib/Format/QualifierAlignmentFixer.h
  clang/tools/clang-format/ClangFormat.cpp
  clang/unittests/Format/FormatTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69764.365765.patch
Type: text/x-patch
Size: 53937 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210811/b13d896b/attachment-0001.bin>


More information about the cfe-commits mailing list