Skip to content
  • Gabriel Charette's avatar
    Automated IWYU fix for TaskRunner includes. · 5ff87ceb
    Gabriel Charette authored
    Pre-requisite to suppress message_loop.h from run_loop.h (issue 703346).
    
    A similar include fix for message_loop.h was done in r471412 but some
    targets are the removal as it looks like they properly didn't need
    message_loop.h but were relying on it to get task runner includes...
    
    The reasoning for this is the same as was once done in https://codereview.chromium.org/2443103003
    (it was then done only on targets that failed to compile instead of via script):
    scoped_refptr<Foo> requires full type of Foo to be defined not just fwd-declared.
    
    Script used:
    
    def Fix(file_path):
      content = refactor_lib.ReadFile(file_path)
    
      if not 'TaskRunner' in content:
        return False
    
      # Assume fwd-decls are correct in first pass.
      if 'class TaskRunner;' in content:
        return False
      if 'class SequencedTaskRunner;' in content:
        return False
      if 'class SingleThreadTaskRunner;' in content:
        return False
    
      # Using base:: prefix ensures we don't match fwd-decls and other things.
      # Will require a few fixups for missing includes in //base proper.
      # Complex prefix in regex attempts to skip comments.
      matches = re.compile(r'(private:|protected:|public:)|(\n *[^/\n][^/\n][^/\n]*base::(Sequenced|SingleThread)TaskRunner\b(>&|\*)?)', re.DOTALL).findall(content)
    
      if not matches:
        return False
    
      # Ignore instances in private sections (probably members or worst case methods
      # only used by impl which must include header already).
      in_private_section = False
    
      found_task_runner = False
      found_sequenced_task_runner = False
      found_single_thread_task_runner = False
      for match in matches:
        if match[0] == 'private:':
          in_private_section = True
          continue
        if match[0] == 'protected:':
          in_private_section = False
          continue
        if match[0] == 'public:':
          in_private_section = False
          continue
    
        # Otherwise match[0] was empty and we have a match[1] for the main thing.
        assert not match[0]
    
        # Only want to add the include if we don't have a match[3] (which indicates
        # this match is for a ref or a pointer).
        if match[3]:
          continue
    
        # Not a ref nor a pointer, count it if not in a private section, match[2]
        # tells which TaskRunner type it is.
        if not in_private_section:
          if not match[2]:
            found_task_runner = True
          elif match[2] == 'Sequenced':
            found_sequenced_task_runner = True
          elif match[2] == 'SingleThread':
            found_single_thread_task_runner = True
          else:
            assert False
    
      updated_content = content
    
      if found_task_runner:
        updated_content = refactor_lib.AddInclude(file_path, content, "base/task_runner.h")
      if found_sequenced_task_runner:
        updated_content = refactor_lib.AddInclude(file_path, content, "base/sequenced_task_runner.h")
      if found_single_thread_task_runner:
        updated_content = refactor_lib.AddInclude(file_path, content, "base/single_thread_task_runner.h")
    
      if updated_content == content:
        return False
    
      # Write updated file
      refactor_lib.WriteFile(file_path, updated_content)
    
      return True
    
    TBR=gab@chromiu.org
    BUG=703346
    CQ_INCLUDE_TRYBOTS=master.tryserver.blink:linux_trusty_blink_rel;master.tryserver.chromium.android:android_optional_gpu_tests_rel;master.tryserver.chromium.linux:linux_optional_gpu_tests_rel;master.tryserver.chromium.mac:mac_optional_gpu_tests_rel;master.tryserver.chromium.win:win_optional_gpu_tests_rel
    
    Review-Url: https://codereview.chromium.org/2884763002 .
    Cr-Commit-Position: refs/heads/master@{#472157}
    5ff87ceb