clang-format: Enable #include sorting by default.

This has seen quite some usage and I am not aware of any issues. Also
add a style option to enable/disable include sorting. The existing
command line flag can from now on be used to override whatever is set
in the style.

llvm-svn: 253202
This commit is contained in:
Daniel Jasper 2015-11-16 12:38:56 +00:00
parent bc09f39476
commit da44677082
9 changed files with 46 additions and 17 deletions

View File

@ -469,6 +469,9 @@ struct FormatStyle {
/// Pointer and reference alignment style.
PointerAlignmentStyle PointerAlignment;
/// \brief If true, clang-format will sort #includes.
bool SortIncludes;
/// \brief If \c true, a space may be inserted after C style casts.
bool SpaceAfterCStyleCast;

View File

@ -284,6 +284,7 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("PenaltyExcessCharacter", Style.PenaltyExcessCharacter);
IO.mapOptional("PenaltyReturnTypeOnItsOwnLine",
Style.PenaltyReturnTypeOnItsOwnLine);
IO.mapOptional("SortIncludes", Style.SortIncludes);
IO.mapOptional("PointerAlignment", Style.PointerAlignment);
IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
IO.mapOptional("SpaceBeforeAssignmentOperators",
@ -507,6 +508,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
LLVMStyle.DisableFormat = false;
LLVMStyle.SortIncludes = true;
return LLVMStyle;
}
@ -635,6 +637,7 @@ FormatStyle getGNUStyle() {
FormatStyle getNoStyle() {
FormatStyle NoStyle = getLLVMStyle();
NoStyle.DisableFormat = true;
NoStyle.SortIncludes = false;
return NoStyle;
}
@ -1743,6 +1746,9 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code,
ArrayRef<tooling::Range> Ranges,
StringRef FileName) {
tooling::Replacements Replaces;
if (!Style.SortIncludes)
return Replaces;
unsigned Prev = 0;
unsigned SearchFrom = 0;
llvm::Regex IncludeRegex(

View File

@ -0,0 +1,10 @@
// RUN: clang-format %s | FileCheck %s
// RUN: clang-format %s -sort-includes -style="{SortIncludes: false}" | FileCheck %s
// RUN: clang-format %s -sort-includes=false | FileCheck %s -check-prefix=NOT-SORTED
#include <b>
#include <a>
// CHECK: <a>
// CHECK-NEXT: <b>
// NOT-SORTED: <b>
// NOT-SORTED-NEXT: <a>

View File

@ -98,9 +98,11 @@ static cl::opt<unsigned>
"clang-format from an editor integration"),
cl::init(0), cl::cat(ClangFormatCategory));
static cl::opt<bool> SortIncludes("sort-includes",
cl::desc("Sort touched include lines"),
cl::cat(ClangFormatCategory));
static cl::opt<bool> SortIncludes(
"sort-includes",
cl::desc("If set, overrides the include sorting behavior determined by the "
"SortIncludes style flag"),
cl::cat(ClangFormatCategory));
static cl::list<std::string> FileNames(cl::Positional, cl::desc("[<file> ...]"),
cl::cat(ClangFormatCategory));
@ -252,17 +254,14 @@ static bool format(StringRef FileName) {
return true;
StringRef AssumedFileName = (FileName == "-") ? AssumeFileName : FileName;
FormatStyle FormatStyle = getStyle(Style, AssumedFileName, FallbackStyle);
Replacements Replaces;
std::string ChangedCode;
if (SortIncludes) {
Replaces =
sortIncludes(FormatStyle, Code->getBuffer(), Ranges, AssumedFileName);
ChangedCode = tooling::applyAllReplacements(Code->getBuffer(), Replaces);
for (const auto &R : Replaces)
Ranges.push_back({R.getOffset(), R.getLength()});
} else {
ChangedCode = Code->getBuffer().str();
}
if (SortIncludes.getNumOccurrences() != 0)
FormatStyle.SortIncludes = SortIncludes;
Replacements Replaces =
sortIncludes(FormatStyle, Code->getBuffer(), Ranges, AssumedFileName);
std::string ChangedCode =
tooling::applyAllReplacements(Code->getBuffer(), Replaces);
for (const auto &R : Replaces)
Ranges.push_back({R.getOffset(), R.getLength()});
bool IncompleteFormat = false;
Replaces = tooling::mergeReplacements(

View File

@ -32,7 +32,7 @@ class ClangFormatCommand(sublime_plugin.TextCommand):
if encoding == 'Undefined':
encoding = 'utf-8'
regions = []
command = [binary, '-sort-includes', '-style', style]
command = [binary, '-style', style]
for region in self.view.sel():
regions.append(region)
region_offset = min(region.a, region.b)

View File

@ -126,7 +126,6 @@ is no active region. If no style is given uses `clang-format-style'."
nil `(,temp-buffer ,temp-file) nil
"-output-replacements-xml"
"-sort-includes"
"-assume-filename" (or (buffer-file-name) "")
"-style" style
"-offset" (number-to-string start)

View File

@ -72,7 +72,7 @@ def main():
startupinfo.wShowWindow = subprocess.SW_HIDE
# Call formatter.
command = [binary, '-style', style, '-cursor', str(cursor), '-sort-includes']
command = [binary, '-style', style, '-cursor', str(cursor)]
if lines != 'all':
command.extend(['-lines', lines])
if fallback_style:

View File

@ -9563,12 +9563,14 @@ TEST_F(FormatTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
CHECK_PARSE_BOOL(DerivePointerAlignment);
CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
CHECK_PARSE_BOOL(DisableFormat);
CHECK_PARSE_BOOL(IndentCaseLabels);
CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
CHECK_PARSE_BOOL(Cpp11BracedListStyle);
CHECK_PARSE_BOOL(SortIncludes);
CHECK_PARSE_BOOL(SpacesInParentheses);
CHECK_PARSE_BOOL(SpacesInSquareBrackets);
CHECK_PARSE_BOOL(SpacesInAngles);

View File

@ -40,6 +40,16 @@ TEST_F(SortIncludesTest, BasicSorting) {
"#include \"b.h\"\n"));
}
TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
Style.SortIncludes = false;
EXPECT_EQ("#include \"a.h\"\n"
"#include \"c.h\"\n"
"#include \"b.h\"\n",
sort("#include \"a.h\"\n"
"#include \"c.h\"\n"
"#include \"b.h\"\n"));
}
TEST_F(SortIncludesTest, MixIncludeAndImport) {
EXPECT_EQ("#include \"a.h\"\n"
"#import \"b.h\"\n"