clang-tools  10.0.0git
TriviallyDestructibleCheck.cpp
Go to the documentation of this file.
1 //===--- TriviallyDestructibleCheck.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 "../utils/LexerUtils.h"
11 #include "../utils/Matchers.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 
15 using namespace clang::ast_matchers;
16 using namespace clang::ast_matchers::internal;
17 using namespace clang::tidy::matchers;
18 
19 namespace clang {
20 namespace tidy {
21 namespace performance {
22 
23 namespace {
24 
25 AST_MATCHER(Decl, isFirstDecl) { return Node.isFirstDecl(); }
26 
27 AST_MATCHER_P(CXXRecordDecl, hasBase, Matcher<QualType>, InnerMatcher) {
28  for (const CXXBaseSpecifier &BaseSpec : Node.bases()) {
29  QualType BaseType = BaseSpec.getType();
30  if (InnerMatcher.matches(BaseType, Finder, Builder))
31  return true;
32  }
33  return false;
34 }
35 
36 } // namespace
37 
38 void TriviallyDestructibleCheck::registerMatchers(MatchFinder *Finder) {
39  if (!getLangOpts().CPlusPlus11)
40  return;
41 
42  Finder->addMatcher(
43  cxxDestructorDecl(
44  isDefaulted(),
45  unless(anyOf(isFirstDecl(), isVirtual(),
46  ofClass(cxxRecordDecl(
47  anyOf(hasBase(unless(isTriviallyDestructible())),
48  has(fieldDecl(unless(
49  hasType(isTriviallyDestructible()))))))))))
50  .bind("decl"),
51  this);
52 }
53 
54 void TriviallyDestructibleCheck::check(const MatchFinder::MatchResult &Result) {
55  const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXDestructorDecl>("decl");
56 
57  // Get locations of both first and out-of-line declarations.
58  SourceManager &SM = *Result.SourceManager;
59  const auto *FirstDecl = cast<CXXMethodDecl>(MatchedDecl->getFirstDecl());
60  const SourceLocation FirstDeclEnd = utils::lexer::findNextTerminator(
61  FirstDecl->getEndLoc(), SM, getLangOpts());
62  const CharSourceRange SecondDeclRange = CharSourceRange::getTokenRange(
63  MatchedDecl->getBeginLoc(),
64  utils::lexer::findNextTerminator(MatchedDecl->getEndLoc(), SM,
65  getLangOpts()));
66  if (FirstDeclEnd.isInvalid() || SecondDeclRange.isInvalid())
67  return;
68 
69  // Report diagnostic.
70  diag(FirstDecl->getLocation(),
71  "class %0 can be made trivially destructible by defaulting the "
72  "destructor on its first declaration")
73  << FirstDecl->getParent()
74  << FixItHint::CreateInsertion(FirstDeclEnd, " = default")
75  << FixItHint::CreateRemoval(SecondDeclRange);
76  diag(MatchedDecl->getLocation(), "destructor definition is here",
77  DiagnosticIDs::Note);
78 }
79 
80 } // namespace performance
81 } // namespace tidy
82 } // namespace clang
const FunctionDecl * Decl
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition: LexerUtils.cpp:73
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
Definition: SourceCode.cpp:227
CodeCompletionBuilder Builder
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool isTriviallyDestructible(QualType Type)
Returns true if Type is trivially destructible.
Definition: TypeTraits.cpp:139
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)