Bug 1610678: Add check to clang plugin to raise error when using namespace mozilla::java is present in C++ source; r=andi

Differential Revision: https://phabricator.services.mozilla.com/D60611

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Aaron Klotz 2020-01-22 18:47:04 +00:00
parent 42ed117231
commit 2977b8a690
8 changed files with 92 additions and 0 deletions

View File

@ -25,6 +25,7 @@ CHECK(NoDuplicateRefCntMemberChecker, "no-duplicate-refcnt-member")
CHECK(NoExplicitMoveConstructorChecker, "no-explicit-move-constructor")
CHECK(NonMemMovableMemberChecker, "non-memmovable-member")
CHECK(NonMemMovableTemplateArgChecker, "non-memmovable-template-arg")
CHECK(NoUsingNamespaceMozillaJavaChecker, "no-using-namespace-mozilla-java")
CHECK(NonParamInsideFunctionDeclChecker, "non-memmovable-template-arg")
CHECK(OverrideBaseCallChecker, "override-base-call")
CHECK(OverrideBaseCallUsageChecker, "override-base-call-usage")

View File

@ -27,6 +27,7 @@
#include "NonMemMovableMemberChecker.h"
#include "NonMemMovableTemplateArgChecker.h"
#include "NonParamInsideFunctionDeclChecker.h"
#include "NoUsingNamespaceMozillaJavaChecker.h"
#include "OverrideBaseCallChecker.h"
#include "OverrideBaseCallUsageChecker.h"
#include "ParamTraitsEnumChecker.h"

View File

@ -347,6 +347,19 @@ AST_MATCHER(CXXDefaultArgExpr, isNullDefaultArg) {
Expr::NPC_NeverValueDependent);
}
AST_MATCHER(UsingDirectiveDecl, isUsingNamespaceMozillaJava) {
const NamespaceDecl *Namespace = Node.getNominatedNamespace();
const std::string &FQName = Namespace->getQualifiedNameAsString();
static const char NAMESPACE[] = "mozilla::java";
static const char PREFIX[] = "mozilla::java::";
// We match both the `mozilla::java` namespace itself as well as any other
// namespaces contained within the `mozilla::java` namespace.
return !FQName.compare(NAMESPACE) ||
!FQName.compare(0, sizeof(PREFIX) - 1, PREFIX);
}
} // namespace ast_matchers
} // namespace clang

View File

@ -0,0 +1,25 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "NoUsingNamespaceMozillaJavaChecker.h"
#include "CustomMatchers.h"
void NoUsingNamespaceMozillaJavaChecker::registerMatchers(MatchFinder *AstMatcher) {
AstMatcher->addMatcher(
usingDirectiveDecl(isUsingNamespaceMozillaJava()).bind("directive"),
this);
}
void NoUsingNamespaceMozillaJavaChecker::check(
const MatchFinder::MatchResult &Result) {
const UsingDirectiveDecl *Directive =
Result.Nodes.getNodeAs<UsingDirectiveDecl>("directive");
const NamespaceDecl *Namespace = Directive->getNominatedNamespace();
diag(Directive->getUsingLoc(),
"using namespace %0 is forbidden",
DiagnosticIDs::Error)
<< Namespace->getQualifiedNameAsString();
}

View File

@ -0,0 +1,20 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef NoUsingNamespaceMozillaJavaChecker_h__
#define NoUsingNamespaceMozillaJavaChecker_h__
#include "plugin.h"
class NoUsingNamespaceMozillaJavaChecker : public BaseCheck {
public:
NoUsingNamespaceMozillaJavaChecker(StringRef CheckName,
ContextType *Context = nullptr)
: BaseCheck(CheckName, Context) {}
void registerMatchers(MatchFinder *AstMatcher) override;
void check(const MatchFinder::MatchResult &Result) override;
};
#endif

View File

@ -32,6 +32,7 @@ HOST_SOURCES += [
'NonMemMovableMemberChecker.cpp',
'NonMemMovableTemplateArgChecker.cpp',
'NonParamInsideFunctionDeclChecker.cpp',
'NoUsingNamespaceMozillaJavaChecker.cpp',
'OverrideBaseCallChecker.cpp',
'OverrideBaseCallUsageChecker.cpp',
'ParamTraitsEnumChecker.cpp',

View File

@ -0,0 +1,30 @@
namespace mozilla {
namespace java {
namespace sdk {
} // namespace sdk
namespace future {
} // namespace future
} // namespace java
} // namespace mozilla
namespace mozilla {
using namespace java; // expected-error{{using namespace mozilla::java is forbidden}}
using namespace java::future; // expected-error{{using namespace mozilla::java::future is forbidden}}
}
using namespace mozilla::java::sdk; // expected-error{{using namespace mozilla::java::sdk is forbidden}}
namespace shouldPass {
namespace java {
}
using namespace java;
}
using namespace shouldPass::java;
void test() {
}

View File

@ -37,6 +37,7 @@ SOURCES += [
'TestNonParameterChecker.cpp',
'TestNonTemporaryClass.cpp',
'TestNoRefcountedInsideLambdas.cpp',
'TestNoUsingNamespaceMozillaJava.cpp',
'TestOverrideBaseCall.cpp',
'TestOverrideBaseCallAnnotation.cpp',
'TestParamTraitsEnum.cpp',