Index: clang-tidy/cert/CERTTidyModule.cpp =================================================================== --- clang-tidy/cert/CERTTidyModule.cpp +++ clang-tidy/cert/CERTTidyModule.cpp @@ -20,6 +20,7 @@ #include "StaticObjectExceptionCheck.h" #include "ThrownExceptionTypeCheck.h" #include "VariadicFunctionDefCheck.h" +#include "VirtualFunctionsInConstructorsOrDestructorsCheck.h" namespace clang { namespace tidy { @@ -39,6 +40,8 @@ // OOP CheckFactories.registerCheck( "cert-oop11-cpp"); + CheckFactories.registerCheck< + VirtualFunctionsInConstructorsOrDestructorsCheck>("cert-oop50-cpp"); // ERR CheckFactories.registerCheck( "cert-err52-cpp"); Index: clang-tidy/cert/CMakeLists.txt =================================================================== --- clang-tidy/cert/CMakeLists.txt +++ clang-tidy/cert/CMakeLists.txt @@ -6,6 +6,7 @@ StaticObjectExceptionCheck.cpp ThrownExceptionTypeCheck.cpp VariadicFunctionDefCheck.cpp + VirtualFunctionsInConstructorsOrDestructorsCheck.cpp LINK_LIBS clangAST Index: clang-tidy/cert/VirtualFunctionsInConstructorsOrDestructorsCheck.h =================================================================== --- /dev/null +++ clang-tidy/cert/VirtualFunctionsInConstructorsOrDestructorsCheck.h @@ -0,0 +1,37 @@ +//===--- VirtualFunctionsInConstructorsOrDestructorsCheck.h - clang-tidy*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_VIRTUAL_FUNCTIONS_IN_CONSTRUCTORS_OR_DESTRUCTORS_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_VIRTUAL_FUNCTIONS_IN_CONSTRUCTORS_OR_DESTRUCTORS_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace cert { + +/// Checks whether there are virtual functions called in constructors or +/// destructors. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/cert-oop50-cpp.html +class VirtualFunctionsInConstructorsOrDestructorsCheck : public ClangTidyCheck { +public: + VirtualFunctionsInConstructorsOrDestructorsCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace cert +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_VIRTUAL_FUNCTIONS_IN_CONSTRUCTORS_OR_DESTRUCTORS_H Index: clang-tidy/cert/VirtualFunctionsInConstructorsOrDestructorsCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/cert/VirtualFunctionsInConstructorsOrDestructorsCheck.cpp @@ -0,0 +1,80 @@ +//===--- VirtualFunctionsInConstructorsOrDestructorsCheck.cpp - clang-tidy-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "VirtualFunctionsInConstructorsOrDestructorsCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace cert { + +namespace { + +AST_MATCHER(CXXMemberCallExpr, isThisMemberCallExpr) { + const MemberExpr *ME = dyn_cast(Node.getCallee()); + // CXXMemberCallExpr + // `-MemberExpr + // `-CXXThisExpr + if (isa(ME->getBase())) + return true; + + // CXXMemberCallExpr + // `-MemberExpr + // `-ImplicitCastExpr + // `-CXXThisExpr + if (const auto* ICE = dyn_cast(ME->getBase())) + return isa(ICE->getSubExpr()); + return false; +} + +} // namespace + +void VirtualFunctionsInConstructorsOrDestructorsCheck::registerMatchers( + MatchFinder *Finder) { + Finder->addMatcher( + cxxMemberCallExpr( + isThisMemberCallExpr(), + hasAncestor( + decl(anyOf(cxxConstructorDecl(), cxxDestructorDecl())))) + .bind("this-member-call"), this); +} + +void VirtualFunctionsInConstructorsOrDestructorsCheck::check( + const MatchFinder::MatchResult &Result) { + const CXXMemberCallExpr *MC = Result.Nodes.getNodeAs( + "this-member-call"); + const CXXMethodDecl *MD = MC->getMethodDecl(); + if (MD && MD->isVirtual()) { + // OOP50-CPP-EX1: It is permissible to call the virtual function with an + // explicitly qualified ID like A::f(); + const MemberExpr *ME = dyn_cast(MC->getCallee()); + if (ME->getQualifier()) + return; + + // OOP50-CPP-EX2: It is permissible to call a virtual function that has the + // final virt-specifier from a constructor or destructor. Similarly, it is + // also permissible to call a virtual function from a constructor or + // destructor of a class that has the final class-virt-specifier. + if (MD->hasAttr() || + MC->getRecordDecl()->hasAttr()) + return; + + diag(MC->getLocStart(), + "Calling virtual function '%0' in a constructor or destructor " + "is prohibited") + << MD->getNameInfo().getName().getAsString(); + } +} + +} // namespace cert +} // namespace tidy +} // namespace clang Index: docs/clang-tidy/checks/cert-oop50-cpp.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/cert-oop50-cpp.rst @@ -0,0 +1,10 @@ +.. title:: clang-tidy - cert-oop50-cpp + +cert-oop50-cpp +============== + +This check flags all virtual functions called in constructors or destructors. + +This check corresponds to the CERT C++ Coding Standard rule +`OOP50-CPP. Do not invoke virtual functions from constructors or destructors +`_. Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -8,6 +8,7 @@ cert-err52-cpp cert-err58-cpp cert-err60-cpp + cert-oop50-cpp cppcoreguidelines-pro-bounds-array-to-pointer-decay cppcoreguidelines-pro-bounds-constant-array-index cppcoreguidelines-pro-bounds-pointer-arithmetic Index: test/clang-tidy/cert-virtual-functions-in-constructors-or-destructors.cpp =================================================================== --- /dev/null +++ test/clang-tidy/cert-virtual-functions-in-constructors-or-destructors.cpp @@ -0,0 +1,70 @@ +// RUN: %check_clang_tidy %s cert-oop50-cpp %t + +class A { +public: + virtual void f() {} +}; + +class B { +public: + B() { + f(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Calling virtual function 'f' in a constructor or destructor is prohibited [cert-oop50-cpp] + this->f(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Calling virtual function 'f' in a constructor or destructor is prohibited + g(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Calling virtual function 'g' in a constructor or destructor is prohibited + this->g(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Calling virtual function 'g' in a constructor or destructor is prohibited + B::f(); + B::g(); + e(); + a_->f(); + } + B(const B& b) { + b.f(); + f(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Calling virtual function 'f' in a constructor or destructor is prohibited + e(); + } + virtual ~B() { + f(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Calling virtual function 'f' in a constructor or destructor is prohibited + e(); + } + + void e() {} + virtual void f() const {}; + virtual void g() {} + + A* a_; +}; + +class D : public B { +public: + D() { + f(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Calling virtual function 'f' in a constructor or destructor is prohibited + B::f(); + g(); + } + D(const D& d) { + f(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Calling virtual function 'f' in a constructor or destructor is prohibited + d.f(); + } + ~D() { + f(); +// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: Calling virtual function 'f' in a constructor or destructor is prohibited + } + void f() const override {} + void g() final override {} +}; + +class E final : public B { +public: + E() { + f(); + } + void f() const override { } +};