2014-07-16 18:00:14 +08:00
|
|
|
//===--- StringReferenceMemberCheck.cpp - clang-tidy ------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "StringReferenceMemberCheck.h"
|
2015-01-14 19:24:38 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2014-07-16 18:00:14 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2015-03-05 21:46:14 +08:00
|
|
|
namespace google {
|
2014-07-16 18:00:14 +08:00
|
|
|
namespace runtime {
|
|
|
|
|
|
|
|
void StringReferenceMemberCheck::registerMatchers(
|
|
|
|
ast_matchers::MatchFinder *Finder) {
|
2015-09-03 00:20:42 +08:00
|
|
|
// Only register the matchers for C++; the functionality currently does not
|
|
|
|
// provide any benefit to other languages, despite being benign.
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
2014-07-16 18:00:14 +08:00
|
|
|
// Look for const references to std::string or ::string.
|
2017-08-02 21:13:11 +08:00
|
|
|
auto String = anyOf(namedDecl(hasName("::std::string")),
|
|
|
|
namedDecl(hasName("::string")));
|
2014-07-16 18:00:14 +08:00
|
|
|
auto ConstString = qualType(isConstQualified(), hasDeclaration(String));
|
|
|
|
|
|
|
|
// Ignore members in template instantiations.
|
2016-11-08 15:50:19 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
fieldDecl(hasType(references(ConstString)), unless(isInstantiated()))
|
|
|
|
.bind("member"),
|
|
|
|
this);
|
2014-07-16 18:00:14 +08:00
|
|
|
}
|
|
|
|
|
2016-11-08 15:50:19 +08:00
|
|
|
void StringReferenceMemberCheck::check(const MatchFinder::MatchResult &Result) {
|
2014-07-16 18:00:14 +08:00
|
|
|
const auto *Member = Result.Nodes.getNodeAs<FieldDecl>("member");
|
2016-01-08 23:21:40 +08:00
|
|
|
diag(Member->getLocStart(), "const string& members are dangerous; it is much "
|
2014-07-16 18:00:14 +08:00
|
|
|
"better to use alternatives, such as pointers or "
|
2016-01-08 23:21:40 +08:00
|
|
|
"simple constants");
|
2014-07-16 18:00:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace runtime
|
2015-03-05 21:46:14 +08:00
|
|
|
} // namespace google
|
2014-07-16 18:00:14 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|