mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-04-02 13:12:09 +00:00
[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
This commit is contained in:
parent
887a399418
commit
3753f912bf
@ -1486,7 +1486,6 @@ unsigned getOffsetAfterHeaderGuardsAndComments(StringRef FileName,
|
|||||||
// code.
|
// code.
|
||||||
// FIXME: do not insert headers into conditional #include blocks, e.g. #includes
|
// FIXME: do not insert headers into conditional #include blocks, e.g. #includes
|
||||||
// surrounded by compile condition "#if...".
|
// surrounded by compile condition "#if...".
|
||||||
// FIXME: do not insert existing headers.
|
|
||||||
// FIXME: insert empty lines between newly created blocks.
|
// FIXME: insert empty lines between newly created blocks.
|
||||||
tooling::Replacements
|
tooling::Replacements
|
||||||
fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
|
fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
|
||||||
@ -1533,10 +1532,12 @@ fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
|
|||||||
TrimmedCode.split(Lines, '\n');
|
TrimmedCode.split(Lines, '\n');
|
||||||
unsigned Offset = MinInsertOffset;
|
unsigned Offset = MinInsertOffset;
|
||||||
unsigned NextLineOffset;
|
unsigned NextLineOffset;
|
||||||
|
std::set<StringRef> ExistingIncludes;
|
||||||
for (auto Line : Lines) {
|
for (auto Line : Lines) {
|
||||||
NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);
|
NextLineOffset = std::min(Code.size(), Offset + Line.size() + 1);
|
||||||
if (IncludeRegex.match(Line, &Matches)) {
|
if (IncludeRegex.match(Line, &Matches)) {
|
||||||
StringRef IncludeName = Matches[2];
|
StringRef IncludeName = Matches[2];
|
||||||
|
ExistingIncludes.insert(IncludeName);
|
||||||
int Category = Categories.getIncludePriority(
|
int Category = Categories.getIncludePriority(
|
||||||
IncludeName, /*CheckMainHeader=*/FirstIncludeOffset < 0);
|
IncludeName, /*CheckMainHeader=*/FirstIncludeOffset < 0);
|
||||||
CategoryEndOffsets[Category] = NextLineOffset;
|
CategoryEndOffsets[Category] = NextLineOffset;
|
||||||
@ -1572,6 +1573,11 @@ fixCppIncludeInsertions(StringRef Code, const tooling::Replacements &Replaces,
|
|||||||
"'#include ...'");
|
"'#include ...'");
|
||||||
(void)Matched;
|
(void)Matched;
|
||||||
auto IncludeName = Matches[2];
|
auto IncludeName = Matches[2];
|
||||||
|
if (ExistingIncludes.find(IncludeName) != ExistingIncludes.end()) {
|
||||||
|
DEBUG(llvm::dbgs() << "Skip adding existing include : " << IncludeName
|
||||||
|
<< "\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
int Category =
|
int Category =
|
||||||
Categories.getIncludePriority(IncludeName, /*CheckMainHeader=*/true);
|
Categories.getIncludePriority(IncludeName, /*CheckMainHeader=*/true);
|
||||||
Offset = CategoryEndOffsets[Category];
|
Offset = CategoryEndOffsets[Category];
|
||||||
|
@ -688,6 +688,29 @@ TEST_F(CleanUpReplacementsTest, NoNewLineAtTheEndOfCode) {
|
|||||||
EXPECT_EQ(Expected, apply(Code, Replaces));
|
EXPECT_EQ(Expected, apply(Code, Replaces));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(CleanUpReplacementsTest, SkipExistingHeaders) {
|
||||||
|
std::string Code = "#include \"a.h\"\n"
|
||||||
|
"#include <vector>\n";
|
||||||
|
std::string Expected = "#include \"a.h\"\n"
|
||||||
|
"#include <vector>\n";
|
||||||
|
tooling::Replacements Replaces = {createInsertion("#include <vector>"),
|
||||||
|
createInsertion("#include \"a.h\"")};
|
||||||
|
EXPECT_EQ(Expected, apply(Code, Replaces));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(CleanUpReplacementsTest, AddIncludesWithDifferentForms) {
|
||||||
|
std::string Code = "#include \"a.h\"\n"
|
||||||
|
"#include <vector>\n";
|
||||||
|
// FIXME: this might not be the best behavior.
|
||||||
|
std::string Expected = "#include \"a.h\"\n"
|
||||||
|
"#include \"vector\"\n"
|
||||||
|
"#include <vector>\n"
|
||||||
|
"#include <a.h>\n";
|
||||||
|
tooling::Replacements Replaces = {createInsertion("#include \"vector\""),
|
||||||
|
createInsertion("#include <a.h>")};
|
||||||
|
EXPECT_EQ(Expected, apply(Code, Replaces));
|
||||||
|
}
|
||||||
|
|
||||||
} // end namespace
|
} // end namespace
|
||||||
} // end namespace format
|
} // end namespace format
|
||||||
} // end namespace clang
|
} // end namespace clang
|
||||||
|
Loading…
x
Reference in New Issue
Block a user