Skip to content

Commit 3753f91

Browse files
author
Eric Liu
committedJun 14, 2016
[clang-format] do not add existing includes.
Summary: do not add existing includes. Reviewers: djasper Subscribers: klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D21323 llvm-svn: 272669
1 parent 887a399 commit 3753f91

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed
 

‎clang/lib/Format/Format.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,6 @@ unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
14861486
// code.
14871487
// FIXME: do not insert headers into conditional #include blocks, e.g. #includes
14881488
// surrounded by compile condition "#if...".
1489-
// FIXME: do not insert existing headers.
14901489
// FIXME: insert empty lines between newly created blocks.
14911490
tooling::Replacements
14921491
fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
@@ -1533,10 +1532,12 @@ fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
15331532
TrimmedCode.split(Lines, '\n');
15341533
unsigned Offset = MinInsertOffset;
15351534
unsigned NextLineOffset;
1535+
std::set<StringRef> ExistingIncludes;
15361536
for (auto Line : Lines) {
15371537
NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);
15381538
if (IncludeRegex.match(Line, &Matches)) {
15391539
StringRef IncludeName = Matches[2];
1540+
ExistingIncludes.insert(IncludeName);
15401541
int Category = Categories.getIncludePriority(
15411542
IncludeName, /*CheckMainHeader=*/FirstIncludeOffset < 0);
15421543
CategoryEndOffsets[Category] = NextLineOffset;
@@ -1572,6 +1573,11 @@ fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
15721573
"'#include ...'");
15731574
(void)Matched;
15741575
auto IncludeName = Matches[2];
1576+
if (ExistingIncludes.find(IncludeName) != ExistingIncludes.end()) {
1577+
DEBUG(llvm::dbgs() << "Skip adding existing include : " << IncludeName
1578+
<< "\n");
1579+
continue;
1580+
}
15751581
int Category =
15761582
Categories.getIncludePriority(IncludeName, /*CheckMainHeader=*/true);
15771583
Offset = CategoryEndOffsets[Category];

‎clang/unittests/Format/CleanupTest.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,29 @@ TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCode) {
688688
EXPECT_EQ(Expected, apply(Code, Replaces));
689689
}
690690

691+
TEST_F(CleanUpReplacementsTest, SkipExistingHeaders) {
692+
std::string Code = "#include \"a.h\"\n"
693+
"#include <vector>\n";
694+
std::string Expected = "#include \"a.h\"\n"
695+
"#include <vector>\n";
696+
tooling::Replacements Replaces = {createInsertion("#include <vector>"),
697+
createInsertion("#include \"a.h\"")};
698+
EXPECT_EQ(Expected, apply(Code, Replaces));
699+
}
700+
701+
TEST_F(CleanUpReplacementsTest, AddIncludesWithDifferentForms) {
702+
std::string Code = "#include \"a.h\"\n"
703+
"#include <vector>\n";
704+
// FIXME: this might not be the best behavior.
705+
std::string Expected = "#include \"a.h\"\n"
706+
"#include \"vector\"\n"
707+
"#include <vector>\n"
708+
"#include <a.h>\n";
709+
tooling::Replacements Replaces = {createInsertion("#include \"vector\""),
710+
createInsertion("#include <a.h>")};
711+
EXPECT_EQ(Expected, apply(Code, Replaces));
712+
}
713+
691714
} // end namespace
692715
} // end namespace format
693716
} // end namespace clang

0 commit comments

Comments
 (0)
Please sign in to comment.