diff options
author | Jan Korous <jkorous@apple.com> | 2018-11-27 16:40:34 +0000 |
---|---|---|
committer | Jan Korous <jkorous@apple.com> | 2018-11-27 16:40:34 +0000 |
commit | 6089b6192ef9fad95e4bcd2c1071815367e74595 (patch) | |
tree | 1819bcd8b0618ba1538e2e915bfbea639ab6b4a2 | |
parent | 6b2f3e07c62c5e20fecf8e3863076271000e0f83 (diff) | |
download | bcm5719-llvm-6089b6192ef9fad95e4bcd2c1071815367e74595.tar.gz bcm5719-llvm-6089b6192ef9fad95e4bcd2c1071815367e74595.zip |
[clangd][NFC] Move SymbolID to a separate file
Prerequisity for textDocument/SymbolInfo
Differential Revision: https://reviews.llvm.org/D54799
llvm-svn: 347674
-rw-r--r-- | clang-tools-extra/clangd/CMakeLists.txt | 1 | ||||
-rw-r--r-- | clang-tools-extra/clangd/Protocol.h | 1 | ||||
-rw-r--r-- | clang-tools-extra/clangd/index/Index.cpp | 29 | ||||
-rw-r--r-- | clang-tools-extra/clangd/index/Index.h | 49 | ||||
-rw-r--r-- | clang-tools-extra/clangd/index/SymbolID.cpp | 58 | ||||
-rw-r--r-- | clang-tools-extra/clangd/index/SymbolID.h | 65 |
6 files changed, 126 insertions, 77 deletions
diff --git a/clang-tools-extra/clangd/CMakeLists.txt b/clang-tools-extra/clangd/CMakeLists.txt index 2aa975a82bc..2c039109d60 100644 --- a/clang-tools-extra/clangd/CMakeLists.txt +++ b/clang-tools-extra/clangd/CMakeLists.txt @@ -46,6 +46,7 @@ add_clang_library(clangDaemon index/IndexAction.cpp index/MemIndex.cpp index/Merge.cpp + index/SymbolID.cpp index/Serialization.cpp index/SymbolCollector.cpp index/YAMLSerialization.cpp diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h index 8e088527f36..ff70babc083 100644 --- a/clang-tools-extra/clangd/Protocol.h +++ b/clang-tools-extra/clangd/Protocol.h @@ -25,6 +25,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROTOCOL_H #include "URI.h" +#include "index/SymbolID.h" #include "llvm/ADT/Optional.h" #include "llvm/Support/JSON.h" #include <bitset> diff --git a/clang-tools-extra/clangd/index/Index.cpp b/clang-tools-extra/clangd/index/Index.cpp index e39d8a350d4..dd4b5eb32a7 100644 --- a/clang-tools-extra/clangd/index/Index.cpp +++ b/clang-tools-extra/clangd/index/Index.cpp @@ -12,7 +12,6 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Error.h" -#include "llvm/Support/SHA1.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -43,34 +42,6 @@ raw_ostream &operator<<(raw_ostream &OS, const SymbolLocation &L) { << "-" << L.End.line() << ":" << L.End.column() << ")"; } -SymbolID::SymbolID(StringRef USR) { - auto Hash = SHA1::hash(arrayRefFromStringRef(USR)); - static_assert(sizeof(Hash) >= RawSize, "RawSize larger than SHA1"); - memcpy(HashValue.data(), Hash.data(), RawSize); -} - -raw_ostream &operator<<(raw_ostream &OS, const SymbolID &ID) { - return OS << toHex(ID.raw()); -} - -SymbolID SymbolID::fromRaw(StringRef Raw) { - SymbolID ID; - assert(Raw.size() == RawSize); - memcpy(ID.HashValue.data(), Raw.data(), RawSize); - return ID; -} - -std::string SymbolID::str() const { return toHex(raw()); } - -Expected<SymbolID> SymbolID::fromStr(StringRef Str) { - if (Str.size() != RawSize * 2) - return createStringError(inconvertibleErrorCode(), "Bad ID length"); - for (char C : Str) - if (!isHexDigit(C)) - return createStringError(inconvertibleErrorCode(), "Bad hex ID"); - return fromRaw(fromHex(Str)); -} - raw_ostream &operator<<(raw_ostream &OS, SymbolOrigin O) { if (O == SymbolOrigin::Unknown) return OS << "unknown"; diff --git a/clang-tools-extra/clangd/index/Index.h b/clang-tools-extra/clangd/index/Index.h index 11c117613ce..a5552d4f39f 100644 --- a/clang-tools-extra/clangd/index/Index.h +++ b/clang-tools-extra/clangd/index/Index.h @@ -11,11 +11,11 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H #include "ExpectedTypes.h" +#include "SymbolID.h" #include "clang/Index/IndexSymbol.h" #include "clang/Lex/Lexer.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseSet.h" -#include "llvm/ADT/Hashing.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringExtras.h" @@ -95,53 +95,6 @@ inline bool operator<(const SymbolLocation &L, const SymbolLocation &R) { } llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolLocation &); -// The class identifies a particular C++ symbol (class, function, method, etc). -// -// As USRs (Unified Symbol Resolution) could be large, especially for functions -// with long type arguments, SymbolID is using truncated SHA1(USR) values to -// guarantee the uniqueness of symbols while using a relatively small amount of -// memory (vs storing USRs directly). -// -// SymbolID can be used as key in the symbol indexes to lookup the symbol. -class SymbolID { -public: - SymbolID() = default; - explicit SymbolID(llvm::StringRef USR); - - bool operator==(const SymbolID &Sym) const { - return HashValue == Sym.HashValue; - } - bool operator<(const SymbolID &Sym) const { - return HashValue < Sym.HashValue; - } - - // The stored hash is truncated to RawSize bytes. - // This trades off memory against the number of symbols we can handle. - constexpr static size_t RawSize = 8; - llvm::StringRef raw() const { - return StringRef(reinterpret_cast<const char *>(HashValue.data()), RawSize); - } - static SymbolID fromRaw(llvm::StringRef); - - // Returns a hex encoded string. - std::string str() const; - static llvm::Expected<SymbolID> fromStr(llvm::StringRef); - -private: - std::array<uint8_t, RawSize> HashValue; -}; - -inline llvm::hash_code hash_value(const SymbolID &ID) { - // We already have a good hash, just return the first bytes. - assert(sizeof(size_t) <= SymbolID::RawSize && "size_t longer than SHA1!"); - size_t Result; - memcpy(&Result, ID.raw().data(), sizeof(size_t)); - return llvm::hash_code(Result); -} - -// Write SymbolID into the given stream. SymbolID is encoded as ID.str(). -llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID); - } // namespace clangd } // namespace clang namespace llvm { diff --git a/clang-tools-extra/clangd/index/SymbolID.cpp b/clang-tools-extra/clangd/index/SymbolID.cpp new file mode 100644 index 00000000000..0ab74810a92 --- /dev/null +++ b/clang-tools-extra/clangd/index/SymbolID.cpp @@ -0,0 +1,58 @@ +//===--- SymbolID.cpp --------------------------------------------*- C++-*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "SymbolID.h" +#include "llvm/Support/SHA1.h" + +using namespace llvm; +namespace clang { +namespace clangd { + +SymbolID::SymbolID(StringRef USR) { + auto Hash = llvm::SHA1::hash(arrayRefFromStringRef(USR)); + static_assert(sizeof(Hash) >= RawSize, "RawSize larger than SHA1"); + memcpy(HashValue.data(), Hash.data(), RawSize); +} + +llvm::StringRef SymbolID::raw() const { + return StringRef(reinterpret_cast<const char *>(HashValue.data()), RawSize); +} + +SymbolID SymbolID::fromRaw(StringRef Raw) { + SymbolID ID; + assert(Raw.size() == RawSize); + memcpy(ID.HashValue.data(), Raw.data(), RawSize); + return ID; +} + +std::string SymbolID::str() const { return toHex(raw()); } + +Expected<SymbolID> SymbolID::fromStr(StringRef Str) { + if (Str.size() != RawSize * 2) + return createStringError(inconvertibleErrorCode(), "Bad ID length"); + for (char C : Str) + if (!isHexDigit(C)) + return createStringError(inconvertibleErrorCode(), "Bad hex ID"); + return fromRaw(fromHex(Str)); +} + +raw_ostream &operator<<(raw_ostream &OS, const SymbolID &ID) { + return OS << toHex(ID.raw()); +} + +llvm::hash_code hash_value(const SymbolID &ID) { + // We already have a good hash, just return the first bytes. + assert(sizeof(size_t) <= SymbolID::RawSize && "size_t longer than SHA1!"); + size_t Result; + memcpy(&Result, ID.raw().data(), sizeof(size_t)); + return llvm::hash_code(Result); +} + +} // namespace clangd +} // namespace clang diff --git a/clang-tools-extra/clangd/index/SymbolID.h b/clang-tools-extra/clangd/index/SymbolID.h new file mode 100644 index 00000000000..aa8208c18fb --- /dev/null +++ b/clang-tools-extra/clangd/index/SymbolID.h @@ -0,0 +1,65 @@ +//===--- SymbolID.h ----------------------------------------------*- C++-*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H + +#include "llvm/ADT/Hashing.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/raw_ostream.h" +#include <array> +#include <string> + +namespace clang { +namespace clangd { + +// The class identifies a particular C++ symbol (class, function, method, etc). +// +// As USRs (Unified Symbol Resolution) could be large, especially for functions +// with long type arguments, SymbolID is using truncated SHA1(USR) values to +// guarantee the uniqueness of symbols while using a relatively small amount of +// memory (vs storing USRs directly). +// +// SymbolID can be used as key in the symbol indexes to lookup the symbol. +class SymbolID { +public: + SymbolID() = default; + explicit SymbolID(llvm::StringRef USR); + + bool operator==(const SymbolID &Sym) const { + return HashValue == Sym.HashValue; + } + bool operator<(const SymbolID &Sym) const { + return HashValue < Sym.HashValue; + } + + // The stored hash is truncated to RawSize bytes. + // This trades off memory against the number of symbols we can handle. + constexpr static size_t RawSize = 8; + llvm::StringRef raw() const; + static SymbolID fromRaw(llvm::StringRef); + + // Returns a hex encoded string. + std::string str() const; + static llvm::Expected<SymbolID> fromStr(llvm::StringRef); + +private: + std::array<uint8_t, RawSize> HashValue; +}; + +llvm::hash_code hash_value(const SymbolID &ID); + +// Write SymbolID into the given stream. SymbolID is encoded as ID.str(). +llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID); + +} // namespace clangd +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H |