2018-02-16 22:15:55 +08:00
|
|
|
//===-- CanonicalIncludes.h - remap #include header -------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-02-16 22:15:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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"
|
2019-09-09 23:32:51 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#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:
|
|
|
|
/// Adds a string-to-string mapping from \p Path to \p CanonicalPath.
|
|
|
|
void addMapping(llvm::StringRef Path, llvm::StringRef CanonicalPath);
|
|
|
|
|
2018-05-24 22:40:24 +08:00
|
|
|
/// Returns the canonical include for symbol with \p QualifiedName.
|
[clangd] Include insertion: require header guards, drop other heuristics, treat .def like .inc.
Summary:
We do have some reports of include insertion behaving badly in some
codebases. Requiring header guards both makes sense in principle, and is
likely to disable this "nice-to-have" feature in codebases where headers don't
follow the expected pattern.
With this we can drop some other heuristics, such as looking at file
extensions to detect known non-headers - implementation files have no guards.
One wrinkle here is #import - objc headers may not have guards because
they're intended to be used via #import. If the header is the main file
or is #included, we won't collect locations - merge should take care of
this if we see the file #imported somewhere. Seems likely to be OK.
Headers which have a canonicalization (stdlib, IWYU) are exempt from this check.
*.inc files continue to be handled by looking up to the including file.
This patch also adds *.def here - tablegen wants this pattern too.
In terms of code structure, the division between SymbolCollector and
CanonicalIncludes has shifted: SymbolCollector is responsible for more.
This is because SymbolCollector has all the SourceManager/HeaderSearch access
needed for checking for guards, and we interleave these checks with the *.def
checks in a loop (potentially).
We could hand all the info into CanonicalIncludes and put the logic there
if that's preferable.
Reviewers: ioeric
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D60316
llvm-svn: 358571
2019-04-17 18:36:02 +08:00
|
|
|
/// \p Header is the file the declaration was reachable from.
|
|
|
|
/// Header itself will be returned if there is no relevant mapping.
|
|
|
|
llvm::StringRef mapHeader(llvm::StringRef Header,
|
2018-03-02 02:30:48 +08:00
|
|
|
llvm::StringRef QualifiedName) const;
|
2018-02-16 22:15:55 +08:00
|
|
|
|
2019-09-09 23:32:51 +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:
|
|
|
|
/// - 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(const LangOptions &Language);
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
private:
|
2018-08-22 21:51:19 +08:00
|
|
|
/// A map from full include path to a canonical path.
|
|
|
|
llvm::StringMap<std::string> FullPathMapping;
|
|
|
|
/// A map from a suffix (one or components of a path) to a canonical path.
|
2019-09-09 23:32:51 +08:00
|
|
|
/// Used only for mapping standard headers.
|
|
|
|
const llvm::StringMap<llvm::StringRef> *StdSuffixHeaderMapping = nullptr;
|
2018-08-22 21:51:19 +08:00
|
|
|
/// A map from fully qualified symbol names to header names.
|
2019-09-09 23:32:51 +08:00
|
|
|
/// Used only for mapping standard symbols.
|
|
|
|
const llvm::StringMap<llvm::StringRef> *StdSymbolMapping = nullptr;
|
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
|
2020-10-05 19:17:52 +08:00
|
|
|
///
|
|
|
|
/// We ignore other pragmas:
|
|
|
|
/// - keep: this is common but irrelevant: we do not currently remove includes
|
|
|
|
/// - export: this is common and potentially interesting, there are three cases:
|
|
|
|
/// * Points to a public header (common): we can suppress include2 if you
|
|
|
|
/// already have include1. Only marginally useful.
|
|
|
|
/// * Points to a private header annotated with `private` (somewhat commmon):
|
|
|
|
/// Not incrementally useful as we support private.
|
|
|
|
/// * Points to a private header without pragmas (rare). This is a reversed
|
|
|
|
/// private pragma, and is valuable but too rare to be worthwhile.
|
|
|
|
/// - no_include: this is about as common as private, but only affects the
|
|
|
|
/// current file, so the value is smaller. We could add support.
|
|
|
|
/// - friend: this is less common than private, has implementation difficulties,
|
|
|
|
/// and affects behavior in a limited scope.
|
|
|
|
/// - associated: extremely rare
|
2018-02-16 22:15:55 +08:00
|
|
|
std::unique_ptr<CommentHandler>
|
|
|
|
collectIWYUHeaderMaps(CanonicalIncludes *Includes);
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
2018-05-30 20:41:19 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_HEADERMAPCOLLECTOR_H
|