1 //===--- TemporaryObjectsCheck.cpp - clang-tidy----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "TemporaryObjectsCheck.h"
11 #include "../utils/OptionsUtils.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include <string>
17 
18 using namespace clang::ast_matchers;
19 
20 namespace clang {
21 namespace tidy {
22 namespace zircon {
23 
AST_MATCHER_P(CXXRecordDecl,matchesAnyName,ArrayRef<std::string>,Names)24 AST_MATCHER_P(CXXRecordDecl, matchesAnyName, ArrayRef<std::string>, Names) {
25   std::string QualifiedName = Node.getQualifiedNameAsString();
26   return llvm::any_of(Names,
27                       [&](StringRef Name) { return QualifiedName == Name; });
28 }
29 
registerMatchers(MatchFinder * Finder)30 void TemporaryObjectsCheck::registerMatchers(MatchFinder *Finder) {
31   // Matcher for default constructors.
32   Finder->addMatcher(
33       cxxTemporaryObjectExpr(hasDeclaration(cxxConstructorDecl(hasParent(
34                                  cxxRecordDecl(matchesAnyName(Names))))))
35           .bind("temps"),
36       this);
37 
38   // Matcher for user-defined constructors.
39   Finder->addMatcher(
40       cxxConstructExpr(allOf(hasParent(cxxFunctionalCastExpr()),
41                              hasDeclaration(cxxConstructorDecl(hasParent(
42                                  cxxRecordDecl(matchesAnyName(Names)))))))
43           .bind("temps"),
44       this);
45 }
46 
check(const MatchFinder::MatchResult & Result)47 void 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 
storeOptions(ClangTidyOptions::OptionMap & Opts)54 void TemporaryObjectsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
55   Options.store(Opts, "Names", utils::options::serializeStringList(Names));
56 }
57 
58 } // namespace zircon
59 } // namespace tidy
60 } // namespace clang
61