clang-tools  9.0.0
CanonicalIncludes.h
Go to the documentation of this file.
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/Support/Regex.h"
25 #include <mutex>
26 #include <string>
27 #include <vector>
28 
29 namespace clang {
30 namespace clangd {
31 
32 /// Maps a definition location onto an #include file, based on a set of filename
33 /// rules.
34 /// Only const methods (i.e. mapHeader) in this class are thread safe.
36 public:
37  CanonicalIncludes() = default;
38 
39  /// Adds a string-to-string mapping from \p Path to \p CanonicalPath.
40  void addMapping(llvm::StringRef Path, llvm::StringRef CanonicalPath);
41 
42  /// Maps files with last path components matching \p Suffix to \p
43  /// CanonicalPath.
44  void addPathSuffixMapping(llvm::StringRef Suffix,
45  llvm::StringRef CanonicalPath);
46 
47  /// Sets the canonical include for any symbol with \p QualifiedName.
48  /// Symbol mappings take precedence over header mappings.
49  void addSymbolMapping(llvm::StringRef QualifiedName,
50  llvm::StringRef CanonicalPath);
51 
52  /// Returns the canonical include for symbol with \p QualifiedName.
53  /// \p Header is the file the declaration was reachable from.
54  /// Header itself will be returned if there is no relevant mapping.
55  llvm::StringRef mapHeader(llvm::StringRef Header,
56  llvm::StringRef QualifiedName) const;
57 
58 private:
59  /// A map from full include path to a canonical path.
60  llvm::StringMap<std::string> FullPathMapping;
61  /// A map from a suffix (one or components of a path) to a canonical path.
62  llvm::StringMap<std::string> SuffixHeaderMapping;
63  /// Maximum number of path components stored in a key of SuffixHeaderMapping.
64  /// Used to reduce the number of lookups into SuffixHeaderMapping.
65  int MaxSuffixComponents = 0;
66  /// A map from fully qualified symbol names to header names.
67  llvm::StringMap<std::string> SymbolMapping;
68 };
69 
70 /// Returns a CommentHandler that parses pragma comment on include files to
71 /// determine when we should include a different header from the header that
72 /// directly defines a symbol. Mappinps are registered with \p Includes.
73 ///
74 /// Currently it only supports IWYU private pragma:
75 /// https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/IWYUPragmas.md#iwyu-pragma-private
76 std::unique_ptr<CommentHandler>
78 
79 /// Adds mapping for system headers and some special symbols (e.g. STL symbols
80 /// in <iosfwd> need to be mapped individually). Approximately, the following
81 /// system headers are handled:
82 /// - C++ standard library e.g. bits/basic_string.h$ -> <string>
83 /// - Posix library e.g. bits/pthreadtypes.h$ -> <pthread.h>
84 /// - Compiler extensions, e.g. include/avx512bwintrin.h$ -> <immintrin.h>
85 /// The mapping is hardcoded and hand-maintained, so it might not cover all
86 /// headers.
88  const LangOptions &Language);
89 
90 } // namespace clangd
91 } // namespace clang
92 
93 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_HEADERMAPCOLLECTOR_H
std::unique_ptr< CommentHandler > collectIWYUHeaderMaps(CanonicalIncludes *Includes)
Returns a CommentHandler that parses pragma comment on include files to determine when we should incl...
void addSymbolMapping(llvm::StringRef QualifiedName, llvm::StringRef CanonicalPath)
Sets the canonical include for any symbol with QualifiedName.
void addSystemHeadersMapping(CanonicalIncludes *Includes, const LangOptions &Language)
Adds mapping for system headers and some special symbols (e.g.
Maps a definition location onto an #include file, based on a set of filename rules.
void addMapping(llvm::StringRef Path, llvm::StringRef CanonicalPath)
Adds a string-to-string mapping from Path to CanonicalPath.
llvm::StringRef mapHeader(llvm::StringRef Header, llvm::StringRef QualifiedName) const
Returns the canonical include for symbol with QualifiedName.
std::string Path
A typedef to represent a file path.
Definition: Path.h:20
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void addPathSuffixMapping(llvm::StringRef Suffix, llvm::StringRef CanonicalPath)
Maps files with last path components matching Suffix to CanonicalPath.