Skip to content

Commit a2adf74

Browse files
ZdravkoDsawenzel
authored andcommitted
Adding handling of more use cases for namespace naming (#20)
improved way of fixing wrong namespaces
1 parent 0b7e378 commit a2adf74

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

aliceO2/NamespaceNamingCheck.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,46 @@
1313
#include <regex>
1414
#include <string>
1515
#include <ctype.h>
16-
16+
1717
using namespace clang::ast_matchers;
1818

1919
namespace clang {
2020
namespace tidy {
2121
namespace aliceO2 {
22+
23+
const std::string VALID_NAME_REGEX = "[a-z][a-z_0-9]+";
24+
const std::string VALID_PATH_REGEX = "(.*/O2/.*)|(.*/test/.*)";
2225

23-
const std::string VALID_NAME_REGEX = "([a-z]+_)*[a-z]+";
26+
bool isOutsideOfTargetScope(std::string filename)
27+
{
28+
return !std::regex_match(filename, std::regex(VALID_PATH_REGEX));
29+
}
2430

2531
void NamespaceNamingCheck::registerMatchers(MatchFinder *Finder) {
2632
const auto validNameMatch = matchesName( std::string("::") + VALID_NAME_REGEX + "$" );
2733

2834
// matches namespace declarations that have invalid name
29-
Finder->addMatcher(namespaceDecl( unless( validNameMatch ) ).bind("namespace-decl"), this);
35+
Finder->addMatcher(namespaceDecl(allOf(
36+
unless(validNameMatch),
37+
unless(isAnonymous())
38+
)).bind("namespace-decl"), this);
3039
// matches usage of namespace
31-
Finder->addMatcher(nestedNameSpecifierLoc(loc(nestedNameSpecifier(specifiesNamespace(
32-
unless( validNameMatch ) )))).bind("namespace-usage"), this );
40+
Finder->addMatcher(nestedNameSpecifierLoc(loc(nestedNameSpecifier(specifiesNamespace(unless(validNameMatch)
41+
)))).bind("namespace-usage"), this );
3342
// matches "using namespace" declarations
34-
Finder->addMatcher(usingDirectiveDecl().bind("using-namespace"), this);
43+
Finder->addMatcher(usingDirectiveDecl(unless(isImplicit()
44+
)).bind("using-namespace"), this);
3545
}
3646

3747
void NamespaceNamingCheck::check(const MatchFinder::MatchResult &Result) {
3848
const auto *MatchedNamespaceDecl = Result.Nodes.getNodeAs<NamespaceDecl>("namespace-decl");
3949
if( MatchedNamespaceDecl )
4050
{
51+
if( isOutsideOfTargetScope( Result.SourceManager->getFilename(MatchedNamespaceDecl->getLocation()).str() ) )
52+
{
53+
return;
54+
}
55+
4156
std::string newName(MatchedNamespaceDecl->getDeclName().getAsString());
4257

4358
fixNamespaceName(newName);
@@ -51,6 +66,10 @@ void NamespaceNamingCheck::check(const MatchFinder::MatchResult &Result) {
5166
if( MatchedNamespaceLoc )
5267
{
5368
const auto *AsNamespace = MatchedNamespaceLoc->getNestedNameSpecifier()->getAsNamespace();
69+
if( isOutsideOfTargetScope( Result.SourceManager->getFilename(AsNamespace->getLocation()).str() ) )
70+
{
71+
return;
72+
}
5473
std::string newName(AsNamespace->getDeclName().getAsString());
5574

5675
fixNamespaceName(newName);
@@ -63,6 +82,11 @@ void NamespaceNamingCheck::check(const MatchFinder::MatchResult &Result) {
6382
const auto *MatchedUsingNamespace = Result.Nodes.getNodeAs<UsingDirectiveDecl>("using-namespace");
6483
if( MatchedUsingNamespace )
6584
{
85+
if( isOutsideOfTargetScope( Result.SourceManager->getFilename(MatchedUsingNamespace->getNominatedNamespace()->getLocation()).str() ) )
86+
{
87+
return;
88+
}
89+
6690
std::string newName(MatchedUsingNamespace->getNominatedNamespace()->getDeclName().getAsString());
6791

6892
if( std::regex_match(newName, std::regex(VALID_NAME_REGEX)) )
@@ -80,9 +104,9 @@ void NamespaceNamingCheck::check(const MatchFinder::MatchResult &Result) {
80104

81105
void NamespaceNamingCheck::fixNamespaceName(std::string &name)
82106
{
83-
for(int i=0; i<name.size(); i++) {
107+
for(int i=name.size()-1; i>=0; i--) {
84108
if(isupper(name[i])) {
85-
if(i != 0 && name[i-1] != '_') {
109+
if(i != 0 && islower(name[i-1])) {
86110
name.insert(i, "_");
87111
i++;
88112
}

test/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ find_program(LLVMFILECHECK FileCheck HINTS PATH)
77
if (LLVMFILECHECK)
88

99
# testing namespace naming checker
10-
add_test(NAME aliceo2namespace COMMAND
11-
${CMAKE_SOURCE_DIR}/check_clang_tidy.py
12-
${CMAKE_SOURCE_DIR}/test/aliceO2-namespace-naming.cpp
10+
add_test(NAME aliceo2namespace COMMAND
11+
${CMAKE_SOURCE_DIR}/check_clang_tidy.py
12+
${CMAKE_SOURCE_DIR}/test/aliceO2-namespace-naming.cpp
1313
aliceO2-namespace-naming ${CMAKE_BINARY_DIR}/tool/O2codecheck)
1414

1515
else()

test/aliceO2-namespace-naming.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
// RUN: %check_clang_tidy %s aliceO2-namespace-naming %t
22

3+
namespace {}
4+
namespace{
5+
}
6+
namespace
7+
{
8+
namespace simple
9+
{
10+
};
11+
};
12+
13+
namespace o2
14+
{
15+
}
16+
namespace o2codecheck
17+
{
18+
}
19+
namespace a_1234_5678_b
20+
{
21+
}
22+
23+
namespace TPC
24+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: namespace 'TPC' does not follow the underscore convention [aliceO2-namespace-naming]
25+
// CHECK-FIXES: {{^}}namespace tpc{{$}}
26+
{
27+
}
28+
29+
using namespace TPC;
30+
// CHECK-MESSAGES: :[[@LINE-1]]:17: warning: namespace 'TPC' does not follow the underscore convention [aliceO2-namespace-naming]
31+
// CHECK-FIXES: {{^}}using namespace tpc;{{$}}
32+
333
namespace OuterNamespaceWrong
434
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: namespace 'OuterNamespaceWrong' does not follow the underscore convention [aliceO2-namespace-naming]
535
// CHECK-FIXES: {{^}}namespace outer_namespace_wrong{{$}}

0 commit comments

Comments
 (0)