xref: /llvm-project/clang/test/Analysis/no-return.c (revision ec6da3fb9d8c7859da22d9eb7e814faf2e5a524a)
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
2 
3 typedef void(fatal_fun)() __attribute__((__noreturn__));
4 fatal_fun* fatal_fptr;
5 void fatal_decl() __attribute__((__noreturn__));
6 
7 int rng();
8 
9 /// This code calls a [[noreturn]] function pointer, which used to be handled
10 /// inconsistently between AST builder and CSA.
11 /// In the result, CSA produces a path where this function returns non-0.
return_zero_or_abort_by_fnptr()12 int return_zero_or_abort_by_fnptr() {
13   if (rng()) fatal_fptr();
14   return 0;
15 }
16 
17 /// This function calls a [[noreturn]] function.
18 /// If it does return, it always returns 0.
return_zero_or_abort_by_direct_fun()19 int return_zero_or_abort_by_direct_fun() {
20   if (rng()) fatal_decl();
21   return 0;
22 }
23 
24 /// Trigger a division by zero issue depending on the return value
25 /// of the called functions.
caller()26 int caller() {
27   int x = 0;
28   // The following if branches must never be taken.
29   if (return_zero_or_abort_by_fnptr())
30     return 1 / x; // no-warning: Dead code.
31   if (return_zero_or_abort_by_direct_fun())
32     return 1 / x; // no-warning: Dead code.
33 
34   // Make sure the warning is still reported when viable.
35   return 1 / x; // expected-warning {{Division by zero}}
36 }
37