Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static inline external_function() // lacks the noreturn attribute
- { /* does not return */ }
- void your_function() __attribute__((noreturn)) {
- external_function(); // the compiler thinks execution may continue ...
- __builtin_unreachable(); // ... and now it knows it won't go beyond here
- }
- int compute(int) __attribute((pure)) { return /* expensive compute */ }
- if(condition) {
- int x = compute(input); // (1) no side effect => keep if x is used
- // (8) x is not used => remove
- printf("hello "); // (2) reachable + side effect => keep
- your_function(); // (3) reachable + side effect => keep
- // (4) unreachable beyond this point
- printf("word!n"); // (5) unreachable => remove
- printf("%dn", x); // (6) unreachable => remove
- // (7) mark 'x' as unused
- } else {
- // follows unreachable code, but can jump here
- // from reachable code, so this is reachable
- do_stuff(); // keep
- }
- #include <stdio.h>
- extern FILE *fopen (const char *__restrict __filename,
- const char *__restrict __modes)
- __attribute__ ((warning ("fopen is used")));
- void
- show_map_without_care (void)
- {
- FILE *f = fopen ("/proc/self/maps", "r");
- do
- {
- char lin[64];
- fgets (lin, sizeof (lin), f);
- fputs (lin, stdout);
- }
- while (!feof (f));
- fclose (f);
- }
- #define func(A) {func(A); __builtin_unreachable();}
- #define func(A) ({func(A); __builtin_unreachable(); (int)0; })
- // file ex.c
- // declare exit without any standard header
- void exit (int);
- // define myexit as a static inline
- static inline void
- myexit (int c)
- {
- exit (c);
- }
- // redeclare it as notreturn
- static inline void myexit (int c) __attribute__ ((noreturn));
- int
- foo (int *p)
- {
- if (!p)
- myexit (1);
- if (p)
- return *p + 2;
- return 0;
- }
- .type foo, @function
- foo:
- .LFB1:
- .cfi_startproc
- testq %rdi, %rdi # p
- je .L5 #,
- movl (%rdi), %eax # *p_2(D), *p_2(D)
- addl $2, %eax #, D.1768
- ret
- .L5:
- pushq %rax #
- .cfi_def_cfa_offset 16
- movb $1, %dil #,
- call exit #
- .cfi_endproc
- .LFE1:
- .size foo, .-foo
- ;;file your_melt_mode.melt
- (module_is_gpl_compatible "GPLv3+")
- (defun my_finish_decl (decl)
- (let ( (tdecl (unbox :tree decl))
- )
- (match tdecl
- (?(tree_function_decl_named
- ?(tree_identifier ?(cstring_same "your_function_name")))
- ;;; code to add the noreturn attribute
- ;;; ....
- ))))
- (register_finish_decl_first my_finish_decl)
- gcc -fplugin=melt
- -fplugin-arg-melt-extra=your_melt_mode.quicklybuilt
- -fplugin-arg-melt-mode=your_adding_attr_mode
- -O2 -I/your/include -c yourfile.c
Add Comment
Please, Sign In to add comment