clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

StringReferenceMemberCheck.cpp
Go to the documentation of this file.
1 //===--- StringReferenceMemberCheck.cpp - clang-tidy ------------*- C++ -*-===//
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 "StringReferenceMemberCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14
15 using namespace clang::ast_matchers;
16
17 namespace clang {
18 namespace tidy {
19 namespace google {
20 namespace runtime {
21
22 void StringReferenceMemberCheck::registerMatchers(
23 ast_matchers::MatchFinder *Finder) {
24 // Only register the matchers for C++; the functionality currently does not
25 // provide any benefit to other languages, despite being benign.
26 if (!getLangOpts().CPlusPlus)
27 return;
28
29 // Look for const references to std::string or ::string.
30 auto String = anyOf(recordDecl(hasName("::std::basic_string")),
31 recordDecl(hasName("::string")));
32 auto ConstString = qualType(isConstQualified(), hasDeclaration(String));
33
34 // Ignore members in template instantiations.
35 Finder->addMatcher(fieldDecl(hasType(references(ConstString)),
36 unless(isInstantiated())).bind("member"),
37 this);
38 }
39
40 void
41 StringReferenceMemberCheck::check(const MatchFinder::MatchResult &Result) {
42 const auto *Member = Result.Nodes.getNodeAs<FieldDecl>("member");
43 diag(Member->getLocStart(), "const string& members are dangerous; it is much "
44 "better to use alternatives, such as pointers or "
45 "simple constants");
46 }
47
48 } // namespace runtime
49 } // namespace google
50 } // namespace tidy
51 } // namespace clang
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
StringReferenceMemberCheck.h
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121