1 //===--- SymbolID.cpp --------------------------------------------*- C++-*-===//
3 // 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
7 //===----------------------------------------------------------------------===//
10 #include "support/Logger.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/Support/SHA1.h"
17 SymbolID::SymbolID(llvm::StringRef USR
) {
18 auto Hash
= llvm::SHA1::hash(llvm::arrayRefFromStringRef(USR
));
19 static_assert(sizeof(Hash
) >= RawSize
, "RawSize larger than SHA1");
20 memcpy(HashValue
.data(), Hash
.data(), RawSize
);
23 llvm::StringRef
SymbolID::raw() const {
24 return llvm::StringRef(reinterpret_cast<const char *>(HashValue
.data()),
28 SymbolID
SymbolID::fromRaw(llvm::StringRef Raw
) {
30 assert(Raw
.size() == RawSize
);
31 memcpy(ID
.HashValue
.data(), Raw
.data(), RawSize
);
35 std::string
SymbolID::str() const { return llvm::toHex(raw()); }
37 llvm::Expected
<SymbolID
> SymbolID::fromStr(llvm::StringRef Str
) {
38 if (Str
.size() != RawSize
* 2)
39 return error("Bad ID length");
41 if (!llvm::isHexDigit(C
))
42 return error("Bad hex ID");
43 return fromRaw(llvm::fromHex(Str
));
46 llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
, const SymbolID
&ID
) {
47 return OS
<< llvm::toHex(ID
.raw());