[clang-tools-extra] [include-cleaner] Handle symbols from system headers. (PR #66089)

kadir çetinkaya via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 18 03:20:58 PDT 2023


================
@@ -176,6 +180,104 @@ headersForSpecialSymbol(const Symbol &S, const SourceManager &SM,
   return std::nullopt;
 }
 
+// A hand-mainained list of include mappings for system headers.
+// The first element is the suffix of the physical file path for the system
+// header, the second element is the final verbatim header spelling.
+const std::pair<llvm::StringRef, llvm::StringRef> IncludeMappings[] = {
+    {"bits/types/struct_timeval.h", "<sys/time.h>"},
+    {"bits/pthreadtypes.h", "<pthread.h>"},
+    {"bits/types/struct_iovec.h", "<sys/uio.h>"},
+    {"linux/limits.h", "<limits.h>"},
+    {"bits/getopt_core.h", "<getopt.h>"},
+    {"asm-generic/socket.h", "<sys/socket.h>"},
+    {"bits/posix1_lim.h", "<limits.h>"},
+    {"bits/time.h", "time.h"},
+    {"bits/getopt_ext.h", "<getopt.h>"},
+    {"bits/types/sigset_t.h", "<signal.h>"},
+    {"bits/types/siginfo_t.h", "<signal.h>"},
+    {"bits/types/FILE.h", "<stdio.h>"},
+    {"asm/unistd_64.h", "<asm/unistd.h>"},
+    {"bits/stat.h", "<sys/stat.h>"},
+    {"asm-generic/ioctls.h", "<sys/ioctls.h>"},
+    {"asm-generic/errno.h", "<errno.h>"},
+    {"asm-generic/int-ll64.h", "<asm/types.h>"},
+    {"bits/sigaction.h", "<signal.h>"},
+    {"bits/types/struct_rusage.h", "<sys/resource.h>"},
+    {"sys/syscall.h", "<syscall.h>"},
+    {"linux/prctl.h", "<sys/prctl.h>"},
+    {"sys/ucontext.h", "<ucontext.h>"},
+    {"bits/types/clockid_t.h", "<time.h>"},
+    {"bits/types/mbstate_t.h", "<uchar.h>"},
+    {"bits/types/mbstate_t.h", "<wchar.h>"},
+    {"bits/types/struct_itimerspec.h", "<time.h>"},
+    {"bits/types/time_t.h", "<time.h>"},
+    {"bits/sockaddr.h", "<time.h>"},
+};
+// The maximum number of path components in a key from SuffixHeaderMapping.
+// Used to minimize the number of lookups in suffix path mappings.
+constexpr int MaxSuffixComponents = 3;
+
+// A mapping for system headers based on a set of filename rules.
+class SystemIncludeMap {
+public:
+  static const SystemIncludeMap &get() {
+    static SystemIncludeMap Instance;
+    static const auto *SystemHeaderMap = [] {
+      const size_t Size = sizeof(IncludeMappings) / sizeof(IncludeMappings[0]);
+      auto *HeaderMap = new std::multimap<llvm::StringRef, llvm::StringRef>();
+      for (size_t I = 0; I < Size; I++) {
+        HeaderMap->insert(IncludeMappings[I]);
+      }
+      return HeaderMap;
+    }();
+
+    // // Check MaxSuffixComponents constant is correct.
+    assert(llvm::all_of(*SystemHeaderMap, [](const auto &KeyAndValue) {
+      return std::distance(
+                 llvm::sys::path::begin(KeyAndValue.first,
+                                        llvm::sys::path::Style::posix),
+                 llvm::sys::path::end(KeyAndValue.first)) <=
+             MaxSuffixComponents;
+    }));
+    // ... and precise.
+    assert(llvm::any_of(*SystemHeaderMap, [](const auto &KeyAndValue) {
+      return std::distance(
+                 llvm::sys::path::begin(KeyAndValue.first,
+                                        llvm::sys::path::Style::posix),
+                 llvm::sys::path::end(KeyAndValue.first)) ==
+             MaxSuffixComponents;
+    }));
+
+    Instance.SuffixHeaderMapping = SystemHeaderMap;
+    return Instance;
+  }
+
+  // Returns the overridden verbatim spellings for HeaderPath that can be
+  // directly included.
+  llvm::SmallVector<llvm::StringRef>
+  mapHeader(llvm::StringRef HeaderPath) const {
----------------
kadircet wrote:

what about making this one static instead and getting rid of the extra singleton interface?

https://github.com/llvm/llvm-project/pull/66089


More information about the cfe-commits mailing list