clang-tools  16.0.0git
CanonicalIncludes.cpp
Go to the documentation of this file.
1 //===-- CanonicalIncludes.h - remap #include headers-------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CanonicalIncludes.h"
10 #include "Headers.h"
11 #include "clang/Basic/FileEntry.h"
12 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/FileSystem/UniqueID.h"
15 #include "llvm/Support/Path.h"
16 #include <algorithm>
17 
18 namespace clang {
19 namespace clangd {
20 namespace {
21 const std::pair<llvm::StringRef, llvm::StringRef> IncludeMappings[] = {
22  {"include/__stddef_max_align_t.h", "<cstddef>"},
23  {"include/__wmmintrin_aes.h", "<wmmintrin.h>"},
24  {"include/__wmmintrin_pclmul.h", "<wmmintrin.h>"},
25  {"include/adxintrin.h", "<immintrin.h>"},
26  {"include/ammintrin.h", "<ammintrin.h>"},
27  {"include/avx2intrin.h", "<immintrin.h>"},
28  {"include/avx512bwintrin.h", "<immintrin.h>"},
29  {"include/avx512cdintrin.h", "<immintrin.h>"},
30  {"include/avx512dqintrin.h", "<immintrin.h>"},
31  {"include/avx512erintrin.h", "<immintrin.h>"},
32  {"include/avx512fintrin.h", "<immintrin.h>"},
33  {"include/avx512ifmaintrin.h", "<immintrin.h>"},
34  {"include/avx512ifmavlintrin.h", "<immintrin.h>"},
35  {"include/avx512pfintrin.h", "<immintrin.h>"},
36  {"include/avx512vbmiintrin.h", "<immintrin.h>"},
37  {"include/avx512vbmivlintrin.h", "<immintrin.h>"},
38  {"include/avx512vlbwintrin.h", "<immintrin.h>"},
39  {"include/avx512vlcdintrin.h", "<immintrin.h>"},
40  {"include/avx512vldqintrin.h", "<immintrin.h>"},
41  {"include/avx512vlintrin.h", "<immintrin.h>"},
42  {"include/avxintrin.h", "<immintrin.h>"},
43  {"include/bmi2intrin.h", "<x86intrin.h>"},
44  {"include/bmiintrin.h", "<x86intrin.h>"},
45  {"include/emmintrin.h", "<emmintrin.h>"},
46  {"include/f16cintrin.h", "<emmintrin.h>"},
47  {"include/float.h", "<cfloat>"},
48  {"include/fma4intrin.h", "<x86intrin.h>"},
49  {"include/fmaintrin.h", "<immintrin.h>"},
50  {"include/fxsrintrin.h", "<immintrin.h>"},
51  {"include/ia32intrin.h", "<x86intrin.h>"},
52  {"include/immintrin.h", "<immintrin.h>"},
53  {"include/inttypes.h", "<cinttypes>"},
54  {"include/limits.h", "<climits>"},
55  {"include/lzcntintrin.h", "<x86intrin.h>"},
56  {"include/mm3dnow.h", "<mm3dnow.h>"},
57  {"include/mm_malloc.h", "<mm_malloc.h>"},
58  {"include/mmintrin.h", "<mmintrin>"},
59  {"include/mwaitxintrin.h", "<x86intrin.h>"},
60  {"include/pkuintrin.h", "<immintrin.h>"},
61  {"include/pmmintrin.h", "<pmmintrin.h>"},
62  {"include/popcntintrin.h", "<popcntintrin.h>"},
63  {"include/prfchwintrin.h", "<x86intrin.h>"},
64  {"include/rdseedintrin.h", "<x86intrin.h>"},
65  {"include/rtmintrin.h", "<immintrin.h>"},
66  {"include/shaintrin.h", "<immintrin.h>"},
67  {"include/smmintrin.h", "<smmintrin.h>"},
68  {"include/stdalign.h", "<cstdalign>"},
69  {"include/stdarg.h", "<cstdarg>"},
70  {"include/stdbool.h", "<cstdbool>"},
71  {"include/stddef.h", "<cstddef>"},
72  {"include/stdint.h", "<cstdint>"},
73  {"include/tbmintrin.h", "<x86intrin.h>"},
74  {"include/tmmintrin.h", "<tmmintrin.h>"},
75  {"include/wmmintrin.h", "<wmmintrin.h>"},
76  {"include/x86intrin.h", "<x86intrin.h>"},
77  {"include/xmmintrin.h", "<xmmintrin.h>"},
78  {"include/xopintrin.h", "<x86intrin.h>"},
79  {"include/xsavecintrin.h", "<immintrin.h>"},
80  {"include/xsaveintrin.h", "<immintrin.h>"},
81  {"include/xsaveoptintrin.h", "<immintrin.h>"},
82  {"include/xsavesintrin.h", "<immintrin.h>"},
83  {"include/xtestintrin.h", "<immintrin.h>"},
84  {"include/_G_config.h", "<cstdio>"},
85  {"include/assert.h", "<cassert>"},
86  {"algorithm", "<algorithm>"},
87  {"valarray", "<valarray>"},
88  {"array", "<array>"},
89  {"atomic", "<atomic>"},
90  {"backward/auto_ptr.h", "<memory>"},
91  {"backward/binders.h", "<string>"},
92  {"bits/algorithmfwd.h", "<algorithm>"},
93  {"bits/alloc_traits.h", "<memory>"},
94  {"bits/allocated_ptr.h", "<memory>"},
95  {"bits/allocator.h", "<allocator>"},
96  {"bits/atomic_base.h", "<atomic>"},
97  {"bits/atomic_lockfree_defines.h", "<exception>"},
98  {"bits/atomic_futex.h", "<atomic>"},
99  {"bits/basic_ios.h", "<ios>"},
100  {"bits/basic_ios.tcc", "<ios>"},
101  {"bits/basic_string.h", "<string>"},
102  {"bits/basic_string.tcc", "<string>"},
103  {"bits/char_traits.h", "<string>"},
104  {"bits/codecvt.h", "<locale>"},
105  {"bits/concept_check.h", "<numeric>"},
106  {"bits/cpp_type_traits.h", "<cmath>"},
107  {"bits/cxxabi_forced.h", "<cxxabi.h>"},
108  {"bits/deque.tcc", "<deque>"},
109  {"bits/exception.h", "<exception>"},
110  {"bits/exception_defines.h", "<exception>"},
111  {"bits/exception_ptr.h", "<exception>"},
112  {"bits/forward_list.h", "<forward_list>"},
113  {"bits/forward_list.tcc", "<forward_list>"},
114  {"bits/fstream.tcc", "<fstream>"},
115  {"bits/functexcept.h", "<list>"},
116  {"bits/functional_hash.h", "<functional>"},
117  {"bits/gslice.h", "<valarray>"},
118  {"bits/gslice_array.h", "<valarray>"},
119  {"bits/hash_bytes.h", "<typeinfo>"},
120  {"bits/hashtable.h", "<unordered_set>"},
121  {"bits/hashtable_policy.h", "<unordered_set>"},
122  {"bits/indirect_array.h", "<valarray>"},
123  {"bits/invoke.h", "<functional>"},
124  {"bits/ios_base.h", "<ios>"},
125  {"bits/istream.tcc", "<istream>"},
126  {"bits/list.tcc", "<list>"},
127  {"bits/locale_classes.h", "<locale>"},
128  {"bits/locale_classes.tcc", "<locale>"},
129  {"bits/locale_conv.h", "<locale>"},
130  {"bits/locale_facets.h", "<locale>"},
131  {"bits/locale_facets.tcc", "<locale>"},
132  {"bits/locale_facets_nonio.h", "<locale>"},
133  {"bits/locale_facets_nonio.tcc", "<locale>"},
134  {"bits/localefwd.h", "<locale>"},
135  {"bits/mask_array.h", "<valarray>"},
136  {"bits/memoryfwd.h", "<memory>"},
137  {"bits/move.h", "<utility>"},
138  {"bits/nested_exception.h", "<exception>"},
139  {"bits/ostream.tcc", "<ostream>"},
140  {"bits/ostream_insert.h", "<ostream>"},
141  {"bits/parse_numbers.h", "<chrono>"},
142  {"bits/postypes.h", "<ios>"},
143  {"bits/predefined_ops.h", "<algorithm>"},
144  {"bits/ptr_traits.h", "<memory>"},
145  {"bits/quoted_string.h", "<iomanip>"},
146  {"bits/random.h", "<random>"},
147  {"bits/random.tcc", "<random>"},
148  {"bits/range_access.h", "<iterator>"},
149  {"bits/refwrap.h", "<functional>"},
150  {"bits/regex.h", "<regex>"},
151  {"bits/regex_automaton.h", "<regex>"},
152  {"bits/regex_compiler.h", "<regex>"},
153  {"bits/regex_constants.h", "<regex>"},
154  {"bits/regex_cursor.h", "<regex>"},
155  {"bits/regex_error.h", "<regex>"},
156  {"bits/regex_executor.h", "<regex>"},
157  {"bits/regex_grep_matcher.h", "<regex>"},
158  {"bits/regex_grep_matcher.tcc", "<regex>"},
159  {"bits/regex_nfa.h", "<regex>"},
160  {"bits/regex_scanner.h", "<regex>"},
161  {"bits/shared_ptr.h", "<memory>"},
162  {"bits/shared_ptr_base.h", "<memory>"},
163  {"bits/shared_ptr_atomic.h", "<atomic>"},
164  {"bits/slice_array.h", "<valarray>"},
165  {"bits/sstream.tcc", "<sstream>"},
166  {"bits/std_abs.h", "<cmath>"},
167  {"bits/std_function.h", "<functional>"},
168  {"bits/std_mutex.h", "<mutex>"},
169  {"bits/stl_algo.h", "<algorithm>"},
170  {"bits/stl_algobase.h", "<algorithm>"},
171  {"bits/stl_bvector.h", "<vector>"},
172  {"bits/stl_construct.h", "<deque>"},
173  {"bits/stl_deque.h", "<deque>"},
174  {"bits/stl_function.h", "<functional>"},
175  {"bits/stl_heap.h", "<heap>"},
176  {"bits/stl_iterator.h", "<iterator>"},
177  {"bits/stl_iterator_base_funcs.h", "<iterator>"},
178  {"bits/stl_iterator_base_types.h", "<iterator>"},
179  {"bits/stl_list.h", "<list>"},
180  {"bits/stl_map.h", "<map>"},
181  {"bits/stl_multimap.h", "<map>"},
182  {"bits/stl_multiset.h", "<set>"},
183  {"bits/stl_numeric.h", "<numeric>"},
184  {"bits/stl_pair.h", "<utility>"},
185  {"bits/stl_queue.h", "<queue>"},
186  {"bits/stl_raw_storage_iter.h", "<memory>"},
187  {"bits/stl_relops.h", "<utility>"},
188  {"bits/stl_set.h", "<set>"},
189  {"bits/stl_stack.h", "<stack>"},
190  {"bits/stl_tempbuf.h", "<memory>"},
191  {"bits/stl_tree.h", "<map>"},
192  {"bits/stl_uninitialized.h", "<memory>"},
193  {"bits/stl_vector.h", "<vector>"},
194  {"bits/stream_iterator.h", "<iterator>"},
195  {"bits/streambuf.tcc", "<streambuf>"},
196  {"bits/streambuf_iterator.h", "<iterator>"},
197  {"bits/stringfwd.h", "<string>"},
198  {"bits/uniform_int_dist.h", "<random>"},
199  {"bits/unique_ptr.h", "<memory>"},
200  {"bits/unordered_map.h", "<unordered_map>"},
201  {"bits/unordered_set.h", "<unordered_set>"},
202  {"bits/uses_allocator.h", "<memory>"},
203  {"bits/valarray_after.h", "<valarray>"},
204  {"bits/valarray_array.h", "<valarray>"},
205  {"bits/valarray_array.tcc", "<valarray>"},
206  {"bits/valarray_before.h", "<valarray>"},
207  {"bits/vector.tcc", "<vector>"},
208  {"bitset", "<bitset>"},
209  {"ccomplex", "<ccomplex>"},
210  {"cctype", "<cctype>"},
211  {"cerrno", "<cerrno>"},
212  {"cfenv", "<cfenv>"},
213  {"cfloat", "<cfloat>"},
214  {"chrono", "<chrono>"},
215  {"cinttypes", "<cinttypes>"},
216  {"climits", "<climits>"},
217  {"clocale", "<clocale>"},
218  {"cmath", "<cmath>"},
219  {"complex", "<complex>"},
220  {"complex.h", "<complex.h>"},
221  {"condition_variable", "<condition_variable>"},
222  {"csetjmp", "<csetjmp>"},
223  {"csignal", "<csignal>"},
224  {"cstdalign", "<cstdalign>"},
225  {"cstdarg", "<cstdarg>"},
226  {"cstdbool", "<cstdbool>"},
227  {"cstdint", "<cstdint>"},
228  {"cstdio", "<cstdio>"},
229  {"cstdlib", "<cstdlib>"},
230  {"cstring", "<cstring>"},
231  {"ctgmath", "<ctgmath>"},
232  {"ctime", "<ctime>"},
233  {"cwchar", "<cwchar>"},
234  {"cwctype", "<cwctype>"},
235  {"cxxabi.h", "<cxxabi.h>"},
236  {"debug/debug.h", "<numeric>"},
237  {"debug/map.h", "<map>"},
238  {"debug/multimap.h", "<multimap>"},
239  {"debug/multiset.h", "<multiset>"},
240  {"debug/set.h", "<set>"},
241  {"deque", "<deque>"},
242  {"exception", "<exception>"},
243  {"ext/alloc_traits.h", "<deque>"},
244  {"ext/atomicity.h", "<memory>"},
245  {"ext/concurrence.h", "<memory>"},
246  {"ext/new_allocator.h", "<string>"},
247  {"ext/numeric_traits.h", "<list>"},
248  {"ext/string_conversions.h", "<string>"},
249  {"ext/type_traits.h", "<cmath>"},
250  {"fenv.h", "<fenv.h>"},
251  {"forward_list", "<forward_list>"},
252  {"fstream", "<fstream>"},
253  {"functional", "<functional>"},
254  {"future", "<future>"},
255  {"initializer_list", "<initializer_list>"},
256  {"iomanip", "<iomanip>"},
257  {"ios", "<ios>"},
258  {"iosfwd", "<iosfwd>"},
259  {"iostream", "<iostream>"},
260  {"istream", "<istream>"},
261  {"iterator", "<iterator>"},
262  {"limits", "<limits>"},
263  {"list", "<list>"},
264  {"locale", "<locale>"},
265  {"map", "<map>"},
266  {"memory", "<memory>"},
267  {"shared_mutex", "<shared_mutex>"},
268  {"mutex", "<mutex>"},
269  {"new", "<new>"},
270  {"numeric", "<numeric>"},
271  {"ostream", "<ostream>"},
272  {"queue", "<queue>"},
273  {"random", "<random>"},
274  {"ratio", "<ratio>"},
275  {"regex", "<regex>"},
276  {"scoped_allocator", "<scoped_allocator>"},
277  {"set", "<set>"},
278  {"sstream", "<sstream>"},
279  {"stack", "<stack>"},
280  {"stdexcept", "<stdexcept>"},
281  {"streambuf", "<streambuf>"},
282  {"string", "<string>"},
283  {"system_error", "<system_error>"},
284  {"tgmath.h", "<tgmath.h>"},
285  {"thread", "<thread>"},
286  {"tuple", "<tuple>"},
287  {"type_traits", "<type_traits>"},
288  {"typeindex", "<typeindex>"},
289  {"typeinfo", "<typeinfo>"},
290  {"unordered_map", "<unordered_map>"},
291  {"unordered_set", "<unordered_set>"},
292  {"utility", "<utility>"},
293  {"vector", "<vector>"},
294  {"include/complex.h", "<complex.h>"},
295  {"include/ctype.h", "<cctype>"},
296  {"include/errno.h", "<cerrno>"},
297  {"include/fenv.h", "<fenv.h>"},
298  {"include/libio.h", "<cstdio>"},
299  {"include/locale.h", "<clocale>"},
300  {"include/math.h", "<cmath>"},
301  {"include/setjmp.h", "<csetjmp>"},
302  {"include/signal.h", "<csignal>"},
303  {"include/stdio.h", "<cstdio>"},
304  {"include/stdlib.h", "<cstdlib>"},
305  {"include/string.h", "<cstring>"},
306  {"include/time.h", "<ctime>"},
307  {"include/wchar.h", "<cwchar>"},
308  {"include/wctype.h", "<cwctype>"},
309  {"bits/cmathcalls.h", "<complex.h>"},
310  {"bits/errno.h", "<cerrno>"},
311  {"bits/fenv.h", "<fenv.h>"},
312  {"bits/huge_val.h", "<cmath>"},
313  {"bits/huge_valf.h", "<cmath>"},
314  {"bits/huge_vall.h", "<cmath>"},
315  {"bits/inf.h", "<cmath>"},
316  {"bits/local_lim.h", "<climits>"},
317  {"bits/locale.h", "<clocale>"},
318  {"bits/mathcalls.h", "<math.h>"},
319  {"bits/mathdef.h", "<cmath>"},
320  {"bits/nan.h", "<cmath>"},
321  {"bits/posix1_lim.h", "<climits>"},
322  {"bits/posix2_lim.h", "<climits>"},
323  {"bits/setjmp.h", "<csetjmp>"},
324  {"bits/sigaction.h", "<csignal>"},
325  {"bits/sigcontext.h", "<csignal>"},
326  {"bits/siginfo.h", "<csignal>"},
327  {"bits/signum.h", "<csignal>"},
328  {"bits/sigset.h", "<csignal>"},
329  {"bits/sigstack.h", "<csignal>"},
330  {"bits/stdint-intn.h", "<cstdint>"},
331  {"bits/stdint-uintn.h", "<cstdint>"},
332  {"bits/stdio_lim.h", "<cstdio>"},
333  {"bits/sys_errlist.h", "<cstdio>"},
334  {"bits/time.h", "<ctime>"},
335  {"bits/timex.h", "<ctime>"},
336  {"bits/typesizes.h", "<cstdio>"},
337  {"bits/wchar.h", "<cwchar>"},
338  {"bits/wordsize.h", "<csetjmp>"},
339  {"bits/xopen_lim.h", "<climits>"},
340  {"include/xlocale.h", "<cstring>"},
341  {"bits/atomic_word.h", "<memory>"},
342  {"bits/basic_file.h", "<fstream>"},
343  {"bits/c\\+\\+allocator.h", "<string>"},
344  {"bits/c\\+\\+config.h", "<cstddef>"},
345  {"bits/c\\+\\+io.h", "<ios>"},
346  {"bits/c\\+\\+locale.h", "<locale>"},
347  {"bits/cpu_defines.h", "<iosfwd>"},
348  {"bits/ctype_base.h", "<locale>"},
349  {"bits/cxxabi_tweaks.h", "<cxxabi.h>"},
350  {"bits/error_constants.h", "<system_error>"},
351  {"bits/gthr-default.h", "<memory>"},
352  {"bits/gthr.h", "<memory>"},
353  {"bits/opt_random.h", "<random>"},
354  {"bits/os_defines.h", "<iosfwd>"},
355  // GNU C headers
356  {"include/aio.h", "<aio.h>"},
357  {"include/aliases.h", "<aliases.h>"},
358  {"include/alloca.h", "<alloca.h>"},
359  {"include/ar.h", "<ar.h>"},
360  {"include/argp.h", "<argp.h>"},
361  {"include/argz.h", "<argz.h>"},
362  {"include/arpa/nameser.h", "<resolv.h>"},
363  {"include/arpa/nameser_compat.h", "<resolv.h>"},
364  {"include/byteswap.h", "<byteswap.h>"},
365  {"include/cpio.h", "<cpio.h>"},
366  {"include/crypt.h", "<crypt.h>"},
367  {"include/dirent.h", "<dirent.h>"},
368  {"include/dlfcn.h", "<dlfcn.h>"},
369  {"include/elf.h", "<elf.h>"},
370  {"include/endian.h", "<endian.h>"},
371  {"include/envz.h", "<envz.h>"},
372  {"include/err.h", "<err.h>"},
373  {"include/error.h", "<error.h>"},
374  {"include/execinfo.h", "<execinfo.h>"},
375  {"include/fcntl.h", "<fcntl.h>"},
376  {"include/features.h", "<features.h>"},
377  {"include/fmtmsg.h", "<fmtmsg.h>"},
378  {"include/fnmatch.h", "<fnmatch.h>"},
379  {"include/fstab.h", "<fstab.h>"},
380  {"include/fts.h", "<fts.h>"},
381  {"include/ftw.h", "<ftw.h>"},
382  {"include/gconv.h", "<gconv.h>"},
383  {"include/getopt.h", "<getopt.h>"},
384  {"include/glob.h", "<glob.h>"},
385  {"include/grp.h", "<grp.h>"},
386  {"include/gshadow.h", "<gshadow.h>"},
387  {"include/iconv.h", "<iconv.h>"},
388  {"include/ifaddrs.h", "<ifaddrs.h>"},
389  {"include/kdb.h", "<kdb.h>"},
390  {"include/langinfo.h", "<langinfo.h>"},
391  {"include/libgen.h", "<libgen.h>"},
392  {"include/libintl.h", "<libintl.h>"},
393  {"include/link.h", "<link.h>"},
394  {"include/malloc.h", "<malloc.h>"},
395  {"include/mcheck.h", "<mcheck.h>"},
396  {"include/memory.h", "<memory.h>"},
397  {"include/mntent.h", "<mntent.h>"},
398  {"include/monetary.h", "<monetary.h>"},
399  {"include/mqueue.h", "<mqueue.h>"},
400  {"include/netdb.h", "<netdb.h>"},
401  {"include/netinet/in.h", "<netinet/in.h>"},
402  {"include/nl_types.h", "<nl_types.h>"},
403  {"include/nss.h", "<nss.h>"},
404  {"include/obstack.h", "<obstack.h>"},
405  {"include/panel.h", "<panel.h>"},
406  {"include/paths.h", "<paths.h>"},
407  {"include/printf.h", "<printf.h>"},
408  {"include/profile.h", "<profile.h>"},
409  {"include/pthread.h", "<pthread.h>"},
410  {"include/pty.h", "<pty.h>"},
411  {"include/pwd.h", "<pwd.h>"},
412  {"include/re_comp.h", "<re_comp.h>"},
413  {"include/regex.h", "<regex.h>"},
414  {"include/regexp.h", "<regexp.h>"},
415  {"include/resolv.h", "<resolv.h>"},
416  {"include/rpc/netdb.h", "<netdb.h>"},
417  {"include/sched.h", "<sched.h>"},
418  {"include/search.h", "<search.h>"},
419  {"include/semaphore.h", "<semaphore.h>"},
420  {"include/sgtty.h", "<sgtty.h>"},
421  {"include/shadow.h", "<shadow.h>"},
422  {"include/spawn.h", "<spawn.h>"},
423  {"include/stab.h", "<stab.h>"},
424  {"include/stdc-predef.h", "<stdc-predef.h>"},
425  {"include/stdio_ext.h", "<stdio_ext.h>"},
426  {"include/strings.h", "<strings.h>"},
427  {"include/stropts.h", "<stropts.h>"},
428  {"include/sudo_plugin.h", "<sudo_plugin.h>"},
429  {"include/sysexits.h", "<sysexits.h>"},
430  {"include/tar.h", "<tar.h>"},
431  {"include/tcpd.h", "<tcpd.h>"},
432  {"include/term.h", "<term.h>"},
433  {"include/term_entry.h", "<term_entry.h>"},
434  {"include/termcap.h", "<termcap.h>"},
435  {"include/termios.h", "<termios.h>"},
436  {"include/thread_db.h", "<thread_db.h>"},
437  {"include/tic.h", "<tic.h>"},
438  {"include/ttyent.h", "<ttyent.h>"},
439  {"include/uchar.h", "<uchar.h>"},
440  {"include/ucontext.h", "<ucontext.h>"},
441  {"include/ulimit.h", "<ulimit.h>"},
442  {"include/unctrl.h", "<unctrl.h>"},
443  {"include/unistd.h", "<unistd.h>"},
444  {"include/utime.h", "<utime.h>"},
445  {"include/utmp.h", "<utmp.h>"},
446  {"include/utmpx.h", "<utmpx.h>"},
447  {"include/values.h", "<values.h>"},
448  {"include/wordexp.h", "<wordexp.h>"},
449  {"fpu_control.h", "<fpu_control.h>"},
450  {"ieee754.h", "<ieee754.h>"},
451  {"gnu/lib-names.h", "<gnu/lib-names.h>"},
452  {"gnu/libc-version.h", "<gnu/libc-version.h>"},
453  {"gnu/option-groups.h", "<gnu/option-groups.h>"},
454  {"gnu/stubs-32.h", "<gnu/stubs-32.h>"},
455  {"gnu/stubs-64.h", "<gnu/stubs-64.h>"},
456  {"gnu/stubs-x32.h", "<gnu/stubs-x32.h>"},
457  {"include/rpc/auth_des.h", "<rpc/auth_des.h>"},
458  {"include/rpc/rpc_msg.h", "<rpc/rpc_msg.h>"},
459  {"include/rpc/pmap_clnt.h", "<rpc/pmap_clnt.h>"},
460  {"include/rpc/rpc.h", "<rpc/rpc.h>"},
461  {"include/rpc/types.h", "<rpc/types.h>"},
462  {"include/rpc/auth_unix.h", "<rpc/auth_unix.h>"},
463  {"include/rpc/key_prot.h", "<rpc/key_prot.h>"},
464  {"include/rpc/pmap_prot.h", "<rpc/pmap_prot.h>"},
465  {"include/rpc/auth.h", "<rpc/auth.h>"},
466  {"include/rpc/svc_auth.h", "<rpc/svc_auth.h>"},
467  {"include/rpc/xdr.h", "<rpc/xdr.h>"},
468  {"include/rpc/pmap_rmt.h", "<rpc/pmap_rmt.h>"},
469  {"include/rpc/des_crypt.h", "<rpc/des_crypt.h>"},
470  {"include/rpc/svc.h", "<rpc/svc.h>"},
471  {"include/rpc/rpc_des.h", "<rpc/rpc_des.h>"},
472  {"include/rpc/clnt.h", "<rpc/clnt.h>"},
473  {"include/scsi/scsi.h", "<scsi/scsi.h>"},
474  {"include/scsi/sg.h", "<scsi/sg.h>"},
475  {"include/scsi/scsi_ioctl.h", "<scsi/scsi_ioctl>"},
476  {"include/netrose/rose.h", "<netrose/rose.h>"},
477  {"include/nfs/nfs.h", "<nfs/nfs.h>"},
478  {"include/netatalk/at.h", "<netatalk/at.h>"},
479  {"include/netinet/ether.h", "<netinet/ether.h>"},
480  {"include/netinet/icmp6.h", "<netinet/icmp6.h>"},
481  {"include/netinet/if_ether.h", "<netinet/if_ether.h>"},
482  {"include/netinet/if_fddi.h", "<netinet/if_fddi.h>"},
483  {"include/netinet/if_tr.h", "<netinet/if_tr.h>"},
484  {"include/netinet/igmp.h", "<netinet/igmp.h>"},
485  {"include/netinet/in_systm.h", "<netinet/in_systm.h>"},
486  {"include/netinet/ip.h", "<netinet/ip.h>"},
487  {"include/netinet/ip6.h", "<netinet/ip6.h>"},
488  {"include/netinet/ip_icmp.h", "<netinet/ip_icmp.h>"},
489  {"include/netinet/tcp.h", "<netinet/tcp.h>"},
490  {"include/netinet/udp.h", "<netinet/udp.h>"},
491  {"include/netrom/netrom.h", "<netrom/netrom.h>"},
492  {"include/protocols/routed.h", "<protocols/routed.h>"},
493  {"include/protocols/rwhod.h", "<protocols/rwhod.h>"},
494  {"include/protocols/talkd.h", "<protocols/talkd.h>"},
495  {"include/protocols/timed.h", "<protocols/timed.h>"},
496  {"include/rpcsvc/klm_prot.x", "<rpcsvc/klm_prot.x>"},
497  {"include/rpcsvc/rstat.h", "<rpcsvc/rstat.h>"},
498  {"include/rpcsvc/spray.x", "<rpcsvc/spray.x>"},
499  {"include/rpcsvc/nlm_prot.x", "<rpcsvc/nlm_prot.x>"},
500  {"include/rpcsvc/nis_callback.x", "<rpcsvc/nis_callback.x>"},
501  {"include/rpcsvc/yp.h", "<rpcsvc/yp.h>"},
502  {"include/rpcsvc/yp.x", "<rpcsvc/yp.x>"},
503  {"include/rpcsvc/nfs_prot.h", "<rpcsvc/nfs_prot.h>"},
504  {"include/rpcsvc/rex.h", "<rpcsvc/rex.h>"},
505  {"include/rpcsvc/yppasswd.h", "<rpcsvc/yppasswd.h>"},
506  {"include/rpcsvc/rex.x", "<rpcsvc/rex.x>"},
507  {"include/rpcsvc/nis_tags.h", "<rpcsvc/nis_tags.h>"},
508  {"include/rpcsvc/nis_callback.h", "<rpcsvc/nis_callback.h>"},
509  {"include/rpcsvc/nfs_prot.x", "<rpcsvc/nfs_prot.x>"},
510  {"include/rpcsvc/bootparam_prot.x", "<rpcsvc/bootparam_prot.x>"},
511  {"include/rpcsvc/rusers.x", "<rpcsvc/rusers.x>"},
512  {"include/rpcsvc/rquota.x", "<rpcsvc/rquota.x>"},
513  {"include/rpcsvc/nis.h", "<rpcsvc/nis.h>"},
514  {"include/rpcsvc/nislib.h", "<rpcsvc/nislib.h>"},
515  {"include/rpcsvc/ypupd.h", "<rpcsvc/ypupd.h>"},
516  {"include/rpcsvc/bootparam.h", "<rpcsvc/bootparam.h>"},
517  {"include/rpcsvc/spray.h", "<rpcsvc/spray.h>"},
518  {"include/rpcsvc/key_prot.h", "<rpcsvc/key_prot.h>"},
519  {"include/rpcsvc/klm_prot.h", "<rpcsvc/klm_prot.h>"},
520  {"include/rpcsvc/sm_inter.h", "<rpcsvc/sm_inter.h>"},
521  {"include/rpcsvc/nlm_prot.h", "<rpcsvc/nlm_prot.h>"},
522  {"include/rpcsvc/yp_prot.h", "<rpcsvc/yp_prot.h>"},
523  {"include/rpcsvc/ypclnt.h", "<rpcsvc/ypclnt.h>"},
524  {"include/rpcsvc/rstat.x", "<rpcsvc/rstat.x>"},
525  {"include/rpcsvc/rusers.h", "<rpcsvc/rusers.h>"},
526  {"include/rpcsvc/key_prot.x", "<rpcsvc/key_prot.x>"},
527  {"include/rpcsvc/sm_inter.x", "<rpcsvc/sm_inter.x>"},
528  {"include/rpcsvc/rquota.h", "<rpcsvc/rquota.h>"},
529  {"include/rpcsvc/nis.x", "<rpcsvc/nis.x>"},
530  {"include/rpcsvc/bootparam_prot.h", "<rpcsvc/bootparam_prot.h>"},
531  {"include/rpcsvc/mount.h", "<rpcsvc/mount.h>"},
532  {"include/rpcsvc/mount.x", "<rpcsvc/mount.x>"},
533  {"include/rpcsvc/nis_object.x", "<rpcsvc/nis_object.x>"},
534  {"include/rpcsvc/yppasswd.x", "<rpcsvc/yppasswd.x>"},
535  {"sys/acct.h", "<sys/acct.h>"},
536  {"sys/auxv.h", "<sys/auxv.h>"},
537  {"sys/cdefs.h", "<sys/cdefs.h>"},
538  {"sys/debugreg.h", "<sys/debugreg.h>"},
539  {"sys/dir.h", "<sys/dir.h>"},
540  {"sys/elf.h", "<sys/elf.h>"},
541  {"sys/epoll.h", "<sys/epoll.h>"},
542  {"sys/eventfd.h", "<sys/eventfd.h>"},
543  {"sys/fanotify.h", "<sys/fanotify.h>"},
544  {"sys/file.h", "<sys/file.h>"},
545  {"sys/fsuid.h", "<sys/fsuid.h>"},
546  {"sys/gmon.h", "<sys/gmon.h>"},
547  {"sys/gmon_out.h", "<sys/gmon_out.h>"},
548  {"sys/inotify.h", "<sys/inotify.h>"},
549  {"sys/io.h", "<sys/io.h>"},
550  {"sys/ioctl.h", "<sys/ioctl.h>"},
551  {"sys/ipc.h", "<sys/ipc.h>"},
552  {"sys/kd.h", "<sys/kd.h>"},
553  {"sys/kdaemon.h", "<sys/kdaemon.h>"},
554  {"sys/klog.h", "<sys/klog.h>"},
555  {"sys/mman.h", "<sys/mman.h>"},
556  {"sys/mount.h", "<sys/mount.h>"},
557  {"sys/msg.h", "<sys/msg.h>"},
558  {"sys/mtio.h", "<sys/mtio.h>"},
559  {"sys/param.h", "<sys/param.h>"},
560  {"sys/pci.h", "<sys/pci.h>"},
561  {"sys/perm.h", "<sys/perm.h>"},
562  {"sys/personality.h", "<sys/personality.h>"},
563  {"sys/poll.h", "<sys/poll.h>"},
564  {"sys/prctl.h", "<sys/prctl.h>"},
565  {"sys/procfs.h", "<sys/procfs.h>"},
566  {"sys/profil.h", "<sys/profil.h>"},
567  {"sys/ptrace.h", "<sys/ptrace.h>"},
568  {"sys/queue.h", "<sys/queue.h>"},
569  {"sys/quota.h", "<sys/quota.h>"},
570  {"sys/raw.h", "<sys/raw.h>"},
571  {"sys/reboot.h", "<sys/reboot.h>"},
572  {"sys/reg.h", "<sys/reg.h>"},
573  {"sys/resource.h", "<sys/resource.h>"},
574  {"sys/select.h", "<sys/select.h>"},
575  {"sys/sem.h", "<sys/sem.h>"},
576  {"sys/sendfile.h", "<sys/sendfile.h>"},
577  {"sys/shm.h", "<sys/shm.h>"},
578  {"sys/signalfd.h", "<sys/signalfd.h>"},
579  {"sys/socket.h", "<sys/socket.h>"},
580  {"sys/stat.h", "<sys/stat.h>"},
581  {"sys/statfs.h", "<sys/statfs.h>"},
582  {"sys/statvfs.h", "<sys/statvfs.h>"},
583  {"sys/swap.h", "<sys/swap.h>"},
584  {"sys/syscall.h", "<sys/syscall.h>"},
585  {"sys/sysctl.h", "<sys/sysctl.h>"},
586  {"sys/sysinfo.h", "<sys/sysinfo.h>"},
587  {"sys/syslog.h", "<sys/syslog.h>"},
588  {"sys/sysmacros.h", "<sys/sysmacros.h>"},
589  {"sys/termios.h", "<sys/termios.h>"},
590  {"sys/time.h", "<sys/select.h>"},
591  {"sys/timeb.h", "<sys/timeb.h>"},
592  {"sys/timerfd.h", "<sys/timerfd.h>"},
593  {"sys/times.h", "<sys/times.h>"},
594  {"sys/timex.h", "<sys/timex.h>"},
595  {"sys/ttychars.h", "<sys/ttychars.h>"},
596  {"sys/ttydefaults.h", "<sys/ttydefaults.h>"},
597  {"sys/types.h", "<sys/types.h>"},
598  {"sys/ucontext.h", "<sys/ucontext.h>"},
599  {"sys/uio.h", "<sys/uio.h>"},
600  {"sys/un.h", "<sys/un.h>"},
601  {"sys/user.h", "<sys/user.h>"},
602  {"sys/ustat.h", "<sys/ustat.h>"},
603  {"sys/utsname.h", "<sys/utsname.h>"},
604  {"sys/vlimit.h", "<sys/vlimit.h>"},
605  {"sys/vm86.h", "<sys/vm86.h>"},
606  {"sys/vtimes.h", "<sys/vtimes.h>"},
607  {"sys/wait.h", "<sys/wait.h>"},
608  {"sys/xattr.h", "<sys/xattr.h>"},
609  {"bits/epoll.h", "<sys/epoll.h>"},
610  {"bits/eventfd.h", "<sys/eventfd.h>"},
611  {"bits/inotify.h", "<sys/inotify.h>"},
612  {"bits/ipc.h", "<sys/ipc.h>"},
613  {"bits/ipctypes.h", "<sys/ipc.h>"},
614  {"bits/mman-linux.h", "<sys/mman.h>"},
615  {"bits/mman.h", "<sys/mman.h>"},
616  {"bits/msq.h", "<sys/msg.h>"},
617  {"bits/resource.h", "<sys/resource.h>"},
618  {"bits/sem.h", "<sys/sem.h>"},
619  {"bits/shm.h", "<sys/shm.h>"},
620  {"bits/signalfd.h", "<sys/signalfd.h>"},
621  {"bits/statfs.h", "<sys/statfs.h>"},
622  {"bits/statvfs.h", "<sys/statvfs.h>"},
623  {"bits/timerfd.h", "<sys/timerfd.h>"},
624  {"bits/utsname.h", "<sys/utsname.h>"},
625  {"bits/auxv.h", "<sys/auxv.h>"},
626  {"bits/byteswap-16.h", "<byteswap.h>"},
627  {"bits/byteswap.h", "<byteswap.h>"},
628  {"bits/confname.h", "<unistd.h>"},
629  {"bits/dirent.h", "<dirent.h>"},
630  {"bits/dlfcn.h", "<dlfcn.h>"},
631  {"bits/elfclass.h", "<link.h>"},
632  {"bits/endian.h", "<endian.h>"},
633  {"bits/environments.h", "<unistd.h>"},
634  {"bits/fcntl-linux.h", "<fcntl.h>"},
635  {"bits/fcntl.h", "<fcntl.h>"},
636  {"bits/in.h", "<netinet/in.h>"},
637  {"bits/ioctl-types.h", "<sys/ioctl.h>"},
638  {"bits/ioctls.h", "<sys/ioctl.h>"},
639  {"bits/link.h", "<link.h>"},
640  {"bits/mqueue.h", "<mqueue.h>"},
641  {"bits/netdb.h", "<netdb.h>"},
642  {"bits/param.h", "<sys/param.h>"},
643  {"bits/poll.h", "<sys/poll.h>"},
644  {"bits/posix_opt.h", "<bits/posix_opt.h>"},
645  {"bits/pthreadtypes.h", "<pthread.h>"},
646  {"bits/sched.h", "<sched.h>"},
647  {"bits/select.h", "<sys/select.h>"},
648  {"bits/semaphore.h", "<semaphore.h>"},
649  {"bits/sigthread.h", "<pthread.h>"},
650  {"bits/sockaddr.h", "<sys/socket.h>"},
651  {"bits/socket.h", "<sys/socket.h>"},
652  {"bits/socket_type.h", "<sys/socket.h>"},
653  {"bits/stab.def", "<stab.h>"},
654  {"bits/stat.h", "<sys/stat.h>"},
655  {"bits/stropts.h", "<stropts.h>"},
656  {"bits/syscall.h", "<sys/syscall.h>"},
657  {"bits/syslog-path.h", "<sys/syslog.h>"},
658  {"bits/termios.h", "<termios.h>"},
659  {"bits/types.h", "<sys/types.h>"},
660  {"bits/uio.h", "<sys/uio.h>"},
661  {"bits/ustat.h", "<sys/ustat.h>"},
662  {"bits/utmp.h", "<utmp.h>"},
663  {"bits/utmpx.h", "<utmpx.h>"},
664  {"bits/waitflags.h", "<sys/wait.h>"},
665  {"bits/waitstatus.h", "<sys/wait.h>"},
666  {"bits/xtitypes.h", "<stropts.h>"},
667 };
668 
669 } // namespace
670 
671 void CanonicalIncludes::addMapping(FileEntryRef Header,
672  llvm::StringRef CanonicalPath) {
673  FullPathMapping[Header.getUniqueID()] = std::string(CanonicalPath);
674 }
675 
676 /// The maximum number of path components in a key from StdSuffixHeaderMapping.
677 /// Used to minimize the number of lookups in suffix path mappings.
678 constexpr int MaxSuffixComponents = 3;
679 
680 llvm::StringRef CanonicalIncludes::mapHeader(FileEntryRef Header) const {
681  auto MapIt = FullPathMapping.find(Header.getUniqueID());
682  if (MapIt != FullPathMapping.end())
683  return MapIt->second;
684 
685  if (!StdSuffixHeaderMapping)
686  return "";
687 
688  int Components = 1;
689 
690  // FIXME: check that this works on Windows and add tests.
691  auto Filename = Header.getName();
692  for (auto It = llvm::sys::path::rbegin(Filename),
693  End = llvm::sys::path::rend(Filename);
694  It != End && Components <= MaxSuffixComponents; ++It, ++Components) {
695  auto SubPath = Filename.substr(It->data() - Filename.begin());
696  auto MappingIt = StdSuffixHeaderMapping->find(SubPath);
697  if (MappingIt != StdSuffixHeaderMapping->end())
698  return MappingIt->second;
699  }
700  return "";
701 }
702 
703 llvm::StringRef CanonicalIncludes::mapSymbol(llvm::StringRef QName) const {
704  return StdSymbolMapping ? StdSymbolMapping->lookup(QName) : "";
705 }
706 
707 std::unique_ptr<CommentHandler>
709  class PragmaCommentHandler : public clang::CommentHandler {
710  public:
711  PragmaCommentHandler(CanonicalIncludes *Includes) : Includes(Includes) {}
712 
713  bool HandleComment(Preprocessor &PP, SourceRange Range) override {
714  auto Pragma = tooling::parseIWYUPragma(
715  PP.getSourceManager().getCharacterData(Range.getBegin()));
716  if (!Pragma || !Pragma->consume_front("private, include "))
717  return false;
718  auto &SM = PP.getSourceManager();
719  // We always insert using the spelling from the pragma.
720  if (auto *FE = SM.getFileEntryForID(SM.getFileID(Range.getBegin())))
721  Includes->addMapping(FE->getLastRef(),
722  isLiteralInclude(*Pragma)
723  ? Pragma->str()
724  : ("\"" + *Pragma + "\"").str());
725  return false;
726  }
727 
728  private:
729  CanonicalIncludes *const Includes;
730  };
731  return std::make_unique<PragmaCommentHandler>(Includes);
732 }
733 
734 void CanonicalIncludes::addSystemHeadersMapping(const LangOptions &Language) {
735  if (Language.CPlusPlus) {
736  static const auto *Symbols = new llvm::StringMap<llvm::StringRef>({
737 #define SYMBOL(Name, NameSpace, Header) {#NameSpace #Name, #Header},
738 #include "clang/Tooling/Inclusions/StdSymbolMap.inc"
739  // There are two std::move()s, this is by far the most common.
740  SYMBOL(move, std::, <utility>)
741  // There are multiple headers for size_t, pick one.
742  SYMBOL(size_t, std::, <cstddef>)
743 #undef SYMBOL
744  });
745  StdSymbolMapping = Symbols;
746  } else if (Language.C11) {
747  static const auto *CSymbols = new llvm::StringMap<llvm::StringRef>({
748 #define SYMBOL(Name, NameSpace, Header) {#Name, #Header},
749 #include "clang/Tooling/Inclusions/CSymbolMap.inc"
750  // There are multiple headers for size_t, pick one.
751  SYMBOL(size_t, None, <stddef.h>)
752 #undef SYMBOL
753  });
754  StdSymbolMapping = CSymbols;
755  }
756 
757  // FIXME: remove the std header mapping once we support ambiguous symbols, now
758  // it serves as a fallback to disambiguate:
759  // - symbols with multiple headers (e.g. std::move)
760  static const auto *SystemHeaderMap = [] {
761  const size_t Size = sizeof(IncludeMappings) / sizeof(IncludeMappings[0]);
762  auto *HeaderMap = new llvm::StringMap<llvm::StringRef>(Size);
763  for (size_t I = 0; I < Size; I++) {
764  auto Result = HeaderMap->insert(IncludeMappings[I]);
765  (void)Result;
766 
767  assert(Result.second && "Duplicate in include mappings");
768  }
769  return HeaderMap;
770  }();
771 
772  // Check MaxSuffixComponents constant is correct.
773  assert(llvm::all_of(SystemHeaderMap->keys(), [](llvm::StringRef Path) {
774  return std::distance(
775  llvm::sys::path::begin(Path, llvm::sys::path::Style::posix),
776  llvm::sys::path::end(Path)) <= MaxSuffixComponents;
777  }));
778  // ... and precise.
779  assert(llvm::any_of(SystemHeaderMap->keys(), [](llvm::StringRef Path) {
780  return std::distance(
781  llvm::sys::path::begin(Path, llvm::sys::path::Style::posix),
782  llvm::sys::path::end(Path)) == MaxSuffixComponents;
783  }));
784 
785  // FIXME: Suffix mapping contains invalid entries for C, so only enable it for
786  // CPP.
787  if (Language.CPlusPlus)
788  StdSuffixHeaderMapping = SystemHeaderMap;
789 }
790 
791 } // namespace clangd
792 } // namespace clang
clang::clangd::CanonicalIncludes::addSystemHeadersMapping
void addSystemHeadersMapping(const LangOptions &Language)
Adds mapping for system headers and some special symbols (e.g.
Definition: CanonicalIncludes.cpp:734
clang::clangd::collectIWYUHeaderMaps
std::unique_ptr< CommentHandler > collectIWYUHeaderMaps(CanonicalIncludes *Includes)
Returns a CommentHandler that parses pragma comment on include files to determine when we should incl...
Definition: CanonicalIncludes.cpp:708
Headers.h
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:26
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:40
CanonicalIncludes.h
SYMBOL
#define SYMBOL(Name, NameSpace, Header)
clang::clangd::CanonicalIncludes
Maps a definition location onto an #include file, based on a set of filename rules.
Definition: CanonicalIncludes.h:37
clang::clangd::CanonicalIncludes::mapHeader
llvm::StringRef mapHeader(FileEntryRef Header) const
Returns the overridden include for files in Header, or "".
Definition: CanonicalIncludes.cpp:680
clang::clangd::CanonicalIncludes::addMapping
void addMapping(FileEntryRef Header, llvm::StringRef CanonicalPath)
Adds a file-to-string mapping from ID to CanonicalPath.
Definition: CanonicalIncludes.cpp:671
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::clangd::isLiteralInclude
bool isLiteralInclude(llvm::StringRef Include)
Returns true if Include is literal include like "path" or <path>.
Definition: Headers.cpp:185
clang::clangd::CanonicalIncludes::mapSymbol
llvm::StringRef mapSymbol(llvm::StringRef QualifiedName) const
Returns the overridden include for symbol with QualifiedName, or "".
Definition: CanonicalIncludes.cpp:703
clang::clangd::Range
Definition: Protocol.h:185
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::MaxSuffixComponents
constexpr int MaxSuffixComponents
The maximum number of path components in a key from StdSuffixHeaderMapping.
Definition: CanonicalIncludes.cpp:678