Skip to content

Commit fa86a4d

Browse files
committed
Added a Sizeof check (see Alice coding guidelines)
1 parent c2c0ed8 commit fa86a4d

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

aliceO2/AliceO2TidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "../ClangTidyModule.h"
1212
#include "../ClangTidyModuleRegistry.h"
1313
#include "MemberNamesCheck.h"
14+
#include "SizeofCheck.h"
1415

1516
namespace clang {
1617
namespace tidy {
@@ -20,6 +21,8 @@ class AliceO2Module : public ClangTidyModule {
2021
public:
2122
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
2223
CheckFactories.registerCheck<MemberNamesCheck>("aliceO2-member-name");
24+
CheckFactories.registerCheck<SizeofCheck>(
25+
"aliceO2-SizeOf");
2326
}
2427
};
2528

aliceO2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(LLVM_LINK_COMPONENTS support)
33
add_clang_library(clangTidyAliceO2Module
44
AliceO2TidyModule.cpp
55
MemberNamesCheck.cpp
6+
SizeofCheck.cpp
67

78
LINK_LIBS
89
clangAST

aliceO2/SizeofCheck.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

aliceO2/SizeofCheck.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

0 commit comments

Comments
 (0)