| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H |
| 10 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H |
| 11 |
|
| 12 |
#include "Protocol.h" |
| 13 |
#include "SourceCode.h" |
| 14 |
#include "index/Symbol.h" |
| 15 |
#include "support/Path.h" |
| 16 |
#include "clang/Basic/FileEntry.h" |
| 17 |
#include "clang/Basic/TokenKinds.h" |
| 18 |
#include "clang/Format/Format.h" |
| 19 |
#include "clang/Frontend/CompilerInstance.h" |
| 20 |
#include "clang/Lex/HeaderSearch.h" |
| 21 |
#include "clang/Lex/Preprocessor.h" |
| 22 |
#include "clang/Tooling/Inclusions/HeaderIncludes.h" |
| 23 |
#include "clang/Tooling/Inclusions/StandardLibrary.h" |
| 24 |
#include "llvm/ADT/ArrayRef.h" |
| 25 |
#include "llvm/ADT/DenseSet.h" |
| 26 |
#include "llvm/ADT/StringRef.h" |
| 27 |
#include "llvm/ADT/StringSet.h" |
| 28 |
#include "llvm/Support/Error.h" |
| 29 |
#include "llvm/Support/FileSystem/UniqueID.h" |
| 30 |
#include <optional> |
| 31 |
#include <string> |
| 32 |
|
| 33 |
namespace clang { |
| 34 |
namespace clangd { |
| 35 |
|
| 36 |
|
| 37 |
bool isLiteralInclude(llvm::StringRef Include); |
| 38 |
|
| 39 |
|
| 40 |
struct HeaderFile { |
| 41 |
std::string File; |
| 42 |
|
| 43 |
|
| 44 |
bool Verbatim; |
| 45 |
|
| 46 |
bool valid() const; |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
struct SymbolInclude { |
| 51 |
|
| 52 |
|
| 53 |
llvm::StringRef Header; |
| 54 |
|
| 55 |
Symbol::IncludeDirective Directive; |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header, |
| 61 |
llvm::StringRef HintPath); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
llvm::SmallVector<SymbolInclude, 1> getRankedIncludes(const Symbol &Sym); |
| 66 |
|
| 67 |
|
| 68 |
struct Inclusion { |
| 69 |
tok::PPKeywordKind Directive; |
| 70 |
std::string Written; |
| 71 |
Path Resolved; |
| 72 |
unsigned HashOffset = 0; |
| 73 |
int HashLine = 0; |
| 74 |
SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; |
| 75 |
std::optional<unsigned> HeaderID; |
| 76 |
bool BehindPragmaKeep = false; |
| 77 |
}; |
| 78 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Inclusion &); |
| 79 |
bool operator==(const Inclusion &LHS, const Inclusion &RHS); |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
struct IncludeGraphNode { |
| 85 |
enum class SourceFlag : uint8_t { |
| 86 |
None = 0, |
| 87 |
|
| 88 |
IsTU = 1 << 0, |
| 89 |
|
| 90 |
HadErrors = 1 << 1, |
| 91 |
}; |
| 92 |
|
| 93 |
SourceFlag Flags = SourceFlag::None; |
| 94 |
llvm::StringRef URI; |
| 95 |
FileDigest Digest{{0}}; |
| 96 |
std::vector<llvm::StringRef> DirectIncludes; |
| 97 |
}; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
using IncludeGraph = llvm::StringMap<IncludeGraphNode>; |
| 103 |
|
| 104 |
inline IncludeGraphNode::SourceFlag operator|(IncludeGraphNode::SourceFlag A, |
| 105 |
IncludeGraphNode::SourceFlag B) { |
| 106 |
return static_cast<IncludeGraphNode::SourceFlag>(static_cast<uint8_t>(A) | |
| 107 |
static_cast<uint8_t>(B)); |
| 108 |
} |
| 109 |
|
| 110 |
inline bool operator&(IncludeGraphNode::SourceFlag A, |
| 111 |
IncludeGraphNode::SourceFlag B) { |
| 112 |
return static_cast<uint8_t>(A) & static_cast<uint8_t>(B); |
| 113 |
} |
| 114 |
|
| 115 |
inline IncludeGraphNode::SourceFlag & |
| 116 |
operator|=(IncludeGraphNode::SourceFlag &A, IncludeGraphNode::SourceFlag B) { |
| 117 |
return A = A | B; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
class IncludeStructure { |
| 130 |
public: |
| 131 |
IncludeStructure() { |
| 132 |
|
| 133 |
RealPathNames.emplace_back(); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
void collect(const CompilerInstance &CI); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
enum class HeaderID : unsigned {}; |
| 145 |
|
| 146 |
std::optional<HeaderID> getID(const FileEntry *Entry) const; |
| 147 |
HeaderID getOrCreateID(FileEntryRef Entry); |
| 148 |
|
| 149 |
StringRef getRealPath(HeaderID ID) const { |
| 150 |
assert(static_cast<unsigned>(ID) <= RealPathNames.size()); |
| 151 |
return RealPathNames[static_cast<unsigned>(ID)]; |
| 152 |
} |
| 153 |
|
| 154 |
bool isSelfContained(HeaderID ID) const { |
| 155 |
return !NonSelfContained.contains(ID); |
| 156 |
} |
| 157 |
|
| 158 |
bool hasIWYUExport(HeaderID ID) const { |
| 159 |
return HasIWYUExport.contains(ID); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
llvm::ArrayRef<std::string> allHeaders() const { return RealPathNames; } |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
llvm::DenseMap<HeaderID, unsigned> |
| 170 |
includeDepth(HeaderID Root = MainFileID) const; |
| 171 |
|
| 172 |
|
| 173 |
llvm::DenseMap<HeaderID, SmallVector<HeaderID>> IncludeChildren; |
| 174 |
|
| 175 |
llvm::DenseMap<tooling::stdlib::Header, llvm::SmallVector<HeaderID>> |
| 176 |
StdlibHeaders; |
| 177 |
|
| 178 |
std::vector<Inclusion> MainFileIncludes; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
static const HeaderID MainFileID = HeaderID(0u); |
| 184 |
|
| 185 |
class RecordHeaders; |
| 186 |
|
| 187 |
private: |
| 188 |
|
| 189 |
|
| 190 |
const FileEntry *MainFileEntry = nullptr; |
| 191 |
|
| 192 |
std::vector<std::string> RealPathNames; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
llvm::DenseMap<llvm::sys::fs::UniqueID, HeaderID> UIDToIndex; |
| 199 |
|
| 200 |
|
| 201 |
llvm::DenseSet<HeaderID> NonSelfContained; |
| 202 |
|
| 203 |
|
| 204 |
llvm::DenseSet<HeaderID> HasIWYUExport; |
| 205 |
}; |
| 206 |
|
| 207 |
|
| 208 |
class IncludeInserter { |
| 209 |
public: |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
IncludeInserter(StringRef FileName, StringRef Code, |
| 214 |
const format::FormatStyle &Style, StringRef BuildDir, |
| 215 |
HeaderSearch *HeaderSearchInfo) |
| 216 |
: FileName(FileName), Code(Code), BuildDir(BuildDir), |
| 217 |
HeaderSearchInfo(HeaderSearchInfo), |
| 218 |
Inserter(FileName, Code, Style.IncludeStyle) {} |
| 219 |
|
| 220 |
void addExisting(const Inclusion &Inc); |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
bool shouldInsertInclude(PathRef DeclaringHeader, |
| 233 |
const HeaderFile &InsertedHeader) const; |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
std::optional<std::string> |
| 247 |
calculateIncludePath(const HeaderFile &InsertedHeader, |
| 248 |
llvm::StringRef IncludingFile) const; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
std::optional<TextEdit> insert(llvm::StringRef VerbatimHeader, |
| 253 |
tooling::IncludeDirective Directive) const; |
| 254 |
|
| 255 |
private: |
| 256 |
StringRef FileName; |
| 257 |
StringRef Code; |
| 258 |
StringRef BuildDir; |
| 259 |
HeaderSearch *HeaderSearchInfo = nullptr; |
| 260 |
llvm::StringSet<> IncludedHeaders; |
| 261 |
tooling::HeaderIncludes Inserter; |
| 262 |
}; |
| 263 |
|
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
namespace llvm { |
| 268 |
|
| 269 |
|
| 270 |
template <> struct DenseMapInfo<clang::clangd::IncludeStructure::HeaderID> { |
| 271 |
static inline clang::clangd::IncludeStructure::HeaderID getEmptyKey() { |
| 272 |
return static_cast<clang::clangd::IncludeStructure::HeaderID>(-1); |
| 273 |
} |
| 274 |
|
| 275 |
static inline clang::clangd::IncludeStructure::HeaderID getTombstoneKey() { |
| 276 |
return static_cast<clang::clangd::IncludeStructure::HeaderID>(-2); |
| 277 |
} |
| 278 |
|
| 279 |
static unsigned |
| 280 |
getHashValue(const clang::clangd::IncludeStructure::HeaderID &Tag) { |
| 281 |
return hash_value(static_cast<unsigned>(Tag)); |
| 282 |
} |
| 283 |
|
| 284 |
static bool isEqual(const clang::clangd::IncludeStructure::HeaderID &LHS, |
| 285 |
const clang::clangd::IncludeStructure::HeaderID &RHS) { |
| 286 |
return LHS == RHS; |
| 287 |
} |
| 288 |
}; |
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
#endif |