clang-tools  16.0.0git
AvoidConstOrRefDataMembersCheck.cpp
Go to the documentation of this file.
1 //===--- AvoidConstOrRefDataMembersCheck.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 "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace cppcoreguidelines {
18 namespace {
19 
20 AST_MATCHER(FieldDecl, isMemberOfLambda) {
21  return Node.getParent()->isLambda();
22 }
23 
24 } // namespace
25 
26 void AvoidConstOrRefDataMembersCheck::registerMatchers(MatchFinder *Finder) {
27  Finder->addMatcher(fieldDecl(unless(isMemberOfLambda()),
28  hasType(hasCanonicalType(referenceType())))
29  .bind("ref"),
30  this);
31  Finder->addMatcher(fieldDecl(unless(isMemberOfLambda()),
32  hasType(qualType(isConstQualified())))
33  .bind("const"),
34  this);
35 }
36 
38  const MatchFinder::MatchResult &Result) {
39  if (const auto *MatchedDecl = Result.Nodes.getNodeAs<FieldDecl>("ref"))
40  diag(MatchedDecl->getLocation(), "member %0 of type %1 is a reference")
41  << MatchedDecl << MatchedDecl->getType();
42  if (const auto *MatchedDecl = Result.Nodes.getNodeAs<FieldDecl>("const"))
43  diag(MatchedDecl->getLocation(), "member %0 of type %1 is const qualified")
44  << MatchedDecl << MatchedDecl->getType();
45 }
46 
47 } // namespace cppcoreguidelines
48 } // namespace tidy
49 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
Definition: InfiniteLoopCheck.cpp:24
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::check
bool check(llvm::StringRef File, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts)
Definition: Check.cpp:418
AvoidConstOrRefDataMembersCheck.h