-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSizeofCheck.cpp
More file actions
49 lines (40 loc) · 1.39 KB
/
SizeofCheck.cpp
File metadata and controls
49 lines (40 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//===--- SizeofCheck.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 "SizeofCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace aliceO2 {
void SizeofCheck::registerMatchers(MatchFinder *Finder) {
// matches sizeof + alignof
Finder->addMatcher(unaryExprOrTypeTraitExpr().bind("unaryexp"), this);
}
void SizeofCheck::check(const MatchFinder::MatchResult &Result) {
const auto *Expr = Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>("unaryexp");
const auto Kind = Expr->getKind();
if (Kind != UETT_SizeOf){
return;
}
// is argument of sizeof a Type?
if (Expr->isArgumentType()) {
diag(Expr->getBeginLoc(), "consider using sizeof() on instance instead on direct type");
return;
}
// we found a correct expression; check if it uses parens
if (dyn_cast<ParenExpr>(Expr->getArgumentExpr())) {
return;
}
// no parens -> issue warning
diag(Expr->getBeginLoc(), "consider using parens () for arguments to sizeof");
}
} // namespace aliceO2
} // namespace tidy
} // namespace clang