1 // RUN: %clang_analyze_cc1 -analyzer-checker=debug.DumpCFG -triple x86_64-apple-darwin12 -fheinous-gnu-extensions %s > %t 2>&1
2 // RUN: FileCheck --input-file=%t -check-prefixes=CHECK,WARNINGS %s
3 
4 // This file is the C version of cfg.cpp.
5 // Tests that are C-specific should go into this file.
6 
7 // CHECK-LABEL: void checkWrap(int i)
8 // CHECK: ENTRY
9 // CHECK-NEXT: Succs (1): B1
10 // CHECK: [B1]
11 // CHECK: Succs (21): B2 B3 B4 B5 B6 B7 B8 B9
12 // CHECK: B10 B11 B12 B13 B14 B15 B16 B17 B18 B19
13 // CHECK: B20 B21 B0
14 // CHECK: [B0 (EXIT)]
15 // CHECK-NEXT: Preds (21): B2 B3 B4 B5 B6 B7 B8 B9
16 // CHECK-NEXT: B10 B11 B12 B13 B14 B15 B16 B17 B18 B19
17 // CHECK-NEXT: B20 B21 B1
checkWrap(int i)18 void checkWrap(int i) {
19   switch(i) {
20     case 0: break;
21     case 1: break;
22     case 2: break;
23     case 3: break;
24     case 4: break;
25     case 5: break;
26     case 6: break;
27     case 7: break;
28     case 8: break;
29     case 9: break;
30     case 10: break;
31     case 11: break;
32     case 12: break;
33     case 13: break;
34     case 14: break;
35     case 15: break;
36     case 16: break;
37     case 17: break;
38     case 18: break;
39     case 19: break;
40   }
41 }
42 
43 // CHECK-LABEL: void checkGCCAsmRValueOutput()
44 // CHECK: [B2 (ENTRY)]
45 // CHECK-NEXT: Succs (1): B1
46 // CHECK: [B1]
47 // CHECK-NEXT:   1: int arg
48 // CHECK-NEXT:   2: arg
49 // CHECK-NEXT:   3: (int)[B1.2] (CStyleCastExpr, NoOp, int)
50 // CHECK-NEXT:   4: asm ("" : "=r" ([B1.3]));
51 // CHECK-NEXT:   5: arg
52 // CHECK-NEXT:   6: asm ("" : "=r" ([B1.5]));
checkGCCAsmRValueOutput()53 void checkGCCAsmRValueOutput() {
54   int arg;
55   __asm__("" : "=r"((int)arg));  // rvalue output operand
56   __asm__("" : "=r"(arg));       // lvalue output operand
57 }
58 
59 // CHECK-LABEL: int overlap_compare(int x)
60 // CHECK: [B2]
61 // CHECK-NEXT:   1: 1
62 // CHECK-NEXT:   2: return [B2.1];
63 // CHECK-NEXT:   Preds (1): B3(Unreachable)
64 // CHECK-NEXT:   Succs (1): B0
65 // CHECK: [B3]
66 // CHECK-NEXT:   1: x
67 // CHECK-NEXT:   2: [B3.1] (ImplicitCastExpr, LValueToRValue, int)
68 // CHECK-NEXT:   3: 5
69 // CHECK-NEXT:   4: [B3.2] > [B3.3]
70 // CHECK-NEXT:   T: if [B4.5] && [B3.4]
71 // CHECK-NEXT:   Preds (1): B4
72 // CHECK-NEXT:   Succs (2): B2(Unreachable) B1
overlap_compare(int x)73 int overlap_compare(int x) {
74   if (x == -1 && x > 5)
75     return 1;
76 
77   return 2;
78 }
79 
80 // CHECK-LABEL: void vla_simple(int x)
81 // CHECK: [B1]
82 // CHECK-NEXT:   1: x
83 // CHECK-NEXT:   2: [B1.1] (ImplicitCastExpr, LValueToRValue, int)
84 // CHECK-NEXT:   3: int vla[x];
vla_simple(int x)85 void vla_simple(int x) {
86   int vla[x];
87 }
88 
89 // CHECK-LABEL: void vla_typedef(int x)
90 // CHECK: [B1]
91 // CHECK-NEXT:   1: x
92 // CHECK-NEXT:   2: [B1.1] (ImplicitCastExpr, LValueToRValue, int)
93 // CHECK-NEXT:   3: typedef int VLA[x];
vla_typedef(int x)94 void vla_typedef(int x) {
95   typedef int VLA[x];
96 }
97 
98 // CHECK-LABEL: void vla_typedef_multi(int x, int y)
99 // CHECK:  [B1]
100 // CHECK-NEXT:   1: y
101 // CHECK-NEXT:   2: [B1.1] (ImplicitCastExpr, LValueToRValue, int)
102 // CHECK-NEXT:   3: x
103 // CHECK-NEXT:   4: [B1.3] (ImplicitCastExpr, LValueToRValue, int)
104 // CHECK-NEXT:   5: typedef int VLA[x][y];
vla_typedef_multi(int x,int y)105 void vla_typedef_multi(int x, int y) {
106   typedef int VLA[x][y];
107 }
108 
109 // CHECK-LABEL: void vla_type_indirect(int x)
110 // CHECK:  [B1]
111 // CHECK-NEXT:   1: int (*p_vla)[x];
112 // CHECK-NEXT:   2: void (*fp_vla)(int *);
vla_type_indirect(int x)113 void vla_type_indirect(int x) {
114   // Should evaluate x
115   // FIXME: does not work
116   int (*p_vla)[x];
117 
118   // Do not evaluate x
119   void (*fp_vla)(int[x]);
120 }
121