llvm-project/clang/lib/Format
Konrad Kleine d46fa023ca [clang-format] SortIncludes should support "@import" lines in Objective-C
Fixes [[ https://github.com/llvm/llvm-project/issues/38995 | #38995 ]]

This is an attempt to modify the regular expression to identify
`@import` and `import` alongside the regular `#include`. The challenging
part was not to support `@` in addition to `#` but how to handle
everything that comes after the `include|import` keywords. Previously
everything that wasn't `"` or `<` was consumed. But as you can see in
this example from the issue #38995, there is no `"` or `<` following the
keyword:

```
@import Foundation;
```

I experimented with a lot of fancy and useful expressions in [this
online regex tool](https://regex101.com) only to find out that some
things are simply not supported by the regex implementation in LLVM.

 * For example the beginning `[\t\ ]*` should be replacable by the
   horizontal whitespace character `\h*` but this will break the
   `SortIncludesTest.LeadingWhitespace` test.

That's why I've chosen to come back to the basic building blocks.

The essential change in this patch is the change from this regular
expression:

```
^[\t\ ]*#[\t\ ]*(import|include)[^"<]*(["<][^">]*[">])
        ~                              ~~~~~~~~~~~~~~
        ^                              ^
        |                              |
        only support # prefix not @    |
                                       only support "" and <> as
delimiters
                                       no support for C++ modules and ;
                                       ending. Also this allows for ">
                                       or <" or "" or <> which all seems
                                       either off or wrong.
```

to this:

```
^[\t\ ]*[@#][\t\ ]*(import|include)([^"]*("[^"]+")|[^<]*(<[^>]+>)|[\t\
]*([^;]+;))
        ~~~~                        ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
        ^                                 ^           ^       ^       ^
        |                                 |           |       |       |
        Now support @ and #.            Clearly support "" and <> as
well as an
                                        include name without enclosing
characters.
                                        Allows for no mixture of "> or
<" or
                                        empty include names.

```

Here is how I've tested this patch:

```
ninja clang-Format
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
```

One side effect of this change is it should partially support
[C++20 Module](https://en.cppreference.com/w/cpp/language/modules)
`import` lines without the optional `export` in front. Adding
this can be a change on its own that shouldn't be too hard. I say
partially because the `@` or `#` are currently *NOT* optional in the
regular expression.

I see an opportunity to optimized the matching to exclude `@include` for
example. But eventually these should be caught by the compiler, so...

With my change, the matching group is not at a fixed position any
longer. I decided to
choose the last match (group) that is not empty.

Reviewed By: HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D121370
2022-04-20 07:03:35 +00:00
..
AffectedRangeManager.cpp [clang-format] Assert Line->First. NFC. 2022-01-24 08:54:55 +01:00
AffectedRangeManager.h
BreakableToken.cpp [clang-format] don't break up #-style comment sections 2022-03-22 15:29:02 +01:00
BreakableToken.h [clang-format] Add option to control the spaces in a line comment 2021-02-01 22:48:50 +01:00
CMakeLists.txt [clang-format] Style to separate definition blocks 2022-01-03 15:47:39 -05:00
ContinuationIndenter.cpp [clang-format] Use range-for loop with drop_end. NFC. 2022-03-21 10:05:06 +01:00
ContinuationIndenter.h [Format] Remove unused LineContainsContinuedForLoopSection. NFC 2022-02-22 09:59:02 +01:00
DefinitionBlockSeparator.cpp [clang-format] Fix DefSeparator empty line issues 2022-02-07 14:23:21 +00:00
DefinitionBlockSeparator.h [clang-format] Fix SeparateDefinitionBlocks issues 2022-01-24 14:23:20 +00:00
Encoding.h
Format.cpp [clang-format] SortIncludes should support "@import" lines in Objective-C 2022-04-20 07:03:35 +00:00
FormatInternal.h Move clang/Tooling/Core/Lookup.h to clang/Tooling/Refactoring/Lookup.h 2020-10-20 10:13:28 +01:00
FormatToken.cpp [clang-format] Move FormatToken::opensBlockOrBlockTypeList to source file. NFC. 2022-02-10 10:51:03 +01:00
FormatToken.h [clang-format] Handle C# 9 `init` accessor specifier. 2022-03-08 13:33:36 +01:00
FormatTokenLexer.cpp [NFC][Lexer] Make Lexer::LangOpts const reference 2022-02-28 15:42:19 +01:00
FormatTokenLexer.h [NFC][Lexer] Make Lexer::LangOpts const reference 2022-02-28 15:42:19 +01:00
MacroExpander.cpp [clang-format] Elide unnecessary braces. NFC. 2022-02-02 14:36:01 +01:00
Macros.h [llvm][clang][NFC] updates inline licence info 2021-08-11 02:48:53 +00:00
NamespaceEndCommentsFixer.cpp [clang-format] Fix namespace format when the name is followed by a macro 2022-03-10 15:00:32 -08:00
NamespaceEndCommentsFixer.h
QualifierAlignmentFixer.cpp [clang-format] Fix a crash in qualifier alignment 2022-04-01 17:30:59 -07:00
QualifierAlignmentFixer.h [clang-format] Fix a crash (assertion) in qualifier alignment when matching template closer is null 2022-01-06 19:40:39 +00:00
SortJavaScriptImports.cpp [clang-format] Elide unnecessary braces. NFC. 2022-02-02 15:28:53 +01:00
SortJavaScriptImports.h
TokenAnalyzer.cpp [clang-format] Reserve vectors when the number of items is known beforehand. NFC. 2022-02-03 10:38:23 +01:00
TokenAnalyzer.h Make clang-format fuzz through Lexing with asserts enabled. 2021-11-19 14:44:06 +01:00
TokenAnnotator.cpp [clang-format] Fix invalid code generation with comments in lambda 2022-03-23 19:40:24 -07:00
TokenAnnotator.h [clang-format] Use ranged for loops. NFC. 2022-02-01 14:10:48 +01:00
UnwrappedLineFormatter.cpp [clang-format] Skip preprocessor lines when finding the record lbrace 2022-04-14 09:31:15 -07:00
UnwrappedLineFormatter.h [clang-format] Fix the issue that empty lines being removed at the beginning of namespace 2021-06-27 15:59:21 +01:00
UnwrappedLineParser.cpp [clang-format] Fix SeparateDefinitionBlocks breaking up function-try-block. 2022-04-13 16:44:04 +02:00
UnwrappedLineParser.h [clang-format] Handle attributes before case label. Relanded. 2022-03-23 16:24:24 +01:00
UsingDeclarationsSorter.cpp [clang-format] Use ranged for loops. NFC. 2022-02-01 14:10:48 +01:00
UsingDeclarationsSorter.h
WhitespaceManager.cpp [clang-format] Reformat. NFC. 2022-03-17 09:27:31 +01:00
WhitespaceManager.h [clang-format] Minimize the damage caused by AlignArrayOfStructures when working on non square arrays 2022-03-12 17:22:31 +00:00