Skip to content

Commit 6089b61

Browse files
committedNov 27, 2018
[clangd][NFC] Move SymbolID to a separate file
Prerequisity for textDocument/SymbolInfo Differential Revision: https://reviews.llvm.org/D54799 llvm-svn: 347674
1 parent 6b2f3e0 commit 6089b61

File tree

6 files changed

+126
-77
lines changed

6 files changed

+126
-77
lines changed
 

Diff for: ‎clang-tools-extra/clangd/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ add_clang_library(clangDaemon
4646
index/IndexAction.cpp
4747
index/MemIndex.cpp
4848
index/Merge.cpp
49+
index/SymbolID.cpp
4950
index/Serialization.cpp
5051
index/SymbolCollector.cpp
5152
index/YAMLSerialization.cpp

Diff for: ‎clang-tools-extra/clangd/Protocol.h

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PROTOCOL_H
2626

2727
#include "URI.h"
28+
#include "index/SymbolID.h"
2829
#include "llvm/ADT/Optional.h"
2930
#include "llvm/Support/JSON.h"
3031
#include <bitset>

Diff for: ‎clang-tools-extra/clangd/index/Index.cpp

-29
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "llvm/ADT/StringExtras.h"
1313
#include "llvm/ADT/StringRef.h"
1414
#include "llvm/Support/Error.h"
15-
#include "llvm/Support/SHA1.h"
1615
#include "llvm/Support/raw_ostream.h"
1716

1817
using namespace llvm;
@@ -43,34 +42,6 @@ raw_ostream &operator<<(raw_ostream &OS, const SymbolLocation &L) {
4342
<< "-" << L.End.line() << ":" << L.End.column() << ")";
4443
}
4544

46-
SymbolID::SymbolID(StringRef USR) {
47-
auto Hash = SHA1::hash(arrayRefFromStringRef(USR));
48-
static_assert(sizeof(Hash) >= RawSize, "RawSize larger than SHA1");
49-
memcpy(HashValue.data(), Hash.data(), RawSize);
50-
}
51-
52-
raw_ostream &operator<<(raw_ostream &OS, const SymbolID &ID) {
53-
return OS << toHex(ID.raw());
54-
}
55-
56-
SymbolID SymbolID::fromRaw(StringRef Raw) {
57-
SymbolID ID;
58-
assert(Raw.size() == RawSize);
59-
memcpy(ID.HashValue.data(), Raw.data(), RawSize);
60-
return ID;
61-
}
62-
63-
std::string SymbolID::str() const { return toHex(raw()); }
64-
65-
Expected<SymbolID> SymbolID::fromStr(StringRef Str) {
66-
if (Str.size() != RawSize * 2)
67-
return createStringError(inconvertibleErrorCode(), "Bad ID length");
68-
for (char C : Str)
69-
if (!isHexDigit(C))
70-
return createStringError(inconvertibleErrorCode(), "Bad hex ID");
71-
return fromRaw(fromHex(Str));
72-
}
73-
7445
raw_ostream &operator<<(raw_ostream &OS, SymbolOrigin O) {
7546
if (O == SymbolOrigin::Unknown)
7647
return OS << "unknown";

Diff for: ‎clang-tools-extra/clangd/index/Index.h

+1-48
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H
1212

1313
#include "ExpectedTypes.h"
14+
#include "SymbolID.h"
1415
#include "clang/Index/IndexSymbol.h"
1516
#include "clang/Lex/Lexer.h"
1617
#include "llvm/ADT/DenseMap.h"
1718
#include "llvm/ADT/DenseSet.h"
18-
#include "llvm/ADT/Hashing.h"
1919
#include "llvm/ADT/Optional.h"
2020
#include "llvm/ADT/SmallVector.h"
2121
#include "llvm/ADT/StringExtras.h"
@@ -95,53 +95,6 @@ inline bool operator<(const SymbolLocation &L, const SymbolLocation &R) {
9595
}
9696
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolLocation &);
9797

98-
// The class identifies a particular C++ symbol (class, function, method, etc).
99-
//
100-
// As USRs (Unified Symbol Resolution) could be large, especially for functions
101-
// with long type arguments, SymbolID is using truncated SHA1(USR) values to
102-
// guarantee the uniqueness of symbols while using a relatively small amount of
103-
// memory (vs storing USRs directly).
104-
//
105-
// SymbolID can be used as key in the symbol indexes to lookup the symbol.
106-
class SymbolID {
107-
public:
108-
SymbolID() = default;
109-
explicit SymbolID(llvm::StringRef USR);
110-
111-
bool operator==(const SymbolID &Sym) const {
112-
return HashValue == Sym.HashValue;
113-
}
114-
bool operator<(const SymbolID &Sym) const {
115-
return HashValue < Sym.HashValue;
116-
}
117-
118-
// The stored hash is truncated to RawSize bytes.
119-
// This trades off memory against the number of symbols we can handle.
120-
constexpr static size_t RawSize = 8;
121-
llvm::StringRef raw() const {
122-
return StringRef(reinterpret_cast<const char *>(HashValue.data()), RawSize);
123-
}
124-
static SymbolID fromRaw(llvm::StringRef);
125-
126-
// Returns a hex encoded string.
127-
std::string str() const;
128-
static llvm::Expected<SymbolID> fromStr(llvm::StringRef);
129-
130-
private:
131-
std::array<uint8_t, RawSize> HashValue;
132-
};
133-
134-
inline llvm::hash_code hash_value(const SymbolID &ID) {
135-
// We already have a good hash, just return the first bytes.
136-
assert(sizeof(size_t) <= SymbolID::RawSize && "size_t longer than SHA1!");
137-
size_t Result;
138-
memcpy(&Result, ID.raw().data(), sizeof(size_t));
139-
return llvm::hash_code(Result);
140-
}
141-
142-
// Write SymbolID into the given stream. SymbolID is encoded as ID.str().
143-
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID);
144-
14598
} // namespace clangd
14699
} // namespace clang
147100
namespace llvm {

Diff for: ‎clang-tools-extra/clangd/index/SymbolID.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===--- SymbolID.cpp --------------------------------------------*- C++-*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "SymbolID.h"
11+
#include "llvm/Support/SHA1.h"
12+
13+
using namespace llvm;
14+
namespace clang {
15+
namespace clangd {
16+
17+
SymbolID::SymbolID(StringRef USR) {
18+
auto Hash = llvm::SHA1::hash(arrayRefFromStringRef(USR));
19+
static_assert(sizeof(Hash) >= RawSize, "RawSize larger than SHA1");
20+
memcpy(HashValue.data(), Hash.data(), RawSize);
21+
}
22+
23+
llvm::StringRef SymbolID::raw() const {
24+
return StringRef(reinterpret_cast<const char *>(HashValue.data()), RawSize);
25+
}
26+
27+
SymbolID SymbolID::fromRaw(StringRef Raw) {
28+
SymbolID ID;
29+
assert(Raw.size() == RawSize);
30+
memcpy(ID.HashValue.data(), Raw.data(), RawSize);
31+
return ID;
32+
}
33+
34+
std::string SymbolID::str() const { return toHex(raw()); }
35+
36+
Expected<SymbolID> SymbolID::fromStr(StringRef Str) {
37+
if (Str.size() != RawSize * 2)
38+
return createStringError(inconvertibleErrorCode(), "Bad ID length");
39+
for (char C : Str)
40+
if (!isHexDigit(C))
41+
return createStringError(inconvertibleErrorCode(), "Bad hex ID");
42+
return fromRaw(fromHex(Str));
43+
}
44+
45+
raw_ostream &operator<<(raw_ostream &OS, const SymbolID &ID) {
46+
return OS << toHex(ID.raw());
47+
}
48+
49+
llvm::hash_code hash_value(const SymbolID &ID) {
50+
// We already have a good hash, just return the first bytes.
51+
assert(sizeof(size_t) <= SymbolID::RawSize && "size_t longer than SHA1!");
52+
size_t Result;
53+
memcpy(&Result, ID.raw().data(), sizeof(size_t));
54+
return llvm::hash_code(Result);
55+
}
56+
57+
} // namespace clangd
58+
} // namespace clang

Diff for: ‎clang-tools-extra/clangd/index/SymbolID.h

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//===--- SymbolID.h ----------------------------------------------*- C++-*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H
11+
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H
12+
13+
#include "llvm/ADT/Hashing.h"
14+
#include "llvm/ADT/StringRef.h"
15+
#include "llvm/Support/Error.h"
16+
#include "llvm/Support/raw_ostream.h"
17+
#include <array>
18+
#include <string>
19+
20+
namespace clang {
21+
namespace clangd {
22+
23+
// The class identifies a particular C++ symbol (class, function, method, etc).
24+
//
25+
// As USRs (Unified Symbol Resolution) could be large, especially for functions
26+
// with long type arguments, SymbolID is using truncated SHA1(USR) values to
27+
// guarantee the uniqueness of symbols while using a relatively small amount of
28+
// memory (vs storing USRs directly).
29+
//
30+
// SymbolID can be used as key in the symbol indexes to lookup the symbol.
31+
class SymbolID {
32+
public:
33+
SymbolID() = default;
34+
explicit SymbolID(llvm::StringRef USR);
35+
36+
bool operator==(const SymbolID &Sym) const {
37+
return HashValue == Sym.HashValue;
38+
}
39+
bool operator<(const SymbolID &Sym) const {
40+
return HashValue < Sym.HashValue;
41+
}
42+
43+
// The stored hash is truncated to RawSize bytes.
44+
// This trades off memory against the number of symbols we can handle.
45+
constexpr static size_t RawSize = 8;
46+
llvm::StringRef raw() const;
47+
static SymbolID fromRaw(llvm::StringRef);
48+
49+
// Returns a hex encoded string.
50+
std::string str() const;
51+
static llvm::Expected<SymbolID> fromStr(llvm::StringRef);
52+
53+
private:
54+
std::array<uint8_t, RawSize> HashValue;
55+
};
56+
57+
llvm::hash_code hash_value(const SymbolID &ID);
58+
59+
// Write SymbolID into the given stream. SymbolID is encoded as ID.str().
60+
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolID &ID);
61+
62+
} // namespace clangd
63+
} // namespace clang
64+
65+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_SYMBOLID_H

0 commit comments

Comments
 (0)
Please sign in to comment.