clang-tools 23.0.0git
TriviallyDestructibleCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
15using namespace clang::ast_matchers;
16using namespace clang::ast_matchers::internal;
17using namespace clang::tidy::matchers;
18
20
21namespace {
22
23// We use isOutOfLine() to find out-of-line defaulted destructor definitions.
24// This is more robust than !isFirstDecl() because with C++20 modules, when a
25// class is visible through both a header include and a module import, its
26// declarations may appear multiple times in the redeclaration chain.
27AST_MATCHER(CXXMethodDecl, isOutOfLine) { return Node.isOutOfLine(); }
28
29AST_MATCHER_P(CXXRecordDecl, hasBase, Matcher<QualType>, InnerMatcher) {
30 return llvm::any_of(Node.bases(), [&](const CXXBaseSpecifier &BaseSpec) {
31 return InnerMatcher.matches(BaseSpec.getType(), Finder, Builder);
32 });
33}
34
35} // namespace
36
38 Finder->addMatcher(
39 cxxDestructorDecl(
40 isDefaulted(), isOutOfLine(),
41 unless(anyOf(isVirtual(),
42 ofClass(cxxRecordDecl(
43 anyOf(hasBase(unless(isTriviallyDestructible())),
44 has(fieldDecl(unless(
45 hasType(isTriviallyDestructible()))))))))))
46 .bind("decl"),
47 this);
48}
49
50void TriviallyDestructibleCheck::check(const MatchFinder::MatchResult &Result) {
51 const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXDestructorDecl>("decl");
52
53 // Get locations of both first and out-of-line declarations.
54 const SourceManager &SM = *Result.SourceManager;
55 const auto *FirstDecl = cast<CXXMethodDecl>(MatchedDecl->getFirstDecl());
56 const SourceLocation FirstDeclEnd = utils::lexer::findNextTerminator(
57 FirstDecl->getEndLoc(), SM, getLangOpts());
58 const CharSourceRange SecondDeclRange = CharSourceRange::getTokenRange(
59 MatchedDecl->getBeginLoc(),
60 utils::lexer::findNextTerminator(MatchedDecl->getEndLoc(), SM,
61 getLangOpts()));
62 if (FirstDeclEnd.isInvalid() || SecondDeclRange.isInvalid())
63 return;
64
65 // Report diagnostic.
66 diag(FirstDecl->getLocation(),
67 "class %0 can be made trivially destructible by defaulting the "
68 "destructor on its first declaration")
69 << FirstDecl->getParent()
70 << FixItHint::CreateInsertion(FirstDeclEnd, " = default")
71 << FixItHint::CreateRemoval(SecondDeclRange);
72 diag(MatchedDecl->getLocation(), "destructor definition is here",
73 DiagnosticIDs::Note);
74}
75
76} // namespace clang::tidy::performance
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
AST_MATCHER(BinaryOperator, isRelationalOperator)
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)