1//===--- TemporaryObjectsCheck.cpp - clang-tidy----------------------------===//
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
9#include "TemporaryObjectsCheck.h"
10#include "../utils/OptionsUtils.h"
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::zircon {
19
20namespace {
21
22AST_MATCHER_P(CXXRecordDecl, matchesAnyName, ArrayRef<StringRef>, Names) {
23 std::string QualifiedName = Node.getQualifiedNameAsString();
24 return llvm::is_contained(Range: Names, Element: QualifiedName);
25}
26
27} // namespace
28
29void TemporaryObjectsCheck::registerMatchers(MatchFinder *Finder) {
30 // Matcher for default constructors.
31 Finder->addMatcher(
32 NodeMatch: cxxTemporaryObjectExpr(hasDeclaration(InnerMatcher: cxxConstructorDecl(hasParent(
33 cxxRecordDecl(matchesAnyName(Names))))))
34 .bind(ID: "temps"),
35 Action: this);
36
37 // Matcher for user-defined constructors.
38 Finder->addMatcher(
39 NodeMatch: traverse(TK: TK_AsIs,
40 InnerMatcher: cxxConstructExpr(hasParent(cxxFunctionalCastExpr()),
41 hasDeclaration(InnerMatcher: cxxConstructorDecl(hasParent(
42 cxxRecordDecl(matchesAnyName(Names))))))
43 .bind(ID: "temps")),
44 Action: this);
45}
46
47void TemporaryObjectsCheck::check(const MatchFinder::MatchResult &Result) {
48 if (const auto *D = Result.Nodes.getNodeAs<CXXConstructExpr>(ID: "temps"))
49 diag(Loc: D->getLocation(),
50 Description: "creating a temporary object of type %q0 is prohibited")
51 << D->getConstructor()->getParent();
52}
53
54void TemporaryObjectsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
55 Options.store(Options&: Opts, LocalName: "Names", Value: utils::options::serializeStringList(Strings: Names));
56}
57
58} // namespace clang::tidy::zircon
59

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of clang-tools-extra/clang-tidy/zircon/TemporaryObjectsCheck.cpp