clang-tools 6.0.0

SizeofContainerCheck.cpp
Go to the documentation of this file.
1 //===--- SizeofContainerCheck.cpp - clang-tidy-----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "SizeofContainerCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13
14 using namespace clang::ast_matchers;
15
16 namespace clang {
17 namespace tidy {
18 namespace misc {
19
20 void SizeofContainerCheck::registerMatchers(MatchFinder *Finder) {
21 Finder->addMatcher(
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(),
28 isConst())))))))))))
29 .bind("sizeof"),
30 // Ignore ARRAYSIZE(<array of containers>) pattern.
31 unless(hasAncestor(binaryOperator(
32 anyOf(hasOperatorName("/"), hasOperatorName("%")),
33 hasLHS(ignoringParenCasts(sizeOfExpr(expr()))),
34 hasRHS(ignoringParenCasts(equalsBoundNode("sizeof"))))))),
35 this);
36 }
37
38 void SizeofContainerCheck::check(const MatchFinder::MatchResult &Result) {
39 const auto *SizeOf =
40 Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>("sizeof");
41
42 auto Diag =
43 diag(SizeOf->getLocStart(), "sizeof() doesn't return the size of the "
44 "container; did you mean .size()?");
45 }
46
47 } // namespace misc
48 } // namespace tidy
49 } // namespace clang
clang::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
SizeofContainerCheck.h
clang
Definition: AndroidTidyModule.cpp:28