clang-tools  10.0.0git
ReturnBracedInitListCheck.cpp
Go to the documentation of this file.
1 //===--- ReturnBracedInitListCheck.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 
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13 #include "clang/Tooling/FixIt.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace modernize {
20 
21 void ReturnBracedInitListCheck::registerMatchers(MatchFinder *Finder) {
22  // Only register the matchers for C++.
23  if (!getLangOpts().CPlusPlus11)
24  return;
25 
26  // Skip list initialization and constructors with an initializer list.
27  auto ConstructExpr =
28  cxxConstructExpr(
29  unless(anyOf(hasDeclaration(cxxConstructorDecl(isExplicit())),
30  isListInitialization(), hasDescendant(initListExpr()),
31  isInTemplateInstantiation())))
32  .bind("ctor");
33 
34  auto CtorAsArgument = materializeTemporaryExpr(anyOf(
35  has(ConstructExpr), has(cxxFunctionalCastExpr(has(ConstructExpr)))));
36 
37  Finder->addMatcher(
38  functionDecl(isDefinition(), // Declarations don't have return statements.
39  returns(unless(anyOf(builtinType(), autoType()))),
40  hasDescendant(returnStmt(hasReturnValue(
41  has(cxxConstructExpr(has(CtorAsArgument)))))))
42  .bind("fn"),
43  this);
44 }
45 
46 void ReturnBracedInitListCheck::check(const MatchFinder::MatchResult &Result) {
47  const auto *MatchedFunctionDecl = Result.Nodes.getNodeAs<FunctionDecl>("fn");
48  const auto *MatchedConstructExpr =
49  Result.Nodes.getNodeAs<CXXConstructExpr>("ctor");
50 
51  // Don't make replacements in macro.
52  SourceLocation Loc = MatchedConstructExpr->getExprLoc();
53  if (Loc.isMacroID())
54  return;
55 
56  // Make sure that the return type matches the constructed type.
57  const QualType ReturnType =
58  MatchedFunctionDecl->getReturnType().getCanonicalType();
59  const QualType ConstructType =
60  MatchedConstructExpr->getType().getCanonicalType();
61  if (ReturnType != ConstructType)
62  return;
63 
64  auto Diag = diag(Loc, "avoid repeating the return type from the "
65  "declaration; use a braced initializer list instead");
66 
67  const SourceRange CallParensRange =
68  MatchedConstructExpr->getParenOrBraceRange();
69 
70  // Make sure there is an explicit constructor call.
71  if (CallParensRange.isInvalid())
72  return;
73 
74  // Make sure that the ctor arguments match the declaration.
75  for (unsigned I = 0, NumParams = MatchedConstructExpr->getNumArgs();
76  I < NumParams; ++I) {
77  if (const auto *VD = dyn_cast<VarDecl>(
78  MatchedConstructExpr->getConstructor()->getParamDecl(I))) {
79  if (MatchedConstructExpr->getArg(I)->getType().getCanonicalType() !=
80  VD->getType().getCanonicalType())
81  return;
82  }
83  }
84 
85  // Range for constructor name and opening brace.
86  CharSourceRange CtorCallSourceRange = CharSourceRange::getTokenRange(
87  Loc, CallParensRange.getBegin().getLocWithOffset(-1));
88 
89  Diag << FixItHint::CreateRemoval(CtorCallSourceRange)
90  << FixItHint::CreateReplacement(CallParensRange.getBegin(), "{")
91  << FixItHint::CreateReplacement(CallParensRange.getEnd(), "}");
92 }
93 
94 } // namespace modernize
95 } // namespace tidy
96 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:227
std::string ReturnType
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//