Index: clang-tidy/misc/CMakeLists.txt =================================================================== --- clang-tidy/misc/CMakeLists.txt +++ clang-tidy/misc/CMakeLists.txt @@ -8,6 +8,7 @@ MisplacedConstCheck.cpp UnconventionalAssignOperatorCheck.cpp BoolPointerImplicitConversionCheck.cpp + CopyConstructorInitCheck.cpp DanglingHandleCheck.cpp DefinitionsInHeadersCheck.cpp FoldInitTypeCheck.cpp Index: clang-tidy/misc/CopyConstructorInitCheck.h =================================================================== --- /dev/null +++ clang-tidy/misc/CopyConstructorInitCheck.h @@ -0,0 +1,36 @@ +//===--- CopyConstructorInitCheck.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_MISC_COPY_CONSTRUCTOR_INIT_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_COPY_CONSTRUCTOR_INIT_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace misc { + +/// Finds copy constructors where the ctor don't call the constructor of the +/// base class. +/// +/// For the user-facing documentation see: +/// http://clang.llvm.org/extra/clang-tidy/checks/misc-copy-constructor-init.html +class CopyConstructorInitCheck : public ClangTidyCheck { +public: + CopyConstructorInitCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult &Result) override; +}; + +} // namespace misc +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_COPY_CONSTRUCTOR_INIT_H Index: clang-tidy/misc/CopyConstructorInitCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/misc/CopyConstructorInitCheck.cpp @@ -0,0 +1,123 @@ +//===--- CopyConstructorInitCheck.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 "CopyConstructorInitCheck.h" +#include "clang/AST/ASTContext.h" +#include "clang/ASTMatchers/ASTMatchFinder.h" +#include "clang/Lex/Lexer.h" + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace misc { + +void CopyConstructorInitCheck::registerMatchers(MatchFinder *Finder) { + if (!getLangOpts().CPlusPlus) + return; + + Finder->addMatcher( + cxxConstructorDecl( + isCopyConstructor(), + hasAnyConstructorInitializer(cxxCtorInitializer( + isBaseInitializer(), + withInitializer(cxxConstructExpr(hasDeclaration( + cxxConstructorDecl(isDefaultConstructor())))))), + unless(isInstantiated())) + .bind("ctor"), + this); +} + +static bool isEmptyClass(const CXXRecordDecl *Class) { + if (!Class->field_empty()) + return false; + return true; +} + +void CopyConstructorInitCheck::check(const MatchFinder::MatchResult &Result) { + const auto *Ctor = Result.Nodes.getNodeAs("ctor"); + std::string ParamName = Ctor->getParamDecl(0)->getNameAsString(); + + // We want only one warning (and FixIt) for each ctor. + std::string FixItInitList; + bool HasRelevantBaseInit = false; + bool ShouldNotDoFixit = false; + SmallVector SafeFixIts; + for (const auto *Init : Ctor->inits()) { + if (!Init->isBaseInitializer()) + continue; + const Type *BaseType = Init->getBaseClass(); + if (const auto *TempSpecTy = dyn_cast(BaseType)) + ShouldNotDoFixit = ShouldNotDoFixit || TempSpecTy->isTypeAlias(); + ShouldNotDoFixit = ShouldNotDoFixit || isa(BaseType); + const auto *BaseClass = BaseType->getAsCXXRecordDecl()->getDefinition(); + if (isEmptyClass(BaseClass) && BaseClass->forallBases(isEmptyClass)) + continue; + const auto *CExpr = dyn_cast(Init->getInit()); + if (!CExpr) + continue; + if (!CExpr->getConstructor()->isDefaultConstructor()) + continue; + HasRelevantBaseInit = true; + // Valid when the initializer is written manually (not generated). + if (Init->getSourceRange().isValid()) { + if (!ParamName.empty()) + SafeFixIts.push_back( + FixItHint::CreateInsertion(CExpr->getLocEnd(), ParamName)); + } else { + if (Init->getSourceLocation().isMacroID() || + Ctor->getLocation().isMacroID() || ShouldNotDoFixit) + break; + FixItInitList += BaseClass->getNameAsString(); + FixItInitList += "(" + ParamName + "), "; + } + } + if (!HasRelevantBaseInit) + return; + + auto Diag = + diag(Ctor->getLocation(), + "calling a base constructor other than the copy constructor") + << SafeFixIts; + + if (FixItInitList.empty() || ParamName.empty() || ShouldNotDoFixit) + return; + + FixItInitList = FixItInitList.substr(0, FixItInitList.size() - 2); + + const SourceManager &SM = Result.Context->getSourceManager(); + SourceLocation StartLoc = Ctor->getLocation(); + StringRef Buffer = SM.getBufferData(SM.getFileID(StartLoc)); + const char *StartChar = SM.getCharacterData(StartLoc); + + Lexer Lex(StartLoc, getLangOpts(), StartChar, StartChar, Buffer.end()); + Token Tok; + // Loop until the beginning of the initialization list (if exists). + while (!Tok.is(tok::colon) && !Tok.is(tok::l_brace)) + Lex.LexFromRawLexer(Tok); + + std::string FixItMsg; + // There is not initialization list in this constructor. + if (Tok.is(tok::l_brace)) + FixItMsg += " : "; + FixItMsg += FixItInitList; + // We want to apply the missing constructor at the beginning of the + // initialization list. + if (Tok.is(tok::colon)) { + Lex.LexFromRawLexer(Tok); + FixItMsg += ','; + } + FixItMsg += ' '; + + Diag << FixItHint::CreateInsertion(Tok.getLocation(), FixItMsg); +} + +} // namespace misc +} // namespace tidy +} // namespace clang Index: clang-tidy/misc/MiscTidyModule.cpp =================================================================== --- clang-tidy/misc/MiscTidyModule.cpp +++ clang-tidy/misc/MiscTidyModule.cpp @@ -13,6 +13,7 @@ #include "ArgumentCommentCheck.h" #include "AssertSideEffectCheck.h" #include "BoolPointerImplicitConversionCheck.h" +#include "CopyConstructorInitCheck.h" #include "DanglingHandleCheck.h" #include "DefinitionsInHeadersCheck.h" #include "FoldInitTypeCheck.h" @@ -76,6 +77,8 @@ "misc-unconventional-assign-operator"); CheckFactories.registerCheck( "misc-bool-pointer-implicit-conversion"); + CheckFactories.registerCheck( + "misc-copy-constructor-init"); CheckFactories.registerCheck("misc-dangling-handle"); CheckFactories.registerCheck( "misc-definitions-in-headers"); Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -152,6 +152,12 @@ Finds member expressions that access static members through instances and replaces them with uses of the appropriate qualified-id. +- New `misc-copy-constructor-init + `_ check + + Finds copy constructors which don't call the constructor of the base class. + + - Added `modernize-use-emplace.IgnoreImplicitConstructors `_ option. Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -103,6 +103,7 @@ misc-argument-comment misc-assert-side-effect misc-bool-pointer-implicit-conversion + misc-copy-constructor-init misc-dangling-handle misc-definitions-in-headers misc-fold-init-type Index: docs/clang-tidy/checks/misc-copy-constructor-init.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/misc-copy-constructor-init.rst @@ -0,0 +1,30 @@ +.. title:: clang-tidy - misc-copy-constructor-init + +misc-copy-constructor-init +===================================== + +Finds copy constructors where the constructor don't call +the constructor of the base class. + +.. code-block:: c++ + + class Copyable { + public: + Copyable() = default; + Copyable(const Copyable &) = default; + }; + class X2 : public Copyable { + X2(const X2 &other) {} // Copyable(other) is missing + }; + +Also finds copy constructors where the constructor of +the base class don't have parameter. + +.. code-block:: c++ + + class X4 : public Copyable { + X4(const X4 &other) : Copyable() {} // other is missing + }; + +The check suggests a fix-it in every scenario including multiple +missing initializers and constructors with template argument. Index: test/clang-tidy/misc-copy-constructor-init.cpp =================================================================== --- /dev/null +++ test/clang-tidy/misc-copy-constructor-init.cpp @@ -0,0 +1,152 @@ +// RUN: %check_clang_tidy %s misc-copy-constructor-init %t + +class Copyable { +public: + Copyable() = default; + Copyable(const Copyable &) = default; + +private: + int a; +}; + +class Copyable2 { +public: + Copyable2() = default; + Copyable2(const Copyable2 &) = default; + +private: + int a; +}; + +class Copyable3 : public Copyable { +public: + Copyable3() = default; + Copyable3(const Copyable3 &) = default; +}; + +template +class Copyable4 { +public: + Copyable4() = default; + Copyable4(const Copyable4 &) = default; + +private: + int a; +}; + +template +class Copyable5 { +public: + Copyable5() = default; + Copyable5(const Copyable5 &) = default; + +private: + int a; +}; + +class EmptyCopyable { +public: + EmptyCopyable() = default; + EmptyCopyable(const EmptyCopyable &) = default; +}; + +template +using CopyableAlias = Copyable5; + +typedef Copyable5 CopyableAlias2; + +class X : public Copyable, public EmptyCopyable { + X(const X &other) : Copyable(other) {} +}; + +class X2 : public Copyable2 { + X2(const X2 &other) {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor other than the copy constructor [misc-copy-constructor-init] + // CHECK-FIXES: X2(const X2 &other) : Copyable2(other) {} +}; + +class X2_A : public Copyable2 { + X2_A(const X2_A &) {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X2_A(const X2_A &) {} +}; + +class X3 : public Copyable, public Copyable2 { + X3(const X3 &other) : Copyable(other) {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X3(const X3 &other) : Copyable2(other), Copyable(other) {} +}; + +class X4 : public Copyable { + X4(const X4 &other) : Copyable() {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X4(const X4 &other) : Copyable(other) {} +}; + +class X5 : public Copyable3 { + X5(const X5 &other) {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X5(const X5 &other) : Copyable3(other) {} +}; + +class X6 : public Copyable2, public Copyable3 { + X6(const X6 &other) {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X6(const X6 &other) : Copyable2(other), Copyable3(other) {} +}; + +class X7 : public Copyable, public Copyable2 { + X7(const X7 &other) : Copyable() {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X7(const X7 &other) : Copyable2(other), Copyable(other) {} +}; + +class X8 : public Copyable4 { + X8(const X8 &other) : Copyable4(other) {} +}; + +class X9 : public Copyable4 { + X9(const X9 &other) : Copyable4() {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X9(const X9 &other) : Copyable4(other) {} +}; + +class X10 : public Copyable4 { + X10(const X10 &other) {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X10(const X10 &other) : Copyable4(other) {} +}; + +class X11 : public Copyable5 { + X11(const X11 &other) {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X11(const X11 &other) : Copyable5(other) {} +}; + +class X12 : public CopyableAlias { + X12(const X12 &other) {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X12(const X12 &other) {} +}; + +template +class X13 : T { + X13(const X13 &other) {} +}; + +template class X13; +template class X13; + +#define FROMMACRO \ + class X14 : public Copyable2 { \ + X14(const X14 &other) {} \ + }; + +FROMMACRO +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: calling a base constructor + +class X15 : public CopyableAlias2 { + X15(const X15 &other) {} + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor + // CHECK-FIXES: X15(const X15 &other) {} +};