11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
15 using namespace clang::ast_matchers;
22 bool isPOSIXTypeName(StringRef ClassName) {
23 static const char *
const TypeNames[] = {
29 return std::binary_search(std::begin(TypeNames), std::end(TypeNames),
33 bool isFILETypeName(StringRef ClassName) {
34 static const char *
const TypeNames[] = {
39 return std::binary_search(std::begin(TypeNames), std::end(TypeNames),
44 return isFILETypeName(Node.getQualifiedNameAsString());
48 return isPOSIXTypeName(Node.getQualifiedNameAsString());
53 void NonCopyableObjectsCheck::registerMatchers(MatchFinder *
Finder) {
64 auto BadFILEType = hasType(namedDecl(isFILEType()).bind(
"type_decl"));
65 auto BadPOSIXType = hasType(namedDecl(isPOSIXType()).bind(
"type_decl"));
66 auto BadEitherType = anyOf(BadFILEType, BadPOSIXType);
69 namedDecl(anyOf(varDecl(BadFILEType), fieldDecl(BadFILEType)))
72 Finder->addMatcher(parmVarDecl(BadPOSIXType).bind(
"decl"),
this);
74 expr(unaryOperator(hasOperatorName(
"*"), BadEitherType)).bind(
"expr"),
78 void NonCopyableObjectsCheck::check(
const MatchFinder::MatchResult &
Result) {
79 const auto *D = Result.Nodes.getNodeAs<NamedDecl>(
"decl");
80 const auto *BD = Result.Nodes.getNodeAs<NamedDecl>(
"type_decl");
81 const auto *E = Result.Nodes.getNodeAs<Expr>(
"expr");
84 diag(D->getLocation(),
"'%0' declared as type '%1', which is unsafe to copy"
85 "; did you mean '%1 *'?")
86 << D->getName() << BD->getName();
89 "expression has opaque data structure type '%0'; type should only be "
90 "used as a pointer and not dereferenced")
std::unique_ptr< ast_matchers::MatchFinder > Finder
AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt)