[llvm-commits] [llvm] r136481 - in /llvm/trunk: include/llvm/ADT/StringExtras.h utils/TableGen/Record.cpp

Chris Lattner clattner at apple.com
Fri Jul 29 12:10:15 PDT 2011


On Jul 29, 2011, at 12:06 PM, David Greene wrote:

> Author: greened
> Date: Fri Jul 29 14:06:58 2011
> New Revision: 136481
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=136481&view=rev
> Log:
> Add a std::string Wrapper for TableGen
> 
> Create a std::string wrapper for use as a DenseMap key.  DenseMap is
> not safe in generate with strings, so this wrapper indicates that only
> strings guaranteed not to have certain values should be used in the
> DenseMap.

This is fine with me in principle, but out of curiosity, why use DenseMap<string, x> instead of StringMap<x>?  The later doesn't need this and is *much* more efficient.

-Chris

> 
> Modified:
>    llvm/trunk/include/llvm/ADT/StringExtras.h
>    llvm/trunk/utils/TableGen/Record.cpp
> 
> Modified: llvm/trunk/include/llvm/ADT/StringExtras.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringExtras.h?rev=136481&r1=136480&r2=136481&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/StringExtras.h (original)
> +++ llvm/trunk/include/llvm/ADT/StringExtras.h Fri Jul 29 14:06:58 2011
> @@ -16,6 +16,7 @@
> 
> #include "llvm/Support/DataTypes.h"
> #include "llvm/ADT/APFloat.h"
> +#include "llvm/ADT/DenseMapInfo.h"
> #include "llvm/ADT/StringRef.h"
> #include <cctype>
> #include <cstdio>
> 
> Modified: llvm/trunk/utils/TableGen/Record.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/TableGen/Record.cpp?rev=136481&r1=136480&r2=136481&view=diff
> ==============================================================================
> --- llvm/trunk/utils/TableGen/Record.cpp (original)
> +++ llvm/trunk/utils/TableGen/Record.cpp Fri Jul 29 14:06:58 2011
> @@ -20,6 +20,52 @@
> using namespace llvm;
> 
> //===----------------------------------------------------------------------===//
> +//    std::string wrapper for DenseMap purposes
> +//===----------------------------------------------------------------------===//
> +
> +/// TableGenStringKey - This is a wrapper for std::string suitable for
> +/// using as a key to a DenseMap.  Because there isn't a particularly
> +/// good way to indicate tombstone or empty keys for strings, we want
> +/// to wrap std::string to indicate that this is a "special" string
> +/// not expected to take on certain values (those of the tombstone and
> +/// empty keys).  This makes things a little safer as it clarifies
> +/// that DenseMap is really not appropriate for general strings.
> +
> +class TableGenStringKey {
> +public:
> +  TableGenStringKey(const std::string &str) : data(str) {}
> +  TableGenStringKey(const char *str) : data(str) {}
> +
> +  const std::string &str() const { return data; }
> +  
> +private:
> +  std::string data;
> +};
> +
> +/// Specialize DenseMapInfo for TableGenStringKey.
> +namespace llvm {
> +
> +template<> struct DenseMapInfo<TableGenStringKey> {
> +  static inline TableGenStringKey getEmptyKey() {
> +    TableGenStringKey Empty("<<<EMPTY KEY>>>");
> +    return Empty;
> +  }
> +  static inline TableGenStringKey getTombstoneKey() {
> +    TableGenStringKey Tombstone("<<<TOMBSTONE KEY>>>");
> +    return Tombstone;
> +  }
> +  static unsigned getHashValue(const TableGenStringKey& Val) {
> +    return HashString(Val.str());
> +  }
> +  static bool isEqual(const TableGenStringKey& LHS,
> +                      const TableGenStringKey& RHS) {
> +    return LHS.str() == RHS.str();
> +  }
> +};
> +
> +}
> +
> +//===----------------------------------------------------------------------===//
> //    Type implementations
> //===----------------------------------------------------------------------===//
> 
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list