Bug 1611160 - Add Clang Plugin for nsIPrincipal r=ckerschb,andi
authorSebastian Streich <sstreich@mozilla.com>
Tue, 03 Mar 2020 14:30:36 +0000 (2020-03-03)
changeset 516611 fb60b22ee6616521b386d90aec07b03b77905f4e
parent 516610 4e5d74671e9808551f118863569dcfb972787ad0
child 516612 c63b816c8c3c29be1382e1ab7dba65b74fc9a58f
push id37177
push userapavel@mozilla.com
push dateTue, 03 Mar 2020 21:41:28 +0000 (2020-03-03)
treeherdermozilla-central@bf8af9af8047 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersckerschb, andi
bugs1611160
milestone75.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1611160 - Add Clang Plugin for nsIPrincipal r=ckerschb,andi Differential Revision: https://phabricator.services.mozilla.com/D63082
build/clang-plugin/Checks.inc
build/clang-plugin/ChecksIncludes.inc
build/clang-plugin/CustomMatchers.h
build/clang-plugin/NoPrincipalGetURI.cpp
build/clang-plugin/NoPrincipalGetURI.h
build/clang-plugin/moz.build
build/clang-plugin/tests/TestNoPrincipalGetUri.cpp
build/clang-plugin/tests/moz.build
--- a/build/clang-plugin/Checks.inc
+++ b/build/clang-plugin/Checks.inc
@@ -14,16 +14,17 @@ CHECK(KungFuDeathGripChecker, "kungfu-de
 #ifdef TARGET_IS_WINDOWS
 CHECK(LoadLibraryUsageChecker, "load-library-usage")
 CHECK(FopenUsageChecker, "fopen-usage")
 #endif
 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")
 CHECK(NoDuplicateRefCntMemberChecker, "no-duplicate-refcnt-member")
 CHECK(NoExplicitMoveConstructorChecker, "no-explicit-move-constructor")
 CHECK(NoNewThreadsChecker, "no-new-threads")
 CHECK(NonMemMovableMemberChecker, "non-memmovable-member")
 CHECK(NonMemMovableTemplateArgChecker, "non-memmovable-template-arg")
--- a/build/clang-plugin/ChecksIncludes.inc
+++ b/build/clang-plugin/ChecksIncludes.inc
@@ -15,16 +15,17 @@
 #include "LoadLibraryUsageChecker.h"
 #include "FopenUsageChecker.h"
 #endif
 #include "KungFuDeathGripChecker.h"
 #include "MustOverrideChecker.h"
 #include "MustReturnFromCallerChecker.h"
 #include "MustUseChecker.h"
 #include "NaNExprChecker.h"
+#include "NoPrincipalGetURI.h"
 #include "NeedsNoVTableTypeChecker.h"
 #include "NoAddRefReleaseOnReturnChecker.h"
 #include "NoAutoTypeChecker.h"
 #include "NoDuplicateRefCntMemberChecker.h"
 #include "NoExplicitMoveConstructorChecker.h"
 #include "NoNewThreadsChecker.h"
 #include "NonMemMovableMemberChecker.h"
 #include "NonMemMovableTemplateArgChecker.h"
--- a/build/clang-plugin/CustomMatchers.h
+++ b/build/clang-plugin/CustomMatchers.h
@@ -154,16 +154,31 @@ AST_MATCHER(BinaryOperator, isInWhitelis
     if (llvm::sys::path::rbegin(FileName)->equals(*itr)) {
       return true;
     }
   }
 
   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) {
 
   // Get the source location of the call
   SourceLocation Loc = Node.getRParenLoc();
   StringRef FileName =
       getFilename(Finder->getASTContext().getSourceManager(), Loc);
new file mode 100644
--- /dev/null
+++ b/build/clang-plugin/NoPrincipalGetURI.cpp
@@ -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);
+}
new file mode 100644
--- /dev/null
+++ b/build/clang-plugin/NoPrincipalGetURI.h
@@ -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
--- a/build/clang-plugin/moz.build
+++ b/build/clang-plugin/moz.build
@@ -29,16 +29,17 @@ HOST_SOURCES += [
     'NoAutoTypeChecker.cpp',
     'NoDuplicateRefCntMemberChecker.cpp',
     'NoExplicitMoveConstructorChecker.cpp',
     'NoNewThreadsChecker.cpp',
     'NonMemMovableMemberChecker.cpp',
     'NonMemMovableTemplateArgChecker.cpp',
     'NonParamInsideFunctionDeclChecker.cpp',
     'NonTrivialTypeInFfiChecker.cpp',
+    'NoPrincipalGetURI.cpp',
     'NoUsingNamespaceMozillaJavaChecker.cpp',
     'OverrideBaseCallChecker.cpp',
     'OverrideBaseCallUsageChecker.cpp',
     'ParamTraitsEnumChecker.cpp',
     'RefCountedCopyConstructorChecker.cpp',
     'RefCountedInsideLambdaChecker.cpp',
     'ScopeChecker.cpp',
     'SprintfLiteralChecker.cpp',
new file mode 100644
--- /dev/null
+++ b/build/clang-plugin/tests/TestNoPrincipalGetUri.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);
+
+}
\ No newline at end of file
--- a/build/clang-plugin/tests/moz.build
+++ b/build/clang-plugin/tests/moz.build
@@ -33,16 +33,17 @@ SOURCES += [
     'TestNoNewThreadsChecker.cpp',
     'TestNonHeapClass.cpp',
     'TestNonMemMovable.cpp',
     'TestNonMemMovableStd.cpp',
     'TestNonMemMovableStdAtomic.cpp',
     'TestNonParameterChecker.cpp',
     'TestNonTemporaryClass.cpp',
     'TestNonTrivialTypeInFfi.cpp',
+    'TestNoPrincipalGetUri.cpp',
     'TestNoRefcountedInsideLambdas.cpp',
     'TestNoUsingNamespaceMozillaJava.cpp',
     'TestOverrideBaseCall.cpp',
     'TestOverrideBaseCallAnnotation.cpp',
     'TestParamTraitsEnum.cpp',
     'TestRefCountedCopyConstructor.cpp',
     'TestSprintfLiteral.cpp',
     'TestStackClass.cpp',