]> granicus.if.org Git - llvm/commitdiff
[PM] Defend against getting slightly wrong template arguments passed
authorChandler Carruth <chandlerc@gmail.com>
Tue, 7 Feb 2017 03:34:08 +0000 (03:34 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Tue, 7 Feb 2017 03:34:08 +0000 (03:34 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294275 91177308-0d34-0410-b5e6-96231b3b80d8

unittests/Transforms/Scalar/LoopPassManagerTest.cpp

index 0209927b9f4a32564ca530e43f1b080a7b2c4c38..227060f0a46e1e9112c564a72cd460f70a832649 100644 (file)
@@ -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,