[llvm] r284892 - [ADT] Don't rely on string literals not being convertible to non-const char* in CachedHashString.
Justin Lebar via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 21 15:10:23 PDT 2016
Author: jlebar
Date: Fri Oct 21 17:10:23 2016
New Revision: 284892
URL: http://llvm.org/viewvc/llvm-project?rev=284892&view=rev
Log:
[ADT] Don't rely on string literals not being convertible to non-const char* in CachedHashString.
The build was breaking on some platforms because we assumed that
CachedHashString("foo") would match the CachedHashString(StringRef)
constructor rather than the CachedHashString(char*) constructor.
To fix this, provide a CachedHashString(const char*) constructor, and
add a dummy argument to the old CachedHashString(char*) constructor.
Modified:
llvm/trunk/include/llvm/ADT/CachedHashString.h
Modified: llvm/trunk/include/llvm/ADT/CachedHashString.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/CachedHashString.h?rev=284892&r1=284891&r2=284892&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/CachedHashString.h (original)
+++ llvm/trunk/include/llvm/ADT/CachedHashString.h Fri Oct 21 17:10:23 2016
@@ -85,7 +85,9 @@ class CachedHashString {
return P == getEmptyKeyPtr() || P == getTombstoneKeyPtr();
}
- explicit CachedHashString(char *EmptyOrTombstonePtr)
+ struct ConstructEmptyOrTombstoneTy {};
+
+ CachedHashString(ConstructEmptyOrTombstoneTy, char *EmptyOrTombstonePtr)
: P(EmptyOrTombstonePtr), Size(0), Hash(0) {
assert(isEmptyOrTombstone());
}
@@ -93,6 +95,8 @@ class CachedHashString {
// TODO: Use small-string optimization to avoid allocating.
public:
+ explicit CachedHashString(const char *S) : CachedHashString(StringRef(S)) {}
+
// Explicit because copying and hashing a string isn't free.
explicit CachedHashString(StringRef S)
: CachedHashString(S, DenseMapInfo<StringRef>::getHashValue(S)) {}
@@ -148,10 +152,12 @@ public:
template <> struct DenseMapInfo<CachedHashString> {
static CachedHashString getEmptyKey() {
- return CachedHashString(CachedHashString::getEmptyKeyPtr());
+ return CachedHashString(CachedHashString::ConstructEmptyOrTombstoneTy(),
+ CachedHashString::getEmptyKeyPtr());
}
static CachedHashString getTombstoneKey() {
- return CachedHashString(CachedHashString::getTombstoneKeyPtr());
+ return CachedHashString(CachedHashString::ConstructEmptyOrTombstoneTy(),
+ CachedHashString::getTombstoneKeyPtr());
}
static unsigned getHashValue(const CachedHashString &S) {
assert(!isEqual(S, getEmptyKey()) && "Cannot hash the empty key!");
More information about the llvm-commits
mailing list