Histroy
The foundation for this patch was done in: https://reviews.llvm.org/D121370. I wanted to "rescue" some pieces from
it.
Unlike D121370, this patch doesn't modify the regular expression to identify an include name itself.
- It simply provides one instead of two copies of regular expression patterns.
- We also provide a function to get the include name from a group of match groups delivered by the shared regular expression that is only instantiated once now. Before when you experimented with the regex for identifying include matches, you'd have to change multiple locations and for example replace Matches[2] with Matches[3] in many locations. This way cumbersome and by having a function clang::tooling::getIncludeNameFromMatches() this makes it more concise and easy to modify.
- The include name was trimmed from unwanted characters ("<>) before and this was done repeatedly in multiple places. Now we have: clang::tooling::trimIncludeName() which does it for you, again in one place and not scattered over the code.
Testing
Here's how I've tested this patch:
$ cd llvm-project $ mkdir build && cd build $ cmake ../llvm/ -DCMAKE_BUILD_TYPE=Release -DCLANG_BUILD_TOOLS=On -DLLVM_ENABLE_PROJECTS=clang -DCLANG_INCLUDE_TESTS=ON -DLLVM_BUILD_UTILS=ON -G Ninja $ ninja clangFormat $ ninja FormatTests $ ./tools/clang/unittests/Format/FormatTests --gtest_filter=SortIncludesTest*
And if that worked I doubled checked that nothing else broke by running
all format checks:
$ ./tools/clang/unittests/Format/FormatTests
What if this was overridable via the Style we could experiment with changing the regex