xref: /llvm-project/clang-tools-extra/clang-tidy/zircon/TemporaryObjectsCheck.cpp (revision 7d2ea6c422d3f5712b7253407005e1a465a76946)
1b6f7c934SJulie Hockett //===--- TemporaryObjectsCheck.cpp - clang-tidy----------------------------===//
2b6f7c934SJulie Hockett //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b6f7c934SJulie Hockett //
7b6f7c934SJulie Hockett //===----------------------------------------------------------------------===//
8b6f7c934SJulie Hockett 
9b6f7c934SJulie Hockett #include "TemporaryObjectsCheck.h"
10b6f7c934SJulie Hockett #include "../utils/OptionsUtils.h"
11b6f7c934SJulie Hockett #include "clang/AST/ASTContext.h"
12b6f7c934SJulie Hockett #include "clang/ASTMatchers/ASTMatchFinder.h"
13b6f7c934SJulie Hockett #include "llvm/ADT/STLExtras.h"
14b6f7c934SJulie Hockett #include "llvm/ADT/SmallVector.h"
15b6f7c934SJulie Hockett #include <string>
16b6f7c934SJulie Hockett 
17b6f7c934SJulie Hockett using namespace clang::ast_matchers;
18b6f7c934SJulie Hockett 
19*7d2ea6c4SCarlos Galvez namespace clang::tidy::zircon {
20b6f7c934SJulie Hockett 
AST_MATCHER_P(CXXRecordDecl,matchesAnyName,ArrayRef<StringRef>,Names)2112cb5405SNathan James AST_MATCHER_P(CXXRecordDecl, matchesAnyName, ArrayRef<StringRef>, Names) {
22b6f7c934SJulie Hockett   std::string QualifiedName = Node.getQualifiedNameAsString();
2333b93044SKazu Hirata   return llvm::is_contained(Names, QualifiedName);
24b6f7c934SJulie Hockett }
25b6f7c934SJulie Hockett 
registerMatchers(MatchFinder * Finder)26b6f7c934SJulie Hockett void TemporaryObjectsCheck::registerMatchers(MatchFinder *Finder) {
27b6f7c934SJulie Hockett   // Matcher for default constructors.
28b6f7c934SJulie Hockett   Finder->addMatcher(
29b6f7c934SJulie Hockett       cxxTemporaryObjectExpr(hasDeclaration(cxxConstructorDecl(hasParent(
30b6f7c934SJulie Hockett                                  cxxRecordDecl(matchesAnyName(Names))))))
31b6f7c934SJulie Hockett           .bind("temps"),
32b6f7c934SJulie Hockett       this);
33b6f7c934SJulie Hockett 
34b6f7c934SJulie Hockett   // Matcher for user-defined constructors.
35b6f7c934SJulie Hockett   Finder->addMatcher(
36027899daSAlexander Kornienko       traverse(TK_AsIs,
37976e0c07SAlexander Kornienko                cxxConstructExpr(hasParent(cxxFunctionalCastExpr()),
38a72307c3SStephen Kelly                                 hasDeclaration(cxxConstructorDecl(hasParent(
39a72307c3SStephen Kelly                                     cxxRecordDecl(matchesAnyName(Names))))))
40a72307c3SStephen Kelly                    .bind("temps")),
41b6f7c934SJulie Hockett       this);
42b6f7c934SJulie Hockett }
43b6f7c934SJulie Hockett 
check(const MatchFinder::MatchResult & Result)44b6f7c934SJulie Hockett void TemporaryObjectsCheck::check(const MatchFinder::MatchResult &Result) {
45b6f7c934SJulie Hockett   if (const auto *D = Result.Nodes.getNodeAs<CXXConstructExpr>("temps"))
46b6f7c934SJulie Hockett     diag(D->getLocation(),
47b6f7c934SJulie Hockett          "creating a temporary object of type %q0 is prohibited")
48b6f7c934SJulie Hockett         << D->getConstructor()->getParent();
49b6f7c934SJulie Hockett }
50b6f7c934SJulie Hockett 
storeOptions(ClangTidyOptions::OptionMap & Opts)51b6f7c934SJulie Hockett void TemporaryObjectsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
52b6f7c934SJulie Hockett   Options.store(Opts, "Names", utils::options::serializeStringList(Names));
53b6f7c934SJulie Hockett }
54b6f7c934SJulie Hockett 
55*7d2ea6c4SCarlos Galvez } // namespace clang::tidy::zircon
56