Skip to content

[clang-format][NFC] Upgrade SortIncludes option to a struct #140497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 13 additions & 29 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5990,41 +5990,25 @@ the configuration (without a prefix: ``Auto``).
**SortIncludes** (``SortIncludesOptions``) :versionbadge:`clang-format 3.8` :ref:`¶ <SortIncludes>`
Controls if and how clang-format will sort ``#includes``.

Possible values:

* ``SI_Never`` (in configuration: ``Never``)
Includes are never sorted.

.. code-block:: c++

#include "B/A.h"
#include "A/B.h"
#include "a/b.h"
#include "A/b.h"
#include "B/a.h"

* ``SI_CaseSensitive`` (in configuration: ``CaseSensitive``)
Includes are sorted in an ASCIIbetical or case sensitive fashion.
Nested configuration flags:

.. code-block:: c++
Includes sorting options.

#include "A/B.h"
#include "A/b.h"
#include "B/A.h"
#include "B/a.h"
#include "a/b.h"
* ``bool Enabled`` If ``true``, includes are sorted based on the other suboptions below.
(``Never`` is deprecated by ``Enabled: false``.)

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

.. code-block:: c++

#include "A/B.h"
#include "A/b.h"
#include "a/b.h"
#include "B/A.h"
#include "B/a.h"

true: false:
#include "A/B.h" vs. #include "A/B.h"
#include "A/b.h" #include "A/b.h"
#include "a/b.h" #include "B/A.h"
#include "B/A.h" #include "B/a.h"
#include "B/a.h" #include "a/b.h"


.. _SortJavaStaticImport:
Expand Down
52 changes: 23 additions & 29 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4365,35 +4365,29 @@ struct FormatStyle {
/// \version 18
bool SkipMacroDefinitionBody;

/// Include sorting options.
enum SortIncludesOptions : int8_t {
/// Includes are never sorted.
/// \code
/// #include "B/A.h"
/// #include "A/B.h"
/// #include "a/b.h"
/// #include "A/b.h"
/// #include "B/a.h"
/// \endcode
SI_Never,
/// Includes are sorted in an ASCIIbetical or case sensitive fashion.
/// \code
/// #include "A/B.h"
/// #include "A/b.h"
/// #include "B/A.h"
/// #include "B/a.h"
/// #include "a/b.h"
/// \endcode
SI_CaseSensitive,
/// Includes are sorted in an alphabetical or case insensitive fashion.
/// \code
/// #include "A/B.h"
/// #include "A/b.h"
/// #include "a/b.h"
/// #include "B/A.h"
/// #include "B/a.h"
/// \endcode
SI_CaseInsensitive,
/// Includes sorting options.
struct SortIncludesOptions {
/// If ``true``, includes are sorted based on the other suboptions below.
/// (``Never`` is deprecated by ``Enabled: false``.)
bool Enabled;
/// Whether or not includes are sorted in a case-insensitive fashion.
/// (``CaseSensitive`` and ``CaseInsensitive`` are deprecated by
/// ``IgnoreCase: false`` and ``IgnoreCase: true``, respectively.)
/// \code
/// true: false:
/// #include "A/B.h" vs. #include "A/B.h"
/// #include "A/b.h" #include "A/b.h"
/// #include "a/b.h" #include "B/A.h"
/// #include "B/A.h" #include "B/a.h"
/// #include "B/a.h" #include "a/b.h"
/// \endcode
bool IgnoreCase;
bool operator==(const SortIncludesOptions &R) const {
return Enabled == R.Enabled && IgnoreCase == R.IgnoreCase;
}
bool operator!=(const SortIncludesOptions &R) const {
return !(*this == R);
}
};

/// Controls if and how clang-format will sort ``#includes``.
Expand Down
34 changes: 22 additions & 12 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,26 @@ template <> struct ScalarEnumerationTraits<FormatStyle::ShortLambdaStyle> {
}
};

template <> struct ScalarEnumerationTraits<FormatStyle::SortIncludesOptions> {
static void enumeration(IO &IO, FormatStyle::SortIncludesOptions &Value) {
IO.enumCase(Value, "Never", FormatStyle::SI_Never);
IO.enumCase(Value, "CaseInsensitive", FormatStyle::SI_CaseInsensitive);
IO.enumCase(Value, "CaseSensitive", FormatStyle::SI_CaseSensitive);
template <> struct MappingTraits<FormatStyle::SortIncludesOptions> {
static void enumInput(IO &IO, FormatStyle::SortIncludesOptions &Value) {
IO.enumCase(Value, "Never", FormatStyle::SortIncludesOptions({}));
IO.enumCase(Value, "CaseInsensitive",
FormatStyle::SortIncludesOptions({/*Enabled=*/true,
/*IgnoreCase=*/true}));
IO.enumCase(Value, "CaseSensitive",
FormatStyle::SortIncludesOptions({/*Enabled=*/true,
/*IgnoreCase=*/false}));

// For backward compatibility.
IO.enumCase(Value, "false", FormatStyle::SI_Never);
IO.enumCase(Value, "true", FormatStyle::SI_CaseSensitive);
IO.enumCase(Value, "false", FormatStyle::SortIncludesOptions({}));
IO.enumCase(Value, "true",
FormatStyle::SortIncludesOptions({/*Enabled=*/true,
/*IgnoreCase=*/false}));
}

static void mapping(IO &IO, FormatStyle::SortIncludesOptions &Value) {
IO.mapOptional("Enabled", Value.Enabled);
IO.mapOptional("IgnoreCase", Value.IgnoreCase);
}
};

Expand Down Expand Up @@ -1636,7 +1647,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
LLVMStyle.ShortNamespaceLines = 1;
LLVMStyle.SkipMacroDefinitionBody = false;
LLVMStyle.SortIncludes = FormatStyle::SI_CaseSensitive;
LLVMStyle.SortIncludes = {/*Enabled=*/true, /*IgnoreCase=*/false};
LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
LLVMStyle.SpaceAfterCStyleCast = false;
Expand Down Expand Up @@ -1901,7 +1912,6 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
"java",
"javax",
};
ChromiumStyle.SortIncludes = FormatStyle::SI_CaseSensitive;
} else if (Language == FormatStyle::LK_JavaScript) {
ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
ChromiumStyle.AllowShortLoopsOnASingleLine = false;
Expand Down Expand Up @@ -2029,7 +2039,7 @@ FormatStyle getClangFormatStyle() {
FormatStyle getNoStyle() {
FormatStyle NoStyle = getLLVMStyle();
NoStyle.DisableFormat = true;
NoStyle.SortIncludes = FormatStyle::SI_Never;
NoStyle.SortIncludes = {};
NoStyle.SortUsingDeclarations = FormatStyle::SUD_Never;
return NoStyle;
}
Expand Down Expand Up @@ -3221,7 +3231,7 @@ static void sortCppIncludes(const FormatStyle &Style,
SmallVector<unsigned, 16> Indices =
llvm::to_vector<16>(llvm::seq<unsigned>(0, Includes.size()));

if (Style.SortIncludes == FormatStyle::SI_CaseInsensitive) {
if (Style.SortIncludes.Enabled && Style.SortIncludes.IgnoreCase) {
stable_sort(Indices, [&](unsigned LHSI, unsigned RHSI) {
const auto LHSFilenameLower = Includes[LHSI].Filename.lower();
const auto RHSFilenameLower = Includes[RHSI].Filename.lower();
Expand Down Expand Up @@ -3596,7 +3606,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
ArrayRef<tooling::Range> Ranges,
StringRef FileName, unsigned *Cursor) {
tooling::Replacements Replaces;
if (!Style.SortIncludes || Style.DisableFormat)
if (!Style.SortIncludes.Enabled || Style.DisableFormat)
return Replaces;
if (isLikelyXml(Code))
return Replaces;
Expand Down
5 changes: 2 additions & 3 deletions clang/tools/clang-format/ClangFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,10 +478,9 @@ static bool format(StringRef FileName, bool ErrorOnIncompleteFormat = false) {
}

if (SortIncludes.getNumOccurrences() != 0) {
FormatStyle->SortIncludes = {};
if (SortIncludes)
FormatStyle->SortIncludes = FormatStyle::SI_CaseSensitive;
else
FormatStyle->SortIncludes = FormatStyle::SI_Never;
FormatStyle->SortIncludes.Enabled = true;
}
unsigned CursorPosition = Cursor;
Replacements Replaces = sortIncludes(*FormatStyle, Code->getBuffer(), Ranges,
Expand Down
19 changes: 13 additions & 6 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InEmptyParentheses);
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, Other);
CHECK_PARSE_NESTED_BOOL(SortIncludes, Enabled);
CHECK_PARSE_NESTED_BOOL(SortIncludes, IgnoreCase);
}

#undef CHECK_PARSE_BOOL
Expand Down Expand Up @@ -976,15 +978,20 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("IncludeIsMainSourceRegex: 'abc$'",
IncludeStyle.IncludeIsMainSourceRegex, "abc$");

Style.SortIncludes = FormatStyle::SI_Never;
Style.SortIncludes = {};
CHECK_PARSE("SortIncludes: true", SortIncludes,
FormatStyle::SI_CaseSensitive);
CHECK_PARSE("SortIncludes: false", SortIncludes, FormatStyle::SI_Never);
FormatStyle::SortIncludesOptions(
{/*Enabled=*/true, /*IgnoreCase=*/false}));
CHECK_PARSE("SortIncludes: false", SortIncludes,
FormatStyle::SortIncludesOptions({}));
CHECK_PARSE("SortIncludes: CaseInsensitive", SortIncludes,
FormatStyle::SI_CaseInsensitive);
FormatStyle::SortIncludesOptions(
{/*Enabled=*/true, /*IgnoreCase=*/true}));
CHECK_PARSE("SortIncludes: CaseSensitive", SortIncludes,
FormatStyle::SI_CaseSensitive);
CHECK_PARSE("SortIncludes: Never", SortIncludes, FormatStyle::SI_Never);
FormatStyle::SortIncludesOptions(
{/*Enabled=*/true, /*IgnoreCase=*/false}));
CHECK_PARSE("SortIncludes: Never", SortIncludes,
FormatStyle::SortIncludesOptions({}));

Style.RawStringFormats.clear();
std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
Expand Down
1 change: 0 additions & 1 deletion clang/unittests/Format/FormatReplacementTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
"#include \"b.h\"\n")});

FormatStyle Style = getLLVMStyle();
Style.SortIncludes = FormatStyle::SI_CaseSensitive;
auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
<< llvm::toString(FormattedReplaces.takeError()) << "\n";
Expand Down
3 changes: 2 additions & 1 deletion clang/unittests/Format/SortImportsTestJava.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class SortImportsTestJava : public testing::Test {
public:
SortImportsTestJava() {
FmtStyle = getGoogleStyle(FormatStyle::LK_Java);
EXPECT_TRUE(FmtStyle.SortIncludes.Enabled);
FmtStyle.JavaImportGroups = {"com.test", "org", "com"};
FmtStyle.SortIncludes = FormatStyle::SI_CaseInsensitive;
FmtStyle.SortIncludes.IgnoreCase = true;
}
};

Expand Down
6 changes: 2 additions & 4 deletions clang/unittests/Format/SortIncludesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ TEST_F(SortIncludesTest, SupportClangFormatOffCStyle) {
}

TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
FmtStyle.SortIncludes = FormatStyle::SI_Never;
FmtStyle.SortIncludes = {};
verifyFormat("#include \"a.h\"\n"
"#include \"c.h\"\n"
"#include \"b.h\"",
Expand Down Expand Up @@ -628,9 +628,7 @@ TEST_F(SortIncludesTest, MainHeaderIsSeparatedWhenRegroupping) {
}

TEST_F(SortIncludesTest, SupportOptionalCaseSensitiveSorting) {
EXPECT_FALSE(FmtStyle.SortIncludes == FormatStyle::SI_CaseInsensitive);

FmtStyle.SortIncludes = FormatStyle::SI_CaseInsensitive;
FmtStyle.SortIncludes.IgnoreCase = true;

verifyFormat("#include \"A/B.h\"\n"
"#include \"A/b.h\"\n"
Expand Down