clang-tools  11.0.0
UseAutoCheck.cpp
Go to the documentation of this file.
1 //===--- UseAutoCheck.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 "UseAutoCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/Basic/CharInfo.h"
14 #include "clang/Tooling/FixIt.h"
15 
16 using namespace clang;
17 using namespace clang::ast_matchers;
18 using namespace clang::ast_matchers::internal;
19 
20 namespace clang {
21 namespace tidy {
22 namespace modernize {
23 namespace {
24 
25 const char IteratorDeclStmtId[] = "iterator_decl";
26 const char DeclWithNewId[] = "decl_new";
27 const char DeclWithCastId[] = "decl_cast";
28 const char DeclWithTemplateCastId[] = "decl_template";
29 
30 size_t GetTypeNameLength(bool RemoveStars, StringRef Text) {
31  enum CharType { Space, Alpha, Punctuation };
32  CharType LastChar = Space, BeforeSpace = Punctuation;
33  size_t NumChars = 0;
34  int TemplateTypenameCntr = 0;
35  for (const unsigned char C : Text) {
36  if (C == '<')
37  ++TemplateTypenameCntr;
38  else if (C == '>')
39  --TemplateTypenameCntr;
40  const CharType NextChar =
41  isAlphanumeric(C)
42  ? Alpha
43  : (isWhitespace(C) ||
44  (!RemoveStars && TemplateTypenameCntr == 0 && C == '*'))
45  ? Space
46  : Punctuation;
47  if (NextChar != Space) {
48  ++NumChars; // Count the non-space character.
49  if (LastChar == Space && NextChar == Alpha && BeforeSpace == Alpha)
50  ++NumChars; // Count a single space character between two words.
51  BeforeSpace = NextChar;
52  }
53  LastChar = NextChar;
54  }
55  return NumChars;
56 }
57 
58 /// Matches variable declarations that have explicit initializers that
59 /// are not initializer lists.
60 ///
61 /// Given
62 /// \code
63 /// iterator I = Container.begin();
64 /// MyType A(42);
65 /// MyType B{2};
66 /// MyType C;
67 /// \endcode
68 ///
69 /// varDecl(hasWrittenNonListInitializer()) maches \c I and \c A but not \c B
70 /// or \c C.
71 AST_MATCHER(VarDecl, hasWrittenNonListInitializer) {
72  const Expr *Init = Node.getAnyInitializer();
73  if (!Init)
74  return false;
75 
76  Init = Init->IgnoreImplicit();
77 
78  // The following test is based on DeclPrinter::VisitVarDecl() to find if an
79  // initializer is implicit or not.
80  if (const auto *Construct = dyn_cast<CXXConstructExpr>(Init)) {
81  return !Construct->isListInitialization() && Construct->getNumArgs() > 0 &&
82  !Construct->getArg(0)->isDefaultArgument();
83  }
84  return Node.getInitStyle() != VarDecl::ListInit;
85 }
86 
87 /// Matches QualTypes that are type sugar for QualTypes that match \c
88 /// SugarMatcher.
89 ///
90 /// Given
91 /// \code
92 /// class C {};
93 /// typedef C my_type;
94 /// typedef my_type my_other_type;
95 /// \endcode
96 ///
97 /// qualType(isSugarFor(recordType(hasDeclaration(namedDecl(hasName("C"))))))
98 /// matches \c my_type and \c my_other_type.
99 AST_MATCHER_P(QualType, isSugarFor, Matcher<QualType>, SugarMatcher) {
100  QualType QT = Node;
101  while (true) {
102  if (SugarMatcher.matches(QT, Finder, Builder))
103  return true;
104 
105  QualType NewQT = QT.getSingleStepDesugaredType(Finder->getASTContext());
106  if (NewQT == QT)
107  return false;
108  QT = NewQT;
109  }
110 }
111 
112 /// Matches named declarations that have one of the standard iterator
113 /// names: iterator, reverse_iterator, const_iterator, const_reverse_iterator.
114 ///
115 /// Given
116 /// \code
117 /// iterator I;
118 /// const_iterator CI;
119 /// \endcode
120 ///
121 /// namedDecl(hasStdIteratorName()) matches \c I and \c CI.
122 AST_MATCHER(NamedDecl, hasStdIteratorName) {
123  static const char *const IteratorNames[] = {"iterator", "reverse_iterator",
124  "const_iterator",
125  "const_reverse_iterator"};
126 
127  for (const char *Name : IteratorNames) {
128  if (hasName(Name).matches(Node, Finder, Builder))
129  return true;
130  }
131  return false;
132 }
133 
134 /// Matches named declarations that have one of the standard container
135 /// names.
136 ///
137 /// Given
138 /// \code
139 /// class vector {};
140 /// class forward_list {};
141 /// class my_ver{};
142 /// \endcode
143 ///
144 /// recordDecl(hasStdContainerName()) matches \c vector and \c forward_list
145 /// but not \c my_vec.
146 AST_MATCHER(NamedDecl, hasStdContainerName) {
147  static const char *const ContainerNames[] = {
148  "array", "deque",
149  "forward_list", "list",
150  "vector",
151 
152  "map", "multimap",
153  "set", "multiset",
154 
155  "unordered_map", "unordered_multimap",
156  "unordered_set", "unordered_multiset",
157 
158  "queue", "priority_queue",
159  "stack"};
160 
161  for (const char *Name : ContainerNames) {
162  if (hasName(Name).matches(Node, Finder, Builder))
163  return true;
164  }
165  return false;
166 }
167 
168 /// Matches declarations whose declaration context is the C++ standard library
169 /// namespace std.
170 ///
171 /// Note that inline namespaces are silently ignored during the lookup since
172 /// both libstdc++ and libc++ are known to use them for versioning purposes.
173 ///
174 /// Given:
175 /// \code
176 /// namespace ns {
177 /// struct my_type {};
178 /// using namespace std;
179 /// }
180 ///
181 /// using std::vector;
182 /// using ns:my_type;
183 /// using ns::list;
184 /// \code
185 ///
186 /// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(isFromStdNamespace())))
187 /// matches "using std::vector" and "using ns::list".
188 AST_MATCHER(Decl, isFromStdNamespace) {
189  const DeclContext *D = Node.getDeclContext();
190 
191  while (D->isInlineNamespace())
192  D = D->getParent();
193 
194  if (!D->isNamespace() || !D->getParent()->isTranslationUnit())
195  return false;
196 
197  const IdentifierInfo *Info = cast<NamespaceDecl>(D)->getIdentifier();
198 
199  return (Info && Info->isStr("std"));
200 }
201 
202 /// Matches declaration reference or member expressions with explicit template
203 /// arguments.
204 AST_POLYMORPHIC_MATCHER(hasExplicitTemplateArgs,
205  AST_POLYMORPHIC_SUPPORTED_TYPES(DeclRefExpr,
206  MemberExpr)) {
207  return Node.hasExplicitTemplateArgs();
208 }
209 
210 /// Returns a DeclarationMatcher that matches standard iterators nested
211 /// inside records with a standard container name.
212 DeclarationMatcher standardIterator() {
213  return decl(
214  namedDecl(hasStdIteratorName()),
215  hasDeclContext(recordDecl(hasStdContainerName(), isFromStdNamespace())));
216 }
217 
218 /// Returns a TypeMatcher that matches typedefs for standard iterators
219 /// inside records with a standard container name.
220 TypeMatcher typedefIterator() {
221  return typedefType(hasDeclaration(standardIterator()));
222 }
223 
224 /// Returns a TypeMatcher that matches records named for standard
225 /// iterators nested inside records named for standard containers.
226 TypeMatcher nestedIterator() {
227  return recordType(hasDeclaration(standardIterator()));
228 }
229 
230 /// Returns a TypeMatcher that matches types declared with using
231 /// declarations and which name standard iterators for standard containers.
232 TypeMatcher iteratorFromUsingDeclaration() {
233  auto HasIteratorDecl = hasDeclaration(namedDecl(hasStdIteratorName()));
234  // Types resulting from using declarations are represented by elaboratedType.
235  return elaboratedType(
236  // Unwrap the nested name specifier to test for one of the standard
237  // containers.
238  hasQualifier(specifiesType(templateSpecializationType(hasDeclaration(
239  namedDecl(hasStdContainerName(), isFromStdNamespace()))))),
240  // the named type is what comes after the final '::' in the type. It
241  // should name one of the standard iterator names.
242  namesType(
243  anyOf(typedefType(HasIteratorDecl), recordType(HasIteratorDecl))));
244 }
245 
246 /// This matcher returns declaration statements that contain variable
247 /// declarations with written non-list initializer for standard iterators.
248 StatementMatcher makeIteratorDeclMatcher() {
249  return declStmt(unless(has(
250  varDecl(anyOf(unless(hasWrittenNonListInitializer()),
251  unless(hasType(isSugarFor(anyOf(
252  typedefIterator(), nestedIterator(),
253  iteratorFromUsingDeclaration())))))))))
254  .bind(IteratorDeclStmtId);
255 }
256 
257 StatementMatcher makeDeclWithNewMatcher() {
258  return declStmt(
259  unless(has(varDecl(anyOf(
260  unless(hasInitializer(ignoringParenImpCasts(cxxNewExpr()))),
261  // FIXME: TypeLoc information is not reliable where CV
262  // qualifiers are concerned so these types can't be
263  // handled for now.
264  hasType(pointerType(
265  pointee(hasCanonicalType(hasLocalQualifiers())))),
266 
267  // FIXME: Handle function pointers. For now we ignore them
268  // because the replacement replaces the entire type
269  // specifier source range which includes the identifier.
270  hasType(pointsTo(
271  pointsTo(parenType(innerType(functionType()))))))))))
272  .bind(DeclWithNewId);
273 }
274 
275 StatementMatcher makeDeclWithCastMatcher() {
276  return declStmt(
277  unless(has(varDecl(unless(hasInitializer(explicitCastExpr()))))))
278  .bind(DeclWithCastId);
279 }
280 
281 StatementMatcher makeDeclWithTemplateCastMatcher() {
282  auto ST =
283  substTemplateTypeParmType(hasReplacementType(equalsBoundNode("arg")));
284 
285  auto ExplicitCall =
286  anyOf(has(memberExpr(hasExplicitTemplateArgs())),
287  has(ignoringImpCasts(declRefExpr(hasExplicitTemplateArgs()))));
288 
289  auto TemplateArg =
290  hasTemplateArgument(0, refersToType(qualType().bind("arg")));
291 
292  auto TemplateCall = callExpr(
293  ExplicitCall,
294  callee(functionDecl(TemplateArg,
295  returns(anyOf(ST, pointsTo(ST), references(ST))))));
296 
297  return declStmt(unless(has(varDecl(
298  unless(hasInitializer(ignoringImplicit(TemplateCall)))))))
299  .bind(DeclWithTemplateCastId);
300 }
301 
302 StatementMatcher makeCombinedMatcher() {
303  return declStmt(
304  // At least one varDecl should be a child of the declStmt to ensure
305  // it's a declaration list and avoid matching other declarations,
306  // e.g. using directives.
307  has(varDecl(unless(isImplicit()))),
308  // Skip declarations that are already using auto.
309  unless(has(varDecl(anyOf(hasType(autoType()),
310  hasType(qualType(hasDescendant(autoType()))))))),
311  anyOf(makeIteratorDeclMatcher(), makeDeclWithNewMatcher(),
312  makeDeclWithCastMatcher(), makeDeclWithTemplateCastMatcher()));
313 }
314 
315 } // namespace
316 
317 UseAutoCheck::UseAutoCheck(StringRef Name, ClangTidyContext *Context)
318  : ClangTidyCheck(Name, Context),
319  MinTypeNameLength(Options.get("MinTypeNameLength", 5)),
320  RemoveStars(Options.get("RemoveStars", false)) {}
321 
322 void UseAutoCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
323  Options.store(Opts, "MinTypeNameLength", MinTypeNameLength);
324  Options.store(Opts, "RemoveStars", RemoveStars);
325 }
326 
327 void UseAutoCheck::registerMatchers(MatchFinder *Finder) {
328  Finder->addMatcher(traverse(ast_type_traits::TK_AsIs, makeCombinedMatcher()),
329  this);
330 }
331 
332 void UseAutoCheck::replaceIterators(const DeclStmt *D, ASTContext *Context) {
333  for (const auto *Dec : D->decls()) {
334  const auto *V = cast<VarDecl>(Dec);
335  const Expr *ExprInit = V->getInit();
336 
337  // Skip expressions with cleanups from the initializer expression.
338  if (const auto *E = dyn_cast<ExprWithCleanups>(ExprInit))
339  ExprInit = E->getSubExpr();
340 
341  const auto *Construct = dyn_cast<CXXConstructExpr>(ExprInit);
342  if (!Construct)
343  continue;
344 
345  // Ensure that the constructor receives a single argument.
346  if (Construct->getNumArgs() != 1)
347  return;
348 
349  // Drill down to the as-written initializer.
350  const Expr *E = (*Construct->arg_begin())->IgnoreParenImpCasts();
351  if (E != E->IgnoreConversionOperator()) {
352  // We hit a conversion operator. Early-out now as they imply an implicit
353  // conversion from a different type. Could also mean an explicit
354  // conversion from the same type but that's pretty rare.
355  return;
356  }
357 
358  if (const auto *NestedConstruct = dyn_cast<CXXConstructExpr>(E)) {
359  // If we ran into an implicit conversion constructor, can't convert.
360  //
361  // FIXME: The following only checks if the constructor can be used
362  // implicitly, not if it actually was. Cases where the converting
363  // constructor was used explicitly won't get converted.
364  if (NestedConstruct->getConstructor()->isConvertingConstructor(false))
365  return;
366  }
367  if (!Context->hasSameType(V->getType(), E->getType()))
368  return;
369  }
370 
371  // Get the type location using the first declaration.
372  const auto *V = cast<VarDecl>(*D->decl_begin());
373 
374  // WARNING: TypeLoc::getSourceRange() will include the identifier for things
375  // like function pointers. Not a concern since this action only works with
376  // iterators but something to keep in mind in the future.
377 
378  SourceRange Range(V->getTypeSourceInfo()->getTypeLoc().getSourceRange());
379  diag(Range.getBegin(), "use auto when declaring iterators")
380  << FixItHint::CreateReplacement(Range, "auto");
381 }
382 
383 void UseAutoCheck::replaceExpr(
384  const DeclStmt *D, ASTContext *Context,
385  llvm::function_ref<QualType(const Expr *)> GetType, StringRef Message) {
386  const auto *FirstDecl = dyn_cast<VarDecl>(*D->decl_begin());
387  // Ensure that there is at least one VarDecl within the DeclStmt.
388  if (!FirstDecl)
389  return;
390 
391  const QualType FirstDeclType = FirstDecl->getType().getCanonicalType();
392 
393  std::vector<FixItHint> StarRemovals;
394  for (const auto *Dec : D->decls()) {
395  const auto *V = cast<VarDecl>(Dec);
396  // Ensure that every DeclStmt child is a VarDecl.
397  if (!V)
398  return;
399 
400  const auto *Expr = V->getInit()->IgnoreParenImpCasts();
401  // Ensure that every VarDecl has an initializer.
402  if (!Expr)
403  return;
404 
405  // If VarDecl and Initializer have mismatching unqualified types.
406  if (!Context->hasSameUnqualifiedType(V->getType(), GetType(Expr)))
407  return;
408 
409  // All subsequent variables in this declaration should have the same
410  // canonical type. For example, we don't want to use `auto` in
411  // `T *p = new T, **pp = new T*;`.
412  if (FirstDeclType != V->getType().getCanonicalType())
413  return;
414 
415  if (RemoveStars) {
416  // Remove explicitly written '*' from declarations where there's more than
417  // one declaration in the declaration list.
418  if (Dec == *D->decl_begin())
419  continue;
420 
421  auto Q = V->getTypeSourceInfo()->getTypeLoc().getAs<PointerTypeLoc>();
422  while (!Q.isNull()) {
423  StarRemovals.push_back(FixItHint::CreateRemoval(Q.getStarLoc()));
424  Q = Q.getNextTypeLoc().getAs<PointerTypeLoc>();
425  }
426  }
427  }
428 
429  // FIXME: There is, however, one case we can address: when the VarDecl pointee
430  // is the same as the initializer, just more CV-qualified. However, TypeLoc
431  // information is not reliable where CV qualifiers are concerned so we can't
432  // do anything about this case for now.
433  TypeLoc Loc = FirstDecl->getTypeSourceInfo()->getTypeLoc();
434  if (!RemoveStars) {
435  while (Loc.getTypeLocClass() == TypeLoc::Pointer ||
436  Loc.getTypeLocClass() == TypeLoc::Qualified)
437  Loc = Loc.getNextTypeLoc();
438  }
439  while (Loc.getTypeLocClass() == TypeLoc::LValueReference ||
440  Loc.getTypeLocClass() == TypeLoc::RValueReference ||
441  Loc.getTypeLocClass() == TypeLoc::Qualified) {
442  Loc = Loc.getNextTypeLoc();
443  }
444  SourceRange Range(Loc.getSourceRange());
445 
446  if (MinTypeNameLength != 0 &&
447  GetTypeNameLength(RemoveStars,
448  tooling::fixit::getText(Loc.getSourceRange(),
449  FirstDecl->getASTContext())) <
450  MinTypeNameLength)
451  return;
452 
453  auto Diag = diag(Range.getBegin(), Message);
454 
455  // Space after 'auto' to handle cases where the '*' in the pointer type is
456  // next to the identifier. This avoids changing 'int *p' into 'autop'.
457  // FIXME: This doesn't work for function pointers because the variable name
458  // is inside the type.
459  Diag << FixItHint::CreateReplacement(Range, RemoveStars ? "auto " : "auto")
460  << StarRemovals;
461 }
462 
463 void UseAutoCheck::check(const MatchFinder::MatchResult &Result) {
464  if (const auto *Decl = Result.Nodes.getNodeAs<DeclStmt>(IteratorDeclStmtId)) {
465  replaceIterators(Decl, Result.Context);
466  } else if (const auto *Decl =
467  Result.Nodes.getNodeAs<DeclStmt>(DeclWithNewId)) {
468  replaceExpr(Decl, Result.Context,
469  [](const Expr *Expr) { return Expr->getType(); },
470  "use auto when initializing with new to avoid "
471  "duplicating the type name");
472  } else if (const auto *Decl =
473  Result.Nodes.getNodeAs<DeclStmt>(DeclWithCastId)) {
474  replaceExpr(
475  Decl, Result.Context,
476  [](const Expr *Expr) {
477  return cast<ExplicitCastExpr>(Expr)->getTypeAsWritten();
478  },
479  "use auto when initializing with a cast to avoid duplicating the type "
480  "name");
481  } else if (const auto *Decl =
482  Result.Nodes.getNodeAs<DeclStmt>(DeclWithTemplateCastId)) {
483  replaceExpr(
484  Decl, Result.Context,
485  [](const Expr *Expr) {
486  return cast<CallExpr>(Expr->IgnoreImplicit())
487  ->getDirectCallee()
488  ->getReturnType();
489  },
490  "use auto when initializing with a template cast to avoid duplicating "
491  "the type name");
492  } else {
493  llvm_unreachable("Bad Callback. No node provided.");
494  }
495 }
496 
497 } // namespace modernize
498 } // namespace tidy
499 } // namespace clang
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
E
const Expr * E
Definition: AvoidBindCheck.cpp:88
clang::clangd::Punctuation
Definition: FuzzyMatch.h:45
clang::tidy::bugprone::Message
static const char Message[]
Definition: ReservedIdentifierCheck.cpp:31
UseAutoCheck.h
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::readability::AST_MATCHER_P
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)
Definition: ConvertMemberFunctionsToStatic.cpp:53
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:35
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
clang::clangd::CharType
CharType
Definition: FuzzyMatch.h:41
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:120
clang::ast_matchers::AST_POLYMORPHIC_MATCHER
AST_POLYMORPHIC_MATCHER(isInAbseilFile, AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc, NestedNameSpecifierLoc))
Matches AST nodes that were found within Abseil files.
Definition: AbseilMatcher.h:31
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
Loc
SourceLocation Loc
'#' location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:111