Пример #1
0
def test_NormalizeIdentifiers_RewriterException(mocker):
    """Test that ClangException is raised if clang_rewriter returns 204."""
    mock_Popen = mocker.patch('subprocess.Popen')
    mock_Popen.return_value = MockProcess(204)
    with pytest.raises(errors.RewriterException):
        normalizer.NormalizeIdentifiers('', '.c', [])
    # RewriterException inherits from ClangException.
    with pytest.raises(errors.ClangException):
        normalizer.NormalizeIdentifiers('', '.c', [])
Пример #2
0
def test_NormalizeIdentifiers_ClangTimeout(mocker):
    """Test that ClangTimeout is raised if clang_rewriter returns with SIGKILL."""
    mock_Popen = mocker.patch('subprocess.Popen')
    mock_Popen.return_value = MockProcess(9)
    with pytest.raises(errors.ClangTimeout):
        normalizer.NormalizeIdentifiers('', '.c', [])
    # ClangTimeout inherits from ClangException.
    with pytest.raises(errors.ClangException):
        normalizer.NormalizeIdentifiers('', '.c', [])
Пример #3
0
def test_NormalizeIdentifiers_variable_names_function_scope():
    """Test that variable name sequence reset for each function."""
    assert normalizer.NormalizeIdentifiers(
        """
int foo(int bar, int car) { int blah = bar; }
int foobar(int hello, int bar) { int test = bar; }
""", '.c', []) == """
Пример #4
0
def test_NormalizeIdentifiers_undefined_not_rewritten():
    """Test that undefined functions and variables are not rewritten."""
    assert normalizer.NormalizeIdentifiers(
        """
void main(int argc, char** argv) {
  undefined_function(undefined_variable);
}
""", '.c', []) == """
Пример #5
0
def test_NormalizeIdentifiers_process_command(mocker):
    """Test the clang_rewriter comand which is run."""
    mock_Popen = mocker.patch("subprocess.Popen")
    mock_Popen.return_value = MockProcess(0)
    normalizer.NormalizeIdentifiers("", ".c", ["-foo"])
    subprocess.Popen.assert_called_once()
    cmd = subprocess.Popen.call_args_list[0][0][0]
    assert cmd[:3] == ["timeout", "-s9", "60"]
Пример #6
0
def test_NormalizeIdentifiers_process_command(mocker):
    """Test the clang_rewriter comand which is run."""
    mock_Popen = mocker.patch('subprocess.Popen')
    mock_Popen.return_value = MockProcess(0)
    normalizer.NormalizeIdentifiers('', '.c', ['-foo'])
    subprocess.Popen.assert_called_once()
    cmd = subprocess.Popen.call_args_list[0][0][0]
    assert cmd[:3] == ['timeout', '-s9', '60']
Пример #7
0
def test_NormalizeIdentifiers_small_c_program():
    """Test the output of a small program."""
    assert (normalizer.NormalizeIdentifiers(
        """
int main(int argc, char** argv) {}
""",
        ".c",
        [],
    ) == """
int A(int a, char** b) {}
""")
Пример #8
0
def test_NormalizeIdentifiers_small_cl_program():
    """Test the output of a small OpenCL program."""
    assert (normalizer.NormalizeIdentifiers(
        """
kernel void foo(global int* bar) {}
""",
        ".cl",
        [],
    ) == """
kernel void A(global int* a) {}
""")
Пример #9
0
def test_NormalizeIdentifiers_printf_not_rewritten():
    """Test that a call to printf is not rewritten."""
    assert normalizer.NormalizeIdentifiers(
        """
#include <stdio.h>

int main(int argc, char** argv) {
  printf("Hello, world!\\n");
  return 0;
}
""", '.c', []) == """
Пример #10
0
def test_NormalizeIdentifiers_variable_names_function_scope():
    """Test that variable name sequence reset for each function."""
    assert (normalizer.NormalizeIdentifiers(
        """
int foo(int bar, int car) { int blah = bar; }
int foobar(int hello, int bar) { int test = bar; }
""",
        ".c",
        [],
    ) == """
int A(int a, int b) { int c = a; }
int B(int a, int b) { int c = b; }
""")
Пример #11
0
def NormalizeIdentifiers(text: str) -> str:
    """Normalize identifiers in C++ source code.

  Args:
    text: The source code to rewrite.

  Returns:
    Source code with identifier names normalized.

  Raises:
    RewriterException: If rewriter found nothing to rewrite.
    ClangTimeout: If rewriter fails to complete within timeout_seconds.
  """
    return normalizer.NormalizeIdentifiers(text, ".cpp", CLANG_ARGS)
Пример #12
0
def NormalizeIdentifiers(text: str) -> str:
    """Normalize identifiers in OpenCL source code.

  Args:
    text: The source code to rewrite.

  Returns:
    Source code with identifier names normalized.

  Raises:
    RewriterException: If rewriter found nothing to rewrite.
    ClangTimeout: If rewriter fails to complete within timeout_seconds.
  """
    return normalizer.NormalizeIdentifiers(text, '.cl',
                                           GetClangArgs(use_shim=False))
Пример #13
0
def SequentialNormalizeIdentifiers(text: str) -> str:
    """Normalize identifiers sequentially in OpenCL source code.

  Args:
    text: The source code to rewrite.

  Returns:
    Source code with identifier names normalized.

  Raises:
    RewriterException: If rewriter found nothing to rewrite.
    ClangTimeout: If rewriter fails to complete within timeout_seconds.
  """
    return normalizer.NormalizeIdentifiers(text,
                                           ".c", [],
                                           sequential_rewrite=True)
Пример #14
0
def SequentialNormalizeIdentifiers(text: str, extra_args=[]) -> str:
    """Normalize identifiers sequentially in OpenCL source code.

  Args:
    text: The source code to rewrite.

  Returns:
    Source code with identifier names normalized.

  Raises:
    RewriterException: If rewriter found nothing to rewrite.
    ClangTimeout: If rewriter fails to complete within timeout_seconds.
  """
    return normalizer.NormalizeIdentifiers(text,
                                           ".cl",
                                           GetClangArgs(use_shim=False,
                                                        use_aux_headers=True,
                                                        extra_args=extra_args),
                                           sequential_rewrite=True)
Пример #15
0
def test_NormalizeIdentifiers_small_c_program():
    """Test the output of a small program."""
    assert normalizer.NormalizeIdentifiers(
        """
int main(int argc, char** argv) {}
""", '.c', []) == """
Пример #16
0
def test_NormalizeIdentifiers_empty_c_file():
    """Test that RewriterException is raised on an empty file."""
    with pytest.raises(errors.RewriterException):
        normalizer.NormalizeIdentifiers('', '.c', [])
Пример #17
0
def test_NormalizeIdentifiers_c_syntax_error():
    """Test that a syntax error does not prevent normalizer output."""
    assert normalizer.NormalizeIdentifiers('int main@@()! ##', '.c', [])