clang-tools  7.0.0
CanonicalIncludes.h
Go to the documentation of this file.
1 //===-- CanonicalIncludes.h - remap #include header -------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // At indexing time, we decide which file to #included for a symbol.
11 // Usually this is the file with the canonical decl, but there are exceptions:
12 // - private headers may have pragmas pointing to the matching public header.
13 // (These are "IWYU" pragmas, named after the include-what-you-use tool).
14 // - the standard library is implemented in many files, without any pragmas.
15 // We have a lookup table for common standard library implementations.
16 // libstdc++ puts char_traits in bits/char_traits.h, but we #include <string>.
17 //
18 //===---------------------------------------------------------------------===//
19 
20 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H
21 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H
22 
23 #include "clang/Lex/Preprocessor.h"
24 #include "llvm/ADT/StringMap.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.
37 public:
38  CanonicalIncludes() = default;
39 
40  /// Adds a string-to-string mapping from \p Path to \p CanonicalPath.
41  void addMapping(llvm::StringRef Path, llvm::StringRef CanonicalPath);
42 
43  /// Maps all files matching \p RE to \p CanonicalPath
44  void addRegexMapping(llvm::StringRef RE, llvm::StringRef CanonicalPath);
45 
46  /// Sets the canonical include for any symbol with \p QualifiedName.
47  /// Symbol mappings take precedence over header mappings.
48  void addSymbolMapping(llvm::StringRef QualifiedName,
49  llvm::StringRef CanonicalPath);
50 
51  /// Returns the canonical include for symbol with \p QualifiedName.
52  /// \p Headers is the include stack: Headers.front() is the file declaring the
53  /// symbol, and Headers.back() is the main file.
54  llvm::StringRef mapHeader(llvm::ArrayRef<std::string> Headers,
55  llvm::StringRef QualifiedName) const;
56 
57 private:
58  // A map from header patterns to header names. This needs to be mutable so
59  // that we can match again a Regex in a const function member.
60  // FIXME(ioeric): All the regexes we have so far are suffix matches. The
61  // performance could be improved by allowing only suffix matches instead of
62  // arbitrary regexes.
63  mutable std::vector<std::pair<llvm::Regex, std::string>>
64  RegexHeaderMappingTable;
65  // A map from fully qualified symbol names to header names.
66  llvm::StringMap<std::string> SymbolMapping;
67  // Guards Regex matching as it's not thread-safe.
68  mutable std::mutex RegexMutex;
69 };
70 
71 /// Returns a CommentHandler that parses pragma comment on include files to
72 /// determine when we should include a different header from the header that
73 /// directly defines a symbol. Mappinps are registered with \p Includes.
74 ///
75 /// Currently it only supports IWYU private pragma:
76 /// https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/IWYUPragmas.md#iwyu-pragma-private
77 std::unique_ptr<CommentHandler>
79 
80 /// Adds mapping for system headers and some special symbols (e.g. STL symbols
81 /// in <iosfwd> need to be mapped individually). Approximately, the following
82 /// system headers are handled:
83 /// - C++ standard library e.g. bits/basic_string.h$ -> <string>
84 /// - Posix library e.g. bits/pthreadtypes.h$ -> <pthread.h>
85 /// - Compiler extensions, e.g. include/avx512bwintrin.h$ -> <immintrin.h>
86 /// The mapping is hardcoded and hand-maintained, so it might not cover all
87 /// headers.
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.
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.
void addRegexMapping(llvm::StringRef RE, llvm::StringRef CanonicalPath)
Maps all files matching RE to CanonicalPath.
std::string Path
A typedef to represent a file path.
Definition: Path.h:21
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::StringRef mapHeader(llvm::ArrayRef< std::string > Headers, llvm::StringRef QualifiedName) const
Returns the canonical include for symbol with QualifiedName.
void addSystemHeadersMapping(CanonicalIncludes *Includes)
Adds mapping for system headers and some special symbols (e.g.