tools/disable_tests/gtest.py
def find_conditions(lines: List[str], start_line: int, test_name: str):
  """Starting from a given line, find the conditions relating to this test.

  We step backwards until we find a preprocessor conditional block which defines
  the MAYBE_Foo macro for this test. The logic is fairly rigid - there are many
  ways in which test disabling could be expressed that we don't handle. We rely
  on the fact that there is a common convention that people stick to very
  consistently.

  The format we recognise looks like:

  #if <some preprocessor condition>
  #define MAYBE_TEST DISABLED_Test
  #else
  #define MAYBE_Test Test
  #endif

  We also allow for the branches to be swapped, i.e. for the false branch to
  define the disabled case. We don't handle anything else (e.g. nested #ifs,
  indirection through other macro definitions, wrapping the whole test, etc.).

  Args:
    lines: The lines of the file, in which to search.
    start_line: The starting point of the search. This should be the line at
      which the MAYBE_Foo macro is used to define the test.
    test_name: The name of the test we're searching for. This is only the test
      name, it doesn't include the suite name.
  """

  # State machine - step backwards until we find a line defining a macro with
  # the given name, keeping track of the most recent #endif we've encountered.
  # Once we've seen such a macro, terminate at the first #if or #ifdef we see.
  #
  # The range between the #if/#ifdef and the #endif is the range defining the
  # conditions under which this test is disabled.
  #
  # We also keep track of which branch disables the test, so we know whether to
  # negate the condition.

  disabled_test = 'DISABLED_' + test_name
  maybe_test = 'MAYBE_' + test_name
  start = None
  found_define = False
  in_disabled_branch = False
  most_recent_endif = None

  disabled_on_true = None

  for i in range(start_line, 0, -1):
    match = get_directive(lines, i)
    if not match:
      continue

    name, args = match
    if name == 'endif':
      most_recent_endif = i
    elif name == 'define' and args[0] == maybe_test:
      if most_recent_endif is None:
        raise Exception(
            f'{maybe_test} is defined outside of a preprocessor conditional')

      found_define = True
      if args[1] == disabled_test:
        in_disabled_branch = True
    elif name == 'else' and in_disabled_branch:
      disabled_on_true = False
      in_disabled_branch = False
    elif name in {'if', 'ifdef'} and found_define:
      if in_disabled_branch:
        disabled_on_true = True

      existing_conds = args[0]

      start = i
      break

  assert start is not None
  assert most_recent_endif is not None

  if not disabled_on_true:
    # TODO: Maybe 'not' should still wrap its args in a list, for consistency?
    existing_conds = ('not', existing_conds)

  return canonicalise(existing_conds), (start, most_recent_endif)
Starting from a given line, find the conditions relating to this test.

  We step backwards until we find a preprocessor conditional block which defines
  the MAYBE_Foo macro for this test. The logic is fairly rigid - there are many
  ways in which test disabling could be expressed that we don't handle. We rely
  on the fact that there is a common convention that people stick to very
  consistently.

  The format we recognise looks like:

  #if <some preprocessor condition>
  #define MAYBE_TEST DISABLED_Test
  #else
  #define MAYBE_Test Test
  #endif

  We also allow for the branches to be swapped, i.e. for the false branch to
  define the disabled case. We don't handle anything else (e.g. nested #ifs,
  indirection through other macro definitions, wrapping the whole test, etc.).

  Args:
    lines: The lines of the file, in which to search.
    start_line: The starting point of the search. This should be the line at
      which the MAYBE_Foo macro is used to define the test.
    test_name: The name of the test we're searching for. This is only the test
      name, it doesn't include the suite name.
  
Starting from a given line, find the conditions relating to this test.
We step backwards until we find a preprocessor conditional block which defines
the MAYBE_Foo macro for this test. The logic is fairly rigid - there are many
ways in which test disabling could be expressed that we don't handle. We rely
on the fact that there is a common convention that people stick to very
consistently.

The format we recognise looks like.



We also allow for the branches to be swapped, i.e. for the false branch to
define the disabled case. We don't handle anything else .
Starting from a given line find the conditions relating to this test . We step backwards until we find a preprocessor conditional block which defines the MAYBE_Foo macro for this test . The logic is fairly rigid - there are many ways in which test disabling could be expressed that we don ' t handle . We rely on the fact that there is a common convention that people stick to very consistently . The format we recognise looks like . We also allow for the branches to be swapped i . e . for the false branch to define the disabled case . We don ' t handle anything else .
def find_conditions(lines: List[str], start_line: int, test_name: str):
   The range between the if/ifdef and the endif is the range defining the
   conditions under which this test is disabled.
   We also keep track of which branch disables the test, so we know whether to
   negate the condition.
  disabled_test = 'DISABLED_' + test_name
  maybe_test = 'MAYBE_' + test_name
  start = None
  found_define = False
  in_disabled_branch = False
  most_recent_endif = None
  disabled_on_true = None
  for i in range(start_line, 0, -1):
    match = get_directive(lines, i)
    if not match:
      continue
    name, args = match
    if name == 'endif':
      most_recent_endif = i
    elif name == 'define' and args[0] == maybe_test:
      if most_recent_endif is None:
        raise Exception(
            f'{maybe_test} is defined outside of a preprocessor conditional')
      found_define = True
      if args[1] == disabled_test:
        in_disabled_branch = True
    elif name == 'else' and in_disabled_branch:
      disabled_on_true = False
      in_disabled_branch = False
    elif name in {'if', 'ifdef'} and found_define:
      if in_disabled_branch:
        disabled_on_true = True
      existing_conds = args[0]
      start = i
      break
  assert start is not None
  assert most_recent_endif is not None
  if not disabled_on_true:
     TODO: Maybe 'not' should still wrap its args in a list, for consistency?
    existing_conds = ('not', existing_conds)
  return canonicalise(existing_conds), (start, most_recent_endif)
def find_conditions ( lines : List [ str ] , start_line : int , test_name : str ) : disabled_test = 'DISABLED_' + test_name maybe_test = 'MAYBE_' + test_name start = None found_define = False in_disabled_branch = False most_recent_endif = None disabled_on_true = None for i in range ( start_line , 0 , - 1 ) : match = get_directive ( lines , i ) if not match : continue name , args = match if name == 'endif' : most_recent_endif = i elif name == 'define' and args [ 0 ] == maybe_test : if most_recent_endif is None : raise Exception ( f'{maybe_test} is defined outside of a preprocessor conditional' ) found_define = True if args [ 1 ] == disabled_test : in_disabled_branch = True elif name == 'else' and in_disabled_branch : disabled_on_true = False in_disabled_branch = False elif name in { 'if' , 'ifdef' } and found_define : if in_disabled_branch : disabled_on_true = True existing_conds = args [ 0 ] start = i break assert start is not None assert most_recent_endif is not None if not disabled_on_true : existing_conds = ( 'not' , existing_conds ) return canonicalise ( existing_conds ) , ( start , most_recent_endif )
Starting from a given line, find the conditions relating to this test.
Starting from a given line find the conditions relating to this test .
"""Starting from a given line, find the conditions relating to this test.

  We step backwards until we find a preprocessor conditional block which defines
  the MAYBE_Foo macro for this test. The logic is fairly rigid - there are many
  ways in which test disabling could be expressed that we don't handle. We rely
  on the fact that there is a common convention that people stick to very
  consistently.

  The format we recognise looks like:

  #if <some preprocessor condition>
  #define MAYBE_TEST DISABLED_Test
  #else
  #define MAYBE_Test Test
  #endif

  We also allow for the branches to be swapped, i.e. for the false branch to
  define the disabled case. We don't handle anything else (e.g. nested #ifs,
  indirection through other macro definitions, wrapping the whole test, etc.).

  Args:
    lines: The lines of the file, in which to search.
    start_line: The starting point of the search. This should be the line at
      which the MAYBE_Foo macro is used to define the test.
    test_name: The name of the test we're searching for. This is only the test
      name, it doesn't include the suite name.
  """ # State machine - step backwards until we find a line defining a macro with # the given name, keeping track of the most recent #endif we've encountered. # Once we've seen such a macro, terminate at the first #if or #ifdef we see. # # The range between the #if/#ifdef and the #endif is the range defining the # conditions under which this test is disabled. # # We also keep track of which branch disables the test, so we know whether to # negate the condition. # TODO: Maybe 'not' should still wrap its args in a list, for consistency?
lines
List[str]
The lines of the file, in which to search.
The lines of the file in which to search .
start_line
int
The starting point of the search. This should be the line at
which the MAYBE_Foo macro is used to define the test.
The starting point of the search . This should be the line at which the MAYBE_Foo macro is used to define the test .
test_name
str
The name of the test we're searching for. This is only the test
name, it doesn't include the suite name.
The name of the test we ' re searching for . This is only the test name it doesn ' t include the suite name .