njames93 created this revision.
njames93 added reviewers: hokein, kadircet.
Herald added a subscriber: xazax.hun.
njames93 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

After D91602 <https://reviews.llvm.org/D91602> gmock support was added to 
include-order check. However that support puts gmock and gtest headers in the 
same category as system headers.
ClangFormat on the other hand for LLVMStyle appears to put system headers after 
these 2 special cases.

This patch synchronizes the behaviour and fixes more false tidy-warnings


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93354

Files:
  clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/llvm-include-order.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/llvm-include-order.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/llvm-include-order.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-include-order.cpp
@@ -6,6 +6,7 @@
 #include "gmock/foo.h"
 #include "i.h"
 #include <s.h>
+#include <a.h>
 #include "llvm/a.h"
 #include "clang/b.h"
 #include "clang-c/c.h" // hi
@@ -19,6 +20,7 @@
 // CHECK-FIXES-NEXT: #include "llvm/a.h"
 // CHECK-FIXES-NEXT: #include "gmock/foo.h"
 // CHECK-FIXES-NEXT: #include "gtest/foo.h"
+// CHECK-FIXES-NEXT: #include <a.h>
 // CHECK-FIXES-NEXT: #include <s.h>
 
 #include "b.h"
Index: clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
+++ clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
@@ -10,6 +10,7 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/STLExtras.h"
 
 #include <map>
 
@@ -61,16 +62,24 @@
   if (IsMainModule)
     return 0;
 
-  // LLVM and clang headers are in the penultimate position.
-  if (Filename.startswith("llvm/") || Filename.startswith("llvm-c/") ||
-      Filename.startswith("clang/") || Filename.startswith("clang-c/"))
+  auto FilenameStartsWith = [&](StringRef Search) {
+    return Filename.startswith(Search);
+  };
+
+  constexpr StringRef LLVMClangHeaders[] = {"llvm/", "llvm-c/", "clang/",
+                                            "clang-c/"};
+  constexpr StringRef OtherHeaders[] = {"gtest/", "gmock/", "isl/", "json/"};
+
+  if (llvm::any_of(LLVMClangHeaders, FilenameStartsWith))
     return 2;
 
-  // System headers are sorted to the end.
-  if (IsAngled || Filename.startswith("gtest/") ||
-      Filename.startswith("gmock/"))
+  if (llvm::any_of(OtherHeaders, FilenameStartsWith))
     return 3;
 
+  // System headers are sorted to the end.
+  if (IsAngled)
+    return 4;
+
   // Other headers are inserted between the main module header and LLVM 
headers.
   return 1;
 }


Index: clang-tools-extra/test/clang-tidy/checkers/llvm-include-order.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/llvm-include-order.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/llvm-include-order.cpp
@@ -6,6 +6,7 @@
 #include "gmock/foo.h"
 #include "i.h"
 #include <s.h>
+#include <a.h>
 #include "llvm/a.h"
 #include "clang/b.h"
 #include "clang-c/c.h" // hi
@@ -19,6 +20,7 @@
 // CHECK-FIXES-NEXT: #include "llvm/a.h"
 // CHECK-FIXES-NEXT: #include "gmock/foo.h"
 // CHECK-FIXES-NEXT: #include "gtest/foo.h"
+// CHECK-FIXES-NEXT: #include <a.h>
 // CHECK-FIXES-NEXT: #include <s.h>
 
 #include "b.h"
Index: clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
+++ clang-tools-extra/clang-tidy/llvm/IncludeOrderCheck.cpp
@@ -10,6 +10,7 @@
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
+#include "llvm/ADT/STLExtras.h"
 
 #include <map>
 
@@ -61,16 +62,24 @@
   if (IsMainModule)
     return 0;
 
-  // LLVM and clang headers are in the penultimate position.
-  if (Filename.startswith("llvm/") || Filename.startswith("llvm-c/") ||
-      Filename.startswith("clang/") || Filename.startswith("clang-c/"))
+  auto FilenameStartsWith = [&](StringRef Search) {
+    return Filename.startswith(Search);
+  };
+
+  constexpr StringRef LLVMClangHeaders[] = {"llvm/", "llvm-c/", "clang/",
+                                            "clang-c/"};
+  constexpr StringRef OtherHeaders[] = {"gtest/", "gmock/", "isl/", "json/"};
+
+  if (llvm::any_of(LLVMClangHeaders, FilenameStartsWith))
     return 2;
 
-  // System headers are sorted to the end.
-  if (IsAngled || Filename.startswith("gtest/") ||
-      Filename.startswith("gmock/"))
+  if (llvm::any_of(OtherHeaders, FilenameStartsWith))
     return 3;
 
+  // System headers are sorted to the end.
+  if (IsAngled)
+    return 4;
+
   // Other headers are inserted between the main module header and LLVM headers.
   return 1;
 }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D93354: [clang-tid... Nathan James via Phabricator via cfe-commits

Reply via email to