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