Bug 1611160 - Add Clang Plugin for nsIPrincipal r=ckerschb,andi

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Sebastian Streich 2020-03-03 14:30:36 +00:00
Родитель 5ee82cb26c
Коммит 882cb13c7b
8 изменённых файлов: 95 добавлений и 0 удалений

Просмотреть файл

@ -19,6 +19,7 @@ CHECK(MustOverrideChecker, "must-override")
CHECK(MustReturnFromCallerChecker, "must-return-from-caller")
CHECK(MustUseChecker, "must-use")
CHECK(NaNExprChecker, "nan-expr")
CHECK(NoPrincipalGetURI, "no-principal-geturi")
CHECK(NeedsNoVTableTypeChecker, "needs-no-vtable-type")
CHECK(NoAddRefReleaseOnReturnChecker, "no-addref-release-on-return")
CHECK(NoAutoTypeChecker, "no-auto-type")

Просмотреть файл

@ -20,6 +20,7 @@
#include "MustReturnFromCallerChecker.h"
#include "MustUseChecker.h"
#include "NaNExprChecker.h"
#include "NoPrincipalGetURI.h"
#include "NeedsNoVTableTypeChecker.h"
#include "NoAddRefReleaseOnReturnChecker.h"
#include "NoAutoTypeChecker.h"

Просмотреть файл

@ -159,6 +159,21 @@ AST_MATCHER(BinaryOperator, isInWhitelistForNaNExpr) {
return false;
}
AST_MATCHER(CallExpr, isInWhiteListForPrincipalGetUri) {
const auto Whitelist = {"nsIPrincipal.h", "BasePrincipal.cpp",
"ContentPrincipal.cpp"};
SourceLocation Loc = Node.getBeginLoc();
StringRef Filename =
getFilename(Finder->getASTContext().getSourceManager(), Loc);
for (auto Exclusion : Whitelist) {
if (Filename.find(Exclusion) != std::string::npos) {
return true;
}
}
return false;
}
/// This matcher will match a list of files which contain NS_NewNamedThread
/// code or names of existing threads that we would like to ignore.
AST_MATCHER(CallExpr, isInAllowlistForThreads) {

Просмотреть файл

@ -0,0 +1,27 @@
/* 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 "NoPrincipalGetURI.h"
#include "CustomMatchers.h"
void NoPrincipalGetURI::registerMatchers(MatchFinder *AstMatcher) {
AstMatcher->addMatcher(
cxxMemberCallExpr(
allOf(callee(cxxMethodDecl(hasName("GetURI"))),
anyOf(on(hasType(asString("class nsIPrincipal *"))),
on(hasType(asString("class nsIPrincipal")))),
unless(isInWhiteListForPrincipalGetUri())),
argumentCountIs(1))
.bind("id"),
this);
}
void NoPrincipalGetURI::check(const MatchFinder::MatchResult &Result) {
const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMemberCallExpr>("id");
diag(MatchedDecl->getExprLoc(),
"nsIPrincipal->GetURI is depricated and will be removed soon. Please "
"consider using the new helper functions of nsIPrincipal",
DiagnosticIDs::Warning);
}

Просмотреть файл

@ -0,0 +1,18 @@
/* 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 NoPrincipalGetURI_h__
#define NoPrincipalGetURI_h__
#include "plugin.h"
class NoPrincipalGetURI : public BaseCheck {
public:
NoPrincipalGetURI(StringRef CheckName, ContextType *Context = nullptr)
: BaseCheck(CheckName, Context) {}
void registerMatchers(MatchFinder *AstMatcher) override;
void check(const MatchFinder::MatchResult &Result) override;
};
#endif

Просмотреть файл

@ -34,6 +34,7 @@ HOST_SOURCES += [
'NonMemMovableTemplateArgChecker.cpp',
'NonParamInsideFunctionDeclChecker.cpp',
'NonTrivialTypeInFfiChecker.cpp',
'NoPrincipalGetURI.cpp',
'NoUsingNamespaceMozillaJavaChecker.cpp',
'OverrideBaseCallChecker.cpp',
'OverrideBaseCallUsageChecker.cpp',

Просмотреть файл

@ -0,0 +1,31 @@
class nsIPrincipal {
public:
void GetURI(int foo){};
};
class SomePrincipal : public nsIPrincipal {
public:
void GetURI(int foo) {}
};
class NullPrincipal : public SomePrincipal {};
class SomeURI {
public:
void GetURI(int foo) {}
};
void f() {
nsIPrincipal *a = new SomePrincipal();
a->GetURI(0); // expected-warning {{nsIPrincipal->GetURI is depricated and will be removed soon. Please consider using the new helper functions of nsIPrincipal}}
nsIPrincipal *b = new NullPrincipal();
b->GetURI(0); // expected-warning {{nsIPrincipal->GetURI is depricated and will be removed soon. Please consider using the new helper functions of nsIPrincipal}}
SomeURI *c = new SomeURI();
c->GetURI(0);
SomePrincipal *d = new SomePrincipal();
d->GetURI(0);
}

Просмотреть файл

@ -38,6 +38,7 @@ SOURCES += [
'TestNonParameterChecker.cpp',
'TestNonTemporaryClass.cpp',
'TestNonTrivialTypeInFfi.cpp',
'TestNoPrincipalGetUri.cpp',
'TestNoRefcountedInsideLambdas.cpp',
'TestNoUsingNamespaceMozillaJava.cpp',
'TestOverrideBaseCall.cpp',