fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct {
  6. char *name;
  7. void *ptr;
  8. } label;
  9.  
  10. #define LABELED(rtype, xname, xlcount) label xname##__labels[xlcount];\
  11. int xname##__lcount = xlcount;\
  12. rtype xname
  13.  
  14. #define LABELS(xname) label *__labels = xname##__labels;\
  15. int __li = 0;
  16.  
  17. #define LABEL(lname) __labels[__li].name = #lname;\
  18. __labels[__li++].ptr = &&lname;
  19.  
  20. #define LABELPTR(xname, lname) getlptr(xname##__labels, xname##__lcount, #lname)
  21.  
  22. void *getlptr(label *labels, int lcount, char *name) {
  23. for (int i = 0; i < lcount; i++) if (!strcmp(name, labels[i].name)) return labels[i].ptr;
  24. return NULL;
  25. }
  26.  
  27. LABELED(void, test, 3) (void *ptr)
  28. {
  29. LABELS(test) {
  30. LABEL(l1);
  31. #define test_l1 LABELPTR(test, l1)
  32. LABEL(l2);
  33. #define test_l2 LABELPTR(test, l2)
  34. LABEL(l3);
  35. #define test_l3 LABELPTR(test, l3)
  36. }
  37.  
  38. if (ptr)
  39. goto *ptr;
  40. return;
  41. l1:
  42. printf("test1\n");
  43. return;
  44. l2:
  45. printf("test2\n");
  46. return;
  47. l3:
  48. printf("test3\n");
  49. return;
  50. }
  51.  
  52. int main(void)
  53. {
  54. test(0);
  55. test(test_l1);
  56. test(test_l2);
  57. test(test_l3);
  58. }
Success #stdin #stdout 0s 10320KB
stdin
Standard input is empty
stdout
test1
test2
test3