Skip to content

Commit 642b8f4

Browse files
ZdravkoDsawenzel
authored andcommitted
Modify check namespace naming. Add check configuration option. (#22)
* Adding a way to configure which files are being checked. * Use this configuration in namespace check.
1 parent a2adf74 commit 642b8f4

File tree

4 files changed

+74
-23
lines changed

4 files changed

+74
-23
lines changed

aliceO2/NamespaceNamingCheck.cpp

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,29 @@ namespace aliceO2 {
2323
const std::string VALID_NAME_REGEX = "[a-z][a-z_0-9]+";
2424
const 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+
2641
bool isOutsideOfTargetScope(std::string filename)
2742
{
2843
return !std::regex_match(filename, std::regex(VALID_PATH_REGEX));
2944
}
3045

3146
void 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

aliceO2/NamespaceNamingCheck.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ namespace aliceO2 {
2222
/// http://clang.llvm.org/extra/clang-tidy/checks/aliceO2-namespace-naming.html
2323
class NamespaceNamingCheck : public ClangTidyCheck {
2424
public:
25-
NamespaceNamingCheck(StringRef Name, ClangTidyContext *Context)
26-
: ClangTidyCheck(Name, Context) {}
25+
NamespaceNamingCheck(StringRef Name, ClangTidyContext *Context);
2726
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2827
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2928

30-
void fixNamespaceName(std::string &name);
29+
bool fixNamespaceName(std::string &name);
30+
void logNameError(SourceLocation Loc, std::string errorName);
3131
};
3232

3333
} // namespace aliceO2

check_clang_tidy.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ def main():
111111
except subprocess.CalledProcessError as e:
112112
diff_output = e.output
113113

114+
if has_check_messages:
115+
messages_file = temp_file_name + '.msg'
116+
write_file(messages_file, clang_tidy_output)
117+
114118
print('------------------------------ Fixes -----------------------------\n' +
115119
diff_output.decode() +
116120
'\n------------------------------------------------------------------')
@@ -126,8 +130,6 @@ def main():
126130
raise
127131

128132
if has_check_messages:
129-
messages_file = temp_file_name + '.msg'
130-
write_file(messages_file, clang_tidy_output)
131133
try:
132134
subprocess.check_output(
133135
['FileCheck', '-input-file=' + messages_file, input_file_name,

test/.clang-tidy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CheckOptions:
2+
- key: CheckPathRegex
3+
value: '.*/test/.*'

0 commit comments

Comments
 (0)