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