[llvm] r294272 - [IR/Analysis] Defend against getting slightly wrong template arguments

Chandler Carruth via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 6 19:17:30 PST 2017


Author: chandlerc
Date: Mon Feb  6 21:17:30 2017
New Revision: 294272

URL: http://llvm.org/viewvc/llvm-project?rev=294272&view=rev
Log:
[IR/Analysis] Defend against getting slightly wrong template arguments
passed into CRTP base classes.

This can sometimes happen and not cause an immediate failure when the
derived class is, itself, a template. You can end up essentially calling
methods on the wrong derived type but a type where many things will
appear to "work".

To fail fast and with a clear error message we can use a static_assert,
but we have to stash that static_assert inside a method body or nested
type that won't need to be completed while building the base class. I've
tried to pick a reasonably small number of places that seemed like
reliably places for this to be instantiated.

Modified:
    llvm/trunk/include/llvm/Analysis/PtrUseVisitor.h
    llvm/trunk/include/llvm/IR/InstVisitor.h

Modified: llvm/trunk/include/llvm/Analysis/PtrUseVisitor.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/PtrUseVisitor.h?rev=294272&r1=294271&r2=294272&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/PtrUseVisitor.h (original)
+++ llvm/trunk/include/llvm/Analysis/PtrUseVisitor.h Mon Feb  6 21:17:30 2017
@@ -196,7 +196,10 @@ class PtrUseVisitor : protected InstVisi
   typedef InstVisitor<DerivedT> Base;
 
 public:
-  PtrUseVisitor(const DataLayout &DL) : PtrUseVisitorBase(DL) {}
+  PtrUseVisitor(const DataLayout &DL) : PtrUseVisitorBase(DL) {
+    static_assert(std::is_base_of<PtrUseVisitor, DerivedT>::value,
+                  "Must pass the derived type to this template!");
+  }
 
   /// \brief Recursively visit the uses of the given pointer.
   /// \returns An info struct about the pointer. See \c PtrInfo for details.

Modified: llvm/trunk/include/llvm/IR/InstVisitor.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/InstVisitor.h?rev=294272&r1=294271&r2=294272&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/InstVisitor.h (original)
+++ llvm/trunk/include/llvm/IR/InstVisitor.h Mon Feb  6 21:17:30 2017
@@ -116,6 +116,9 @@ public:
   // visit - Finally, code to visit an instruction...
   //
   RetTy visit(Instruction &I) {
+    static_assert(std::is_base_of<InstVisitor, SubClass>::value,
+                  "Must pass the derived type to this template!");
+
     switch (I.getOpcode()) {
     default: llvm_unreachable("Unknown instruction type encountered!");
       // Build the switch statement using the Instruction.def file...




More information about the llvm-commits mailing list