clang-tools 20.0.0git
TemporaryObjectsCheck.cpp
Go to the documentation of this file.
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
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 "llvm/ADT/SmallVector.h"
15#include <string>
16
17using namespace clang::ast_matchers;
18
20
21AST_MATCHER_P(CXXRecordDecl, matchesAnyName, ArrayRef<StringRef>, Names) {
22 std::string QualifiedName = Node.getQualifiedNameAsString();
23 return llvm::is_contained(Names, QualifiedName);
24}
25
26void TemporaryObjectsCheck::registerMatchers(MatchFinder *Finder) {
27 // Matcher for default constructors.
28 Finder->addMatcher(
29 cxxTemporaryObjectExpr(hasDeclaration(cxxConstructorDecl(hasParent(
30 cxxRecordDecl(matchesAnyName(Names))))))
31 .bind("temps"),
32 this);
33
34 // Matcher for user-defined constructors.
35 Finder->addMatcher(
36 traverse(TK_AsIs,
37 cxxConstructExpr(hasParent(cxxFunctionalCastExpr()),
38 hasDeclaration(cxxConstructorDecl(hasParent(
39 cxxRecordDecl(matchesAnyName(Names))))))
40 .bind("temps")),
41 this);
42}
43
44void TemporaryObjectsCheck::check(const MatchFinder::MatchResult &Result) {
45 if (const auto *D = Result.Nodes.getNodeAs<CXXConstructExpr>("temps"))
46 diag(D->getLocation(),
47 "creating a temporary object of type %q0 is prohibited")
48 << D->getConstructor()->getParent();
49}
50
53}
54
55} // namespace clang::tidy::zircon
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
AST_MATCHER_P(CXXRecordDecl, matchesAnyName, ArrayRef< StringRef >, Names)
llvm::StringMap< ClangTidyValue > OptionMap