[PATCH] D20362: [clang-format] Make formatReplacements() also sort #includes.
Eric Liu via cfe-commits
cfe-commits at lists.llvm.org
Wed May 18 05:46:08 PDT 2016
ioeric created this revision.
ioeric added reviewers: bkramer, djasper.
ioeric added a subscriber: cfe-commits.
Herald added a subscriber: klimek.
[clang-format] Make formatReplacements() also sort #includes.
http://reviews.llvm.org/D20362
Files:
lib/Format/Format.cpp
unittests/Format/FormatTest.cpp
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -11559,6 +11559,31 @@
EXPECT_EQ(Expected, applyAllReplacements(Code, FinalReplaces));
}
+TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
+ std::string Code = "#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "\n"
+ "int main() {\n"
+ " return 0;\n"
+ "}";
+ std::string Expected = "#include \"a.h\"\n"
+ "#include \"b.h\"\n"
+ "#include \"c.h\"\n"
+ "\n"
+ "int main() {\n"
+ " return 0;\n"
+ "}";
+ FileID ID = Context.createInMemoryFile("fix.cpp", Code);
+ tooling::Replacements Replaces;
+ Replaces.insert(tooling::Replacement(
+ Context.Sources, Context.getLocation(ID, 1, 1), 0, "#include \"b.h\"\n"));
+
+ format::FormatStyle Style = format::getLLVMStyle();
+ Style.SortIncludes = true;
+ auto FinalReplaces = formatReplacements(Code, Replaces, Style);
+ EXPECT_EQ(Expected, applyAllReplacements(Code, FinalReplaces));
+}
+
} // end namespace
} // end namespace format
} // end namespace clang
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -2140,13 +2140,23 @@
const tooling::Replacements &Replaces,
const FormatStyle &Style) {
// We need to use lambda function here since there are two versions of
+ // `sortIncludes`.
+ auto SortIncludes = [](const FormatStyle &Style, StringRef Code,
+ std::vector<tooling::Range> Ranges,
+ StringRef FileName) -> tooling::Replacements {
+ return sortIncludes(Style, Code, Ranges, FileName);
+ };
+ tooling::Replacements SortedReplaces =
+ processReplacements(SortIncludes, Code, Replaces, Style);
+
+ // We need to use lambda function here since there are two versions of
// `reformat`.
auto Reformat = [](const FormatStyle &Style, StringRef Code,
std::vector<tooling::Range> Ranges,
StringRef FileName) -> tooling::Replacements {
return reformat(Style, Code, Ranges, FileName);
};
- return processReplacements(Reformat, Code, Replaces, Style);
+ return processReplacements(Reformat, Code, SortedReplaces, Style);
}
tooling::Replacements
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20362.57601.patch
Type: text/x-patch
Size: 2654 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160518/e2e29229/attachment.bin>
More information about the cfe-commits
mailing list