SortIncludesTest.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //===- unittest/Format/SortIncludesTest.cpp - Include sort unit tests -----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. namespace {
  17. class SortIncludesTest : public ::testing::Test {
  18. protected:
  19. std::vector<tooling::Range> GetCodeRange(StringRef Code) {
  20. return std::vector<tooling::Range>(1, tooling::Range(0, Code.size()));
  21. }
  22. std::string sort(StringRef Code, StringRef FileName = "input.cpp") {
  23. auto Ranges = GetCodeRange(Code);
  24. auto Sorted =
  25. applyAllReplacements(Code, sortIncludes(Style, Code, Ranges, FileName));
  26. EXPECT_TRUE(static_cast<bool>(Sorted));
  27. auto Result = applyAllReplacements(
  28. *Sorted, reformat(Style, *Sorted, Ranges, FileName));
  29. EXPECT_TRUE(static_cast<bool>(Result));
  30. return *Result;
  31. }
  32. unsigned newCursor(llvm::StringRef Code, unsigned Cursor) {
  33. sortIncludes(Style, Code, GetCodeRange(Code), "input.cpp", &Cursor);
  34. return Cursor;
  35. }
  36. FormatStyle Style = getLLVMStyle();
  37. };
  38. TEST_F(SortIncludesTest, BasicSorting) {
  39. EXPECT_EQ("#include \"a.h\"\n"
  40. "#include \"b.h\"\n"
  41. "#include \"c.h\"\n",
  42. sort("#include \"a.h\"\n"
  43. "#include \"c.h\"\n"
  44. "#include \"b.h\"\n"));
  45. }
  46. TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
  47. // Identical #includes have led to a failure with an unstable sort.
  48. std::string Code = "#include <a>\n"
  49. "#include <b>\n"
  50. "#include <b>\n"
  51. "#include <b>\n"
  52. "#include <b>\n"
  53. "#include <c>\n";
  54. EXPECT_TRUE(sortIncludes(Style, Code, GetCodeRange(Code), "a.cc").empty());
  55. }
  56. TEST_F(SortIncludesTest, SupportClangFormatOff) {
  57. EXPECT_EQ("#include <a>\n"
  58. "#include <b>\n"
  59. "#include <c>\n"
  60. "// clang-format off\n"
  61. "#include <b>\n"
  62. "#include <a>\n"
  63. "#include <c>\n"
  64. "// clang-format on\n",
  65. sort("#include <b>\n"
  66. "#include <a>\n"
  67. "#include <c>\n"
  68. "// clang-format off\n"
  69. "#include <b>\n"
  70. "#include <a>\n"
  71. "#include <c>\n"
  72. "// clang-format on\n"));
  73. }
  74. TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
  75. Style.SortIncludes = false;
  76. EXPECT_EQ("#include \"a.h\"\n"
  77. "#include \"c.h\"\n"
  78. "#include \"b.h\"\n",
  79. sort("#include \"a.h\"\n"
  80. "#include \"c.h\"\n"
  81. "#include \"b.h\"\n"));
  82. }
  83. TEST_F(SortIncludesTest, MixIncludeAndImport) {
  84. EXPECT_EQ("#include \"a.h\"\n"
  85. "#import \"b.h\"\n"
  86. "#include \"c.h\"\n",
  87. sort("#include \"a.h\"\n"
  88. "#include \"c.h\"\n"
  89. "#import \"b.h\"\n"));
  90. }
  91. TEST_F(SortIncludesTest, FixTrailingComments) {
  92. EXPECT_EQ("#include \"a.h\" // comment\n"
  93. "#include \"bb.h\" // comment\n"
  94. "#include \"ccc.h\"\n",
  95. sort("#include \"a.h\" // comment\n"
  96. "#include \"ccc.h\"\n"
  97. "#include \"bb.h\" // comment\n"));
  98. }
  99. TEST_F(SortIncludesTest, LeadingWhitespace) {
  100. EXPECT_EQ("#include \"a.h\"\n"
  101. "#include \"b.h\"\n"
  102. "#include \"c.h\"\n",
  103. sort(" #include \"a.h\"\n"
  104. " #include \"c.h\"\n"
  105. " #include \"b.h\"\n"));
  106. EXPECT_EQ("#include \"a.h\"\n"
  107. "#include \"b.h\"\n"
  108. "#include \"c.h\"\n",
  109. sort("# include \"a.h\"\n"
  110. "# include \"c.h\"\n"
  111. "# include \"b.h\"\n"));
  112. }
  113. TEST_F(SortIncludesTest, GreaterInComment) {
  114. EXPECT_EQ("#include \"a.h\"\n"
  115. "#include \"b.h\" // >\n"
  116. "#include \"c.h\"\n",
  117. sort("#include \"a.h\"\n"
  118. "#include \"c.h\"\n"
  119. "#include \"b.h\" // >\n"));
  120. }
  121. TEST_F(SortIncludesTest, SortsLocallyInEachBlock) {
  122. EXPECT_EQ("#include \"a.h\"\n"
  123. "#include \"c.h\"\n"
  124. "\n"
  125. "#include \"b.h\"\n",
  126. sort("#include \"a.h\"\n"
  127. "#include \"c.h\"\n"
  128. "\n"
  129. "#include \"b.h\"\n"));
  130. }
  131. TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
  132. EXPECT_EQ("#include \"a.h\"\n"
  133. "#include \"c.h\"\n"
  134. "#include <b.h>\n"
  135. "#include <d.h>\n",
  136. sort("#include <d.h>\n"
  137. "#include <b.h>\n"
  138. "#include \"c.h\"\n"
  139. "#include \"a.h\"\n"));
  140. Style = getGoogleStyle(FormatStyle::LK_Cpp);
  141. EXPECT_EQ("#include <b.h>\n"
  142. "#include <d.h>\n"
  143. "#include \"a.h\"\n"
  144. "#include \"c.h\"\n",
  145. sort("#include <d.h>\n"
  146. "#include <b.h>\n"
  147. "#include \"c.h\"\n"
  148. "#include \"a.h\"\n"));
  149. }
  150. TEST_F(SortIncludesTest, HandlesMultilineIncludes) {
  151. EXPECT_EQ("#include \"a.h\"\n"
  152. "#include \"b.h\"\n"
  153. "#include \"c.h\"\n",
  154. sort("#include \"a.h\"\n"
  155. "#include \\\n"
  156. "\"c.h\"\n"
  157. "#include \"b.h\"\n"));
  158. }
  159. TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
  160. Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
  161. EXPECT_EQ("#include \"llvm/a.h\"\n"
  162. "#include \"b.h\"\n"
  163. "#include \"c.h\"\n",
  164. sort("#include \"llvm/a.h\"\n"
  165. "#include \"c.h\"\n"
  166. "#include \"b.h\"\n",
  167. "a.cc"));
  168. EXPECT_EQ("#include \"llvm/a.h\"\n"
  169. "#include \"b.h\"\n"
  170. "#include \"c.h\"\n",
  171. sort("#include \"llvm/a.h\"\n"
  172. "#include \"c.h\"\n"
  173. "#include \"b.h\"\n",
  174. "a_test.cc"));
  175. EXPECT_EQ("#include \"llvm/input.h\"\n"
  176. "#include \"b.h\"\n"
  177. "#include \"c.h\"\n",
  178. sort("#include \"llvm/input.h\"\n"
  179. "#include \"c.h\"\n"
  180. "#include \"b.h\"\n",
  181. "input.mm"));
  182. // Don't allow prefixes.
  183. EXPECT_EQ("#include \"b.h\"\n"
  184. "#include \"c.h\"\n"
  185. "#include \"llvm/not_a.h\"\n",
  186. sort("#include \"llvm/not_a.h\"\n"
  187. "#include \"c.h\"\n"
  188. "#include \"b.h\"\n",
  189. "a.cc"));
  190. // Don't do this for _main and other suffixes.
  191. EXPECT_EQ("#include \"b.h\"\n"
  192. "#include \"c.h\"\n"
  193. "#include \"llvm/a.h\"\n",
  194. sort("#include \"llvm/a.h\"\n"
  195. "#include \"c.h\"\n"
  196. "#include \"b.h\"\n",
  197. "a_main.cc"));
  198. // Don't do this in headers.
  199. EXPECT_EQ("#include \"b.h\"\n"
  200. "#include \"c.h\"\n"
  201. "#include \"llvm/a.h\"\n",
  202. sort("#include \"llvm/a.h\"\n"
  203. "#include \"c.h\"\n"
  204. "#include \"b.h\"\n",
  205. "a.h"));
  206. // Only do this in the first #include block.
  207. EXPECT_EQ("#include <a>\n"
  208. "\n"
  209. "#include \"b.h\"\n"
  210. "#include \"c.h\"\n"
  211. "#include \"llvm/a.h\"\n",
  212. sort("#include <a>\n"
  213. "\n"
  214. "#include \"llvm/a.h\"\n"
  215. "#include \"c.h\"\n"
  216. "#include \"b.h\"\n",
  217. "a.cc"));
  218. // Only recognize the first #include with a matching basename as main include.
  219. EXPECT_EQ("#include \"a.h\"\n"
  220. "#include \"b.h\"\n"
  221. "#include \"c.h\"\n"
  222. "#include \"llvm/a.h\"\n",
  223. sort("#include \"b.h\"\n"
  224. "#include \"a.h\"\n"
  225. "#include \"c.h\"\n"
  226. "#include \"llvm/a.h\"\n",
  227. "a.cc"));
  228. }
  229. TEST_F(SortIncludesTest, NegativePriorities) {
  230. Style.IncludeCategories = {{".*important_os_header.*", -1}, {".*", 1}};
  231. EXPECT_EQ("#include \"important_os_header.h\"\n"
  232. "#include \"c_main.h\"\n"
  233. "#include \"a_other.h\"\n",
  234. sort("#include \"c_main.h\"\n"
  235. "#include \"a_other.h\"\n"
  236. "#include \"important_os_header.h\"\n",
  237. "c_main.cc"));
  238. // check stable when re-run
  239. EXPECT_EQ("#include \"important_os_header.h\"\n"
  240. "#include \"c_main.h\"\n"
  241. "#include \"a_other.h\"\n",
  242. sort("#include \"important_os_header.h\"\n"
  243. "#include \"c_main.h\"\n"
  244. "#include \"a_other.h\"\n",
  245. "c_main.cc"));
  246. }
  247. TEST_F(SortIncludesTest, CalculatesCorrectCursorPosition) {
  248. std::string Code = "#include <ccc>\n" // Start of line: 0
  249. "#include <bbbbbb>\n" // Start of line: 15
  250. "#include <a>\n"; // Start of line: 33
  251. EXPECT_EQ(31u, newCursor(Code, 0));
  252. EXPECT_EQ(13u, newCursor(Code, 15));
  253. EXPECT_EQ(0u, newCursor(Code, 33));
  254. EXPECT_EQ(41u, newCursor(Code, 10));
  255. EXPECT_EQ(23u, newCursor(Code, 25));
  256. EXPECT_EQ(10u, newCursor(Code, 43));
  257. }
  258. } // end namespace
  259. } // end namespace format
  260. } // end namespace clang