Skip to content

Commit 9506d75

Browse files
committed
Unit testing; Use Regex in MemberNamesCheck
1 parent c61cfd8 commit 9506d75

File tree

6 files changed

+61
-13
lines changed

6 files changed

+61
-13
lines changed

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
cmake_minimum_required(VERSION 3.4.3)
2+
enable_testing()
3+
4+
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
5+
# require at least gcc 4.9 !!
6+
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
7+
message(FATAL_ERROR "GCC version must be at least 4.9!")
8+
endif()
9+
endif()
210

311

412
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
@@ -39,4 +47,7 @@ add_subdirectory(aliceO2)
3947
add_subdirectory(reporting)
4048

4149

50+
# for testing
51+
add_subdirectory(test)
52+
4253
endif()

aliceO2/MemberNamesCheck.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "clang/AST/ASTContext.h"
1212
#include "clang/ASTMatchers/ASTMatchFinder.h"
1313
#include <iostream>
14+
#include <regex>
1415

1516
using namespace clang::ast_matchers;
1617

@@ -24,17 +25,20 @@ void MemberNamesCheck::registerMatchers(MatchFinder *Finder) {
2425

2526
void MemberNamesCheck::check(const MatchFinder::MatchResult &Result) {
2627
const auto *MatchedDecl = Result.Nodes.getNodeAs<FieldDecl>("field_decl1");
27-
if(MatchedDecl) {
28+
if (MatchedDecl) {
2829
// check that we are inside the AliceO2 namespace to exlude system stuff
29-
if ( MatchedDecl->getQualifiedNameAsString().find("AliceO2::") != 0 )
30-
return;
30+
// FIXME: needs to be configurable
31+
if (MatchedDecl->getQualifiedNameAsString().find("AliceO2::") != 0)
32+
return;
3133

32-
if (MatchedDecl->getName().startswith("m")){
33-
return;
34+
if (std::regex_match(MatchedDecl->getNameAsString(), Regex)) {
35+
return;
3436
}
35-
diag(MatchedDecl->getLocation(), "field declaration %0 does not match naming rule", DiagnosticIDs::Error)
36-
<< MatchedDecl;
37-
// << FixItHint::CreateInsertion(MatchedDecl->getLocation(), "f");
37+
38+
diag(MatchedDecl->getLocation(),
39+
"field declaration %0 does not match naming rule",
40+
DiagnosticIDs::Error)
41+
<< MatchedDecl;
3842
}
3943
}
4044

aliceO2/MemberNamesCheck.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,29 @@
1111
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ALICEO2_MEMBER_NAMES_H
1212

1313
#include "../ClangTidy.h"
14+
#include <regex>
1415

1516
namespace clang {
1617
namespace tidy {
1718
namespace aliceO2 {
1819

19-
/// FIXME: Write a short description.
20+
/// Checking the class member naming convention
2021
///
21-
/// For the user-facing documentation see:
22-
/// http://clang.llvm.org/extra/clang-tidy/checks/aliceO2-member-names.html
2322
class MemberNamesCheck : public ClangTidyCheck {
23+
private:
24+
const std::string Pattern; // the regular expression string
25+
const std::regex Regex; // the regular expression class
2426
public:
2527
MemberNamesCheck(StringRef Name, ClangTidyContext *Context)
26-
: ClangTidyCheck(Name, Context) {}
28+
: ClangTidyCheck(Name, Context), Pattern(Options.get("Pattern","m[A-Z].*")), Regex(Pattern)
29+
{}
30+
2731
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2832
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
33+
34+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override {
35+
Options.store(Opts, "Pattern", Pattern);
36+
}
2937
};
3038

3139
} // namespace aliceO2

test/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# testing if regex works correctly (has problem with gcc < 4.9)
2+
add_executable(testregex testregex.cpp)
3+
add_test(testregexcorrect testregex)
4+
5+
6+
# correctness testing of checks

test/test1.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ namespace AliceO2 {
1212

1313
class B {
1414
public:
15-
void foo();
15+
virtual void foo();
16+
};
17+
18+
class D : public B {
19+
public:
20+
virtual void foo();
1621
};
1722

1823
void consumer(A &a, B &b, C &c){

test/testregex.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <regex>
2+
#ifdef NDEBUG
3+
#undef NDEBUG
4+
#endif
5+
#include <cassert>
6+
7+
// testing correct compiler support for regular expressions
8+
// (For example: although the code compiles fine with gcc4.8; it does not
9+
// execute)
10+
int main() {
11+
assert(std::regex_match("mGood", std::regex("m[A-Z][a-z]+")));
12+
assert(std::regex_match("mGood", std::regex("m[A-Z].*")));
13+
return 0;
14+
}

0 commit comments

Comments
 (0)