Guest User

Untitled

a guest
Nov 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. static inline external_function() // lacks the noreturn attribute
  2. { /* does not return */ }
  3.  
  4. void your_function() __attribute__((noreturn)) {
  5. external_function(); // the compiler thinks execution may continue ...
  6. __builtin_unreachable(); // ... and now it knows it won't go beyond here
  7. }
  8.  
  9. int compute(int) __attribute((pure)) { return /* expensive compute */ }
  10. if(condition) {
  11. int x = compute(input); // (1) no side effect => keep if x is used
  12. // (8) x is not used => remove
  13. printf("hello "); // (2) reachable + side effect => keep
  14. your_function(); // (3) reachable + side effect => keep
  15. // (4) unreachable beyond this point
  16. printf("word!n"); // (5) unreachable => remove
  17. printf("%dn", x); // (6) unreachable => remove
  18. // (7) mark 'x' as unused
  19. } else {
  20. // follows unreachable code, but can jump here
  21. // from reachable code, so this is reachable
  22. do_stuff(); // keep
  23. }
  24.  
  25. #include <stdio.h>
  26.  
  27. extern FILE *fopen (const char *__restrict __filename,
  28. const char *__restrict __modes)
  29. __attribute__ ((warning ("fopen is used")));
  30.  
  31. void
  32. show_map_without_care (void)
  33. {
  34. FILE *f = fopen ("/proc/self/maps", "r");
  35. do
  36. {
  37. char lin[64];
  38. fgets (lin, sizeof (lin), f);
  39. fputs (lin, stdout);
  40. }
  41. while (!feof (f));
  42. fclose (f);
  43. }
  44.  
  45. #define func(A) {func(A); __builtin_unreachable();}
  46.  
  47. #define func(A) ({func(A); __builtin_unreachable(); (int)0; })
  48.  
  49. // file ex.c
  50. // declare exit without any standard header
  51. void exit (int);
  52.  
  53. // define myexit as a static inline
  54. static inline void
  55. myexit (int c)
  56. {
  57. exit (c);
  58. }
  59.  
  60. // redeclare it as notreturn
  61. static inline void myexit (int c) __attribute__ ((noreturn));
  62.  
  63. int
  64. foo (int *p)
  65. {
  66. if (!p)
  67. myexit (1);
  68. if (p)
  69. return *p + 2;
  70. return 0;
  71. }
  72.  
  73. .type foo, @function
  74. foo:
  75. .LFB1:
  76. .cfi_startproc
  77. testq %rdi, %rdi # p
  78. je .L5 #,
  79. movl (%rdi), %eax # *p_2(D), *p_2(D)
  80. addl $2, %eax #, D.1768
  81. ret
  82. .L5:
  83. pushq %rax #
  84. .cfi_def_cfa_offset 16
  85. movb $1, %dil #,
  86. call exit #
  87. .cfi_endproc
  88. .LFE1:
  89. .size foo, .-foo
  90.  
  91. ;;file your_melt_mode.melt
  92. (module_is_gpl_compatible "GPLv3+")
  93. (defun my_finish_decl (decl)
  94. (let ( (tdecl (unbox :tree decl))
  95. )
  96. (match tdecl
  97. (?(tree_function_decl_named
  98. ?(tree_identifier ?(cstring_same "your_function_name")))
  99. ;;; code to add the noreturn attribute
  100. ;;; ....
  101. ))))
  102. (register_finish_decl_first my_finish_decl)
  103.  
  104. gcc -fplugin=melt
  105. -fplugin-arg-melt-extra=your_melt_mode.quicklybuilt
  106. -fplugin-arg-melt-mode=your_adding_attr_mode
  107. -O2 -I/your/include -c yourfile.c
Add Comment
Please, Sign In to add comment