2018-02-16 22:15:55 +08:00
|
|
|
//===-- CanonicalIncludes.h - remap #include header -------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// At indexing time, we decide which file to #included for a symbol.
|
|
|
|
// Usually this is the file with the canonical decl, but there are exceptions:
|
|
|
|
// - private headers may have pragmas pointing to the matching public header.
|
|
|
|
// (These are "IWYU" pragmas, named after the include-what-you-use tool).
|
|
|
|
// - the standard library is implemented in many files, without any pragmas.
|
|
|
|
// We have a lookup table for common standard library implementations.
|
|
|
|
// libstdc++ puts char_traits in bits/char_traits.h, but we #include <string>.
|
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-02-16 22:15:55 +08:00
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_CANONICALINCLUDES_H
|
|
|
|
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/Support/Regex.h"
|
2018-02-20 02:48:44 +08:00
|
|
|
#include <mutex>
|
2018-02-16 22:15:55 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
/// Maps a definition location onto an #include file, based on a set of filename
|
|
|
|
/// rules.
|
2018-02-20 02:48:44 +08:00
|
|
|
/// Only const methods (i.e. mapHeader) in this class are thread safe.
|
2018-02-16 22:15:55 +08:00
|
|
|
class CanonicalIncludes {
|
|
|
|
public:
|
|
|
|
CanonicalIncludes() = default;
|
|
|
|
|
|
|
|
/// Adds a string-to-string mapping from \p Path to \p CanonicalPath.
|
|
|
|
void addMapping(llvm::StringRef Path, llvm::StringRef CanonicalPath);
|
|
|
|
|
|
|
|
/// Maps all files matching \p RE to \p CanonicalPath
|
|
|
|
void addRegexMapping(llvm::StringRef RE, llvm::StringRef CanonicalPath);
|
|
|
|
|
2018-03-02 02:06:40 +08:00
|
|
|
/// Sets the canonical include for any symbol with \p QualifiedName.
|
|
|
|
/// Symbol mappings take precedence over header mappings.
|
|
|
|
void addSymbolMapping(llvm::StringRef QualifiedName,
|
|
|
|
llvm::StringRef CanonicalPath);
|
|
|
|
|
2018-05-24 22:40:24 +08:00
|
|
|
/// Returns the canonical include for symbol with \p QualifiedName.
|
|
|
|
/// \p Headers is the include stack: Headers.front() is the file declaring the
|
|
|
|
/// symbol, and Headers.back() is the main file.
|
|
|
|
llvm::StringRef mapHeader(llvm::ArrayRef<std::string> Headers,
|
2018-03-02 02:30:48 +08:00
|
|
|
llvm::StringRef QualifiedName) const;
|
2018-02-16 22:15:55 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// A map from header patterns to header names. This needs to be mutable so
|
|
|
|
// that we can match again a Regex in a const function member.
|
|
|
|
// FIXME(ioeric): All the regexes we have so far are suffix matches. The
|
|
|
|
// performance could be improved by allowing only suffix matches instead of
|
|
|
|
// arbitrary regexes.
|
|
|
|
mutable std::vector<std::pair<llvm::Regex, std::string>>
|
|
|
|
RegexHeaderMappingTable;
|
2018-03-02 02:06:40 +08:00
|
|
|
// A map from fully qualified symbol names to header names.
|
|
|
|
llvm::StringMap<std::string> SymbolMapping;
|
2018-02-20 02:48:44 +08:00
|
|
|
// Guards Regex matching as it's not thread-safe.
|
|
|
|
mutable std::mutex RegexMutex;
|
2018-02-16 22:15:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Returns a CommentHandler that parses pragma comment on include files to
|
|
|
|
/// determine when we should include a different header from the header that
|
|
|
|
/// directly defines a symbol. Mappinps are registered with \p Includes.
|
|
|
|
///
|
|
|
|
/// Currently it only supports IWYU private pragma:
|
|
|
|
/// https://github.com/include-what-you-use/include-what-you-use/blob/master/docs/IWYUPragmas.md#iwyu-pragma-private
|
|
|
|
std::unique_ptr<CommentHandler>
|
|
|
|
collectIWYUHeaderMaps(CanonicalIncludes *Includes);
|
|
|
|
|
2018-03-02 02:06:40 +08:00
|
|
|
/// Adds mapping for system headers and some special symbols (e.g. STL symbols
|
|
|
|
/// in <iosfwd> need to be mapped individually). Approximately, the following
|
|
|
|
/// system headers are handled:
|
2018-02-16 22:15:55 +08:00
|
|
|
/// - C++ standard library e.g. bits/basic_string.h$ -> <string>
|
|
|
|
/// - Posix library e.g. bits/pthreadtypes.h$ -> <pthread.h>
|
|
|
|
/// - Compiler extensions, e.g. include/avx512bwintrin.h$ -> <immintrin.h>
|
|
|
|
/// The mapping is hardcoded and hand-maintained, so it might not cover all
|
|
|
|
/// headers.
|
|
|
|
void addSystemHeadersMapping(CanonicalIncludes *Includes);
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
2018-05-30 20:41:19 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_HEADERMAPCOLLECTOR_H
|