[Lldb-commits] [PATCH] D65942: Disallow implicit conversion from const char* to bool in llvm::toStringRef

Raphael Isemann via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Aug 8 05:32:31 PDT 2019


teemperor created this revision.
teemperor added reviewers: davide, labath.
Herald added subscribers: llvm-commits, lldb-commits, dexonsmith.
Herald added projects: LLDB, LLVM.

We use toStringRef in LLDB in a few places to convert const char* variables to llvm::StringRef.
toStringRef however has no const char* overload (as this is already implemented in the constructor),
so calling toStringRef with a const char will implicitly convert the pointer to true/false, which means
that toStringRef("Hello") will produce the string "true" without any compiler warning.

This patch adds a deleted overload for const char* to make this a compiler error and fixes the
places where we incorrectly use toStringRef to convert const char* to llvm::StringRef with it.


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D65942

Files:
  lldb/source/Symbol/TypeSystem.cpp
  llvm/include/llvm/ADT/StringExtras.h


Index: llvm/include/llvm/ADT/StringExtras.h
===================================================================
--- llvm/include/llvm/ADT/StringExtras.h
+++ llvm/include/llvm/ADT/StringExtras.h
@@ -50,6 +50,8 @@
 
 /// Construct a string ref from a boolean.
 inline StringRef toStringRef(bool B) { return StringRef(B ? "true" : "false"); }
+/// Prevent implicit conversation from string literal to boolean true.
+StringRef toStringRef(const char *C) = delete;
 
 /// Construct a string ref from an array ref of unsigned chars.
 inline StringRef toStringRef(ArrayRef<uint8_t> Input) {
Index: lldb/source/Symbol/TypeSystem.cpp
===================================================================
--- lldb/source/Symbol/TypeSystem.cpp
+++ lldb/source/Symbol/TypeSystem.cpp
@@ -219,7 +219,7 @@
       }
       error = llvm::make_error<llvm::StringError>(
           "TypeSystem for language " +
-              llvm::toStringRef(Language::GetNameForLanguageType(language)) +
+              llvm::StringRef(Language::GetNameForLanguageType(language)) +
               " doesn't exist",
           llvm::inconvertibleErrorCode());
       return std::move(error);
@@ -236,7 +236,7 @@
         }
         error = llvm::make_error<llvm::StringError>(
             "TypeSystem for language " +
-                llvm::toStringRef(Language::GetNameForLanguageType(language)) +
+                llvm::StringRef(Language::GetNameForLanguageType(language)) +
                 " doesn't exist",
             llvm::inconvertibleErrorCode());
         return std::move(error);
@@ -246,7 +246,7 @@
     if (!can_create) {
       error = llvm::make_error<llvm::StringError>(
           "Unable to find type system for language " +
-              llvm::toStringRef(Language::GetNameForLanguageType(language)),
+              llvm::StringRef(Language::GetNameForLanguageType(language)),
           llvm::inconvertibleErrorCode());
     } else {
       // Cache even if we get a shared pointer that contains a null type system
@@ -259,7 +259,7 @@
       }
       error = llvm::make_error<llvm::StringError>(
           "TypeSystem for language " +
-              llvm::toStringRef(Language::GetNameForLanguageType(language)) +
+              llvm::StringRef(Language::GetNameForLanguageType(language)) +
               " doesn't exist",
           llvm::inconvertibleErrorCode());
     }
@@ -288,7 +288,7 @@
       }
       error = llvm::make_error<llvm::StringError>(
           "TypeSystem for language " +
-              llvm::toStringRef(Language::GetNameForLanguageType(language)) +
+              llvm::StringRef(Language::GetNameForLanguageType(language)) +
               " doesn't exist",
           llvm::inconvertibleErrorCode());
       return std::move(error);
@@ -305,7 +305,7 @@
         }
         error = llvm::make_error<llvm::StringError>(
             "TypeSystem for language " +
-                llvm::toStringRef(Language::GetNameForLanguageType(language)) +
+                llvm::StringRef(Language::GetNameForLanguageType(language)) +
                 " doesn't exist",
             llvm::inconvertibleErrorCode());
         return std::move(error);
@@ -315,7 +315,7 @@
     if (!can_create) {
       error = llvm::make_error<llvm::StringError>(
           "Unable to find type system for language " +
-              llvm::toStringRef(Language::GetNameForLanguageType(language)),
+              llvm::StringRef(Language::GetNameForLanguageType(language)),
           llvm::inconvertibleErrorCode());
     } else {
       // Cache even if we get a shared pointer that contains a null type system
@@ -328,7 +328,7 @@
       }
       error = llvm::make_error<llvm::StringError>(
           "TypeSystem for language " +
-              llvm::toStringRef(Language::GetNameForLanguageType(language)) +
+              llvm::StringRef(Language::GetNameForLanguageType(language)) +
               " doesn't exist",
           llvm::inconvertibleErrorCode());
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65942.214119.patch
Type: text/x-patch
Size: 3957 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20190808/5e8c6f14/attachment-0001.bin>


More information about the lldb-commits mailing list