[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:57 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() {
----------------
kadircet wrote:

i think it's important that we also make these mappings different per language. most of them will likely be language independent, but at least libc extensions are likely to have different spellings for c vs c++.

moreover these fell more like private -> public mappings we perform via PragmaIncludes, so what about just moving them there? that way we can also initialize the mapping based on language options and also benefit all the other users of pragma includes with similar mappings?

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


More information about the cfe-commits mailing list