llvm/clang/lib
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
..
Analysis [msan] Advance before destroying entry 2022-04-19 16:42:04 -07:00
APINotes
ARCMigrate
AST [NFC] Remove unused variable 2022-04-19 15:19:40 +08:00
ASTMatchers Reland "[ASTMatchers] Output currently matching node on crash" 2022-04-05 21:47:16 +01:00
Basic [SystemZ] Handle SystemZ specific inline assembly address operands. 2022-04-19 16:55:45 +02:00
CodeGen [CodeGen] Fix -Wswitch after D116462 2022-04-19 17:33:15 -07:00
CrossTU [analyzer][ctu] Only import const and trivial VarDecls 2022-04-01 13:49:39 +02:00
DirectoryWatcher
Driver [PS5] Avoid a driver crash 2022-04-19 15:55:32 -07:00
Edit
ExtractAPI [clang][extract-api] Add support for true anonymous enums 2022-04-12 20:42:17 +01:00
Format [clang-format] SortIncludes should support "@import" lines in Objective-C 2022-04-20 07:03:35 +00:00
Frontend [misexpect] Re-implement MisExpect Diagnostics 2022-04-19 21:23:48 +00:00
FrontendTool [C++20][Modules][HU 1/5] Introduce header units as a module type. 2022-03-25 09:17:14 +00:00
Headers [clang] Adding Platform/Architecture Specific Resource Header Installation Targets 2022-04-19 10:10:07 -04:00
Index [clang] Reformat 2022-03-24 05:56:23 -07:00
IndexSerialization
Interpreter
Lex [clang][lexer] Allow u8 character literal prefixes in C2x 2022-04-19 09:57:51 +02:00
Parse [Clang] Use of decltype(capture) in parameter-declaration-clause 2022-04-18 15:58:25 +02:00
Rewrite
Sema [clang][Sema] Add flag to LookupName to force C/ObjC codepath 2022-04-19 12:57:14 -07:00
Serialization [modules] Merge variable template specializations. 2022-04-19 14:48:42 -07:00
StaticAnalyzer [analyzer] Implemented RangeSet::Factory::castTo function to perform promotions, truncations and conversions. 2022-04-19 22:34:03 +03:00
Testing
Tooling [clang-format] SortIncludes should support "@import" lines in Objective-C 2022-04-20 07:03:35 +00:00
CMakeLists.txt [clang][extract-api] Refactor ExtractAPI and improve docs 2022-03-22 13:21:57 -07:00