r256170 - clang-format: Only consider the first #include that looks right to be
Daniel Jasper via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 21 09:28:24 PST 2015
Author: djasper
Date: Mon Dec 21 11:28:24 2015
New Revision: 256170
URL: http://llvm.org/viewvc/llvm-project?rev=256170&view=rev
Log:
clang-format: Only consider the first #include that looks right to be
the main #include.
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/SortIncludesTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=256170&r1=256169&r2=256170&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Dec 21 11:28:24 2015
@@ -1816,6 +1816,7 @@ tooling::Replacements sortIncludes(const
FileName.endswith(".mm");
StringRef FileStem = llvm::sys::path::stem(FileName);
bool FirstIncludeBlock = true;
+ bool MainIncludeFound = false;
// Create pre-compiled regular expressions for the #include categories.
SmallVector<llvm::Regex, 4> CategoryRegexs;
@@ -1845,12 +1846,14 @@ tooling::Replacements sortIncludes(const
break;
}
}
- if (IsSource && Category > 0 && FirstIncludeBlock &&
- IncludeName.startswith("\"")) {
+ if (IsSource && !MainIncludeFound && Category > 0 &&
+ FirstIncludeBlock && IncludeName.startswith("\"")) {
StringRef HeaderStem =
llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
- if (FileStem.startswith(HeaderStem))
+ if (FileStem.startswith(HeaderStem)) {
Category = 0;
+ MainIncludeFound = true;
+ }
}
IncludesInBlock.push_back({IncludeName, Line, Prev, Category});
} else if (!IncludesInBlock.empty()) {
Modified: cfe/trunk/unittests/Format/SortIncludesTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/SortIncludesTest.cpp?rev=256170&r1=256169&r2=256170&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/SortIncludesTest.cpp (original)
+++ cfe/trunk/unittests/Format/SortIncludesTest.cpp Mon Dec 21 11:28:24 2015
@@ -204,6 +204,17 @@ TEST_F(SortIncludesTest, LeavesMainHeade
"#include \"c.h\"\n"
"#include \"b.h\"\n",
"a.cc"));
+
+ // Only recognize the first #include with a matching basename as main include.
+ EXPECT_EQ("#include \"a.h\"\n"
+ "#include \"b.h\"\n"
+ "#include \"c.h\"\n"
+ "#include \"llvm/a.h\"\n",
+ sort("#include \"b.h\"\n"
+ "#include \"a.h\"\n"
+ "#include \"c.h\"\n"
+ "#include \"llvm/a.h\"\n",
+ "a.cc"));
}
TEST_F(SortIncludesTest, NegativePriorities) {
More information about the cfe-commits
mailing list