]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Allow linters to share resources by shoving them on the Engine
authorepriestley <git@epriestley.com>
Mon, 12 May 2014 02:28:46 +0000 (19:28 -0700)
committerepriestley <git@epriestley.com>
Mon, 12 May 2014 02:28:46 +0000 (19:28 -0700)
Summary:
Currently, the Phutil XHPAST Linter and vanilla XHPAST Linter reuse the same parse tree, but do this by having explicit knowledge of one another.

Instead, let them synchronize by writing to a glorified array of globals on the Engine. They no longer require knowledge of one another, so this can work under `.arclint`.

(This could probably be a little cleaner by putting more logic in the shared base class, but Facebook has some kind of goofy subclass of this thing and //this// patch won't disrupt it, while a cleaner one might.)

This should unblock D9057.

Test Plan: Ran unit tests and normal lint, got accurate looking results without duplicate invocations showing up in `--trace`.

Reviewers: btrahan, joshuaspence

Reviewed By: joshuaspence

Subscribers: epriestley

Differential Revision: https://secure.phabricator.com/D9059

src/lint/engine/ArcanistLintEngine.php
src/lint/engine/PhutilLintEngine.php
src/lint/linter/ArcanistBaseXHPASTLinter.php
src/lint/linter/ArcanistPhutilXHPASTLinter.php
src/lint/linter/ArcanistXHPASTLinter.php
src/lint/linter/__tests__/ArcanistPhutilXHPASTLinterTestCase.php

index d634bba9e458d891beb37eee58016d76787e630b..6f030a13bd15d68cf928fac2a423ed6fe61b1553 100644 (file)
@@ -65,6 +65,8 @@ abstract class ArcanistLintEngine {
   private $postponedLinters = array();
   private $configurationManager;
 
+  private $linterResources = array();
+
   public function __construct() {
 
   }
@@ -548,4 +550,38 @@ abstract class ArcanistLintEngine {
   }
 
 
+  /**
+   * Get a named linter resource shared by another linter.
+   *
+   * This mechanism allows linters to share arbitrary resources, like the
+   * results of computation. If several linters need to perform the same
+   * expensive computation step, they can use a named resource to synchronize
+   * construction of the result so it doesn't need to be built multiple
+   * times.
+   *
+   * @param string  Resource identifier.
+   * @param wild    Optionally, default value to return if resource does not
+   *                exist.
+   * @return wild   Resource, or default value if not present.
+   */
+  public function getLinterResource($key, $default = null) {
+    return idx($this->linterResources, $key, $default);
+  }
+
+
+  /**
+   * Set a linter resource that other linters can accesss.
+   *
+   * See @{method:getLinterResource} for a description of this mechanism.
+   *
+   * @param string Resource identifier.
+   * @param wild   Resource.
+   * @return this
+   */
+  public function setLinterResource($key, $value) {
+    $this->linterResources[$key] = $value;
+    return $this;
+  }
+
+
 }
index 968f9b2078ba16f9280fe3a447e0ffcfff8191f9..666ffbc166ec9b49631826e0d0924a8afa3799ff 100644 (file)
@@ -58,7 +58,6 @@ class PhutilLintEngine extends ArcanistLintEngine {
     $linters[] = $xhpast_linter;
 
     $linters[] = id(new ArcanistPhutilXHPASTLinter())
-      ->setXHPASTLinter($xhpast_linter)
       ->setPaths($php_paths);
 
     $merge_conflict_linter = id(new ArcanistMergeConflictLinter());
index 192a94430d798420809a6ce3174a9248a43d662e..1fe7d7780204d75b848bdb3227fab081eacd5a0e 100644 (file)
@@ -1,10 +1,14 @@
 <?php
 
 /**
- * @group linter
+ * @task sharing Sharing Parse Trees
  */
 abstract class ArcanistBaseXHPASTLinter extends ArcanistFutureLinter {
 
+  private $futures = array();
+  private $trees = array();
+  private $exceptions = array();
+
   protected final function raiseLintAtToken(
     XHPASTToken $token,
     $code,
@@ -31,4 +35,117 @@ abstract class ArcanistBaseXHPASTLinter extends ArcanistFutureLinter {
       $replace);
   }
 
+  protected function buildFutures(array $paths) {
+    return $this->getXHPASTLinter()->buildSharedFutures($paths);
+  }
+
+
+/* -(  Sharing Parse Trees  )------------------------------------------------ */
+
+
+  /**
+   * Get the linter object which is responsible for building parse trees.
+   *
+   * When the engine specifies that several XHPAST linters should execute,
+   * we designate one of them as the one which will actually build parse trees.
+   * The other linters share trees, so they don't have to recompute them.
+   *
+   * Roughly, the first linter to execute elects itself as the builder.
+   * Subsequent linters request builds and retrieve results from it.
+   *
+   * @return ArcanistBaseXHPASTLinter Responsible linter.
+   * @task sharing
+   */
+  protected function getXHPASTLinter() {
+    $resource_key = 'xhpast.linter';
+
+    // If we're the first linter to run, share ourselves. Otherwise, grab the
+    // previously shared linter.
+
+    $engine = $this->getEngine();
+    $linter = $engine->getLinterResource($resource_key);
+    if (!$linter) {
+      $linter = $this;
+      $engine->setLinterResource($resource_key, $linter);
+    }
+
+    $base_class = __CLASS__;
+    if (!($linter instanceof $base_class)) {
+      throw new Exception(
+        pht(
+          'Expected resource "%s" to be an instance of "%s"!',
+          $resource_key,
+          $base_class));
+    }
+
+    return $linter;
+  }
+
+
+  /**
+   * Build futures on this linter, for use and to share with other linters.
+   *
+   * @param list<string> Paths to build futures for.
+   * @return list<ExecFuture> Futures.
+   * @task sharing
+   */
+  protected function buildSharedFutures(array $paths) {
+    foreach ($paths as $path) {
+      if (!isset($this->futures[$path])) {
+        $this->futures[$path] = xhpast_get_parser_future($this->getData($path));
+      }
+    }
+    return array_select_keys($this->futures, $paths);
+  }
+
+
+  /**
+   * Get a path's tree from the responsible linter.
+   *
+   * @param   string           Path to retrieve tree for.
+   * @return  XHPASTTree|null  Tree, or null if unparseable.
+   * @task sharing
+   */
+  protected function getXHPASTTreeForPath($path) {
+
+    // If we aren't the linter responsible for actually building the parse
+    // trees, go get the tree from that linter.
+    if ($this->getXHPASTLinter() !== $this) {
+      return $this->getXHPASTLinter()->getXHPASTTreeForPath($path);
+    }
+
+    if (!array_key_exists($path, $this->trees)) {
+      $this->trees[$path] = null;
+      try {
+        $this->trees[$path] = XHPASTTree::newFromDataAndResolvedExecFuture(
+          $this->getData($path),
+          $this->futures[$path]->resolve());
+        $root = $this->trees[$path]->getRootNode();
+        $root->buildSelectCache();
+        $root->buildTokenCache();
+      } catch (Exception $ex) {
+        $this->exceptions[$path] = $ex;
+      }
+    }
+
+    return $this->trees[$path];
+  }
+
+
+  /**
+   * Get a path's parse exception from the responsible linter.
+   *
+   * @param   string          Path to retrieve exception for.
+   * @return  Exeption|null   Parse exception, if available.
+   * @task sharing
+   */
+  protected function getXHPASTExceptionForPath($path) {
+    if ($this->getXHPASTLinter() !== $this) {
+      return $this->getXHPASTLinter()->getXHPASTExceptionForPath($path);
+    }
+
+    return idx($this->exceptions, $path);
+  }
+
+
 }
index 92b8ac6efd038b86da8e67c4a3b5a56576c8be97..b5a59d5971412ce7acaa4b8d73751d3f695ffe09 100644 (file)
@@ -1,24 +1,15 @@
 <?php
 
-/**
- * @group linter
- */
 final class ArcanistPhutilXHPASTLinter extends ArcanistBaseXHPASTLinter {
 
   const LINT_ARRAY_COMBINE           = 2;
   const LINT_DEPRECATED_FUNCTION     = 3;
   const LINT_UNSAFE_DYNAMIC_STRING   = 4;
 
-  private $xhpastLinter;
   private $deprecatedFunctions = array();
   private $dynamicStringFunctions = array();
   private $dynamicStringClasses = array();
 
-  public function setXHPASTLinter(ArcanistXHPASTLinter $linter) {
-    $this->xhpastLinter = $linter;
-    return $this;
-  }
-
   public function setDeprecatedFunctions($map) {
     $this->deprecatedFunctions = $map;
     return $this;
@@ -34,15 +25,6 @@ final class ArcanistPhutilXHPASTLinter extends ArcanistBaseXHPASTLinter {
     return $this;
   }
 
-  public function setEngine(ArcanistLintEngine $engine) {
-    if (!$this->xhpastLinter) {
-      throw new Exception(
-        'Call setXHPASTLinter() before using ArcanistPhutilXHPASTLinter.');
-    }
-    $this->xhpastLinter->setEngine($engine);
-    return parent::setEngine($engine);
-  }
-
   public function getLintNameMap() {
     return array(
       self::LINT_ARRAY_COMBINE           => 'array_combine() Unreliable',
@@ -73,17 +55,8 @@ final class ArcanistPhutilXHPASTLinter extends ArcanistBaseXHPASTLinter {
     return $version;
   }
 
-  protected function buildFutures(array $paths) {
-    return $this->xhpastLinter->buildFutures($paths);
-  }
-
-  public function willLintPath($path) {
-    $this->xhpastLinter->willLintPath($path);
-    return parent::willLintPath($path);
-  }
-
   protected function resolveFuture($path, Future $future) {
-    $tree = $this->xhpastLinter->getXHPASTTreeForPath($path);
+    $tree = $this->getXHPASTLinter()->getXHPASTTreeForPath($path);
     if (!$tree) {
       return;
     }
index 9cf651d04cffbdbf4d9739cd7c331e256f9331b5..dc28841252c5ff30ed66e0aac2556814a2a84813 100644 (file)
@@ -2,14 +2,9 @@
 
 /**
  * Uses XHPAST to apply lint rules to PHP.
- *
- * @group linter
  */
 final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
 
-  private $futures = array();
-  private $trees = array();
-
   const LINT_PHP_SYNTAX_ERROR          = 1;
   const LINT_UNABLE_TO_PARSE           = 2;
   const LINT_VARIABLE_VARIABLE         = 3;
@@ -130,38 +125,6 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
     );
   }
 
-  protected function buildFutures(array $paths) {
-    foreach ($paths as $path) {
-      if (!isset($this->futures[$path])) {
-        $this->futures[$path] = xhpast_get_parser_future($this->getData($path));
-      }
-    }
-    return array_select_keys($this->futures, $paths);
-  }
-
-  public function getXHPASTTreeForPath($path) {
-    if (!array_key_exists($path, $this->trees)) {
-      $this->trees[$path] = null;
-      try {
-        $this->trees[$path] = XHPASTTree::newFromDataAndResolvedExecFuture(
-          $this->getData($path),
-          $this->futures[$path]->resolve());
-        $root = $this->trees[$path]->getRootNode();
-        $root->buildSelectCache();
-        $root->buildTokenCache();
-      } catch (XHPASTSyntaxErrorException $ex) {
-        $this->raiseLintAtLine(
-          $ex->getErrorLine(),
-          1,
-          self::LINT_PHP_SYNTAX_ERROR,
-          'This file contains a syntax error: '.$ex->getMessage());
-      } catch (Exception $ex) {
-        $this->raiseLintAtPath(self::LINT_UNABLE_TO_PARSE, $ex->getMessage());
-      }
-    }
-    return $this->trees[$path];
-  }
-
   public function getCacheVersion() {
     $version = '4';
     $path = xhpast_get_binary_path();
@@ -174,6 +137,16 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
   protected function resolveFuture($path, Future $future) {
     $tree = $this->getXHPASTTreeForPath($path);
     if (!$tree) {
+      $ex = $this->getXHPASTExceptionForPath($path);
+      if ($ex instanceof XHPASTSyntaxErrorException) {
+        $this->raiseLintAtLine(
+          $ex->getErrorLine(),
+          1,
+          self::LINT_PHP_SYNTAX_ERROR,
+          'This file contains a syntax error: '.$ex->getMessage());
+      } else if ($ex instanceof Exception) {
+        $this->raiseLintAtPath(self::LINT_UNABLE_TO_PARSE, $ex->getMessage());
+      }
       return;
     }
 
index 32e5a4f91e0c7dfbf9eca7b7654868df9c491e8f..f1036583f2757ff5be32a9c7727f0e2c982b5099 100644 (file)
@@ -8,7 +8,6 @@ final class ArcanistPhutilXHPASTLinterTestCase
 
   public function testPhutilXHPASTLint() {
     $linter = new ArcanistPhutilXHPASTLinter();
-    $linter->setXHPASTLinter(new ArcanistXHPASTLinter());
     $linter->setDeprecatedFunctions(array(
       'deprecated_function' => 'This function is most likely deprecated.',
     ));