xref: /llvm-project/clang/test/Preprocessor/line-directive.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -std=c99 -fsyntax-only -verify -pedantic %s
2 // RUN: not %clang_cc1 -E %s 2>&1 | grep 'blonk.c:92:2: error: ABC'
3 // RUN: not %clang_cc1 -E %s 2>&1 | grep 'blonk.c:93:2: error: DEF'
4 // RUN: not %clang_cc1 -E %s 2>&1 | grep 'line-directive.c:11:2: error: MAIN7'
5 // RUN: not %clang_cc1 -E %s 2>&1 | grep 'enter-1:118:2: error: ENTER1'
6 // RUN: not %clang_cc1 -E %s 2>&1 | grep 'main:121:2: error: MAIN2'
7 
8 // expected-error@+1{{cannot pop empty include stack}}
9 # 20 "" 2
10 
11 // a push/pop before any other line control
12 # 10 "enter-0" 1 // expected-warning {{this style of line directive is a GNU extension}}
13 # 11 "" 2 // pop to main file: expected-warning {{this style of line directive is a GNU extension}}
14 #error MAIN7
15 // expected-error@-1{{MAIN7}}
16 
17 #line 'a'            // expected-error {{#line directive requires a positive integer argument}}
18 #line 0              // expected-warning {{#line directive with zero argument is a GNU extension}}
19 #line 00             // expected-warning {{#line directive with zero argument is a GNU extension}}
20 #line 2147483648     // expected-warning {{C requires #line number to be less than 2147483648, allowed as extension}}
21 #line 42             // ok
22 #line 42 'a'         // expected-error {{invalid filename for #line directive}}
23 #line 42 "foo/bar/baz.h"  // ok
24 
25 
26 // #line directives expand macros.
27 #define A 42 "foo"
28 #line A
29 
30 # 42 // expected-warning {{this style of line directive is a GNU extension}}
31 # 42 "foo" // expected-warning {{this style of line directive is a GNU extension}}
32 # 42 "foo" 2 // expected-error {{invalid line marker flag '2': cannot pop empty include stack}}
33 // The next two lines do not get diagnosed because they are considered to be
34 // within the system header, where diagnostics are suppressed.
35 # 42 "foo" 1 3  // enter
36 # 42 "foo" 2 3  // exit
37 # 42 "foo" 2 3 4 // expected-error {{invalid line marker flag '2': cannot pop empty include stack}}
38 # 42 "foo" 3 4 // expected-warning {{this style of line directive is a GNU extension}}
39 
40 # 'a'            // expected-error {{invalid preprocessing directive}}
41 # 42 'f'         // expected-error {{invalid filename for line marker directive}}
42 # 42 1 3         // expected-error {{invalid filename for line marker directive}}
43 # 42 "foo" 3 1   // expected-error {{invalid flag line marker directive}}
44 # 42 "foo" 42    // expected-error {{invalid flag line marker directive}}
45 # 42 "foo" 1 2   // expected-error {{invalid flag line marker directive}}
46 # 42a33          // expected-error {{GNU line marker directive requires a simple digit sequence}}
47 
48 // These are checked by the RUN line.
49 #line 92 "blonk.c"
50 #error ABC
51 #error DEF
52 // expected-error@-2 {{ABC}}
53 #line 150
54 // expected-error@-3 {{DEF}}
55 
56 
57 // Verify that linemarker diddling of the system header flag works.
58 
59 # 192 "glomp.h" // not a system header.: expected-warning {{this style of line directive is a GNU extension}}
60 typedef int x;  // expected-note {{previous definition is here}}
61 typedef int x;  // expected-warning {{redefinition of typedef 'x' is a C11 feature}}
62 
63 # 192 "glomp.h" 3 // System header.
64 typedef int y;  // ok
65 typedef int y;  // ok
66 
67 typedef int q;  // q is in system header.
68 
69 #line 42 "blonk.h"  // doesn't change system headerness.
70 
71 typedef int z;  // ok
72 typedef int z;  // ok
73 
74 # 97     // doesn't change system headerness.
75 
76 typedef int z1;  // ok
77 typedef int z1;  // ok
78 
79 # 42 "blonk.h"  // DOES change system headerness.
80 
81 typedef int w;  // expected-note {{previous definition is here}}
82 typedef int w;  // expected-warning {{redefinition of typedef 'w' is a C11 feature}}
83 
84 typedef int q;  // original definition in system header, should not diagnose.
85 
86 // This should not produce an "extra tokens at end of #line directive" warning,
87 // because #line is allowed to contain expanded tokens.
88 #define EMPTY()
89 #line 2 "foo.c" EMPTY( )
90 #line 2 "foo.c" NONEMPTY( )  // expected-warning{{extra tokens at end of #line directive}}
91 
92 // PR3940
93 #line 0xf  // expected-error {{#line directive requires a simple digit sequence}}
94 #line 42U  // expected-error {{#line directive requires a simple digit sequence}}
95 
96 
97 // Line markers are digit strings interpreted as decimal numbers, this is
98 // 10, not 8.
99 #line 010  // expected-warning {{#line directive interprets number as decimal, not octal}}
100 extern int array[__LINE__ == 10 ? 1:-1];
101 
102 # 020      // expected-warning {{GNU line marker directive interprets number as decimal, not octal}} expected-warning {{this style of line directive is a GNU extension}}
103 extern int array_gnuline[__LINE__ == 20 ? 1:-1];
104 
105 /* PR3917 */
106 #line 41
107 extern char array2[\
108 _\
109 _LINE__ == 42 ? 1: -1];  /* line marker is location of first _ */
110 
111 # 51 // expected-warning {{this style of line directive is a GNU extension}}
112 extern char array2_gnuline[\
113 _\
114 _LINE__ == 52 ? 1: -1];  /* line marker is location of first _ */
115 
116 #line 0 "line-directive.c" // expected-warning {{#line directive with zero argument is a GNU extension}}
117 undefined t; // expected-error {{unknown type name 'undefined'}}
118 
119 # 115 "main" // expected-warning {{this style of line directive is a GNU extension}}
120 # 116 "enter-1" 1 // expected-warning {{this style of line directive is a GNU extension}}
121 # 117 "enter-2" 1 // expected-warning {{this style of line directive is a GNU extension}}
122 # 118 "" 2 // pop to enter-1: expected-warning {{this style of line directive is a GNU extension}}
123 #error ENTER1
124 // expected-error@-1{{ENTER1}}
125 # 121 "" 2 // pop to "main": expected-warning {{this style of line directive is a GNU extension}}
126 #error MAIN2
127 // expected-error@-1{{MAIN2}}
128