clang-tools  11.0.0
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 
17 using namespace clang::ast_matchers;
18 
19 namespace clang {
20 namespace tidy {
21 namespace zircon {
22 
23 AST_MATCHER_P(CXXRecordDecl, matchesAnyName, ArrayRef<std::string>, Names) {
24  std::string QualifiedName = Node.getQualifiedNameAsString();
25  return llvm::any_of(Names,
26  [&](StringRef Name) { return QualifiedName == Name; });
27 }
28 
29 void 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(ast_type_traits::TK_AsIs,
40  cxxConstructExpr(hasParent(cxxFunctionalCastExpr()),
41  hasDeclaration(cxxConstructorDecl(hasParent(
42  cxxRecordDecl(matchesAnyName(Names))))))
43  .bind("temps")),
44  this);
45 }
46 
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 
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
TemporaryObjectsCheck.h
clang::tidy::zircon::AST_MATCHER_P
AST_MATCHER_P(CXXRecordDecl, matchesAnyName, ArrayRef< std::string >, Names)
Definition: TemporaryObjectsCheck.cpp:23
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::utils::options::serializeStringList
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
Definition: OptionsUtils.cpp:30
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:111