summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans Wennborg <hans@chromium.org>2022-01-10 19:45:13 +0100
committerHans Wennborg <hans@chromium.org>2022-01-11 11:51:04 +0100
commit0b48d0fe1292929f0cd61a2ca8114d794e245daa (patch)
tree2e810d4ec9ff768984aaa2fd6174773949be4fa0
parentfe2c4af905074c7a7745aad3177ad32ac2bd30cf (diff)
downloadllvm-0b48d0fe1292929f0cd61a2ca8114d794e245daa.tar.gz
[ADT] Add an in-place version of toHex()
and use that to simplify MD5's hex string code which was previously using a string stream, as well as Clang's CGDebugInfo::computeChecksum(). Differential revision: https://reviews.llvm.org/D116960
-rw-r--r--clang/lib/CodeGen/CGDebugInfo.cpp10
-rw-r--r--llvm/include/llvm/ADT/StringExtras.h28
-rw-r--r--llvm/include/llvm/Support/MD5.h2
-rw-r--r--llvm/lib/Support/MD5.cpp11
4 files changed, 24 insertions, 27 deletions
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 313a927c26d3..1a9080604a79 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -354,13 +354,9 @@ CGDebugInfo::computeChecksum(FileID FID, SmallString<32> &Checksum) const {
if (!MemBuffer)
return None;
- llvm::MD5 Hash;
- llvm::MD5::MD5Result Result;
-
- Hash.update(MemBuffer->getBuffer());
- Hash.final(Result);
-
- Hash.stringifyResult(Result, Checksum);
+ llvm::toHex(
+ llvm::MD5::hash(llvm::arrayRefFromStringRef(MemBuffer->getBuffer())),
+ /*LowerCase*/ true, Checksum);
return llvm::DIFile::CSK_MD5;
}
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index 912b9bb53c83..81a0954226d6 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -29,7 +29,6 @@
namespace llvm {
-template<typename T> class SmallVectorImpl;
class raw_ostream;
/// hexdigit - Return the hexadecimal character for the
@@ -166,21 +165,26 @@ inline std::string utohexstr(uint64_t X, bool LowerCase = false) {
/// Convert buffer \p Input to its hexadecimal representation.
/// The returned string is double the size of \p Input.
-inline std::string toHex(StringRef Input, bool LowerCase = false) {
- size_t Length = Input.size();
-
- std::string Output;
- Output.reserve(2 * Length);
- for (size_t i = 0; i < Length; ++i) {
- const unsigned char c = Input[i];
- Output.push_back(hexdigit(c >> 4, LowerCase));
- Output.push_back(hexdigit(c & 15, LowerCase));
+inline void toHex(ArrayRef<uint8_t> Input, bool LowerCase,
+ SmallVectorImpl<char> &Output) {
+ const size_t Length = Input.size();
+ Output.resize_for_overwrite(Length * 2);
+
+ for (size_t i = 0; i < Length; i++) {
+ const uint8_t c = Input[i];
+ Output[i * 2 ] = hexdigit(c >> 4, LowerCase);
+ Output[i * 2 + 1] = hexdigit(c & 15, LowerCase);
}
- return Output;
}
inline std::string toHex(ArrayRef<uint8_t> Input, bool LowerCase = false) {
- return toHex(toStringRef(Input), LowerCase);
+ SmallString<16> Output;
+ toHex(Input, LowerCase, Output);
+ return std::string(Output);
+}
+
+inline std::string toHex(StringRef Input, bool LowerCase = false) {
+ return toHex(arrayRefFromStringRef(Input), LowerCase);
}
/// Store the binary representation of the two provided values, \p MSB and
diff --git a/llvm/include/llvm/Support/MD5.h b/llvm/include/llvm/Support/MD5.h
index 3b960cd4fd88..70d046601346 100644
--- a/llvm/include/llvm/Support/MD5.h
+++ b/llvm/include/llvm/Support/MD5.h
@@ -88,7 +88,7 @@ public:
/// Translates the bytes in \p Res to a hex string that is
/// deposited into \p Str. The result will be of length 32.
- static void stringifyResult(MD5Result &Result, SmallString<32> &Str);
+ static void stringifyResult(MD5Result &Result, SmallVectorImpl<char> &Str);
/// Computes the hash for a given bytes.
static std::array<uint8_t, 16> hash(ArrayRef<uint8_t> Data);
diff --git a/llvm/lib/Support/MD5.cpp b/llvm/lib/Support/MD5.cpp
index 9dceb4d418cd..caadde389504 100644
--- a/llvm/lib/Support/MD5.cpp
+++ b/llvm/lib/Support/MD5.cpp
@@ -40,10 +40,9 @@
#include "llvm/Support/MD5.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Endian.h"
-#include "llvm/Support/Format.h"
-#include "llvm/Support/raw_ostream.h"
#include <array>
#include <cstdint>
#include <cstring>
@@ -281,14 +280,12 @@ StringRef MD5::result() {
SmallString<32> MD5::MD5Result::digest() const {
SmallString<32> Str;
- raw_svector_ostream Res(Str);
- for (int i = 0; i < 16; ++i)
- Res << format("%.2x", Bytes[i]);
+ toHex(Bytes, /*LowerCase*/ true, Str);
return Str;
}
-void MD5::stringifyResult(MD5Result &Result, SmallString<32> &Str) {
- Str = Result.digest();
+void MD5::stringifyResult(MD5Result &Result, SmallVectorImpl<char> &Str) {
+ toHex(Result.Bytes, /*LowerCase*/ true, Str);
}
std::array<uint8_t, 16> MD5::hash(ArrayRef<uint8_t> Data) {