xref: /llvm-project/clang-tools-extra/clangd/index/CanonicalIncludes.h (revision 1f7c7d4bdd7b847d82b19bcd097029fabb449b90)
1c5105f9eSEric Liu //===-- CanonicalIncludes.h - remap #include header -------------*- C++ -*-===//
2c5105f9eSEric Liu //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c5105f9eSEric Liu //
7c5105f9eSEric Liu //===----------------------------------------------------------------------===//
8c5105f9eSEric Liu //
9c5105f9eSEric Liu // At indexing time, we decide which file to #included for a symbol.
10c5105f9eSEric Liu // Usually this is the file with the canonical decl, but there are exceptions:
11c5105f9eSEric Liu // - private headers may have pragmas pointing to the matching public header.
12c5105f9eSEric Liu //   (These are "IWYU" pragmas, named after the include-what-you-use tool).
13c5105f9eSEric Liu // - the standard library is implemented in many files, without any pragmas.
14c5105f9eSEric Liu //   We have a lookup table for common standard library implementations.
15c5105f9eSEric Liu //   libstdc++ puts char_traits in bits/char_traits.h, but we #include <string>.
16c5105f9eSEric Liu //
178e35f1e7SKirill Bobyrev //===----------------------------------------------------------------------===//
18c5105f9eSEric Liu 
19c5105f9eSEric Liu #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H
20c5105f9eSEric Liu #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H
21c5105f9eSEric Liu 
223de4d5e6SKirill Bobyrev #include "clang/Basic/FileEntry.h"
23*1f7c7d4bSViktoriia Bakalova #include "clang/Basic/LangOptions.h"
24c5105f9eSEric Liu #include "llvm/ADT/StringMap.h"
258b76709bSIlya Biryukov #include "llvm/ADT/StringRef.h"
26c5105f9eSEric Liu 
27c5105f9eSEric Liu namespace clang {
28c5105f9eSEric Liu namespace clangd {
29c5105f9eSEric Liu 
30c5105f9eSEric Liu /// Maps a definition location onto an #include file, based on a set of filename
31c5105f9eSEric Liu /// rules.
32709bde88SEric Liu /// Only const methods (i.e. mapHeader) in this class are thread safe.
33c5105f9eSEric Liu class CanonicalIncludes {
34c5105f9eSEric Liu public:
35*1f7c7d4bSViktoriia Bakalova   /// Returns the overridden verbatim spelling for files in \p Header that can
36*1f7c7d4bSViktoriia Bakalova   /// be directly included (i.e., contains quotes "" or angled brackets <>), or
37*1f7c7d4bSViktoriia Bakalova   /// "" if the spelling could not be found.
38*1f7c7d4bSViktoriia Bakalova   llvm::StringRef mapHeader(llvm::StringRef HeaderPath) const;
39c5105f9eSEric Liu 
408b76709bSIlya Biryukov   /// Adds mapping for system headers and some special symbols (e.g. STL symbols
418b76709bSIlya Biryukov   /// in <iosfwd> need to be mapped individually). Approximately, the following
428b76709bSIlya Biryukov   /// system headers are handled:
438b76709bSIlya Biryukov   ///   - C++ standard library e.g. bits/basic_string.h$ -> <string>
448b76709bSIlya Biryukov   ///   - Posix library e.g. bits/pthreadtypes.h$ -> <pthread.h>
458b76709bSIlya Biryukov   ///   - Compiler extensions, e.g. include/avx512bwintrin.h$ -> <immintrin.h>
468b76709bSIlya Biryukov   /// The mapping is hardcoded and hand-maintained, so it might not cover all
478b76709bSIlya Biryukov   /// headers.
488b76709bSIlya Biryukov   void addSystemHeadersMapping(const LangOptions &Language);
498b76709bSIlya Biryukov 
50c5105f9eSEric Liu private:
5122abe49fSIlya Biryukov   /// A map from a suffix (one or components of a path) to a canonical path.
528b76709bSIlya Biryukov   /// Used only for mapping standard headers.
538b76709bSIlya Biryukov   const llvm::StringMap<llvm::StringRef> *StdSuffixHeaderMapping = nullptr;
54c5105f9eSEric Liu };
55c5105f9eSEric Liu } // namespace clangd
56c5105f9eSEric Liu } // namespace clang
57c5105f9eSEric Liu 
585bd643d3SChristian Kühnel #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H
59