1 //===--- CopyConstructorInitCheck.cpp - clang-tidy-------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "CopyConstructorInitCheck.h" 10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "clang/Lex/Lexer.h" 13 14 using namespace clang::ast_matchers; 15 16 namespace clang { 17 namespace tidy { 18 namespace bugprone { 19 20 void CopyConstructorInitCheck::registerMatchers(MatchFinder *Finder) { 21 // In the future this might be extended to move constructors? 22 Finder->addMatcher( 23 cxxConstructorDecl( 24 isCopyConstructor(), 25 hasAnyConstructorInitializer(cxxCtorInitializer( 26 isBaseInitializer(), 27 withInitializer(cxxConstructExpr(hasDeclaration( 28 cxxConstructorDecl(isDefaultConstructor())))))), 29 unless(isInstantiated())) 30 .bind("ctor"), 31 this); 32 } 33 34 void CopyConstructorInitCheck::check(const MatchFinder::MatchResult &Result) { 35 const auto *Ctor = Result.Nodes.getNodeAs<CXXConstructorDecl>("ctor"); 36 std::string ParamName = Ctor->getParamDecl(0)->getNameAsString(); 37 38 // We want only one warning (and FixIt) for each ctor. 39 std::string FixItInitList; 40 bool HasRelevantBaseInit = false; 41 bool ShouldNotDoFixit = false; 42 bool HasWrittenInitializer = false; 43 SmallVector<FixItHint, 2> SafeFixIts; 44 for (const auto *Init : Ctor->inits()) { 45 bool CtorInitIsWritten = Init->isWritten(); 46 HasWrittenInitializer = HasWrittenInitializer || CtorInitIsWritten; 47 if (!Init->isBaseInitializer()) 48 continue; 49 const Type *BaseType = Init->getBaseClass(); 50 // Do not do fixits if there is a type alias involved or one of the bases 51 // are explicitly initialized. In the latter case we not do fixits to avoid 52 // -Wreorder warnings. 53 if (const auto *TempSpecTy = dyn_cast<TemplateSpecializationType>(BaseType)) 54 ShouldNotDoFixit = ShouldNotDoFixit || TempSpecTy->isTypeAlias(); 55 ShouldNotDoFixit = ShouldNotDoFixit || isa<TypedefType>(BaseType); 56 ShouldNotDoFixit = ShouldNotDoFixit || CtorInitIsWritten; 57 const CXXRecordDecl *BaseClass = 58 BaseType->getAsCXXRecordDecl()->getDefinition(); 59 if (BaseClass->field_empty() && 60 BaseClass->forallBases( 61 [](const CXXRecordDecl *Class) { return Class->field_empty(); })) 62 continue; 63 bool NonCopyableBase = false; 64 for (const auto *Ctor : BaseClass->ctors()) { 65 if (Ctor->isCopyConstructor() && 66 (Ctor->getAccess() == AS_private || Ctor->isDeleted())) { 67 NonCopyableBase = true; 68 break; 69 } 70 } 71 if (NonCopyableBase) 72 continue; 73 const auto *CExpr = dyn_cast<CXXConstructExpr>(Init->getInit()); 74 if (!CExpr || !CExpr->getConstructor()->isDefaultConstructor()) 75 continue; 76 HasRelevantBaseInit = true; 77 if (CtorInitIsWritten) { 78 if (!ParamName.empty()) 79 SafeFixIts.push_back( 80 FixItHint::CreateInsertion(CExpr->getEndLoc(), ParamName)); 81 } else { 82 if (Init->getSourceLocation().isMacroID() || 83 Ctor->getLocation().isMacroID() || ShouldNotDoFixit) 84 break; 85 FixItInitList += BaseClass->getNameAsString(); 86 FixItInitList += "(" + ParamName + "), "; 87 } 88 } 89 if (!HasRelevantBaseInit) 90 return; 91 92 auto Diag = diag(Ctor->getLocation(), 93 "calling a base constructor other than the copy constructor") 94 << SafeFixIts; 95 96 if (FixItInitList.empty() || ParamName.empty() || ShouldNotDoFixit) 97 return; 98 99 std::string FixItMsg{FixItInitList.substr(0, FixItInitList.size() - 2)}; 100 SourceLocation FixItLoc; 101 // There is no initialization list in this constructor. 102 if (!HasWrittenInitializer) { 103 FixItLoc = Ctor->getBody()->getBeginLoc(); 104 FixItMsg = " : " + FixItMsg; 105 } else { 106 // We apply the missing ctors at the beginning of the initialization list. 107 FixItLoc = (*Ctor->init_begin())->getSourceLocation(); 108 FixItMsg += ','; 109 } 110 FixItMsg += ' '; 111 112 Diag << FixItHint::CreateInsertion(FixItLoc, FixItMsg); 113 } 114 115 } // namespace bugprone 116 } // namespace tidy 117 } // namespace clang 118