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