[test] add an option for disabling linter cache in the pre_submit check

Adds a flag to specify whether to disable the linter caching.

R=machenbach@chromium.org,sergiyb@chromium.org
CC=​​yangguo@chromium.org

Bug: v8:8482
Change-Id: I62a9b7cffb3adb50b136659568ad52078675ca4b
No-Try: true
Reviewed-on: https://chromium-review.googlesource.com/c/1370029
Reviewed-by: Michael Achenbach <machenbach@chromium.org>
Commit-Queue: Tamer Tas <tmrts@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58329}
diff --git a/tools/unittests/v8_presubmit_test.py b/tools/unittests/v8_presubmit_test.py
index 1d670ea..2c66d18 100755
--- a/tools/unittests/v8_presubmit_test.py
+++ b/tools/unittests/v8_presubmit_test.py
@@ -18,7 +18,7 @@
 class FakeCachedProcessor(CacheableSourceFileProcessor):
   def __init__(self, cache_file_path):
     super(FakeCachedProcessor, self).__init__(
-      cache_file_path=cache_file_path, file_type='.test')
+      use_cache=True, cache_file_path=cache_file_path, file_type='.test')
   def GetProcessorWorker(self):
     return object
   def GetProcessorScript(self):
diff --git a/tools/v8_presubmit.py b/tools/v8_presubmit.py
index 4acd77c..0dcb68e 100755
--- a/tools/v8_presubmit.py
+++ b/tools/v8_presubmit.py
@@ -235,7 +235,8 @@
   the files requiring intervention after processing the source files.
   """
 
-  def __init__(self, cache_file_path, file_type):
+  def __init__(self, use_cache, cache_file_path, file_type):
+    self.use_cache = use_cache
     self.cache_file_path = cache_file_path
     self.file_type = file_type
 
@@ -260,9 +261,10 @@
     return command
 
   def ProcessFiles(self, files):
-    cache = FileContentsCache(self.cache_file_path)
-    cache.Load()
-    files = cache.FilterUnchangedFiles(files)
+    if self.use_cache:
+      cache = FileContentsCache(self.cache_file_path)
+      cache.Load()
+      files = cache.FilterUnchangedFiles(files)
 
     if len(files) == 0:
       print 'No changes in %s files detected. Skipping check' % self.file_type
@@ -272,10 +274,12 @@
     print (
       'Total %s files found that require formatting: %d' %
       (self.file_type, len(files_requiring_changes)))
-    for file in files_requiring_changes:
-      cache.RemoveFile(file)
+    if self.use_cache:
+      for file in files_requiring_changes:
+        cache.RemoveFile(file)
 
-    cache.Save()
+      cache.Save()
+
     return files_requiring_changes == []
 
   def DetectFilesToChange(self, files):
@@ -306,9 +310,9 @@
   Lint files to check that they follow the google code style.
   """
 
-  def __init__(self):
+  def __init__(self, use_cache=True):
     super(CppLintProcessor, self).__init__(
-      cache_file_path='.cpplint-cache', file_type='C/C++')
+      use_cache=use_cache, cache_file_path='.cpplint-cache', file_type='C/C++')
 
   def IsRelevant(self, name):
     return name.endswith('.cc') or name.endswith('.h')
@@ -348,9 +352,9 @@
   Check .tq files to verify they follow the Torque style guide.
   """
 
-  def __init__(self):
+  def __init__(self, use_cache=True):
     super(TorqueFormatProcessor, self).__init__(
-      cache_file_path='.torquelint-cache', file_type='Torque')
+      use_cache=use_cache, cache_file_path='.torquelint-cache', file_type='Torque')
 
   def IsRelevant(self, name):
     return name.endswith('.tq')
@@ -660,6 +664,9 @@
   result = optparse.OptionParser()
   result.add_option('--no-lint', help="Do not run cpplint", default=False,
                     action="store_true")
+  result.add_option('--no-linter-cache', help="Do not cache linter results", default=False,
+                    action="store_true")
+
   return result
 
 
@@ -670,11 +677,13 @@
   success = True
   print "Running checkdeps..."
   success &= CheckDeps(workspace)
+  use_linter_cache = not options.no_linter_cache
   if not options.no_lint:
     print "Running C++ lint check..."
-    success &= CppLintProcessor().RunOnPath(workspace)
+    success &= CppLintProcessor(use_cache=use_linter_cache).RunOnPath(workspace)
+
   print "Running Torque formatting check..."
-  success &= TorqueFormatProcessor().RunOnPath(workspace)
+  success &= TorqueFormatProcessor(use_cache=use_linter_cache).RunOnPath(workspace)
   print "Running copyright header, trailing whitespaces and " \
         "two empty lines between declarations check..."
   success &= SourceProcessor().RunOnPath(workspace)