r269924 - [clang-format] Make formatReplacements() also sort #includes.
Eric Liu via cfe-commits
cfe-commits at lists.llvm.org
Wed May 18 06:43:48 PDT 2016
Author: ioeric
Date: Wed May 18 08:43:48 2016
New Revision: 269924
URL: http://llvm.org/viewvc/llvm-project?rev=269924&view=rev
Log:
[clang-format] Make formatReplacements() also sort #includes.
Summary: [clang-format] Make formatReplacements() also sort #includes.
Reviewers: bkramer, djasper
Subscribers: klimek, cfe-commits
Differential Revision: http://reviews.llvm.org/D20362
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=269924&r1=269923&r2=269924&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed May 18 08:43:48 2016
@@ -2140,13 +2140,23 @@ tooling::Replacements formatReplacements
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
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=269924&r1=269923&r2=269924&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed May 18 08:43:48 2016
@@ -11559,6 +11559,31 @@ TEST_F(ReplacementTest, FixOnlyAffectedC
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
More information about the cfe-commits
mailing list