-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathNamespaceNamingCheck.cpp
More file actions
174 lines (155 loc) · 5.56 KB
/
NamespaceNamingCheck.cpp
File metadata and controls
174 lines (155 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//===--- NamespaceNamingCheck.cpp - clang-tidy-----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "NamespaceNamingCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchersMacros.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <regex>
#include <string>
#include <ctype.h>
using namespace clang::ast_matchers;
namespace clang {
namespace ast_matchers {
AST_MATCHER_P(UsingDirectiveDecl, nominatedNamespace,
internal::Matcher<NamespaceDecl>, InnerMatcher)
{
return InnerMatcher.matches(*Node.getNominatedNamespace(), Finder, Builder);
}
} // namespace ast_matchers
namespace tidy {
namespace aliceO2 {
const std::string VALID_NAME_REGEX = "[a-z][a-z_0-9]+";
std::string VALID_PATH_REGEX = "";
NamespaceNamingCheck::NamespaceNamingCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context)
{
VALID_PATH_REGEX = Options.getLocalOrGlobal("CheckPathRegex","");
if( VALID_PATH_REGEX == "" )
{
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\' "
"or pass the key-value pair with the command line argument --config");
exit(1);
}
}
bool isOutsideOfTargetScope(std::string filename)
{
return !std::regex_match(filename, std::regex(VALID_PATH_REGEX));
}
void NamespaceNamingCheck::registerMatchers(MatchFinder *Finder) {
const auto validNameMatch = matchesName( std::string("::") + VALID_NAME_REGEX + "$" );
const auto inO2NSMatch = matchesName("^::o2::");
// matches namespace declarations that have invalid name
Finder->addMatcher(namespaceDecl(allOf(
inO2NSMatch,
unless(validNameMatch),
unless(isAnonymous())
)).bind("namespace-decl"), this);
// matches usage of namespace
Finder->addMatcher(nestedNameSpecifierLoc(loc(nestedNameSpecifier(specifiesNamespace(allOf(
inO2NSMatch,
unless(validNameMatch)
))))).bind("namespace-usage"), this);
// matches "using namespace" declarations
Finder->addMatcher(usingDirectiveDecl(allOf(
unless(isImplicit()),
nominatedNamespace(allOf(
inO2NSMatch,
unless(validNameMatch)
)))).bind("using-namespace"), this);
}
void NamespaceNamingCheck::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedNamespaceDecl = Result.Nodes.getNodeAs<NamespaceDecl>("namespace-decl");
if( MatchedNamespaceDecl )
{
if( isOutsideOfTargetScope( Result.SourceManager->getFilename(MatchedNamespaceDecl->getLocation()).str() ) )
{
return;
}
std::string newName(MatchedNamespaceDecl->getDeclName().getAsString());
std::string oldName=newName;
if(fixNamespaceName(newName))
{
diag(MatchedNamespaceDecl->getLocation(), "namespace %q0 does not follow the underscore convention")
<< MatchedNamespaceDecl
<< FixItHint::CreateReplacement(MatchedNamespaceDecl->getLocation(), newName);
}
else
{
logNameError(MatchedNamespaceDecl->getLocation(), oldName);
}
}
const auto *MatchedNamespaceLoc = Result.Nodes.getNodeAs<NestedNameSpecifierLoc>("namespace-usage");
if( MatchedNamespaceLoc )
{
const auto *AsNamespace = MatchedNamespaceLoc->getNestedNameSpecifier()->getAsNamespace();
if( isOutsideOfTargetScope( Result.SourceManager->getFilename(AsNamespace->getLocation()).str() ) )
{
return;
}
std::string newName(AsNamespace->getDeclName().getAsString());
std::string oldName=newName;
if(fixNamespaceName(newName))
{
diag(MatchedNamespaceLoc->getLocalBeginLoc(), "namespace %q0 does not follow the underscore convention")
<< AsNamespace
<< FixItHint::CreateReplacement(MatchedNamespaceLoc->getLocalBeginLoc(), newName);
}
else
{
logNameError(MatchedNamespaceLoc->getLocalBeginLoc(), oldName);
}
}
const auto *MatchedUsingNamespace = Result.Nodes.getNodeAs<UsingDirectiveDecl>("using-namespace");
if( MatchedUsingNamespace )
{
if( isOutsideOfTargetScope( Result.SourceManager->getFilename(MatchedUsingNamespace->getNominatedNamespace()->getLocation()).str() ) )
{
return;
}
std::string newName(MatchedUsingNamespace->getNominatedNamespace()->getDeclName().getAsString());
std::string oldName=newName;
if(fixNamespaceName(newName))
{
diag(MatchedUsingNamespace->getLocation(), "namespace %q0 does not follow the underscore convention")
<< MatchedUsingNamespace->getNominatedNamespace()
<< FixItHint::CreateReplacement(MatchedUsingNamespace->getLocation(), newName);
}
else
{
logNameError(MatchedNamespaceDecl->getLocation(), oldName);
}
}
}
bool NamespaceNamingCheck::fixNamespaceName(std::string &name)
{
std::string replace_option = Options.get(name, "").str();
if( replace_option != "" )
{
name = replace_option;
return true;
}
for(int i=name.size()-1; i>=0; i--) {
if(isupper(name[i])) {
if(i != 0 && islower(name[i-1])) {
name.insert(i, "_");
i++;
}
name[i] = tolower(name[i]);
}
}
return std::regex_match( name, std::regex(VALID_NAME_REGEX) );
}
void NamespaceNamingCheck::logNameError(SourceLocation Loc, std::string errorName)
{
diag(Loc, "Could not fix \'%0\'", DiagnosticIDs::Level::Error)
<< errorName;
}
} // namespace aliceO2
} // namespace tidy
} // namespace clang