blob: 33b6ff2eacb795eeeaf3b1868ad5386f4bc43b39 [file] [log] [blame]
Eric Liuc5105f92018-02-16 14:15:55 +00001//===--- Headers.h - Include headers -----------------------------*- C++-*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Eric Liuc5105f92018-02-16 14:15:55 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H
10#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H
11
12#include "Path.h"
Eric Liu155f5a42018-05-14 12:19:16 +000013#include "Protocol.h"
Eric Liu63f419a2018-05-15 15:29:32 +000014#include "SourceCode.h"
Dmitri Gribenko08b49b52019-02-28 13:23:03 +000015#include "index/Symbol.h"
Eric Liu63f419a2018-05-15 15:29:32 +000016#include "clang/Format/Format.h"
17#include "clang/Lex/HeaderSearch.h"
Eric Liu155f5a42018-05-14 12:19:16 +000018#include "clang/Lex/PPCallbacks.h"
Eric Liu528eb652018-06-04 09:04:28 +000019#include "clang/Tooling/Inclusions/HeaderIncludes.h"
Eric Liudd662772019-01-28 14:01:55 +000020#include "llvm/ADT/ArrayRef.h"
Eric Liuc5105f92018-02-16 14:15:55 +000021#include "llvm/ADT/StringRef.h"
Eric Liu63f419a2018-05-15 15:29:32 +000022#include "llvm/ADT/StringSet.h"
Eric Liuc5105f92018-02-16 14:15:55 +000023#include "llvm/Support/Error.h"
Jonas Devliegherefc514902018-10-10 13:27:25 +000024#include "llvm/Support/VirtualFileSystem.h"
Eric Liuc5105f92018-02-16 14:15:55 +000025
26namespace clang {
27namespace clangd {
Eric Liu709bde82018-02-19 18:48:44 +000028
Eric Liu6c8e8582018-02-26 08:32:13 +000029/// Returns true if \p Include is literal include like "path" or <path>.
30bool isLiteralInclude(llvm::StringRef Include);
31
32/// Represents a header file to be #include'd.
33struct HeaderFile {
34 std::string File;
35 /// If this is true, `File` is a literal string quoted with <> or "" that
36 /// can be #included directly; otherwise, `File` is an absolute file path.
37 bool Verbatim;
38
39 bool valid() const;
40};
41
Eric Liudd662772019-01-28 14:01:55 +000042/// Creates a `HeaderFile` from \p Header which can be either a URI or a literal
43/// include.
44llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header,
45 llvm::StringRef HintPath);
46
47// Returns include headers for \p Sym sorted by popularity. If two headers are
48// equally popular, prefer the shorter one.
49llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym);
50
Eric Liu155f5a42018-05-14 12:19:16 +000051// An #include directive that we found in the main file.
52struct Inclusion {
53 Range R; // Inclusion range.
54 std::string Written; // Inclusion name as written e.g. <vector>.
55 Path Resolved; // Resolved path of included file. Empty if not resolved.
Sam McCall991e3162018-11-20 10:58:48 +000056 unsigned HashOffset = 0; // Byte offset from start of file to #.
57 SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
Eric Liu155f5a42018-05-14 12:19:16 +000058};
Kadir Cetinkaya53995522018-11-30 16:59:00 +000059llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Inclusion &);
Eric Liu155f5a42018-05-14 12:19:16 +000060
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000061// Contains information about one file in the build grpah and its direct
62// dependencies. Doesn't own the strings it references (IncludeGraph is
63// self-contained).
64struct IncludeGraphNode {
Kadir Cetinkayaadbb3472019-07-04 09:52:04 +000065 enum class SourceFlag : uint8_t {
66 None = 0,
67 // Whether current file is a main file rather than a header.
68 IsTU = 1 << 0,
69 // Whether current file had any uncompilable errors during indexing.
70 HadErrors = 1 << 1,
71 };
72
73 SourceFlag Flags = SourceFlag::None;
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000074 llvm::StringRef URI;
Simon Pilgrim2d12d812018-12-04 14:07:29 +000075 FileDigest Digest{{0}};
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000076 std::vector<llvm::StringRef> DirectIncludes;
77};
78// FileURI and FileInclusions are references to keys of the map containing
79// them.
Kadir Cetinkaya53995522018-11-30 16:59:00 +000080// Important: The graph generated by those callbacks might contain cycles, self
81// edges and multi edges.
Kadir Cetinkayad08eab42018-11-27 16:08:53 +000082using IncludeGraph = llvm::StringMap<IncludeGraphNode>;
83
Kadir Cetinkayaadbb3472019-07-04 09:52:04 +000084inline IncludeGraphNode::SourceFlag operator|(IncludeGraphNode::SourceFlag A,
85 IncludeGraphNode::SourceFlag B) {
86 return static_cast<IncludeGraphNode::SourceFlag>(static_cast<uint8_t>(A) |
87 static_cast<uint8_t>(B));
88}
89
90inline bool operator&(IncludeGraphNode::SourceFlag A,
91 IncludeGraphNode::SourceFlag B) {
92 return static_cast<uint8_t>(A) & static_cast<uint8_t>(B);
93}
94
95inline IncludeGraphNode::SourceFlag &
96operator|=(IncludeGraphNode::SourceFlag &A, IncludeGraphNode::SourceFlag B) {
97 return A = A | B;
98}
99
Sam McCall3f0243f2018-07-03 08:09:29 +0000100// Information captured about the inclusion graph in a translation unit.
101// This includes detailed information about the direct #includes, and summary
102// information about all transitive includes.
103//
104// It should be built incrementally with collectIncludeStructureCallback().
105// When we build the preamble, we capture and store its include structure along
106// with the preamble data. When we use the preamble, we can copy its
107// IncludeStructure and use another collectIncludeStructureCallback() to fill
108// in any non-preamble inclusions.
109class IncludeStructure {
110public:
111 std::vector<Inclusion> MainFileIncludes;
112
113 // Return all transitively reachable files, and their minimum include depth.
114 // All transitive includes (absolute paths), with their minimum include depth.
115 // Root --> 0, #included file --> 1, etc.
116 // Root is clang's name for a file, which may not be absolute.
117 // Usually it should be SM.getFileEntryForID(SM.getMainFileID())->getName().
118 llvm::StringMap<unsigned> includeDepth(llvm::StringRef Root) const;
119
120 // This updates IncludeDepth(), but not MainFileIncludes.
121 void recordInclude(llvm::StringRef IncludingName,
122 llvm::StringRef IncludedName,
123 llvm::StringRef IncludedRealName);
124
125private:
126 // Identifying files in a way that persists from preamble build to subsequent
127 // builds is surprisingly hard. FileID is unavailable in InclusionDirective(),
128 // and RealPathName and UniqueID are not preseved in the preamble.
129 // We use the FileEntry::Name, which is stable, interned into a "file index".
130 // The paths we want to expose are the RealPathName, so store those too.
131 std::vector<std::string> RealPathNames; // In file index order.
132 unsigned fileIndex(llvm::StringRef Name);
133 llvm::StringMap<unsigned> NameToIndex; // Values are file indexes.
134 // Maps a file's index to that of the files it includes.
135 llvm::DenseMap<unsigned, SmallVector<unsigned, 8>> IncludeChildren;
136};
137
Eric Liu155f5a42018-05-14 12:19:16 +0000138/// Returns a PPCallback that visits all inclusions in the main file.
139std::unique_ptr<PPCallbacks>
Sam McCall3f0243f2018-07-03 08:09:29 +0000140collectIncludeStructureCallback(const SourceManager &SM, IncludeStructure *Out);
Eric Liu155f5a42018-05-14 12:19:16 +0000141
Eric Liu63f419a2018-05-15 15:29:32 +0000142// Calculates insertion edit for including a new header in a file.
143class IncludeInserter {
144public:
Eric Liu00d99bd2019-04-11 09:36:36 +0000145 // If \p HeaderSearchInfo is nullptr (e.g. when compile command is
146 // infeasible), this will only try to insert verbatim headers, and
147 // include path of non-verbatim header will not be shortened.
Eric Liu63f419a2018-05-15 15:29:32 +0000148 IncludeInserter(StringRef FileName, StringRef Code,
149 const format::FormatStyle &Style, StringRef BuildDir,
Eric Liu00d99bd2019-04-11 09:36:36 +0000150 HeaderSearch *HeaderSearchInfo)
Eric Liu63f419a2018-05-15 15:29:32 +0000151 : FileName(FileName), Code(Code), BuildDir(BuildDir),
152 HeaderSearchInfo(HeaderSearchInfo),
153 Inserter(FileName, Code, Style.IncludeStyle) {}
154
Eric Liufd9f4262018-09-27 14:27:02 +0000155 void addExisting(const Inclusion &Inc);
Eric Liu63f419a2018-05-15 15:29:32 +0000156
Eric Liu8f3678d2018-06-15 13:34:18 +0000157 /// Checks whether to add an #include of the header into \p File.
158 /// An #include will not be added if:
159 /// - Either \p DeclaringHeader or \p InsertedHeader is already (directly)
160 /// in \p Inclusions (including those included via different paths).
161 /// - \p DeclaringHeader or \p InsertedHeader is the same as \p File.
162 ///
Eric Liu417c8892019-04-16 14:35:49 +0000163 /// \param DeclaringHeader is path of the original header corresponding to \p
Eric Liu8f3678d2018-06-15 13:34:18 +0000164 /// InsertedHeader e.g. the header that declares a symbol.
165 /// \param InsertedHeader The preferred header to be inserted. This could be
Simon Pilgrimcbd8c762018-06-26 17:00:43 +0000166 /// the same as DeclaringHeader but must be provided.
Eric Liu417c8892019-04-16 14:35:49 +0000167 bool shouldInsertInclude(PathRef DeclaringHeader,
Eric Liu8f3678d2018-06-15 13:34:18 +0000168 const HeaderFile &InsertedHeader) const;
169
170 /// Determines the preferred way to #include a file, taking into account the
171 /// search path. Usually this will prefer a shorter representation like
172 /// 'Foo/Bar.h' over a longer one like 'Baz/include/Foo/Bar.h'.
Eric Liu63f419a2018-05-15 15:29:32 +0000173 ///
Eric Liu63f419a2018-05-15 15:29:32 +0000174 /// \param InsertedHeader The preferred header to be inserted. This could be
175 /// the same as DeclaringHeader but must be provided.
Eric Liu8f3678d2018-06-15 13:34:18 +0000176 ///
Kadir Cetinkaya1f6d9842019-07-03 07:47:19 +0000177 /// \param IncludingFile is the absolute path of the file that InsertedHeader
178 /// will be inserted.
179 ///
Eric Liu8f3678d2018-06-15 13:34:18 +0000180 /// \return A quoted "path" or <path> to be included.
Kadir Cetinkaya1f6d9842019-07-03 07:47:19 +0000181 std::string calculateIncludePath(const HeaderFile &InsertedHeader,
182 llvm::StringRef IncludingFile) const;
Eric Liu8f3678d2018-06-15 13:34:18 +0000183
184 /// Calculates an edit that inserts \p VerbatimHeader into code. If the header
185 /// is already included, this returns None.
186 llvm::Optional<TextEdit> insert(llvm::StringRef VerbatimHeader) const;
Eric Liu63f419a2018-05-15 15:29:32 +0000187
188private:
189 StringRef FileName;
190 StringRef Code;
191 StringRef BuildDir;
Eric Liu00d99bd2019-04-11 09:36:36 +0000192 HeaderSearch *HeaderSearchInfo = nullptr;
Eric Liufd9f4262018-09-27 14:27:02 +0000193 llvm::StringSet<> IncludedHeaders; // Both written and resolved.
194 tooling::HeaderIncludes Inserter; // Computers insertion replacement.
Eric Liu63f419a2018-05-15 15:29:32 +0000195};
Eric Liuc5105f92018-02-16 14:15:55 +0000196
197} // namespace clangd
198} // namespace clang
199
200#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H