diff options
Diffstat (limited to 'clang-tools-extra/clangd/index/Index.h')
| -rw-r--r-- | clang-tools-extra/clangd/index/Index.h | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/clang-tools-extra/clangd/index/Index.h b/clang-tools-extra/clangd/index/Index.h index 76148f1cb77..de1e4c38ec6 100644 --- a/clang-tools-extra/clangd/index/Index.h +++ b/clang-tools-extra/clangd/index/Index.h @@ -56,14 +56,19 @@ struct SymbolLocation { uint32_t Column : 12; // 0-based }; - // The URI of the source file where a symbol occurs. - llvm::StringRef FileURI; - /// The symbol range, using half-open range [Start, End). Position Start; Position End; - explicit operator bool() const { return !FileURI.empty(); } + explicit operator bool() const { return !StringRef(FileURI).empty(); } + + // The URI of the source file where a symbol occurs. + // The string must be null-terminated. + // + // We avoid using llvm::StringRef here to save memory. + // WARNING: unless you know what you are doing, it is recommended to use it + // via llvm::StringRef. + const char *FileURI = ""; }; inline bool operator==(const SymbolLocation::Position &L, const SymbolLocation::Position &R) { @@ -76,12 +81,16 @@ inline bool operator<(const SymbolLocation::Position &L, std::make_tuple(R.line(), R.column()); } inline bool operator==(const SymbolLocation &L, const SymbolLocation &R) { - return std::tie(L.FileURI, L.Start, L.End) == - std::tie(R.FileURI, R.Start, R.End); + assert(L.FileURI && R.FileURI); + return !std::strcmp(L.FileURI, R.FileURI) && + std::tie(L.Start, L.End) == std::tie(R.Start, R.End); } inline bool operator<(const SymbolLocation &L, const SymbolLocation &R) { - return std::tie(L.FileURI, L.Start, L.End) < - std::tie(R.FileURI, R.Start, R.End); + assert(L.FileURI && R.FileURI); + int Cmp = std::strcmp(L.FileURI, R.FileURI); + if (Cmp != 0) + return Cmp < 0; + return std::tie(L.Start, L.End) < std::tie(R.Start, R.End); } llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolLocation &); @@ -288,12 +297,19 @@ raw_ostream &operator<<(raw_ostream &, Symbol::SymbolFlag); template <typename Callback> void visitStrings(Symbol &S, const Callback &CB) { CB(S.Name); CB(S.Scope); - CB(S.CanonicalDeclaration.FileURI); - CB(S.Definition.FileURI); CB(S.Signature); CB(S.CompletionSnippetSuffix); CB(S.Documentation); CB(S.ReturnType); + auto RawCharPointerCB = [&CB](const char *&P) { + llvm::StringRef S(P); + CB(S); + assert(!S.data()[S.size()] && "Visited StringRef must be null-terminated"); + P = S.data(); + }; + RawCharPointerCB(S.CanonicalDeclaration.FileURI); + RawCharPointerCB(S.Definition.FileURI); + for (auto &Include : S.IncludeHeaders) CB(Include.IncludeHeader); } |

