1 //===-- CanonicalIncludes.h - remap #include header -------------*- 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 // At indexing time, we decide which file to #included for a symbol.
10 // Usually this is the file with the canonical decl, but there are exceptions:
11 // - private headers may have pragmas pointing to the matching public header.
12 //   (These are "IWYU" pragmas, named after the include-what-you-use tool).
13 // - the standard library is implemented in many files, without any pragmas.
14 //   We have a lookup table for common standard library implementations.
15 //   libstdc++ puts char_traits in bits/char_traits.h, but we #include <string>.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H
20 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H
21 
22 #include "clang/Lex/Preprocessor.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/Support/Regex.h"
26 #include <mutex>
27 #include <string>
28 #include <vector>
29 
30 namespace clang {
31 namespace clangd {
32 
33 /// Maps a definition location onto an #include file, based on a set of filename
34 /// rules.
35 /// Only const methods (i.e. mapHeader) in this class are thread safe.
36 class CanonicalIncludes {
37 public:
38   /// Adds a string-to-string mapping from \p Path to \p CanonicalPath.
39   void addMapping(llvm::StringRef Path, llvm::StringRef CanonicalPath);
40 
41   /// Returns the canonical include for symbol with \p QualifiedName.
42   /// \p Header is the file the declaration was reachable from.
43   /// Header itself will be returned if there is no relevant mapping.
44   llvm::StringRef mapHeader(llvm::StringRef Header,
45                             llvm::StringRef QualifiedName) const;
46 
47   /// Adds mapping for system headers and some special symbols (e.g. STL symbols
48   /// in <iosfwd> need to be mapped individually). Approximately, the following
49   /// system headers are handled:
50   ///   - C++ standard library e.g. bits/basic_string.h$ -> <string>
51   ///   - Posix library e.g. bits/pthreadtypes.h$ -> <pthread.h>
52   ///   - Compiler extensions, e.g. include/avx512bwintrin.h$ -> <immintrin.h>
53   /// The mapping is hardcoded and hand-maintained, so it might not cover all
54   /// headers.
55   void addSystemHeadersMapping(const LangOptions &Language);
56 
57 private:
58   /// A map from full include path to a canonical path.
59   llvm::StringMap<std::string> FullPathMapping;
60   /// A map from a suffix (one or components of a path) to a canonical path.
61   /// Used only for mapping standard headers.
62   const llvm::StringMap<llvm::StringRef> *StdSuffixHeaderMapping = nullptr;
63   /// A map from fully qualified symbol names to header names.
64   /// Used only for mapping standard symbols.
65   const llvm::StringMap<llvm::StringRef> *StdSymbolMapping = nullptr;
66 };
67 
68 /// Returns a CommentHandler that parses pragma comment on include files to
69 /// determine when we should include a different header from the header that
70 /// directly defines a symbol. Mappinps are registered with \p Includes.
71 ///
72 /// Currently it only supports IWYU private pragma:
73 /// https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/IWYUPragmas.md#iwyu-pragma-private
74 ///
75 /// We ignore other pragmas:
76 /// - keep: this is common but irrelevant: we do not currently remove includes
77 /// - export: this is common and potentially interesting, there are three cases:
78 ///    * Points to a public header (common): we can suppress include2 if you
79 ///      already have include1. Only marginally useful.
80 ///    * Points to a private header annotated with `private` (somewhat commmon):
81 ///      Not incrementally useful as we support private.
82 ///    * Points to a private header without pragmas (rare). This is a reversed
83 ///      private pragma, and is valuable but too rare to be worthwhile.
84 /// - no_include: this is about as common as private, but only affects the
85 ///   current file, so the value is smaller. We could add support.
86 /// - friend: this is less common than private, has implementation difficulties,
87 ///   and affects behavior in a limited scope.
88 /// - associated: extremely rare
89 std::unique_ptr<CommentHandler>
90 collectIWYUHeaderMaps(CanonicalIncludes *Includes);
91 
92 } // namespace clangd
93 } // namespace clang
94 
95 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_HEADERMAPCOLLECTOR_H
96