Skip to content

Commit

Permalink
Check only ::o2 subnamespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
anerokhi authored and sawenzel committed Dec 13, 2019
1 parent 9478379 commit acafa24
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions aliceO2/NamespaceNamingCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "NamespaceNamingCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchersMacros.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include <regex>
#include <string>
Expand All @@ -17,6 +18,13 @@
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 {

Expand All @@ -42,18 +50,26 @@ bool isOutsideOfTargetScope(std::string filename)

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(unless(validNameMatch)
)))).bind("namespace-usage"), this );
Finder->addMatcher(nestedNameSpecifierLoc(loc(nestedNameSpecifier(specifiesNamespace(allOf(
inO2NSMatch,
unless(validNameMatch)
))))).bind("namespace-usage"), this);
// matches "using namespace" declarations
Finder->addMatcher(usingDirectiveDecl(unless(isImplicit()
)).bind("using-namespace"), this);
Finder->addMatcher(usingDirectiveDecl(allOf(
unless(isImplicit()),
nominatedNamespace(allOf(
inO2NSMatch,
unless(validNameMatch)
)))).bind("using-namespace"), this);
}

void NamespaceNamingCheck::check(const MatchFinder::MatchResult &Result) {
Expand Down Expand Up @@ -112,11 +128,6 @@ void NamespaceNamingCheck::check(const MatchFinder::MatchResult &Result) {
std::string newName(MatchedUsingNamespace->getNominatedNamespace()->getDeclName().getAsString());
std::string oldName=newName;

if( std::regex_match(newName, std::regex(VALID_NAME_REGEX)) )
{
return;
}

if(fixNamespaceName(newName))
{
diag(MatchedUsingNamespace->getLocation(), "namespace %q0 does not follow the underscore convention")
Expand Down

0 comments on commit acafa24

Please sign in to comment.