@@ -23,14 +23,29 @@ namespace aliceO2 {
2323const std::string VALID_NAME_REGEX = " [a-z][a-z_0-9]+" ;
2424const std::string VALID_PATH_REGEX = " (.*/O2/.*)|(.*/test/.*)" ;
2525
26+ const std::string VALID_NAME_REGEX = " [a-z][a-z_0-9]+" ;
27+ std::string VALID_PATH_REGEX = " " ;
28+
29+ NamespaceNamingCheck::NamespaceNamingCheck (StringRef Name, ClangTidyContext *Context)
30+ : ClangTidyCheck(Name, Context)
31+ {
32+ VALID_PATH_REGEX = Options.getLocalOrGlobal (" CheckPathRegex" ," " );
33+ if ( VALID_PATH_REGEX == " " )
34+ {
35+ fprintf (stderr," Error: User must provide a .clang-tidy file in any of the parent directories of the source files containing a key-value pair for key \' CheckPathRegex\' "
36+ " or pass the key-value pair with the command line argument --config" );
37+ exit (1 );
38+ }
39+ }
40+
2641bool isOutsideOfTargetScope (std::string filename)
2742{
2843 return !std::regex_match (filename, std::regex (VALID_PATH_REGEX));
2944}
3045
3146void NamespaceNamingCheck::registerMatchers (MatchFinder *Finder) {
3247 const auto validNameMatch = matchesName ( std::string (" ::" ) + VALID_NAME_REGEX + " $" );
33-
48+
3449 // matches namespace declarations that have invalid name
3550 Finder->addMatcher (namespaceDecl (allOf (
3651 unless (validNameMatch),
@@ -52,16 +67,21 @@ void NamespaceNamingCheck::check(const MatchFinder::MatchResult &Result) {
5267 {
5368 return ;
5469 }
55-
5670 std::string newName (MatchedNamespaceDecl->getDeclName ().getAsString ());
57-
58- fixNamespaceName (newName);
59-
60- diag (MatchedNamespaceDecl->getLocation (), " namespace %0 does not follow the underscore convention" )
71+ std::string oldName=newName;
72+
73+ if (fixNamespaceName (newName))
74+ {
75+ diag (MatchedNamespaceDecl->getLocation (), " namespace %0 does not follow the underscore convention" )
6176 << MatchedNamespaceDecl
6277 << FixItHint::CreateReplacement (MatchedNamespaceDecl->getLocation (), newName);
78+ }
79+ else
80+ {
81+ logNameError (MatchedNamespaceDecl->getLocation (), oldName);
82+ }
6383 }
64-
84+
6585 const auto *MatchedNamespaceLoc = Result.Nodes .getNodeAs <NestedNameSpecifierLoc>(" namespace-usage" );
6686 if ( MatchedNamespaceLoc )
6787 {
@@ -71,12 +91,18 @@ void NamespaceNamingCheck::check(const MatchFinder::MatchResult &Result) {
7191 return ;
7292 }
7393 std::string newName (AsNamespace->getDeclName ().getAsString ());
74-
75- fixNamespaceName (newName);
76-
77- diag (MatchedNamespaceLoc->getLocalBeginLoc (), " namespace %0 does not follow the underscore convention" )
94+ std::string oldName=newName;
95+
96+ if (fixNamespaceName (newName))
97+ {
98+ diag (MatchedNamespaceLoc->getLocalBeginLoc (), " namespace %0 does not follow the underscore convention" )
7899 << AsNamespace
79100 << FixItHint::CreateReplacement (MatchedNamespaceLoc->getLocalBeginLoc (), newName);
101+ }
102+ else
103+ {
104+ logNameError (MatchedNamespaceLoc->getLocalBeginLoc (), oldName);
105+ }
80106 }
81107
82108 const auto *MatchedUsingNamespace = Result.Nodes .getNodeAs <UsingDirectiveDecl>(" using-namespace" );
@@ -86,24 +112,36 @@ void NamespaceNamingCheck::check(const MatchFinder::MatchResult &Result) {
86112 {
87113 return ;
88114 }
89-
90115 std::string newName (MatchedUsingNamespace->getNominatedNamespace ()->getDeclName ().getAsString ());
91-
116+ std::string oldName=newName;
117+
92118 if ( std::regex_match (newName, std::regex (VALID_NAME_REGEX)) )
93119 {
94120 return ;
95121 }
96-
97- fixNamespaceName (newName);
98-
99- diag (MatchedUsingNamespace->getLocation (), " namespace %0 does not follow the underscore convention" )
122+
123+ if ( fixNamespaceName (newName))
124+ {
125+ diag (MatchedUsingNamespace->getLocation (), " namespace %0 does not follow the underscore convention" )
100126 << MatchedUsingNamespace->getNominatedNamespace ()
101127 << FixItHint::CreateReplacement (MatchedUsingNamespace->getLocation (), newName);
128+ }
129+ else
130+ {
131+ logNameError (MatchedNamespaceDecl->getLocation (), oldName);
132+ }
102133 }
103134}
104135
105- void NamespaceNamingCheck::fixNamespaceName (std::string &name)
136+ bool NamespaceNamingCheck::fixNamespaceName (std::string &name)
106137{
138+ std::string replace_option = Options.get (name, " " );
139+ if ( replace_option != " " )
140+ {
141+ name = replace_option;
142+ return true ;
143+ }
144+
107145 for (int i=name.size ()-1 ; i>=0 ; i--) {
108146 if (isupper (name[i])) {
109147 if (i != 0 && islower (name[i-1 ])) {
@@ -113,6 +151,14 @@ void NamespaceNamingCheck::fixNamespaceName(std::string &name)
113151 name[i] = tolower (name[i]);
114152 }
115153 }
154+
155+ return std::regex_match ( name, std::regex (VALID_NAME_REGEX) );
156+ }
157+
158+ void NamespaceNamingCheck::logNameError (SourceLocation Loc, std::string errorName)
159+ {
160+ diag (Loc, " Could not fix \' %0\' " , DiagnosticIDs::Level::Error)
161+ << errorName;
116162}
117163
118164} // namespace aliceO2
0 commit comments