test-long-names.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Test of using the API with very long names. */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "libgccjit.h"
  5. #include "harness.h"
  6. /* 65KB */
  7. #define NAME_LENGTH (65 * 1024)
  8. static struct long_names
  9. {
  10. char struct_name[NAME_LENGTH];
  11. char fn_name[NAME_LENGTH];
  12. char local_name[NAME_LENGTH];
  13. char block_name[NAME_LENGTH];
  14. } long_names;
  15. static void
  16. populate_name (const char *prefix, char *buffer)
  17. {
  18. int i;
  19. /* Begin with the given prefix: */
  20. sprintf (buffer, prefix);
  21. /* Populate the rest of the buffer with 0123456789 repeatedly: */
  22. for (i = strlen (prefix); i < NAME_LENGTH - 1; i++)
  23. buffer[i] = '0' + (i % 10);
  24. /* NIL-terminate the buffer: */
  25. buffer[NAME_LENGTH - 1] = '\0';
  26. }
  27. static void
  28. populate_names (void)
  29. {
  30. populate_name ("struct_", long_names.struct_name);
  31. populate_name ("test_fn_", long_names.fn_name);
  32. populate_name ("local_", long_names.local_name);
  33. populate_name ("block_", long_names.block_name);
  34. }
  35. void
  36. create_code (gcc_jit_context *ctxt, void *user_data)
  37. {
  38. /* Where "ETC" is a very long suffix, let's try to inject the
  39. equivalent of:
  40. struct struct_ETC;
  41. int
  42. test_fn_ETC ()
  43. {
  44. int local_ETC;
  45. local_ETC = 42;
  46. return local_ETC;
  47. }
  48. to verify that the API copes with such long names. */
  49. populate_names ();
  50. gcc_jit_type *int_type =
  51. gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  52. /* We don't yet use this struct. */
  53. (void)gcc_jit_context_new_opaque_struct (ctxt, NULL,
  54. long_names.struct_name);
  55. gcc_jit_function *test_fn =
  56. gcc_jit_context_new_function (ctxt, NULL,
  57. GCC_JIT_FUNCTION_EXPORTED,
  58. int_type,
  59. long_names.fn_name,
  60. 0, NULL,
  61. 0);
  62. gcc_jit_lvalue *local =
  63. gcc_jit_function_new_local (test_fn,
  64. NULL,
  65. int_type,
  66. long_names.local_name);
  67. gcc_jit_block *block =
  68. gcc_jit_function_new_block (test_fn, long_names.block_name);
  69. gcc_jit_block_add_assignment (
  70. block,
  71. NULL,
  72. local,
  73. gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 42));
  74. gcc_jit_block_end_with_return (
  75. block, NULL,
  76. gcc_jit_lvalue_as_rvalue (local));
  77. }
  78. void
  79. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  80. {
  81. CHECK_NON_NULL (result);
  82. typedef int (*my_fn_type) (void);
  83. CHECK_NON_NULL (result);
  84. my_fn_type my_fn =
  85. (my_fn_type)gcc_jit_result_get_code (result, long_names.fn_name);
  86. CHECK_NON_NULL (my_fn);
  87. int val = my_fn ();
  88. CHECK_VALUE (val, 42);
  89. }