1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
//===--- UnusedRaiiCheck.cpp - clang-tidy ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "UnusedRaiiCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang::tidy::bugprone {
namespace {
AST_MATCHER(CXXRecordDecl, hasNonTrivialDestructor) {
// TODO: If the dtor is there but empty we don't want to warn either.
return Node.hasDefinition() && Node.hasNonTrivialDestructor();
}
} // namespace
void UnusedRaiiCheck::registerMatchers(MatchFinder *Finder) {
// Look for temporaries that are constructed in-place and immediately
// destroyed.
Finder->addMatcher(
mapAnyOf(cxxConstructExpr, cxxUnresolvedConstructExpr)
.with(hasParent(compoundStmt().bind("compound")),
anyOf(hasType(hasCanonicalType(recordType(hasDeclaration(
cxxRecordDecl(hasNonTrivialDestructor()))))),
hasType(hasCanonicalType(templateSpecializationType(
hasDeclaration(classTemplateDecl(has(
cxxRecordDecl(hasNonTrivialDestructor())))))))))
.bind("expr"),
this);
}
template <typename T>
void reportDiagnostic(DiagnosticBuilder D, const T *Node, SourceRange SR,
bool DefaultConstruction) {
const char *Replacement = " give_me_a_name";
// If this is a default ctor we have to remove the parens or we'll introduce a
// most vexing parse.
if (DefaultConstruction) {
D << FixItHint::CreateReplacement(CharSourceRange::getTokenRange(SR),
Replacement);
return;
}
// Otherwise just suggest adding a name. To find the place to insert the name
// find the first TypeLoc in the children of E, which always points to the
// written type.
D << FixItHint::CreateInsertion(SR.getBegin(), Replacement);
}
void UnusedRaiiCheck::check(const MatchFinder::MatchResult &Result) {
const auto *E = Result.Nodes.getNodeAs<Expr>("expr");
// We ignore code expanded from macros to reduce the number of false
// positives.
if (E->getBeginLoc().isMacroID())
return;
// Don't emit a warning for the last statement in the surrounding compound
// statement.
const auto *CS = Result.Nodes.getNodeAs<CompoundStmt>("compound");
const auto *LastExpr = dyn_cast<Expr>(CS->body_back());
if (LastExpr && E == LastExpr->IgnoreUnlessSpelledInSource())
return;
// Emit a warning.
auto D = diag(E->getBeginLoc(), "object destroyed immediately after "
"creation; did you mean to name the object?");
if (const auto *Node = dyn_cast<CXXConstructExpr>(E))
reportDiagnostic(D, Node, Node->getParenOrBraceRange(),
Node->getNumArgs() == 0 ||
isa<CXXDefaultArgExpr>(Node->getArg(0)));
if (const auto *Node = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
auto SR = SourceRange(Node->getLParenLoc(), Node->getRParenLoc());
auto DefaultConstruction = Node->getNumArgs() == 0;
if (!DefaultConstruction) {
auto *FirstArg = Node->getArg(0);
DefaultConstruction = isa<CXXDefaultArgExpr>(FirstArg);
if (auto *ILE = dyn_cast<InitListExpr>(FirstArg)) {
DefaultConstruction = ILE->getNumInits() == 0;
SR = SourceRange(ILE->getLBraceLoc(), ILE->getRBraceLoc());
}
}
reportDiagnostic(D, Node, SR, DefaultConstruction);
}
}
} // namespace clang::tidy::bugprone
|