clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
19 void SizeofContainerCheck::registerMatchers(MatchFinder *Finder) {
20 Finder->addMatcher(
21 expr(unless(isInTemplateInstantiation()),
22 expr(sizeOfExpr(has(
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 anyOf(hasOperatorName("/"), hasOperatorName("%")),
32 hasLHS(ignoringParenCasts(sizeOfExpr(expr()))),
33 hasRHS(ignoringParenCasts(equalsBoundNode("sizeof"))))))),
34 this);
35 }
36
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->getLocStart(), "sizeof() doesn't return the size of the "
43 "container; did you mean .size()?");
44 }
45
46 } // namespace tidy
47 } // namespace clang
48
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
SizeofContainerCheck.h
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121