File tree Expand file tree Collapse file tree 4 files changed +88
-0
lines changed
Expand file tree Collapse file tree 4 files changed +88
-0
lines changed Original file line number Diff line number Diff line change 1111#include " ../ClangTidyModule.h"
1212#include " ../ClangTidyModuleRegistry.h"
1313#include " MemberNamesCheck.h"
14+ #include " SizeofCheck.h"
1415
1516namespace clang {
1617namespace tidy {
@@ -20,6 +21,8 @@ class AliceO2Module : public ClangTidyModule {
2021public:
2122 void addCheckFactories (ClangTidyCheckFactories &CheckFactories) override {
2223 CheckFactories.registerCheck <MemberNamesCheck>(" aliceO2-member-name" );
24+ CheckFactories.registerCheck <SizeofCheck>(
25+ " aliceO2-SizeOf" );
2326 }
2427};
2528
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support)
33add_clang_library(clangTidyAliceO2Module
44 AliceO2TidyModule.cpp
55 MemberNamesCheck.cpp
6+ SizeofCheck.cpp
67
78 LINK_LIBS
89 clangAST
Original file line number Diff line number Diff line change 1+ // ===--- SizeofCheck.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 " SizeofCheck.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 aliceO2 {
19+
20+ void SizeofCheck::registerMatchers (MatchFinder *Finder) {
21+ // matches sizeof + alignof
22+ Finder->addMatcher (unaryExprOrTypeTraitExpr ().bind (" unaryexp" ), this );
23+ }
24+
25+ void SizeofCheck::check (const MatchFinder::MatchResult &Result) {
26+ const auto *Expr = Result.Nodes .getNodeAs <UnaryExprOrTypeTraitExpr>(" unaryexp" );
27+ const auto Kind = Expr->getKind ();
28+ if (Kind != UETT_SizeOf){
29+ return ;
30+ }
31+
32+ // is argument of sizeof a Type?
33+ if (Expr->isArgumentType ()) {
34+ diag (Expr->getLocStart (), " consider using sizeof() on instance instead on direct type" );
35+ return ;
36+ }
37+
38+ // we found a correct expression; check if it uses parens
39+ if (dyn_cast<ParenExpr>(Expr->getArgumentExpr ())) {
40+ return ;
41+ }
42+
43+ // no parens -> issue warning
44+ diag (Expr->getLocStart (), " consider using parens () for arguments to sizeof" );
45+ }
46+
47+ } // namespace aliceO2
48+ } // namespace tidy
49+ } // namespace clang
Original file line number Diff line number Diff line change 1+ // ===--- SizeofCheck.h - clang-tidy------------------------------*- C++ -*-===//
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+ #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALICEO2_SIZEOF_H
11+ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALICEO2_SIZEOF_H
12+
13+ #include " ../ClangTidy.h"
14+
15+ namespace clang {
16+ namespace tidy {
17+ namespace aliceO2 {
18+
19+ // / FIXME: Write a short description.
20+ // /
21+ // / For the user-facing documentation see:
22+ // / http://clang.llvm.org/extra/clang-tidy/checks/aliceO2-SizeOf.html
23+ class SizeofCheck : public ClangTidyCheck {
24+ public:
25+ SizeofCheck (StringRef Name, ClangTidyContext *Context)
26+ : ClangTidyCheck(Name, Context) {}
27+ void registerMatchers (ast_matchers::MatchFinder *Finder) override ;
28+ void check (const ast_matchers::MatchFinder::MatchResult &Result) override ;
29+ };
30+
31+ } // namespace aliceO2
32+ } // namespace tidy
33+ } // namespace clang
34+
35+ #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALICEO2_SIZEOF_H
You can’t perform that action at this time.
0 commit comments