From: Chandler Carruth Date: Tue, 7 Feb 2017 03:17:30 +0000 (+0000) Subject: [IR/Analysis] Defend against getting slightly wrong template arguments X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e4145c2707e05419885195355d00a56f1013a0cb;p=llvm [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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294272 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Analysis/PtrUseVisitor.h b/include/llvm/Analysis/PtrUseVisitor.h index 6e61fc3be38..2fe7c672526 100644 --- a/include/llvm/Analysis/PtrUseVisitor.h +++ b/include/llvm/Analysis/PtrUseVisitor.h @@ -196,7 +196,10 @@ class PtrUseVisitor : protected InstVisitor, typedef InstVisitor Base; public: - PtrUseVisitor(const DataLayout &DL) : PtrUseVisitorBase(DL) {} + PtrUseVisitor(const DataLayout &DL) : PtrUseVisitorBase(DL) { + static_assert(std::is_base_of::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. diff --git a/include/llvm/IR/InstVisitor.h b/include/llvm/IR/InstVisitor.h index 088d3e0fbfa..55579819fd3 100644 --- a/include/llvm/IR/InstVisitor.h +++ b/include/llvm/IR/InstVisitor.h @@ -116,6 +116,9 @@ public: // visit - Finally, code to visit an instruction... // RetTy visit(Instruction &I) { + static_assert(std::is_base_of::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...