clang-tools 23.0.0git
TemporaryObjectsCheck.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/CheckUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "llvm/ADT/STLExtras.h"
15#include <string>
16
17using namespace clang::ast_matchers;
18
19namespace clang::tidy::fuchsia {
20
21namespace {
22
23constexpr llvm::StringLiteral DeprecatedCheckName = "zircon-temporary-objects";
24constexpr llvm::StringLiteral CanonicalCheckName = "fuchsia-temporary-objects";
25
26AST_MATCHER_P(CXXRecordDecl, matchesAnyName, ArrayRef<StringRef>, Names) {
27 const std::string QualifiedName = Node.getQualifiedNameAsString();
28 return llvm::is_contained(Names, QualifiedName);
29}
30
31} // namespace
32
34 ClangTidyContext *Context)
35 : ClangTidyCheck(Name, Context),
36 Names(utils::options::parseStringList(Options.get("Names", ""))) {
37 if (Name == DeprecatedCheckName)
38 utils::diagDeprecatedCheckAlias(*this, *Context, DeprecatedCheckName,
39 CanonicalCheckName);
40}
41
42void TemporaryObjectsCheck::registerMatchers(MatchFinder *Finder) {
43 // Matcher for default constructors.
44 Finder->addMatcher(
45 cxxTemporaryObjectExpr(hasDeclaration(cxxConstructorDecl(hasParent(
46 cxxRecordDecl(matchesAnyName(Names))))))
47 .bind("temps"),
48 this);
49
50 // Matcher for user-defined constructors.
51 Finder->addMatcher(
52 traverse(TK_AsIs,
53 cxxConstructExpr(hasParent(cxxFunctionalCastExpr()),
54 hasDeclaration(cxxConstructorDecl(hasParent(
55 cxxRecordDecl(matchesAnyName(Names))))))
56 .bind("temps")),
57 this);
58}
59
60void TemporaryObjectsCheck::check(const MatchFinder::MatchResult &Result) {
61 if (const auto *D = Result.Nodes.getNodeAs<CXXConstructExpr>("temps"))
62 diag(D->getLocation(),
63 "creating a temporary object of type %q0 is prohibited")
64 << D->getConstructor()->getParent();
65}
66
70
71} // namespace clang::tidy::fuchsia
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
TemporaryObjectsCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
AST_MATCHER_P(Stmt, isStatementIdenticalToBoundNode, std::string, ID)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
void diagDeprecatedCheckAlias(ClangTidyCheck &Check, const ClangTidyContext &Context, StringRef DeprecatedName, StringRef CanonicalName)
Emits a configuration diagnostic when a deprecated check alias is enabled and the canonical check nam...
Definition CheckUtils.h:10
llvm::StringMap< ClangTidyValue > OptionMap