diff options
author | Daniel Jasper <djasper@google.com> | 2015-12-21 13:40:49 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2015-12-21 13:40:49 +0000 |
commit | 32d75fa293d9db70ddf8a20797e625f152b1efb5 (patch) | |
tree | a7c599dbc918b4dd5b93cd827fc3a086d44bbf3f | |
parent | 5da2f6cd03aa56411f7fcb0c481bdbd56d309acb (diff) | |
download | llvm-32d75fa293d9db70ddf8a20797e625f152b1efb5.zip llvm-32d75fa293d9db70ddf8a20797e625f152b1efb5.tar.gz llvm-32d75fa293d9db70ddf8a20797e625f152b1efb5.tar.bz2 |
clang-format: Only try to find the "main" include in the first block of
includes.
llvm-svn: 256153
-rw-r--r-- | clang/lib/Format/Format.cpp | 5 | ||||
-rw-r--r-- | clang/unittests/Format/SortIncludesTest.cpp | 13 |
2 files changed, 17 insertions, 1 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index c9058a58..9b9a1b3 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1814,6 +1814,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, FileName.endswith(".cxx") || FileName.endswith(".m") || FileName.endswith(".mm"); StringRef FileStem = llvm::sys::path::stem(FileName); + bool FirstIncludeBlock = true; // Create pre-compiled regular expressions for the #include categories. SmallVector<llvm::Regex, 4> CategoryRegexs; @@ -1843,7 +1844,8 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, break; } } - if (IsSource && Category > 0 && IncludeName.startswith("\"")) { + if (IsSource && Category > 0 && FirstIncludeBlock && + IncludeName.startswith("\"")) { StringRef HeaderStem = llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1)); if (FileStem.startswith(HeaderStem)) @@ -1854,6 +1856,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, Cursor); IncludesInBlock.clear(); + FirstIncludeBlock = false; } Prev = Pos + 1; } diff --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp index ce83091..d96227d 100644 --- a/clang/unittests/Format/SortIncludesTest.cpp +++ b/clang/unittests/Format/SortIncludesTest.cpp @@ -191,6 +191,19 @@ TEST_F(SortIncludesTest, LeavesMainHeaderFirst) { "#include \"c.h\"\n" "#include \"b.h\"\n", "a.h")); + + // Only do this in the first #include block. + EXPECT_EQ("#include <a>\n" + "\n" + "#include \"b.h\"\n" + "#include \"c.h\"\n" + "#include \"llvm/a.h\"\n", + sort("#include <a>\n" + "\n" + "#include \"llvm/a.h\"\n" + "#include \"c.h\"\n" + "#include \"b.h\"\n", + "a.cc")); } TEST_F(SortIncludesTest, NegativePriorities) { |