brintos

brintos / llvm-project-archived public Read only

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