def test_CompileLlvmBytecode_ClangTimeout(mocker): """Test that ClangTimeout is raised if clang returns with SIGKILL.""" mock_Popen = mocker.patch("subprocess.Popen") mock_Popen.return_value = MockProcess(9) with test.Raises(errors.ClangTimeout): clang.CompileLlvmBytecode("", ".c", []) # ClangTimeout inherits from ClangException. with test.Raises(errors.ClangException): clang.CompileLlvmBytecode("", ".c", [])
def test_CompileLlvmBytecode_small_c_program(): """Test that bytecode is produced for a small C program.""" bc = clang.CompileLlvmBytecode("int main() {}", ".c", []) # The full bytecode output is too fragile to compare against, e.g. it contains # the file path, target layout, exact LLVM build version, etc. Instead we just # check for some expected lines. assert "source_filename =" in bc assert "define i32 @main() #0 {" in bc assert "ret i32 0" in bc
def test_CompileLlvmBytecode_command(mocker): """Test the clang comand which is run.""" mock_Popen = mocker.patch("subprocess.Popen") mock_Popen.return_value = MockProcess(0) clang.CompileLlvmBytecode("", ".c", ["-foo"]) subprocess.Popen.assert_called_once() cmd = subprocess.Popen.call_args_list[0][0][0] assert cmd[:3] == ["timeout", "-s9", "60"] assert cmd[5:] == ["-S", "-emit-llvm", "-o", "-", "-foo"]
def test_CompileLlvmBytecode_command(mocker): """Test the clang comand which is run.""" mock_Popen = mocker.patch('subprocess.Popen') mock_Popen.return_value = MockProcess(0) clang.CompileLlvmBytecode('', '.c', ['-foo']) subprocess.Popen.assert_called_once() cmd = subprocess.Popen.call_args_list[0][0][0] assert cmd[:3] == ['timeout', '-s9', '60'] assert cmd[5:] == ['-S', '-emit-llvm', '-o', '-', '-foo']
def CompileLlvmBytecode(text: str) -> str: """A preprocessor which attempts to compile the given code. Args: text: Code to compile. Returns: LLVM IR of input source code. """ return clang.CompileLlvmBytecode(text, ".c", CLANG_ARGS)
def Compile(text: str) -> str: """A preprocessor which attempts to compile the given code. Args: text: Code to compile. Returns: The input code, unmodified. """ clang.CompileLlvmBytecode(text, ".cpp", CLANG_ARGS) return text
def Compile(text: str) -> str: """Check that the OpenCL source compiles. This does not modify the input. Args: text: OpenCL source to check. Returns: Unmodified OpenCL source. """ # We must override the flag -Wno-implicit-function-declaration from # GetClangArgs() to ensure that undefined functions are treated as errors. clang.CompileLlvmBytecode( text, '.cl', GetClangArgs(use_shim=False) + ['-Werror=implicit-function-declaration']) return text
def CompileLlvmBytecode(text: str, header_file=None, use_aux_headers: bool = True, extra_args: typing.List[str] = []) -> str: """A preprocessor which attempts to compile the given code. Args: text: Code to compile. Returns: LLVM IR of input source code. """ # We must override the flag -Wno-implicit-function-declaration from # GetClangArgs() to ensure that undefined functions are treated as errors. return clang.CompileLlvmBytecode( text, ".cl", GetClangArgs(use_shim=False, use_aux_headers=use_aux_headers, extra_args=extra_args ), # + ["-Werror=implicit-function-declaration"], header_file=header_file, )
def test_CompileLlvmBytecode_c_syntax_error(): """Test that a ClangException is raised for a C program with syntax error.""" with test.Raises(errors.ClangException): clang.CompileLlvmBytecode("int main@@()! ##", ".c", [])
def test_CompileLlvmBytecode_small_cl_program(): """Test that bytecode is produced for a small OpenCL program.""" bc = clang.CompileLlvmBytecode("kernel void A() {}", ".cl", []) assert "source_filename =" in bc assert "target datalayout =" in bc assert "target triple =" in bc
def test_CompileLlvmBytecode_empty_c_file(): """Test that bytecode is produced for an empty C program.""" bc = clang.CompileLlvmBytecode("", ".c", []) assert "source_filename =" in bc assert "target datalayout =" in bc assert "target triple =" in bc
def test_CompileLlvmBytecode_ClangException(mocker): """Test that ClangException is raised if clang returns non-zero.""" mock_Popen = mocker.patch("subprocess.Popen") mock_Popen.return_value = MockProcess(1) with test.Raises(errors.ClangException): clang.CompileLlvmBytecode("", ".c", [])
def test_CompileLlvmBytecode_small_cl_program(): """Test that bytecode is produced for a small OpenCL program.""" bc = clang.CompileLlvmBytecode('kernel void A() {}', '.cl', []) assert 'source_filename =' in bc assert 'target datalayout =' in bc assert 'target triple =' in bc
def test_CompileLlvmBytecode_empty_c_file(): """Test that bytecode is produced for an empty C program.""" bc = clang.CompileLlvmBytecode('', '.c', []) assert 'source_filename =' in bc assert 'target datalayout =' in bc assert 'target triple =' in bc