clang-tools 22.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
19
20namespace {
21
22AST_MATCHER_P(CXXRecordDecl, matchesAnyName, ArrayRef<StringRef>, Names) {
23 std::string QualifiedName = Node.getQualifiedNameAsString();
24 return llvm::is_contained(Names, QualifiedName);
25}
26
27} // namespace
28
29void TemporaryObjectsCheck::registerMatchers(MatchFinder *Finder) {
30 // Matcher for default constructors.
31 Finder->addMatcher(
32 cxxTemporaryObjectExpr(hasDeclaration(cxxConstructorDecl(hasParent(
33 cxxRecordDecl(matchesAnyName(Names))))))
34 .bind("temps"),
35 this);
36
37 // Matcher for user-defined constructors.
38 Finder->addMatcher(
39 traverse(TK_AsIs,
40 cxxConstructExpr(hasParent(cxxFunctionalCastExpr()),
41 hasDeclaration(cxxConstructorDecl(hasParent(
42 cxxRecordDecl(matchesAnyName(Names))))))
43 .bind("temps")),
44 this);
45}
46
47void TemporaryObjectsCheck::check(const MatchFinder::MatchResult &Result) {
48 if (const auto *D = Result.Nodes.getNodeAs<CXXConstructExpr>("temps"))
49 diag(D->getLocation(),
50 "creating a temporary object of type %q0 is prohibited")
51 << D->getConstructor()->getParent();
52}
53
57
58} // namespace clang::tidy::zircon
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap