teak-llvm/clang-tools-extra/clang-tidy/misc/SizeofContainerCheck.cpp
Mandeep Singh Grang 7c7ea7d0ae [clang-tools-extra] Format sources with clang-format. NFC.
Summary:
Ran clang-format on all .c/.cpp/.h files in clang-tools-extra.
Excluded the test, unittests, clang-reorder-fields, include-fixer, modularize and pptrace directories.

Reviewers: klimek, alexfh

Subscribers: nemanjai

Tags: #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D26329

llvm-svn: 286221
2016-11-08 07:50:19 +00:00

50 lines
1.8 KiB
C++

//===--- SizeofContainerCheck.cpp - clang-tidy-----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "SizeofContainerCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace misc {
void SizeofContainerCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
expr(unless(isInTemplateInstantiation()),
expr(sizeOfExpr(has(ignoringParenImpCasts(
expr(hasType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
matchesName("^(::std::|::string)"),
unless(matchesName("^::std::(bitset|array)$")),
hasMethod(cxxMethodDecl(hasName("size"), isPublic(),
isConst())))))))))))
.bind("sizeof"),
// Ignore ARRAYSIZE(<array of containers>) pattern.
unless(hasAncestor(binaryOperator(
anyOf(hasOperatorName("/"), hasOperatorName("%")),
hasLHS(ignoringParenCasts(sizeOfExpr(expr()))),
hasRHS(ignoringParenCasts(equalsBoundNode("sizeof"))))))),
this);
}
void SizeofContainerCheck::check(const MatchFinder::MatchResult &Result) {
const auto *SizeOf =
Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>("sizeof");
auto Diag =
diag(SizeOf->getLocStart(), "sizeof() doesn't return the size of the "
"container; did you mean .size()?");
}
} // namespace misc
} // namespace tidy
} // namespace clang