|
| 1 | +// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -verify=expected,default %s |
| 2 | +// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -analyzer-config legacy-inlining-prevention=false -verify=expected,disabled %s |
| 3 | + |
| 4 | +int getNum(void); // Get an opaque number. |
| 5 | + |
| 6 | +void clang_analyzer_numTimesReached(void); |
| 7 | +void clang_analyzer_dump(int arg); |
| 8 | + |
| 9 | +//----------------------------------------------------------------------------- |
| 10 | +// Simple case: inlined function never reaches `analyzer-max-loop`. |
| 11 | + |
| 12 | +int inner_simple(void) { |
| 13 | + clang_analyzer_numTimesReached(); // expected-warning {{2}} |
| 14 | + return 42; |
| 15 | +} |
| 16 | + |
| 17 | +int outer_simple(void) { |
| 18 | + int x = inner_simple(); |
| 19 | + int y = inner_simple(); |
| 20 | + return 53 / (x - y); // expected-warning {{Division by zero}} |
| 21 | +} |
| 22 | + |
| 23 | +//----------------------------------------------------------------------------- |
| 24 | +// Inlined function always reaches `analyzer-max-loop`. |
| 25 | + |
| 26 | +int inner_fixed_loop_1(void) { |
| 27 | + int i; |
| 28 | + clang_analyzer_numTimesReached(); // expected-warning {{1}} |
| 29 | + for (i = 0; i < 10; i++); |
| 30 | + clang_analyzer_numTimesReached(); // no-warning |
| 31 | + return 42; |
| 32 | +} |
| 33 | + |
| 34 | +int outer_fixed_loop_1(void) { |
| 35 | + int x = inner_fixed_loop_1(); |
| 36 | + int y = inner_fixed_loop_1(); |
| 37 | + return 53 / (x - y); // no-warning |
| 38 | +} |
| 39 | + |
| 40 | +//----------------------------------------------------------------------------- |
| 41 | +// Inlined function always reaches `analyzer-max-loop`; inlining is prevented |
| 42 | +// even for different entry points. |
| 43 | +// This test uses `clang_analyzer_dump` and distinct `arg` values because |
| 44 | +// `clang_analyzer_numTimesReached` only counts the paths reaching that node |
| 45 | +// during the analysis of one particular entry point, so it cannot distinguish |
| 46 | +// "two entry points reached this, both with one path" (where the two reports |
| 47 | +// are unified as duplicates by the generic report postprocessing) and "one |
| 48 | +// entry point reached this with one path" (where naturally nothing shows that |
| 49 | +// the second entry point _tried_ to reach it). |
| 50 | + |
| 51 | +int inner_fixed_loop_2(int arg) { |
| 52 | + // Identical copy of inner_fixed_loop_1 |
| 53 | + int i; |
| 54 | + clang_analyzer_dump(arg); // expected-warning {{2}} |
| 55 | + for (i = 0; i < 10; i++); |
| 56 | + clang_analyzer_dump(arg); // no-warning |
| 57 | + return 42; |
| 58 | +} |
| 59 | + |
| 60 | +int outer_1_fixed_loop_2(void) { |
| 61 | + return inner_fixed_loop_2(1); |
| 62 | +} |
| 63 | + |
| 64 | +int outer_2_fixed_loop_2(void) { |
| 65 | + return inner_fixed_loop_2(2); |
| 66 | +} |
| 67 | + |
| 68 | +//----------------------------------------------------------------------------- |
| 69 | +// Inlined function reaches `analyzer-max-loop` only in its second call. The |
| 70 | +// function is inlined twice but the second call doesn't finish and ends up |
| 71 | +// being conservatively evaluated. |
| 72 | + |
| 73 | +int inner_parametrized_loop_1(int count) { |
| 74 | + int i; |
| 75 | + clang_analyzer_numTimesReached(); // expected-warning {{2}} |
| 76 | + for (i = 0; i < count; i++); |
| 77 | + clang_analyzer_numTimesReached(); // expected-warning {{1}} |
| 78 | + return 42; |
| 79 | +} |
| 80 | + |
| 81 | +int outer_parametrized_loop_1(void) { |
| 82 | + int x = inner_parametrized_loop_1(2); |
| 83 | + int y = inner_parametrized_loop_1(10); |
| 84 | + return 53 / (x - y); // no-warning |
| 85 | +} |
| 86 | + |
| 87 | +//----------------------------------------------------------------------------- |
| 88 | +// Inlined function reaches `analyzer-max-loop` on its first call, so the |
| 89 | +// second call isn't inlined (although it could be fully evaluated). |
| 90 | + |
| 91 | +int inner_parametrized_loop_2(int count) { |
| 92 | + int i; |
| 93 | + clang_analyzer_numTimesReached(); // expected-warning {{1}} |
| 94 | + for (i = 0; i < count; i++); |
| 95 | + clang_analyzer_numTimesReached(); // no-warning |
| 96 | + return 42; |
| 97 | +} |
| 98 | + |
| 99 | +int outer_parametrized_loop_2(void) { |
| 100 | + int y = inner_parametrized_loop_2(10); |
| 101 | + int x = inner_parametrized_loop_2(2); |
| 102 | + return 53 / (x - y); // no-warning |
| 103 | +} |
| 104 | + |
| 105 | +//----------------------------------------------------------------------------- |
| 106 | +// Inlined function may or may not reach `analyzer-max-loop` depending on an |
| 107 | +// opaque check before the loop. This is very similar to the "fixed loop" |
| 108 | +// cases: the function is placed on the "don't inline" list when any execution |
| 109 | +// path reaches `analyzer-max-loop` (even if other execution paths reach the |
| 110 | +// end of the function). |
| 111 | + |
| 112 | +int inner_conditional_loop(void) { |
| 113 | + int i; |
| 114 | + clang_analyzer_numTimesReached(); // expected-warning {{1}} |
| 115 | + if (getNum() == 777) { |
| 116 | + for (i = 0; i < 10; i++); |
| 117 | + } |
| 118 | + clang_analyzer_numTimesReached(); // expected-warning {{1}} |
| 119 | + return 42; |
| 120 | +} |
| 121 | + |
| 122 | +int outer_1_conditional_loop(void) { |
| 123 | + return inner_conditional_loop(); |
| 124 | +} |
| 125 | + |
| 126 | +int outer_2_conditional_loop(void) { |
| 127 | + return inner_conditional_loop(); |
| 128 | +} |
| 129 | + |
| 130 | +//----------------------------------------------------------------------------- |
| 131 | +// Inlined function executes an opaque loop that may or may not reach |
| 132 | +// `analyzer-max-loop`. Historically, before the "don't assume third iteration" |
| 133 | +// commit (bb27d5e5c6b194a1440b8ac4e5ace68d0ee2a849) this worked like the |
| 134 | +// `conditional_loop` cases: the analyzer was able to find a path reaching |
| 135 | +// `analyzer-max-loop` so inlining was disabled. After that commit the analyzer |
| 136 | +// does not _assume_ a third (or later) iteration (i.e. does not enter those |
| 137 | +// iterations if the loop condition is an unknown value), so e.g. this test |
| 138 | +// function does not reach `analyzer-max-loop` iterations and the inlining is |
| 139 | +// not disabled. |
| 140 | +// Unfortunately this change significantly increased the workload and |
| 141 | +// runtime of the analyzer (more entry points used up their budget), so the |
| 142 | +// option `legacy-inlining-prevention` was introduced and enabled by default to |
| 143 | +// suppress the inlining in situations where the "don't assume third iteration" |
| 144 | +// logic activates. |
| 145 | +// This testcase demonstrate that the inlining is prevented with the default |
| 146 | +// `legacy-inlining-prevention=true` config, but is not prevented when this |
| 147 | +// option is disabled (set to false). |
| 148 | + |
| 149 | +int inner_opaque_loop_1(void) { |
| 150 | + int i; |
| 151 | + clang_analyzer_numTimesReached(); // default-warning {{1}} disabled-warning {{2}} |
| 152 | + for (i = 0; i < getNum(); i++); |
| 153 | + return i; |
| 154 | +} |
| 155 | + |
| 156 | +int outer_opaque_loop_1(void) { |
| 157 | + int iterCount = inner_opaque_loop_1(); |
| 158 | + |
| 159 | + // The first call to `inner_opaque_loop_1()` splits three execution paths that |
| 160 | + // differ in the number of performed iterations (0, 1 or 2). The function |
| 161 | + // `inner_opaque_loop_1` is added to the "do not inline this" list when the |
| 162 | + // path that performed two iterations tries to enter the third iteration (and |
| 163 | + // the "don't assume third iteration" logic prevents this) -- but the other |
| 164 | + // two paths (which performed 0 and 1 iterations) would reach and inline the |
| 165 | + // second `inner_opaque_loop_1()` before this would happen (because the |
| 166 | + // default traversal is a complex heuristic that happens to prefer this). The |
| 167 | + // following `if` will discard these "early exit" paths to highlight the |
| 168 | + // difference between the default and disabled state: |
| 169 | + if (iterCount < 2) |
| 170 | + return 0; |
| 171 | + |
| 172 | + return inner_opaque_loop_1(); |
| 173 | +} |
| 174 | + |
| 175 | +//----------------------------------------------------------------------------- |
| 176 | +// Another less contrived testcase that demonstrates the difference between the |
| 177 | +// enabled (default) and disabled state of `legacy-inlining-prevention`. |
| 178 | +// Here the two calls to `inner_opaque_loop_2()` are in different entry points |
| 179 | +// so the first call is fully analyzed (and can put the function on the "do |
| 180 | +// not inline" list) before reaching the second call. |
| 181 | +// This test uses `clang_analyzer_dump` because (as explained in an earlier |
| 182 | +// comment block) `clang_analyzer_numTimesReached` is not suitable for counting |
| 183 | +// visits from separate entry points. |
| 184 | + |
| 185 | +int inner_opaque_loop_2(int arg) { |
| 186 | + int i; |
| 187 | + clang_analyzer_dump(arg); // default-warning {{2}} |
| 188 | + // disabled-warning@-1 {{1}} disabled-warning@-1 {{2}} |
| 189 | + for (i = 0; i < getNum(); i++); |
| 190 | + return i; |
| 191 | +} |
| 192 | + |
| 193 | +int outer_1_opaque_loop_2(void) { |
| 194 | + return inner_opaque_loop_2(1); |
| 195 | +} |
| 196 | +int outer_2_opaque_loop(void) { |
| 197 | + return inner_opaque_loop_2(2); |
| 198 | +} |
0 commit comments