1#include <math.h>
2#include <stdlib.h>
3#include <stdio.h>
4
5#include "libgccjit.h"
6
7#include "harness.h"
8
9void
10create_code (gcc_jit_context *ctxt, void *user_data)
11{
12  /* Let's try to inject the equivalent of:
13       void
14       test_fn ()
15       {
16	 goto label;
17       }
18
19       void
20       other_fn ()
21       {
22         label:
23       };
24     where the destination block is in another function.
25  */
26  gcc_jit_type *void_t =
27    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
28
29  /* Build the test_fn.  */
30  gcc_jit_function *test_fn =
31    gcc_jit_context_new_function (ctxt, NULL,
32                                  GCC_JIT_FUNCTION_EXPORTED,
33                                  void_t,
34                                  "test_fn",
35                                  0, NULL,
36                                  0);
37  /* Build the other_fn.  */
38  gcc_jit_function *other_fn =
39    gcc_jit_context_new_function (ctxt, NULL,
40                                  GCC_JIT_FUNCTION_EXPORTED,
41                                  void_t,
42                                  "other_fn",
43                                  0, NULL,
44                                  0);
45
46  gcc_jit_block *initial =
47    gcc_jit_function_new_block (test_fn, "initial");
48  gcc_jit_block *block_within_other_fn =
49    gcc_jit_function_new_block (other_fn, "block_within_other_fn");
50
51  gcc_jit_block_end_with_jump (initial, NULL, block_within_other_fn);
52}
53
54void
55verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
56{
57  CHECK_VALUE (result, NULL);
58
59  CHECK_STRING_VALUE (gcc_jit_context_get_first_error (ctxt),
60		      "gcc_jit_block_end_with_jump:"
61		      " target block is not in same function:"
62		      " source block initial is in function test_fn"
63		      " whereas target block block_within_other_fn"
64		      " is in function other_fn");
65  /* Example of a testcase in which the last error != first error.  */
66  CHECK_STRING_VALUE (gcc_jit_context_get_last_error (ctxt),
67		      "unterminated block in other_fn: block_within_other_fn");
68}
69