1 //===--- SizeofContainerCheck.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 
9 #include "SizeofContainerCheck.h"
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 bugprone {
18 
registerMatchers(MatchFinder * Finder)19 void SizeofContainerCheck::registerMatchers(MatchFinder *Finder) {
20   Finder->addMatcher(
21       expr(unless(isInTemplateInstantiation()),
22            expr(sizeOfExpr(has(ignoringParenImpCasts(
23                     expr(hasType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
24                         matchesName("^(::std::|::string)"),
25                         unless(matchesName("^::std::(bitset|array)$")),
26                         hasMethod(cxxMethodDecl(hasName("size"), isPublic(),
27                                                 isConst())))))))))))
28                .bind("sizeof"),
29            // Ignore ARRAYSIZE(<array of containers>) pattern.
30            unless(hasAncestor(binaryOperator(
31                hasAnyOperatorName("/", "%"),
32                hasLHS(ignoringParenCasts(sizeOfExpr(expr()))),
33                hasRHS(ignoringParenCasts(equalsBoundNode("sizeof"))))))),
34       this);
35 }
36 
check(const MatchFinder::MatchResult & Result)37 void SizeofContainerCheck::check(const MatchFinder::MatchResult &Result) {
38   const auto *SizeOf =
39       Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>("sizeof");
40 
41   auto Diag =
42       diag(SizeOf->getBeginLoc(), "sizeof() doesn't return the size of the "
43                                   "container; did you mean .size()?");
44 }
45 
46 } // namespace bugprone
47 } // namespace tidy
48 } // namespace clang
49