clang-tools 24.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
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "llvm/ADT/STLExtras.h"
14#include <string>
15
16using namespace clang::ast_matchers;
17
18namespace clang::tidy::fuchsia {
19
20namespace {
21
22AST_MATCHER_P(CXXRecordDecl, matchesAnyName, ArrayRef<StringRef>, Names) {
23 const std::string QualifiedName = Node.getQualifiedNameAsString();
24 return llvm::is_contained(Names, QualifiedName);
25}
26
27} // namespace
28
30 ClangTidyContext *Context)
31 : ClangTidyCheck(Name, Context),
32 Names(utils::options::parseStringList(Options.get("Names", ""))) {}
33
34void TemporaryObjectsCheck::registerMatchers(MatchFinder *Finder) {
35 // Matcher for default constructors.
36 Finder->addMatcher(
37 cxxTemporaryObjectExpr(hasDeclaration(cxxConstructorDecl(hasParent(
38 cxxRecordDecl(matchesAnyName(Names))))))
39 .bind("temps"),
40 this);
41
42 // Matcher for user-defined constructors.
43 Finder->addMatcher(
44 traverse(TK_AsIs,
45 cxxConstructExpr(hasParent(cxxFunctionalCastExpr()),
46 hasDeclaration(cxxConstructorDecl(hasParent(
47 cxxRecordDecl(matchesAnyName(Names))))))
48 .bind("temps")),
49 this);
50}
51
52void TemporaryObjectsCheck::check(const MatchFinder::MatchResult &Result) {
53 if (const auto *D = Result.Nodes.getNodeAs<CXXConstructExpr>("temps"))
54 diag(D->getLocation(),
55 "creating a temporary object of type %q0 is prohibited")
56 << D->getConstructor()->getParent();
57}
58
62
63} // 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.
llvm::StringMap< ClangTidyValue > OptionMap