aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/Format/SortIncludesTest.cpp
AgeCommit message (Collapse)AuthorFilesLines
2024-07-05[clang-format] Skip block commented out includes when sorting them (#97787)Owen Pan1-0/+9
Fixes #97539.
2024-05-20[clang-format][NFC] Remove redundnat llvm::, clang::, etc.Owen Pan1-3/+3
2024-05-19[clang-format][NFC] Clean up SortIncludesTest.cppOwen Pan1-972/+970
Wherever applicable, replace EXPECT_EQ with verifyFormat and std::string with StringRef. Also, change a raw string literal to a regular one.
2024-04-25[clang-format] Do not update cursor pos if no includes replacement (#77456)NorthBlue3331-3/+119
Fix https://github.com/llvm/llvm-project/issues/77450. --------- Signed-off-by: NorthBlue333 <north333@free.fr> Co-authored-by: Owen Pan <owenpiano@gmail.com>
2024-02-06[clang-format] Add MainIncludeChar option. (#78752)j-jorge1-0/+106
Resolves #27008, #39735, #53013, #63619. Hello, this PR adds the MainIncludeChar option to clang-format, allowing to select which include syntax must be considered when searching for the main header: quotes (`#include "foo.hpp"`, the default), brackets (`#include <foo.hpp>`), or both. The lack of support for brackets has been reported many times, see the linked issues, so I am pretty sure there is a need for it :) A short note about why I did not implement a regex approach as discussed in #53013: while a regex would have allowed many extra ways to describe the main header, the bug descriptions listed above suggest a very simple need: support brackets for the main header. This PR answers this needs in a quite simple way, with a very simple style option. IMHO the feature space covered by the regex (again, for which there is no demand :)) can be implemented latter, in addition to the proposed option. The PR also includes tests for the option with and without grouped includes.
2023-10-27[clang-format][NFC] Remove extraneous newlines in remaining unit testsOwen Pan1-123/+123
2022-12-08[clang] Don't including None.h (NFC)Kazu Hirata1-1/+0
These source files no longer use None, so they do not need to include None.h. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-04-28[clang-format] add a regression test for include sortingKrasimir Georgiev1-0/+14
This adds a regression test from the comments on https://reviews.llvm.org/D121370. Reviewed By: MyDeveloperDay, curdeius Differential Revision: https://reviews.llvm.org/D124513
2022-04-28Revert "[clang-format] SortIncludes should support "@import" lines in ↵Krasimir Georgiev1-97/+0
Objective-C" This reverts commit d46fa023caa2db5a9f1e21dd038bcb626261d958. Regressed include order in some cases with trailing comments, see the comments on https://reviews.llvm.org/D121370. Will add a regression test in a follow-up commit.
2022-04-27[clang-format] Adjust editor cursor position past #include blocksowenca1-0/+15
Fixes #55027. Differential Revision: https://reviews.llvm.org/D124452
2022-04-20[clang-format] SortIncludes should support "@import" lines in Objective-CKonrad Kleine1-0/+97
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-02-01[clang-format] Don't break block comments when sorting includes.Marek Kurdej1-0/+15
Fixes https://github.com/llvm/llvm-project/issues/34626. Before, the include sorter would break the code: ``` #include <stdio.h> #include <stdint.h> /* long comment */ ``` and change it into: ``` #include <stdint.h> /* long #include <stdio.h> comment */ ``` This commit handles only the most basic case of a single block comment on an include line, but does not try to handle all the possible edge cases with multiple comments. Reviewed By: HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D118627
2021-12-12[clang-format] [PR49298] Sort includes pass will sort inside raw stringsmydeveloperday1-0/+147
https://github.com/llvm/llvm-project/issues/48642 clang-format does not respect raw string literals when sorting includes ``` const char *RawStr = R"( )"; ``` Running clang-format over with SortIncludes enabled transforms this code to: ``` const char *RawStr = R"( )"; ``` The following code tries to minimize this impact during IncludeSorting, by treating R"( and )" as equivalent of // clang-format off/on Reviewed By: HazardyKnusperkeks, curdeius Differential Revision: https://reviews.llvm.org/D115168 Fixes #48642
2021-05-04[Format] Don't sort includes if DisableFormat is trueNathan James1-0/+11
Fixes https://llvm.org/PR35099. I'm not sure if this decision was intentional but its definitely confusing for users. Reviewed By: MyDeveloperDay, HazardyKnusperkeks, curdeius Differential Revision: https://reviews.llvm.org/D101628
2021-03-05[clang-format] Rename case sortingBjörn Schäpers1-2/+2
As discussed in D95017 the names case sensitive and insensitive should be switched. This amends a8105b3766e4. Differential Revision: https://reviews.llvm.org/D97927
2021-02-02[clang-format] Add case aware include sorting.Kent Sommer1-1/+44
Adds an option to [clang-format] which sorts headers in an alphabetical manner using case only for tie-breakers. The options is off by default in favor of the current ASCIIbetical sorting style. Reviewed By: MyDeveloperDay, curdeius, HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D95017
2021-01-26Revert "[clang-format] add case aware include sorting"Marek Kurdej1-43/+0
This reverts commit 3395a336b02538d0bb768ccfae11c9b6151b102e as there was a post-merge doubt about option naming and type.
2021-01-25[clang-format] add case aware include sortingLukas Barth1-0/+43
* Adds an option to [clang-format] which sorts headers in an alphabetical manner using case only for tie-breakers. The options is off by default in favor of the current ASCIIbetical sorting style. Reviewed By: curdeius, HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D95017
2021-01-11[clang-format] Find main include after block ended with #pragma hdrstopRafał Jelonek1-0/+39
Find main include in first include block not ended with #pragma hdrstop Reviewed By: curdeius Differential Revision: https://reviews.llvm.org/D94217
2021-01-11[clang-format] turn on formatting after "clang-format on" while sorting includesRafał Jelonek1-0/+37
Formatting is not active after "clang-format on" due to merging lines while formatting is off. Also, use trimmed line. Behaviour with LF is different than with CRLF. Reviewed By: curdeius, MyDeveloperDay Differential Revision: https://reviews.llvm.org/D94206
2021-01-11[clang-format] Skip UTF8 Byte Order Mark while sorting includesRafał Jelonek1-0/+36
If file contain BOM then first instruction (include or clang-format off) is ignored Reviewed By: MyDeveloperDay Differential Revision: https://reviews.llvm.org/D94201
2020-12-05[clang-format] Add option for case sensitive regexes for sorted includesmydeveloperday1-15/+67
I think the title says everything. Reviewed By: MyDeveloperDay Patch By: HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D91507
2020-12-03[clang-format] De-duplicate includes with leading or trailing whitespace.Marek Kurdej1-0/+13
This fixes PR46555 (https://bugs.llvm.org/show_bug.cgi?id=46555). Reviewed By: MyDeveloperDay Differential Revision: https://reviews.llvm.org/D88296
2020-10-20[format] foo.<name>.h should be the main-header for foo.<name>.ccHaojian Wu1-1/+8
This fixes a regression introduced in https://reviews.llvm.org/D88640. Differential Revision: https://reviews.llvm.org/D89783
2020-10-01[Format] Don't treat compound extension headers (foo.proto.h) as foo.cc ↵Haojian Wu1-0/+10
main-file header. We receive internal bugs about this false positives after D86597. Differential Revision: https://reviews.llvm.org/D88640.
2020-05-02[clang-format] NFC - clang-format the FormatTestsmydeveloperday1-5/+7
Summary: Ensure the clang-format unit tests are themselves clang-formatted Having areas of the llvm code which are clang-format clean, give us more areas to run new clang-format binaries on ensuring we haven't broken anything. It seems to me we SHOULD have this clang-formatted at a minimum, otherwise how can we expect others to use clang-format if we "don't eat our own dogfood", also if the tests are dependent on the formatting of the code then that would also be bad! Reviewed By: sammccall Subscribers: cfe-commits Tags: #clang, #clang-format Differential Revision: https://reviews.llvm.org/D79204
2019-11-12Allow additional file suffixes/extensions considered as source in main ↵mydeveloperday1-0/+51
include grouping Summary: By additional regex match, grouping of main include can be enabled in files that are not normally considered as a C/C++ source code. For example, this might be useful in templated code, where template implementations are being held in *Impl.hpp files. On the occassion, 'assume-filename' option description was reworded as it was misleading. It has nothing to do with `style=file` option and it does not influence sourced style filename. Reviewers: rsmith, ioeric, krasimir, sylvestre.ledru, MyDeveloperDay Reviewed By: MyDeveloperDay Subscribers: MyDeveloperDay, cfe-commits Patch by: furdyna Tags: #clang Differential Revision: https://reviews.llvm.org/D67750
2019-10-01[clang-format] [PR43372] - clang-format shows replacements in DOS files when ↵Paul Hoad1-0/+8
no replacement is needed Summary: This is a patch to fix PR43372 (https://bugs.llvm.org/show_bug.cgi?id=43372) - clang-format can't format file with includes, ( which really keep providing replacements for already sorted headers.) A similar issue was addressed by @krasimir in {D60199}, however, this seemingly only prevented the issue when the files being formatted did not contain windows line endings (\r\n) It's possible this is related to https://twitter.com/StephanTLavavej/status/1176722938243895296 given who @STL_MSFT works for! As people often used the existence of replacements to determine if a file needs clang-formatting, this is probably pretty important for windows users There may be a better way of comparing 2 strings and ignoring \r (which appear in both Results and Code), I couldn't choose between this idiom or the copy_if approach, but I'm happy to change it to whatever people consider more performant. Reviewers: krasimir, klimek, owenpan, ioeric Reviewed By: krasimir Subscribers: cfe-commits, STL_MSFT, krasimir Tags: #clang-format, #clang, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D68227 llvm-svn: 373388
2019-09-26[SortIncludesTest] Add SortPriority fields to fix ↵Mikael Holmen1-2/+2
-Wmissing-field-initializers after D64695/r372919 llvm-svn: 372944
2019-09-25[clang-format] Modified SortIncludes and IncludeCategories to priority for ↵Paul Hoad1-0/+71
sorting #includes within the Group Category. Summary: This new Style rule is made as a part of adding support for NetBSD KNF in clang-format. NetBSD have it's own priority of includes which should be followed while formatting NetBSD code. This style sorts the Cpp Includes according to the priorities of NetBSD, as mentioned in the [Style Guide](http://cvsweb.netbsd.org/bsdweb.cgi/src/share/misc/style?rev=HEAD&content-type=text/x-cvsweb-markup) The working of this Style rule shown below: **Configuration:** This revision introduces a new field under IncludeCategories named `SortPriority` which defines the priority of ordering the `#includes` and the `Priority` will define the categories for grouping the `#include blocks`. Reviewers: cfe-commits, mgorny, christos, MyDeveloperDay Reviewed By: MyDeveloperDay Subscribers: lebedev.ri, rdwampler, christos, mgorny, krytarowski Patch By: Manikishan Tags: #clang, #clang-format Differential Revision: https://reviews.llvm.org/D64695 llvm-svn: 372919
2019-04-04[clang-format] Preserve include blocks in ObjC Google styleKrasimir Georgiev1-0/+12
Summary: r357567 started to regroup include block for Google style; it was meant to apply only for C++. This patch reverts this for ObjC. Reviewers: ioeric Reviewed By: ioeric Subscribers: thakis, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D60263 llvm-svn: 357695
2019-04-03[clang-format] Do not emit replacements while regrouping if Cpp includes are OKKrasimir Georgiev1-9/+25
Summary: Currently clang-format would always emit a replacement for multi-block #include sections if `IBS_Regroup`, even if the sections are correct: ``` % cat ~/test.h #include <a.h> #include "b.h" % bin/clang-format --output-replacements-xml -style=google ~/test.h <?xml version='1.0'?> <replacements xml:space='preserve' incomplete_format='false'> <replacement offset='0' length='30'>#include &lt;a.h>&#10;&#10;#include "b.h"</replacement> </replacements> % ``` This change makes clang-format not emit replacements in this case. The logic is similar to the one implemented for Java in r354452. Reviewers: ioeric Reviewed By: ioeric Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D60199 llvm-svn: 357599
2019-04-03[clang-format] Regroup #includes into blocks for Google styleEric Liu1-3/+13
Summary: Regrouping #includes in blocks separated by blank lines when sorting C++ #include headers was implemented recently, and it has been preferred in Google's C++ style guide: https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes Reviewers: sammccall, klimek Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D60116 llvm-svn: 357567
2019-03-02[clang-format] clang-format off/on not respected when using C Style commentsPaul Hoad1-0/+37
Summary: If the clang-format on/off is in a /* comment */ then the sorting of headers is not ignored PR40901 - https://bugs.llvm.org/show_bug.cgi?id=40901 Reviewers: djasper, klimek, JonasToth, krasimir, alexfh Reviewed By: alexfh Subscribers: alexfh, cfe-commits, llvm-commits Tags: #clang, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D58819 llvm-svn: 355266
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth1-4/+3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2018-05-14[clang-format] Move #include related style to libToolingCoreEric Liu1-22/+22
Summary: This will be shared by include insertion/deletion library. Reviewers: ilya-biryukov Reviewed By: ilya-biryukov Subscribers: mgorny, klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D46758 llvm-svn: 332284
2017-11-27[clang-format] Add option to group multiple #include blocks when sorting ↵Krasimir Georgiev1-0/+182
includes Summary: This patch allows grouping multiple #include blocks together and sort all includes as one big block. Additionally, sorted includes can be regrouped after sorting based on configured categories. Contributed by @KrzysztofKapusta! Reviewers: krasimir Reviewed By: krasimir Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D40288 llvm-svn: 319024
2017-08-29[clang-format] Do not format likely xmlKrasimir Georgiev1-0/+11
Summary: This patch detects the leading '<' in likely xml files and stops formatting in that case. A recent use of a Qt xml file with a .ts extension triggered this: http://doc.qt.io/qt-4.8/linguist-ts-file-format.html Reviewers: djasper Reviewed By: djasper Subscribers: sammccall, cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D37136 llvm-svn: 311999
2017-06-29[clang-format] Switch to case-insensitive header matching and use it toChandler Carruth1-0/+23
improve support for LLVM-style include sorting. This really is a collection of improvements to the rules for LLVM include sorting: - We have gmock headers now, so it adds support for those to one of the categories. - LLVM does use 'FooTest.cpp' files to test 'Foo.h' so it adds that suffix for finding a main header. - At times the test file's case may not match the header file's case, so switch to case-insensitive regex matching of header names. With this set of changes, I can't spot any misbehaviors when re-sorting all of LLVM's unittest '#include' lines. Thanks to Eric and Daniel for help testing and refining the patch during review! Differential Revision: https://reviews.llvm.org/D33932 llvm-svn: 306759
2016-08-30clang-format: Correctly calculate affected ranges when sorting #includes.Daniel Jasper1-2/+14
affectedRanges takes a start and an end offset, not offset and length. llvm-svn: 280165
2016-08-10Make clang-format remove duplicate headers when sorting #includes.Eric Liu1-6/+83
Summary: When sorting #includes, #include directives that have the same text will be deduplicated when sorting #includes, and only the first #include in the duplicate #includes remains. If the `Cursor` is provided and put on a deleted #include, it will be put on the remaining #include in the duplicate #includes. Reviewers: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D23274 llvm-svn: 278206
2016-07-11Make tooling::applyAllReplacements return llvm::Expected<string> instead of ↵Eric Liu1-3/+6
empty string to indicate potential error. Summary: return llvm::Expected<> to carry error status and error information. This is the first step towards introducing "Error" into tooling::Replacements. Reviewers: djasper, klimek Subscribers: ioeric, klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D21601 llvm-svn: 275062
2016-03-21clang-format: Make include sorting's main include detection configurable.Daniel Jasper1-1/+20
This patch adds a regular expression to configure suffixes of an included file to check whether it is the "main" include of the current file. Previously, clang-format has allowed arbitrary suffixes on the formatted file, which is still the case when no IncludeMainRegex is specified. llvm-svn: 263943
2016-03-03clang-format: Use stable_sort when sorting #includes.Daniel Jasper1-4/+18
Otherwise, clang-format can output useless replacements in the presence of identical #includes llvm-svn: 262630
2015-12-21clang-format: Only consider the first #include that looks right to beDaniel Jasper1-0/+11
the main #include. llvm-svn: 256170
2015-12-21clang-format: Only try to find the "main" include in the first block ofDaniel Jasper1-0/+13
includes. llvm-svn: 256153
2015-12-21clang-format: Extend detection of the "main" #include to use the filenameDaniel Jasper1-5/+25
Before, the first (non-system) header in a file was considered to be the main include. This is conservative as it makes clang-format change the #include order less often. Instead implement some basic usage of the filename itself. With this patch, clang-format considers every header to be a main include if the header file's basename is a prefix to the filename the #include is in. llvm-svn: 256148
2015-12-16clang-format: Extend header sort category implementation.Daniel Jasper1-0/+8
Specifically, it is sometimes necessary to keep certain #includes as the first #include, even before the main #include for a .cc file. Switching the category to be signed instead of unsigned isn't ideal, but it seems as good of an option as any and is fully backwards compatible. llvm-svn: 255757
2015-11-23clang-format: Make moving of the Cursor work properly when sorting #includes.Daniel Jasper1-0/+20
llvm-svn: 253860
2015-11-21clang-format: Make sorting includes respect // clang-format offDaniel Jasper1-0/+19
llvm-svn: 253772