1 //===--- UseEqualsDefaultCheck.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 "UseEqualsDefaultCheck.h" 10 #include "../utils/LexerUtils.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 14 15 using namespace clang::ast_matchers; 16 17 namespace clang { 18 namespace tidy { 19 namespace modernize { 20 21 static const char SpecialFunction[] = "SpecialFunction"; 22 23 /// Finds all the named non-static fields of \p Record. 24 static std::set<const FieldDecl *> 25 getAllNamedFields(const CXXRecordDecl *Record) { 26 std::set<const FieldDecl *> Result; 27 for (const auto *Field : Record->fields()) { 28 // Static data members are not in this range. 29 if (Field->isUnnamedBitfield()) 30 continue; 31 Result.insert(Field); 32 } 33 return Result; 34 } 35 36 /// Returns the names of the direct bases of \p Record, both virtual and 37 /// non-virtual. 38 static std::set<const Type *> getAllDirectBases(const CXXRecordDecl *Record) { 39 std::set<const Type *> Result; 40 for (auto Base : Record->bases()) { 41 // CXXBaseSpecifier. 42 const auto *BaseType = Base.getTypeSourceInfo()->getType().getTypePtr(); 43 Result.insert(BaseType); 44 } 45 return Result; 46 } 47 48 /// Returns a matcher that matches member expressions where the base is 49 /// the variable declared as \p Var and the accessed member is the one declared 50 /// as \p Field. 51 internal::Matcher<Expr> accessToFieldInVar(const FieldDecl *Field, 52 const ValueDecl *Var) { 53 return ignoringImpCasts( 54 memberExpr(hasObjectExpression(declRefExpr(to(varDecl(equalsNode(Var))))), 55 member(fieldDecl(equalsNode(Field))))); 56 } 57 58 /// Check that the given constructor has copy signature and that it 59 /// copy-initializes all its bases and members. 60 static bool isCopyConstructorAndCanBeDefaulted(ASTContext *Context, 61 const CXXConstructorDecl *Ctor) { 62 // An explicitly-defaulted constructor cannot have default arguments. 63 if (Ctor->getMinRequiredArguments() != 1) 64 return false; 65 66 const auto *Record = Ctor->getParent(); 67 const auto *Param = Ctor->getParamDecl(0); 68 69 // Base classes and members that have to be copied. 70 auto BasesToInit = getAllDirectBases(Record); 71 auto FieldsToInit = getAllNamedFields(Record); 72 73 // Ensure that all the bases are copied. 74 for (const auto *Base : BasesToInit) { 75 // The initialization of a base class should be a call to a copy 76 // constructor of the base. 77 if (match( 78 traverse(TK_AsIs, 79 cxxConstructorDecl( 80 forEachConstructorInitializer(cxxCtorInitializer( 81 isBaseInitializer(), 82 withInitializer(cxxConstructExpr( 83 hasType(equalsNode(Base)), 84 hasDeclaration( 85 cxxConstructorDecl(isCopyConstructor())), 86 argumentCountIs(1), 87 hasArgument(0, declRefExpr(to(varDecl( 88 equalsNode(Param))))))))))), 89 *Ctor, *Context) 90 .empty()) 91 return false; 92 } 93 94 // Ensure that all the members are copied. 95 for (const auto *Field : FieldsToInit) { 96 auto AccessToFieldInParam = accessToFieldInVar(Field, Param); 97 // The initialization is a CXXConstructExpr for class types. 98 if (match(traverse( 99 TK_AsIs, 100 cxxConstructorDecl( 101 forEachConstructorInitializer(cxxCtorInitializer( 102 isMemberInitializer(), forField(equalsNode(Field)), 103 withInitializer(anyOf( 104 AccessToFieldInParam, 105 initListExpr(has(AccessToFieldInParam)), 106 cxxConstructExpr( 107 hasDeclaration( 108 cxxConstructorDecl(isCopyConstructor())), 109 argumentCountIs(1), 110 hasArgument(0, AccessToFieldInParam)))))))), 111 *Ctor, *Context) 112 .empty()) 113 return false; 114 } 115 116 // Ensure that we don't do anything else, like initializing an indirect base. 117 return Ctor->getNumCtorInitializers() == 118 BasesToInit.size() + FieldsToInit.size(); 119 } 120 121 /// Checks that the given method is an overloading of the assignment 122 /// operator, has copy signature, returns a reference to "*this" and copies 123 /// all its members and subobjects. 124 static bool isCopyAssignmentAndCanBeDefaulted(ASTContext *Context, 125 const CXXMethodDecl *Operator) { 126 const auto *Record = Operator->getParent(); 127 const auto *Param = Operator->getParamDecl(0); 128 129 // Base classes and members that have to be copied. 130 auto BasesToInit = getAllDirectBases(Record); 131 auto FieldsToInit = getAllNamedFields(Record); 132 133 const auto *Compound = cast<CompoundStmt>(Operator->getBody()); 134 135 // The assignment operator definition has to end with the following return 136 // statement: 137 // return *this; 138 if (Compound->body_empty() || 139 match(traverse( 140 TK_AsIs, 141 returnStmt(has(ignoringParenImpCasts(unaryOperator( 142 hasOperatorName("*"), hasUnaryOperand(cxxThisExpr())))))), 143 *Compound->body_back(), *Context) 144 .empty()) 145 return false; 146 147 // Ensure that all the bases are copied. 148 for (const auto *Base : BasesToInit) { 149 // Assignment operator of a base class: 150 // Base::operator=(Other); 151 // 152 // Clang translates this into: 153 // ((Base*)this)->operator=((Base)Other); 154 // 155 // So we are looking for a member call that fulfills: 156 if (match(traverse( 157 TK_AsIs, 158 compoundStmt(has(ignoringParenImpCasts(cxxMemberCallExpr( 159 // - The object is an implicit cast of 'this' to a 160 // pointer to 161 // a base class. 162 onImplicitObjectArgument(implicitCastExpr( 163 hasImplicitDestinationType(hasCanonicalType(pointsTo( 164 type(equalsNode(Base->getCanonicalTypeInternal() 165 .getTypePtr()))))), 166 hasSourceExpression(cxxThisExpr()))), 167 // - The called method is the operator=. 168 callee(cxxMethodDecl(isCopyAssignmentOperator())), 169 // - The argument is (an implicit cast to a Base of) 170 // the argument taken by "Operator". 171 argumentCountIs(1), 172 hasArgument( 173 0, declRefExpr(to(varDecl(equalsNode(Param)))))))))), 174 *Compound, *Context) 175 .empty()) 176 return false; 177 } 178 179 // Ensure that all the members are copied. 180 for (const auto *Field : FieldsToInit) { 181 // The assignment of data members: 182 // Field = Other.Field; 183 // Is a BinaryOperator in non-class types, and a CXXOperatorCallExpr 184 // otherwise. 185 auto LHS = memberExpr(hasObjectExpression(cxxThisExpr()), 186 member(fieldDecl(equalsNode(Field)))); 187 auto RHS = accessToFieldInVar(Field, Param); 188 if (match(traverse(TK_AsIs, 189 compoundStmt(has(ignoringParenImpCasts(binaryOperation( 190 hasOperatorName("="), hasLHS(LHS), hasRHS(RHS)))))), 191 *Compound, *Context) 192 .empty()) 193 return false; 194 } 195 196 // Ensure that we don't do anything else. 197 return Compound->size() == BasesToInit.size() + FieldsToInit.size() + 1; 198 } 199 200 /// Returns false if the body has any non-whitespace character. 201 static bool bodyEmpty(const ASTContext *Context, const CompoundStmt *Body) { 202 bool Invalid = false; 203 StringRef Text = Lexer::getSourceText( 204 CharSourceRange::getCharRange(Body->getLBracLoc().getLocWithOffset(1), 205 Body->getRBracLoc()), 206 Context->getSourceManager(), Context->getLangOpts(), &Invalid); 207 return !Invalid && std::strspn(Text.data(), " \t\r\n") == Text.size(); 208 } 209 210 UseEqualsDefaultCheck::UseEqualsDefaultCheck(StringRef Name, 211 ClangTidyContext *Context) 212 : ClangTidyCheck(Name, Context), 213 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {} 214 215 void UseEqualsDefaultCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { 216 Options.store(Opts, "IgnoreMacros", IgnoreMacros); 217 } 218 219 void UseEqualsDefaultCheck::registerMatchers(MatchFinder *Finder) { 220 // Skip unions since constructors with empty bodies behave differently 221 // in comparison with structs/classes. 222 223 // Destructor. 224 Finder->addMatcher(cxxDestructorDecl(unless(hasParent(recordDecl(isUnion()))), 225 isDefinition()) 226 .bind(SpecialFunction), 227 this); 228 Finder->addMatcher( 229 cxxConstructorDecl( 230 unless(hasParent(recordDecl(isUnion()))), isDefinition(), 231 anyOf( 232 // Default constructor. 233 allOf(unless(hasAnyConstructorInitializer(isWritten())), 234 parameterCountIs(0)), 235 // Copy constructor. 236 allOf(isCopyConstructor(), 237 // Discard constructors that can be used as a copy 238 // constructor because all the other arguments have 239 // default values. 240 parameterCountIs(1)))) 241 .bind(SpecialFunction), 242 this); 243 // Copy-assignment operator. 244 Finder->addMatcher( 245 cxxMethodDecl(unless(hasParent(recordDecl(isUnion()))), isDefinition(), 246 isCopyAssignmentOperator(), 247 // isCopyAssignmentOperator() allows the parameter to be 248 // passed by value, and in this case it cannot be 249 // defaulted. 250 hasParameter(0, hasType(lValueReferenceType()))) 251 .bind(SpecialFunction), 252 this); 253 } 254 255 void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) { 256 // Both CXXConstructorDecl and CXXDestructorDecl inherit from CXXMethodDecl. 257 const auto *SpecialFunctionDecl = 258 Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction); 259 260 if (IgnoreMacros && SpecialFunctionDecl->getLocation().isMacroID()) 261 return; 262 263 // Discard explicitly deleted/defaulted special member functions and those 264 // that are not user-provided (automatically generated). 265 if (SpecialFunctionDecl->isDeleted() || 266 SpecialFunctionDecl->isExplicitlyDefaulted() || 267 SpecialFunctionDecl->isLateTemplateParsed() || 268 SpecialFunctionDecl->isTemplateInstantiation() || 269 !SpecialFunctionDecl->isUserProvided() || !SpecialFunctionDecl->hasBody()) 270 return; 271 272 const auto *Body = dyn_cast<CompoundStmt>(SpecialFunctionDecl->getBody()); 273 if (!Body) 274 return; 275 276 // If there is code inside the body, don't warn. 277 if (!SpecialFunctionDecl->isCopyAssignmentOperator() && !Body->body_empty()) 278 return; 279 280 // If there are comments inside the body, don't do the change. 281 bool ApplyFix = SpecialFunctionDecl->isCopyAssignmentOperator() || 282 bodyEmpty(Result.Context, Body); 283 284 std::vector<FixItHint> RemoveInitializers; 285 unsigned MemberType; 286 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(SpecialFunctionDecl)) { 287 if (Ctor->getNumParams() == 0) { 288 MemberType = 0; 289 } else { 290 if (!isCopyConstructorAndCanBeDefaulted(Result.Context, Ctor)) 291 return; 292 MemberType = 1; 293 // If there are constructor initializers, they must be removed. 294 for (const auto *Init : Ctor->inits()) { 295 RemoveInitializers.emplace_back( 296 FixItHint::CreateRemoval(Init->getSourceRange())); 297 } 298 } 299 } else if (isa<CXXDestructorDecl>(SpecialFunctionDecl)) { 300 MemberType = 2; 301 } else { 302 if (!isCopyAssignmentAndCanBeDefaulted(Result.Context, SpecialFunctionDecl)) 303 return; 304 MemberType = 3; 305 } 306 307 // The location of the body is more useful inside a macro as spelling and 308 // expansion locations are reported. 309 SourceLocation Location = SpecialFunctionDecl->getLocation(); 310 if (Location.isMacroID()) 311 Location = Body->getBeginLoc(); 312 313 auto Diag = diag( 314 Location, 315 "use '= default' to define a trivial %select{default constructor|copy " 316 "constructor|destructor|copy-assignment operator}0"); 317 Diag << MemberType; 318 319 if (ApplyFix) { 320 // Skipping comments, check for a semicolon after Body->getSourceRange() 321 Optional<Token> Token = utils::lexer::findNextTokenSkippingComments( 322 Body->getSourceRange().getEnd().getLocWithOffset(1), 323 Result.Context->getSourceManager(), Result.Context->getLangOpts()); 324 StringRef Replacement = 325 Token && Token->is(tok::semi) ? "= default" : "= default;"; 326 Diag << FixItHint::CreateReplacement(Body->getSourceRange(), Replacement) 327 << RemoveInitializers; 328 } 329 } 330 331 } // namespace modernize 332 } // namespace tidy 333 } // namespace clang 334