chandlerc updated this revision to Diff 103885.
chandlerc marked an inline comment as done.
chandlerc added a comment.

Update based on review comments.


https://reviews.llvm.org/D33932

Files:
  include/clang/Format/Format.h
  lib/Format/Format.cpp
  unittests/Format/SortIncludesTest.cpp

Index: unittests/Format/SortIncludesTest.cpp
===================================================================
--- unittests/Format/SortIncludesTest.cpp
+++ unittests/Format/SortIncludesTest.cpp
@@ -266,6 +266,47 @@
                  "a.cc"));
 }
 
+TEST_F(SortIncludesTest, SupportCaseInsensitiveMatching) {
+  // Setup an regex for main includes so we can cover those as well.
+  Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
+
+  // First check that case sensitive matching works correctly by having both
+  // main header and grouping that depend on case.
+  Style.IncludeRegexCaseInsensitive = false;
+  EXPECT_EQ("#include \"GTest/GTest.h\"\n"
+            "#include \"LLVM/z.h\"\n"
+            "#include \"b.h\"\n"
+            "#include \"c.h\"\n"
+            "#include \"llvm/A.h\"\n"
+            "#include \"gmock/gmock.h\"\n",
+            sort("#include \"c.h\"\n"
+                 "#include \"b.h\"\n"
+                 "#include \"GTest/GTest.h\"\n"
+                 "#include \"llvm/A.h\"\n"
+                 "#include \"gmock/gmock.h\"\n"
+                 "#include \"LLVM/z.h\"\n",
+                 "a_TEST.cc"));
+
+  // Now turn insitive matching on and ensure both main header detection and
+  // grouping work in a case insensitive manner.
+  Style.IncludeRegexCaseInsensitive = true;
+  EXPECT_EQ("#include \"llvm/A.h\"\n"
+            "#include \"b.h\"\n"
+            "#include \"c.h\"\n"
+            "#include \"LLVM/z.h\"\n"
+            "#include \"llvm/X.h\"\n"
+            "#include \"GTest/GTest.h\"\n"
+            "#include \"gmock/gmock.h\"\n",
+            sort("#include \"c.h\"\n"
+                 "#include \"b.h\"\n"
+                 "#include \"GTest/GTest.h\"\n"
+                 "#include \"llvm/A.h\"\n"
+                 "#include \"gmock/gmock.h\"\n"
+                 "#include \"llvm/X.h\"\n"
+                 "#include \"LLVM/z.h\"\n",
+                 "a_TEST.cc"));
+}
+
 TEST_F(SortIncludesTest, NegativePriorities) {
   Style.IncludeCategories = {{".*important_os_header.*", -1}, {".*", 1}};
   EXPECT_EQ("#include \"important_os_header.h\"\n"
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -349,6 +349,8 @@
     IO.mapOptional("ForEachMacros", Style.ForEachMacros);
     IO.mapOptional("IncludeCategories", Style.IncludeCategories);
     IO.mapOptional("IncludeIsMainRegex", Style.IncludeIsMainRegex);
+    IO.mapOptional("IncludeRegexCaseInsensitive",
+                   Style.IncludeRegexCaseInsensitive);
     IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels);
     IO.mapOptional("IndentWidth", Style.IndentWidth);
     IO.mapOptional("IndentWrappedFunctionNames",
@@ -579,9 +581,10 @@
   LLVMStyle.ForEachMacros.push_back("Q_FOREACH");
   LLVMStyle.ForEachMacros.push_back("BOOST_FOREACH");
   LLVMStyle.IncludeCategories = {{"^\"(llvm|llvm-c|clang|clang-c)/", 2},
-                                 {"^(<|\"(gtest|isl|json)/)", 3},
+                                 {"^(<|\"(gtest|gmock|isl|json)/)", 3},
                                  {".*", 1}};
-  LLVMStyle.IncludeIsMainRegex = "$";
+  LLVMStyle.IncludeIsMainRegex = "(Test)?$";
+  LLVMStyle.IncludeRegexCaseInsensitive = true;
   LLVMStyle.IndentCaseLabels = false;
   LLVMStyle.IndentWrappedFunctionNames = false;
   LLVMStyle.IndentWidth = 2;
@@ -1409,7 +1412,10 @@
       : Style(Style), FileName(FileName) {
     FileStem = llvm::sys::path::stem(FileName);
     for (const auto &Category : Style.IncludeCategories)
-      CategoryRegexs.emplace_back(Category.Regex);
+      CategoryRegexs.emplace_back(Category.Regex,
+                                  Style.IncludeRegexCaseInsensitive
+                                      ? llvm::Regex::IgnoreCase
+                                      : llvm::Regex::NoFlags);
     IsMainFile = FileName.endswith(".c") || FileName.endswith(".cc") ||
                  FileName.endswith(".cpp") || FileName.endswith(".c++") ||
                  FileName.endswith(".cxx") || FileName.endswith(".m") ||
@@ -1437,9 +1443,13 @@
       return false;
     StringRef HeaderStem =
         llvm::sys::path::stem(IncludeName.drop_front(1).drop_back(1));
-    if (FileStem.startswith(HeaderStem)) {
+    if (FileStem.startswith(HeaderStem) ||
+        (Style.IncludeRegexCaseInsensitive &&
+         FileStem.startswith_lower(HeaderStem))) {
       llvm::Regex MainIncludeRegex(
-          (HeaderStem + Style.IncludeIsMainRegex).str());
+          (HeaderStem + Style.IncludeIsMainRegex).str(),
+          Style.IncludeRegexCaseInsensitive ? llvm::Regex::IgnoreCase
+                                            : llvm::Regex::NoFlags);
       if (MainIncludeRegex.match(FileStem))
         return true;
     }
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -966,7 +966,7 @@
   ///   IncludeCategories:
   ///     - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
   ///       Priority:        2
-  ///     - Regex:           '^(<|"(gtest|isl|json)/)'
+  ///     - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
   ///       Priority:        3
   ///     - Regex:           '.*'
   ///       Priority:        1
@@ -986,6 +986,12 @@
   /// as the "main" include in both a.cc and a_test.cc.
   std::string IncludeIsMainRegex;
 
+  /// \brief Match include regular expressions in a case insensitive manner.
+  ///
+  /// This applies both to ``IncludeIsMainRegex`` and the regular expressions
+  /// inside ``IncludeCategories``.
+  bool IncludeRegexCaseInsensitive;
+
   /// \brief Indent case labels one level from the switch statement.
   ///
   /// When ``false``, use the same indentation level as for the switch statement.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to