11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 using namespace clang::ast_matchers;
20 void SizeofContainerCheck::registerMatchers(MatchFinder *
Finder) {
22 expr(unless(isInTemplateInstantiation()),
23 expr(sizeOfExpr(has(ignoringParenImpCasts(
24 expr(hasType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
25 matchesName(
"^(::std::|::string)"),
26 unless(matchesName(
"^::std::(bitset|array)$")),
27 hasMethod(cxxMethodDecl(hasName(
"size"), isPublic(),
31 unless(hasAncestor(binaryOperator(
32 anyOf(hasOperatorName(
"/"), hasOperatorName(
"%")),
33 hasLHS(ignoringParenCasts(sizeOfExpr(expr()))),
34 hasRHS(ignoringParenCasts(equalsBoundNode(
"sizeof"))))))),
38 void SizeofContainerCheck::check(
const MatchFinder::MatchResult &Result) {
40 Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>(
"sizeof");
43 diag(SizeOf->getLocStart(),
"sizeof() doesn't return the size of the "
44 "container; did you mean .size()?");
std::unique_ptr< ast_matchers::MatchFinder > Finder