[PATCH] PR10140 - StringPool's PooledStringPtr has non-const operator== causing bad OR-result

Nikola Smiljanić popizdeh at gmail.com
Wed Jun 18 17:11:31 PDT 2014


Tests fail with original code. Tests don't compile if only conversion operator is marked as explicit. Const qualifying comparison operators gets the tests to compile and pass.

http://reviews.llvm.org/D4194

Files:
  include/llvm/Support/StringPool.h
  unittests/Support/CMakeLists.txt
  unittests/Support/StringPool.cpp

Index: include/llvm/Support/StringPool.h
===================================================================
--- include/llvm/Support/StringPool.h
+++ include/llvm/Support/StringPool.h
@@ -128,10 +128,10 @@
     }
 
     inline const char *operator*() const { return begin(); }
-    inline operator bool() const { return S != nullptr; }
+    inline explicit operator bool() const { return S != nullptr; }
 
-    inline bool operator==(const PooledStringPtr &That) { return S == That.S; }
-    inline bool operator!=(const PooledStringPtr &That) { return S != That.S; }
+    inline bool operator==(const PooledStringPtr &That) const { return S == That.S; }
+    inline bool operator!=(const PooledStringPtr &That) const { return S != That.S; }
   };
 
 } // End llvm namespace
Index: unittests/Support/CMakeLists.txt
===================================================================
--- unittests/Support/CMakeLists.txt
+++ unittests/Support/CMakeLists.txt
@@ -30,6 +30,7 @@
   ProgramTest.cpp
   RegexTest.cpp
   SourceMgrTest.cpp
+  StringPool.cpp
   SwapByteOrderTest.cpp
   ThreadLocalTest.cpp
   TimeValueTest.cpp
Index: unittests/Support/StringPool.cpp
===================================================================
--- /dev/null
+++ unittests/Support/StringPool.cpp
@@ -0,0 +1,34 @@
+//===- llvm/unittest/Support/ThreadLocalTest.cpp - Therad Local tests   ---===//
+//
+//		       The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/StringPool.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+class PooledStringPtrTest : public ::testing::Test {
+};
+
+TEST_F(PooledStringPtrTest, OperatorEquals) {
+  StringPool pool;
+  const PooledStringPtr a = pool.intern("a");
+  const PooledStringPtr b = pool.intern("b");
+  EXPECT_FALSE(a == b);
+}
+
+TEST_F(PooledStringPtrTest, OperatorNotEquals) {
+  StringPool pool;
+  const PooledStringPtr a = pool.intern("a");
+  const PooledStringPtr b = pool.intern("b");
+  EXPECT_TRUE(a != b);
+}
+
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4194.10596.patch
Type: text/x-patch
Size: 2188 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140619/8257f1e8/attachment.bin>


More information about the llvm-commits mailing list