[llvm] r294275 - [PM] Defend against getting slightly wrong template arguments passed

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


Author: chandlerc
Date: Mon Feb  6 21:34:08 2017
New Revision: 294275

URL: http://llvm.org/viewvc/llvm-project?rev=294275&view=rev
Log:
[PM] 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 they
would definitely get triggered on use.

This is the last of the patch series defending against this that I have
planned, so far no bugs other than the original were found.

Modified:
    llvm/trunk/unittests/Transforms/Scalar/LoopPassManagerTest.cpp

Modified: llvm/trunk/unittests/Transforms/Scalar/LoopPassManagerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Transforms/Scalar/LoopPassManagerTest.cpp?rev=294275&r1=294274&r2=294275&view=diff
==============================================================================
--- llvm/trunk/unittests/Transforms/Scalar/LoopPassManagerTest.cpp (original)
+++ llvm/trunk/unittests/Transforms/Scalar/LoopPassManagerTest.cpp Mon Feb  6 21:34:08 2017
@@ -46,7 +46,10 @@ public:
 
     DerivedT *Handle;
 
-    Analysis(DerivedT &Handle) : Handle(&Handle) {}
+    Analysis(DerivedT &Handle) : Handle(&Handle) {
+      static_assert(std::is_base_of<MockAnalysisHandleBase, DerivedT>::value,
+                    "Must pass the derived type to this template!");
+    }
 
   public:
     class Result {
@@ -152,7 +155,10 @@ public:
 
     DerivedT *Handle;
 
-    Pass(DerivedT &Handle) : Handle(&Handle) {}
+    Pass(DerivedT &Handle) : Handle(&Handle) {
+      static_assert(std::is_base_of<MockPassHandleBase, DerivedT>::value,
+                    "Must pass the derived type to this template!");
+    }
 
   public:
     PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,




More information about the llvm-commits mailing list