[llvm] r351572 - [adt] Twine(nullptr) derefs the nullptr. Add a deleted Twine(std::nullptr_t)

Daniel Sanders via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 18 10:40:35 PST 2019


Author: dsanders
Date: Fri Jan 18 10:40:35 2019
New Revision: 351572

URL: http://llvm.org/viewvc/llvm-project?rev=351572&view=rev
Log:
[adt] Twine(nullptr) derefs the nullptr. Add a deleted Twine(std::nullptr_t)

Summary:
nullptr can implicitly convert to Twine as Twine(nullptr) in which case it
resolves to Twine(const char *). This constructor derefs the pointer and
therefore doesn't work. Add a Twine(std::nullptr_t) = delete to make it a
compile time error.

It turns out that in-tree usage of Twine(nullptr) is confined to a single
private method in IRBuilder where foldConstant(... const Twine &Name = nullptr)
and this method is only ever called with an explicit Name argument as making it
a mandatory argument doesn't cause compile-time or run-time errors.

Reviewers: jyknight

Reviewed By: jyknight

Subscribers: dexonsmith, kristina, llvm-commits

Differential Revision: https://reviews.llvm.org/D56870

Modified:
    llvm/trunk/include/llvm/ADT/Twine.h
    llvm/trunk/include/llvm/IR/IRBuilder.h

Modified: llvm/trunk/include/llvm/ADT/Twine.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/Twine.h?rev=351572&r1=351571&r2=351572&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/Twine.h (original)
+++ llvm/trunk/include/llvm/ADT/Twine.h Fri Jan 18 10:40:35 2019
@@ -274,6 +274,9 @@ namespace llvm {
 
       assert(isValid() && "Invalid twine!");
     }
+    /// Delete the implicit conversion from nullptr as Twine(const char *)
+    /// cannot take nullptr.
+    /*implicit*/ Twine(std::nullptr_t) = delete;
 
     /// Construct from an std::string.
     /*implicit*/ Twine(const std::string &Str) : LHSKind(StdStringKind) {

Modified: llvm/trunk/include/llvm/IR/IRBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/IRBuilder.h?rev=351572&r1=351571&r2=351572&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/IRBuilder.h (original)
+++ llvm/trunk/include/llvm/IR/IRBuilder.h Fri Jan 18 10:40:35 2019
@@ -1004,7 +1004,7 @@ private:
   }
 
   Value *foldConstant(Instruction::BinaryOps Opc, Value *L,
-                      Value *R, const Twine &Name = nullptr) const {
+                      Value *R, const Twine &Name) const {
     auto *LC = dyn_cast<Constant>(L);
     auto *RC = dyn_cast<Constant>(R);
     return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;




More information about the llvm-commits mailing list