Skip to content

Commit 5ddcd76

Browse files
authored
[clang-format][NFC] Upgrade SortIncludes option to a struct (#140497)
This allows adding other suboptions e.g. IgnoreExtension in #137840.
1 parent 6da2acf commit 5ddcd76

File tree

8 files changed

+77
-85
lines changed

8 files changed

+77
-85
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5990,41 +5990,25 @@ the configuration (without a prefix: ``Auto``).
59905990
**SortIncludes** (``SortIncludesOptions``) :versionbadge:`clang-format 3.8` :ref:`<SortIncludes>`
59915991
Controls if and how clang-format will sort ``#includes``.
59925992

5993-
Possible values:
5994-
5995-
* ``SI_Never`` (in configuration: ``Never``)
5996-
Includes are never sorted.
5997-
5998-
.. code-block:: c++
5999-
6000-
#include "B/A.h"
6001-
#include "A/B.h"
6002-
#include "a/b.h"
6003-
#include "A/b.h"
6004-
#include "B/a.h"
6005-
6006-
* ``SI_CaseSensitive`` (in configuration: ``CaseSensitive``)
6007-
Includes are sorted in an ASCIIbetical or case sensitive fashion.
5993+
Nested configuration flags:
60085994

6009-
.. code-block:: c++
5995+
Includes sorting options.
60105996

6011-
#include "A/B.h"
6012-
#include "A/b.h"
6013-
#include "B/A.h"
6014-
#include "B/a.h"
6015-
#include "a/b.h"
5997+
* ``bool Enabled`` If ``true``, includes are sorted based on the other suboptions below.
5998+
(``Never`` is deprecated by ``Enabled: false``.)
60165999

6017-
* ``SI_CaseInsensitive`` (in configuration: ``CaseInsensitive``)
6018-
Includes are sorted in an alphabetical or case insensitive fashion.
6000+
* ``bool IgnoreCase`` Whether or not includes are sorted in a case-insensitive fashion.
6001+
(``CaseSensitive`` and ``CaseInsensitive`` are deprecated by
6002+
``IgnoreCase: false`` and ``IgnoreCase: true``, respectively.)
60196003

60206004
.. code-block:: c++
60216005

6022-
#include "A/B.h"
6023-
#include "A/b.h"
6024-
#include "a/b.h"
6025-
#include "B/A.h"
6026-
#include "B/a.h"
6027-
6006+
true: false:
6007+
#include "A/B.h" vs. #include "A/B.h"
6008+
#include "A/b.h" #include "A/b.h"
6009+
#include "a/b.h" #include "B/A.h"
6010+
#include "B/A.h" #include "B/a.h"
6011+
#include "B/a.h" #include "a/b.h"
60286012

60296013

60306014
.. _SortJavaStaticImport:

clang/include/clang/Format/Format.h

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4365,35 +4365,29 @@ struct FormatStyle {
43654365
/// \version 18
43664366
bool SkipMacroDefinitionBody;
43674367

4368-
/// Include sorting options.
4369-
enum SortIncludesOptions : int8_t {
4370-
/// Includes are never sorted.
4371-
/// \code
4372-
/// #include "B/A.h"
4373-
/// #include "A/B.h"
4374-
/// #include "a/b.h"
4375-
/// #include "A/b.h"
4376-
/// #include "B/a.h"
4377-
/// \endcode
4378-
SI_Never,
4379-
/// Includes are sorted in an ASCIIbetical or case sensitive fashion.
4380-
/// \code
4381-
/// #include "A/B.h"
4382-
/// #include "A/b.h"
4383-
/// #include "B/A.h"
4384-
/// #include "B/a.h"
4385-
/// #include "a/b.h"
4386-
/// \endcode
4387-
SI_CaseSensitive,
4388-
/// Includes are sorted in an alphabetical or case insensitive fashion.
4389-
/// \code
4390-
/// #include "A/B.h"
4391-
/// #include "A/b.h"
4392-
/// #include "a/b.h"
4393-
/// #include "B/A.h"
4394-
/// #include "B/a.h"
4395-
/// \endcode
4396-
SI_CaseInsensitive,
4368+
/// Includes sorting options.
4369+
struct SortIncludesOptions {
4370+
/// If ``true``, includes are sorted based on the other suboptions below.
4371+
/// (``Never`` is deprecated by ``Enabled: false``.)
4372+
bool Enabled;
4373+
/// Whether or not includes are sorted in a case-insensitive fashion.
4374+
/// (``CaseSensitive`` and ``CaseInsensitive`` are deprecated by
4375+
/// ``IgnoreCase: false`` and ``IgnoreCase: true``, respectively.)
4376+
/// \code
4377+
/// true: false:
4378+
/// #include "A/B.h" vs. #include "A/B.h"
4379+
/// #include "A/b.h" #include "A/b.h"
4380+
/// #include "a/b.h" #include "B/A.h"
4381+
/// #include "B/A.h" #include "B/a.h"
4382+
/// #include "B/a.h" #include "a/b.h"
4383+
/// \endcode
4384+
bool IgnoreCase;
4385+
bool operator==(const SortIncludesOptions &R) const {
4386+
return Enabled == R.Enabled && IgnoreCase == R.IgnoreCase;
4387+
}
4388+
bool operator!=(const SortIncludesOptions &R) const {
4389+
return !(*this == R);
4390+
}
43974391
};
43984392

43994393
/// Controls if and how clang-format will sort ``#includes``.

clang/lib/Format/Format.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -659,15 +659,26 @@ template <> struct ScalarEnumerationTraits<FormatStyle::ShortLambdaStyle> {
659659
}
660660
};
661661

662-
template <> struct ScalarEnumerationTraits<FormatStyle::SortIncludesOptions> {
663-
static void enumeration(IO &IO, FormatStyle::SortIncludesOptions &Value) {
664-
IO.enumCase(Value, "Never", FormatStyle::SI_Never);
665-
IO.enumCase(Value, "CaseInsensitive", FormatStyle::SI_CaseInsensitive);
666-
IO.enumCase(Value, "CaseSensitive", FormatStyle::SI_CaseSensitive);
662+
template <> struct MappingTraits<FormatStyle::SortIncludesOptions> {
663+
static void enumInput(IO &IO, FormatStyle::SortIncludesOptions &Value) {
664+
IO.enumCase(Value, "Never", FormatStyle::SortIncludesOptions({}));
665+
IO.enumCase(Value, "CaseInsensitive",
666+
FormatStyle::SortIncludesOptions({/*Enabled=*/true,
667+
/*IgnoreCase=*/true}));
668+
IO.enumCase(Value, "CaseSensitive",
669+
FormatStyle::SortIncludesOptions({/*Enabled=*/true,
670+
/*IgnoreCase=*/false}));
667671

668672
// For backward compatibility.
669-
IO.enumCase(Value, "false", FormatStyle::SI_Never);
670-
IO.enumCase(Value, "true", FormatStyle::SI_CaseSensitive);
673+
IO.enumCase(Value, "false", FormatStyle::SortIncludesOptions({}));
674+
IO.enumCase(Value, "true",
675+
FormatStyle::SortIncludesOptions({/*Enabled=*/true,
676+
/*IgnoreCase=*/false}));
677+
}
678+
679+
static void mapping(IO &IO, FormatStyle::SortIncludesOptions &Value) {
680+
IO.mapOptional("Enabled", Value.Enabled);
681+
IO.mapOptional("IgnoreCase", Value.IgnoreCase);
671682
}
672683
};
673684

@@ -1636,7 +1647,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
16361647
LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
16371648
LLVMStyle.ShortNamespaceLines = 1;
16381649
LLVMStyle.SkipMacroDefinitionBody = false;
1639-
LLVMStyle.SortIncludes = FormatStyle::SI_CaseSensitive;
1650+
LLVMStyle.SortIncludes = {/*Enabled=*/true, /*IgnoreCase=*/false};
16401651
LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
16411652
LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
16421653
LLVMStyle.SpaceAfterCStyleCast = false;
@@ -1901,7 +1912,6 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
19011912
"java",
19021913
"javax",
19031914
};
1904-
ChromiumStyle.SortIncludes = FormatStyle::SI_CaseSensitive;
19051915
} else if (Language == FormatStyle::LK_JavaScript) {
19061916
ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
19071917
ChromiumStyle.AllowShortLoopsOnASingleLine = false;
@@ -2029,7 +2039,7 @@ FormatStyle getClangFormatStyle() {
20292039
FormatStyle getNoStyle() {
20302040
FormatStyle NoStyle = getLLVMStyle();
20312041
NoStyle.DisableFormat = true;
2032-
NoStyle.SortIncludes = FormatStyle::SI_Never;
2042+
NoStyle.SortIncludes = {};
20332043
NoStyle.SortUsingDeclarations = FormatStyle::SUD_Never;
20342044
return NoStyle;
20352045
}
@@ -3220,7 +3230,7 @@ static void sortCppIncludes(const FormatStyle &Style,
32203230
SmallVector<unsigned, 16> Indices =
32213231
llvm::to_vector<16>(llvm::seq<unsigned>(0, Includes.size()));
32223232

3223-
if (Style.SortIncludes == FormatStyle::SI_CaseInsensitive) {
3233+
if (Style.SortIncludes.Enabled && Style.SortIncludes.IgnoreCase) {
32243234
stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
32253235
const auto LHSFilenameLower = Includes[LHSI].Filename.lower();
32263236
const auto RHSFilenameLower = Includes[RHSI].Filename.lower();
@@ -3595,7 +3605,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
35953605
ArrayRef<tooling::Range> Ranges,
35963606
StringRef FileName, unsigned *Cursor) {
35973607
tooling::Replacements Replaces;
3598-
if (!Style.SortIncludes || Style.DisableFormat)
3608+
if (!Style.SortIncludes.Enabled || Style.DisableFormat)
35993609
return Replaces;
36003610
if (isLikelyXml(Code))
36013611
return Replaces;

clang/tools/clang-format/ClangFormat.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,10 +478,9 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
478478
}
479479

480480
if (SortIncludes.getNumOccurrences() != 0) {
481+
FormatStyle->SortIncludes = {};
481482
if (SortIncludes)
482-
FormatStyle->SortIncludes = FormatStyle::SI_CaseSensitive;
483-
else
484-
FormatStyle->SortIncludes = FormatStyle::SI_Never;
483+
FormatStyle->SortIncludes.Enabled = true;
485484
}
486485
unsigned CursorPosition = Cursor;
487486
Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges,

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
257257
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
258258
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
259259
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, Other);
260+
CHECK_PARSE_NESTED_BOOL(SortIncludes, Enabled);
261+
CHECK_PARSE_NESTED_BOOL(SortIncludes, IgnoreCase);
260262
}
261263

262264
#undef CHECK_PARSE_BOOL
@@ -976,15 +978,20 @@ TEST(ConfigParseTest, ParsesConfiguration) {
976978
CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
977979
IncludeStyle.IncludeIsMainSourceRegex, "abc$");
978980

979-
Style.SortIncludes = FormatStyle::SI_Never;
981+
Style.SortIncludes = {};
980982
CHECK_PARSE("SortIncludes: true", SortIncludes,
981-
FormatStyle::SI_CaseSensitive);
982-
CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
983+
FormatStyle::SortIncludesOptions(
984+
{/*Enabled=*/true, /*IgnoreCase=*/false}));
985+
CHECK_PARSE("SortIncludes: false", SortIncludes,
986+
FormatStyle::SortIncludesOptions({}));
983987
CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
984-
FormatStyle::SI_CaseInsensitive);
988+
FormatStyle::SortIncludesOptions(
989+
{/*Enabled=*/true, /*IgnoreCase=*/true}));
985990
CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
986-
FormatStyle::SI_CaseSensitive);
987-
CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
991+
FormatStyle::SortIncludesOptions(
992+
{/*Enabled=*/true, /*IgnoreCase=*/false}));
993+
CHECK_PARSE("SortIncludes: Never", SortIncludes,
994+
FormatStyle::SortIncludesOptions({}));
988995

989996
Style.RawStringFormats.clear();
990997
std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {

clang/unittests/Format/FormatReplacementTest.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
7070
"#include \"b.h\"\n")});
7171

7272
FormatStyle Style = getLLVMStyle();
73-
Style.SortIncludes = FormatStyle::SI_CaseSensitive;
7473
auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
7574
EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
7675
<< llvm::toString(FormattedReplaces.takeError()) << "\n";

clang/unittests/Format/SortImportsTestJava.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ class SortImportsTestJava : public testing::Test {
3131
public:
3232
SortImportsTestJava() {
3333
FmtStyle = getGoogleStyle(FormatStyle::LK_Java);
34+
EXPECT_TRUE(FmtStyle.SortIncludes.Enabled);
3435
FmtStyle.JavaImportGroups = {"com.test", "org", "com"};
35-
FmtStyle.SortIncludes = FormatStyle::SI_CaseInsensitive;
36+
FmtStyle.SortIncludes.IgnoreCase = true;
3637
}
3738
};
3839

clang/unittests/Format/SortIncludesTest.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ TEST_F(SortIncludesTest, SupportClangFormatOffCStyle) {
284284
}
285285

286286
TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
287-
FmtStyle.SortIncludes = FormatStyle::SI_Never;
287+
FmtStyle.SortIncludes = {};
288288
verifyFormat("#include \"a.h\"\n"
289289
"#include \"c.h\"\n"
290290
"#include \"b.h\"",
@@ -628,9 +628,7 @@ TEST_F(SortIncludesTest, MainHeaderIsSeparatedWhenRegroupping) {
628628
}
629629

630630
TEST_F(SortIncludesTest, SupportOptionalCaseSensitiveSorting) {
631-
EXPECT_FALSE(FmtStyle.SortIncludes == FormatStyle::SI_CaseInsensitive);
632-
633-
FmtStyle.SortIncludes = FormatStyle::SI_CaseInsensitive;
631+
FmtStyle.SortIncludes.IgnoreCase = true;
634632

635633
verifyFormat("#include \"A/B.h\"\n"
636634
"#include \"A/b.h\"\n"

0 commit comments

Comments
 (0)