1313#include < regex>
1414#include < string>
1515#include < ctype.h>
16-
16+
1717using namespace clang ::ast_matchers;
1818
1919namespace clang {
2020namespace tidy {
2121namespace 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
2531void 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
3747void 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
81105void 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 }
0 commit comments