clang-tools  8.0.0
TemporaryObjectsCheck.cpp
Go to the documentation of this file.
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 
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 
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(hasParent(cxxFunctionalCastExpr()),
41  hasDeclaration(cxxConstructorDecl(
42  hasParent(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
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
const Decl * D
Definition: XRefs.cpp:79
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
AST_MATCHER_P(FunctionDecl, throws, internal::Matcher< Type >, InnerMatcher)