xref: /aosp_15_r20/external/angle/src/tests/preprocessor_tests/define_test.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2012 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #include <sstream>
8 
9 #include "PreprocessorTest.h"
10 #include "compiler/preprocessor/Token.h"
11 
12 namespace angle
13 {
14 
15 using testing::_;
16 
17 class DefineTest : public SimplePreprocessorTest
18 {};
19 
TEST_F(DefineTest,NonIdentifier)20 TEST_F(DefineTest, NonIdentifier)
21 {
22     const char *input =
23         "#define 2 foo\n"
24         "2\n";
25     const char *expected =
26         "\n"
27         "2\n";
28     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
29         .Times(1);
30     EXPECT_CALL(mDiagnostics,
31                 print(pp::Diagnostics::PP_UNEXPECTED_TOKEN, pp::SourceLocation(0, 1), "2"));
32 
33     preprocess(input, expected);
34 }
35 
TEST_F(DefineTest,RedefinePredefined)36 TEST_F(DefineTest, RedefinePredefined)
37 {
38     const char *input =
39         "#define __LINE__ 10\n"
40         "__LINE__\n"
41         "#define __FILE__ 20\n"
42         "__FILE__\n"
43         "#define __VERSION__ 200\n"
44         "__VERSION__\n"
45         "#define GL_ES 0\n"
46         "GL_ES\n";
47     const char *expected =
48         "\n"
49         "2\n"
50         "\n"
51         "0\n"
52         "\n"
53         "100\n"
54         "\n"
55         "1\n";
56     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
57         .Times(1);
58     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_PREDEFINED_REDEFINED,
59                                     pp::SourceLocation(0, 1), "__LINE__"));
60     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_PREDEFINED_REDEFINED,
61                                     pp::SourceLocation(0, 3), "__FILE__"));
62     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_PREDEFINED_REDEFINED,
63                                     pp::SourceLocation(0, 5), "__VERSION__"));
64     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_PREDEFINED_REDEFINED,
65                                     pp::SourceLocation(0, 7), "GL_ES"));
66 
67     preprocess(input, expected);
68 }
69 
TEST_F(DefineTest,ReservedUnderScore1)70 TEST_F(DefineTest, ReservedUnderScore1)
71 {
72     const char *input =
73         "#define __foo bar\n"
74         "__foo\n";
75     const char *expected =
76         "\n"
77         "bar\n";
78     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
79         .Times(1);
80     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_WARNING_MACRO_NAME_RESERVED,
81                                     pp::SourceLocation(0, 1), "__foo"));
82 
83     preprocess(input, expected);
84 }
85 
TEST_F(DefineTest,ReservedUnderScore2)86 TEST_F(DefineTest, ReservedUnderScore2)
87 {
88     const char *input =
89         "#define foo__bar baz\n"
90         "foo__bar\n";
91     const char *expected =
92         "\n"
93         "baz\n";
94     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
95         .Times(1);
96     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_WARNING_MACRO_NAME_RESERVED,
97                                     pp::SourceLocation(0, 1), "foo__bar"));
98 
99     preprocess(input, expected);
100 }
101 
TEST_F(DefineTest,ReservedGL)102 TEST_F(DefineTest, ReservedGL)
103 {
104     const char *input =
105         "#define GL_foo bar\n"
106         "GL_foo\n";
107     const char *expected =
108         "\n"
109         "GL_foo\n";
110     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
111         .Times(1);
112     EXPECT_CALL(mDiagnostics,
113                 print(pp::Diagnostics::PP_MACRO_NAME_RESERVED, pp::SourceLocation(0, 1), "GL_foo"));
114 
115     preprocess(input, expected);
116 }
117 
TEST_F(DefineTest,ObjRedefineValid)118 TEST_F(DefineTest, ObjRedefineValid)
119 {
120     const char *input =
121         "#define foo (1-1)\n"
122         "#define foo /* whitespace */ (1-1) /* other */ \n"
123         "foo\n";
124     const char *expected =
125         "\n"
126         "\n"
127         "(1-1)\n";
128     // No error or warning.
129     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
130         .Times(1);
131     EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
132 
133     preprocess(input, expected);
134 }
135 
TEST_F(DefineTest,ObjRedefineInvalid)136 TEST_F(DefineTest, ObjRedefineInvalid)
137 {
138     const char *input =
139         "#define foo (0)\n"
140         "#define foo (1-1)\n"
141         "foo\n";
142     const char *expected =
143         "\n"
144         "\n"
145         "(0)\n";
146     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
147         .Times(1);
148     EXPECT_CALL(mDiagnostics,
149                 print(pp::Diagnostics::PP_MACRO_REDEFINED, pp::SourceLocation(0, 2), "foo"));
150 
151     preprocess(input, expected);
152 }
153 
TEST_F(DefineTest,FuncRedefineValid)154 TEST_F(DefineTest, FuncRedefineValid)
155 {
156     const char *input =
157         "#define foo(a) ( a )\n"
158         "#define foo( a )( /* whitespace */ a /* other */ )\n"
159         "foo(b)\n";
160     const char *expected =
161         "\n"
162         "\n"
163         "( b )\n";
164     // No error or warning.
165     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
166         .Times(1);
167     EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0);
168 
169     preprocess(input, expected);
170 }
171 
TEST_F(DefineTest,FuncRedefineInvalid)172 TEST_F(DefineTest, FuncRedefineInvalid)
173 {
174     const char *input =
175         "#define foo(b) ( a )\n"
176         "#define foo(b) ( b )\n"
177         "foo(1)\n";
178     const char *expected =
179         "\n"
180         "\n"
181         "( a )\n";
182     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
183         .Times(1);
184     EXPECT_CALL(mDiagnostics,
185                 print(pp::Diagnostics::PP_MACRO_REDEFINED, pp::SourceLocation(0, 2), "foo"));
186 
187     preprocess(input, expected);
188 }
189 
TEST_F(DefineTest,ObjBasic)190 TEST_F(DefineTest, ObjBasic)
191 {
192     const char *input =
193         "#define foo 1\n"
194         "foo\n";
195     const char *expected =
196         "\n"
197         "1\n";
198     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
199         .Times(1);
200     preprocess(input, expected);
201 }
202 
TEST_F(DefineTest,ObjEmpty)203 TEST_F(DefineTest, ObjEmpty)
204 {
205     const char *input =
206         "#define foo\n"
207         "foo\n";
208     const char *expected =
209         "\n"
210         "\n";
211     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
212         .Times(1);
213     preprocess(input, expected);
214 }
215 
TEST_F(DefineTest,ObjChain)216 TEST_F(DefineTest, ObjChain)
217 {
218     const char *input =
219         "#define foo 1\n"
220         "#define bar foo\n"
221         "bar\n";
222     const char *expected =
223         "\n"
224         "\n"
225         "1\n";
226     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
227         .Times(1);
228     preprocess(input, expected);
229 }
230 
TEST_F(DefineTest,ObjChainReverse)231 TEST_F(DefineTest, ObjChainReverse)
232 {
233     const char *input =
234         "#define bar foo\n"
235         "#define foo 1\n"
236         "bar\n";
237     const char *expected =
238         "\n"
239         "\n"
240         "1\n";
241     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
242         .Times(1);
243     preprocess(input, expected);
244 }
245 
TEST_F(DefineTest,ObjRecursive)246 TEST_F(DefineTest, ObjRecursive)
247 {
248     const char *input =
249         "#define foo bar\n"
250         "#define bar baz\n"
251         "#define baz foo\n"
252         "foo\n"
253         "bar\n"
254         "baz\n";
255     const char *expected =
256         "\n"
257         "\n"
258         "\n"
259         "foo\n"
260         "bar\n"
261         "baz\n";
262     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
263         .Times(1);
264     preprocess(input, expected);
265 }
266 
TEST_F(DefineTest,ObjCompositeChain)267 TEST_F(DefineTest, ObjCompositeChain)
268 {
269     const char *input =
270         "#define foo 1\n"
271         "#define bar a foo\n"
272         "bar\n";
273     const char *expected =
274         "\n"
275         "\n"
276         "a 1\n";
277     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
278         .Times(1);
279     preprocess(input, expected);
280 }
281 
TEST_F(DefineTest,ObjCompositeChainReverse)282 TEST_F(DefineTest, ObjCompositeChainReverse)
283 {
284     const char *input =
285         "#define bar a foo\n"
286         "#define foo 1\n"
287         "bar\n";
288     const char *expected =
289         "\n"
290         "\n"
291         "a 1\n";
292     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
293         .Times(1);
294     preprocess(input, expected);
295 }
296 
TEST_F(DefineTest,ObjCompositeRecursive)297 TEST_F(DefineTest, ObjCompositeRecursive)
298 {
299     const char *input =
300         "#define foo a bar\n"
301         "#define bar b baz\n"
302         "#define baz c foo\n"
303         "foo\n"
304         "bar\n"
305         "baz\n";
306     const char *expected =
307         "\n"
308         "\n"
309         "\n"
310         "a b c foo\n"
311         "b c a bar\n"
312         "c a b baz\n";
313     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
314         .Times(1);
315     preprocess(input, expected);
316 }
317 
TEST_F(DefineTest,ObjChainSelfRecursive)318 TEST_F(DefineTest, ObjChainSelfRecursive)
319 {
320     const char *input =
321         "#define foo foo\n"
322         "#define bar foo\n"
323         "bar\n";
324     const char *expected =
325         "\n"
326         "\n"
327         "foo\n";
328     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
329         .Times(1);
330     preprocess(input, expected);
331 }
332 
TEST_F(DefineTest,ObjectLikeWithParens)333 TEST_F(DefineTest, ObjectLikeWithParens)
334 {
335     const char *input =
336         "#define foo ()1\n"
337         "foo()\n"
338         "#define bar ()2\n"
339         "bar()\n";
340     const char *expected =
341         "\n"
342         "()1()\n"
343         "\n"
344         "()2()\n";
345     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
346         .Times(1);
347     preprocess(input, expected);
348 }
349 
TEST_F(DefineTest,FuncEmpty)350 TEST_F(DefineTest, FuncEmpty)
351 {
352     const char *input =
353         "#define foo()\n"
354         "foo()\n";
355     const char *expected =
356         "\n"
357         "\n";
358     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
359         .Times(1);
360     preprocess(input, expected);
361 }
362 
TEST_F(DefineTest,FuncNoArgs)363 TEST_F(DefineTest, FuncNoArgs)
364 {
365     const char *input =
366         "#define foo() bar\n"
367         "foo()\n";
368     const char *expected =
369         "\n"
370         "bar\n";
371     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
372         .Times(1);
373     preprocess(input, expected);
374 }
375 
TEST_F(DefineTest,FuncOneArgUnused)376 TEST_F(DefineTest, FuncOneArgUnused)
377 {
378     const char *input =
379         "#define foo(x) 1\n"
380         "foo(bar)\n";
381     const char *expected =
382         "\n"
383         "1\n";
384     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
385         .Times(1);
386     preprocess(input, expected);
387 }
388 
TEST_F(DefineTest,FuncTwoArgsUnused)389 TEST_F(DefineTest, FuncTwoArgsUnused)
390 {
391     const char *input =
392         "#define foo(x,y) 1\n"
393         "foo(bar,baz)\n";
394     const char *expected =
395         "\n"
396         "1\n";
397     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
398         .Times(1);
399     preprocess(input, expected);
400 }
401 
TEST_F(DefineTest,FuncOneArg)402 TEST_F(DefineTest, FuncOneArg)
403 {
404     const char *input =
405         "#define foo(x) ((x)+1)\n"
406         "foo(bar)\n";
407     const char *expected =
408         "\n"
409         "((bar)+1)\n";
410     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
411         .Times(1);
412     preprocess(input, expected);
413 }
414 
TEST_F(DefineTest,FuncTwoArgs)415 TEST_F(DefineTest, FuncTwoArgs)
416 {
417     const char *input =
418         "#define foo(x,y) ((x)*(y))\n"
419         "foo(bar,baz)\n";
420     const char *expected =
421         "\n"
422         "((bar)*(baz))\n";
423     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
424         .Times(1);
425     preprocess(input, expected);
426 }
427 
TEST_F(DefineTest,FuncEmptyArgs)428 TEST_F(DefineTest, FuncEmptyArgs)
429 {
430     const char *input =
431         "#define zero() pass\n"
432         "#define one(x) pass\n"
433         "#define two(x,y) pass\n"
434         "zero()\n"
435         "one()\n"
436         "two(,)\n";
437     const char *expected =
438         "\n"
439         "\n"
440         "\n"
441         "pass\n"
442         "pass\n"
443         "pass\n";
444     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
445         .Times(1);
446     preprocess(input, expected);
447 }
448 
TEST_F(DefineTest,FuncMacroAsParam)449 TEST_F(DefineTest, FuncMacroAsParam)
450 {
451     const char *input =
452         "#define x 0\n"
453         "#define foo(x) x\n"
454         "foo(1)\n";
455     const char *expected =
456         "\n"
457         "\n"
458         "1\n";
459     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
460         .Times(1);
461     preprocess(input, expected);
462 }
463 
TEST_F(DefineTest,FuncOneArgMulti)464 TEST_F(DefineTest, FuncOneArgMulti)
465 {
466     const char *input =
467         "#define foo(x) (x)\n"
468         "foo(this is a multi-word argument)\n";
469     const char *expected =
470         "\n"
471         "(this is a multi-word argument)\n";
472     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
473         .Times(1);
474     preprocess(input, expected);
475 }
476 
TEST_F(DefineTest,FuncTwoArgsMulti)477 TEST_F(DefineTest, FuncTwoArgsMulti)
478 {
479     const char *input =
480         "#define foo(x,y) x,two fish,red fish,y\n"
481         "foo(one fish, blue fish)\n";
482     const char *expected =
483         "\n"
484         "one fish,two fish,red fish,blue fish\n";
485     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
486         .Times(1);
487     preprocess(input, expected);
488 }
489 
TEST_F(DefineTest,FuncCompose)490 TEST_F(DefineTest, FuncCompose)
491 {
492     const char *input =
493         "#define bar(x) (1+(x))\n"
494         "#define foo(y) (2*(y))\n"
495         "foo(bar(3))\n";
496     const char *expected =
497         "\n"
498         "\n"
499         "(2*((1+(3))))\n";
500     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
501         .Times(1);
502     preprocess(input, expected);
503 }
504 
TEST_F(DefineTest,FuncArgWithParens)505 TEST_F(DefineTest, FuncArgWithParens)
506 {
507     const char *input =
508         "#define foo(x) (x)\n"
509         "foo(argument(with parens) FTW)\n";
510     const char *expected =
511         "\n"
512         "(argument(with parens) FTW)\n";
513     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
514         .Times(1);
515     preprocess(input, expected);
516 }
517 
TEST_F(DefineTest,FuncMacroAsNonMacro)518 TEST_F(DefineTest, FuncMacroAsNonMacro)
519 {
520     const char *input =
521         "#define foo(bar) bar\n"
522         "foo bar\n";
523     const char *expected =
524         "\n"
525         "foo bar\n";
526     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
527         .Times(1);
528     preprocess(input, expected);
529 }
530 
TEST_F(DefineTest,FuncExtraNewlines)531 TEST_F(DefineTest, FuncExtraNewlines)
532 {
533     const char *input =
534         "#define foo(a) (a)\n"
535         "foo\n"
536         "(\n"
537         "1\n"
538         ")\n";
539     const char *expected =
540         "\n"
541         "\n"
542         "\n"
543         "\n"
544         "(1)\n";
545     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
546         .Times(1);
547     preprocess(input, expected);
548 }
549 
TEST_F(DefineTest,ChainObjToFunc)550 TEST_F(DefineTest, ChainObjToFunc)
551 {
552     const char *input =
553         "#define foo() pass\n"
554         "#define bar foo()\n"
555         "bar\n";
556     const char *expected =
557         "\n"
558         "\n"
559         "pass\n";
560     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
561         .Times(1);
562     preprocess(input, expected);
563 }
564 
TEST_F(DefineTest,ChainObjToNonFunc)565 TEST_F(DefineTest, ChainObjToNonFunc)
566 {
567     const char *input =
568         "#define pass() fail\n"
569         "#define bar pass\n"
570         "bar\n";
571     const char *expected =
572         "\n"
573         "\n"
574         "pass\n";
575     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
576         .Times(1);
577     preprocess(input, expected);
578 }
579 
TEST_F(DefineTest,ChainObjToFuncWithArgs)580 TEST_F(DefineTest, ChainObjToFuncWithArgs)
581 {
582     const char *input =
583         "#define foo(fail) fail\n"
584         "#define bar foo(pass)\n"
585         "bar\n";
586     const char *expected =
587         "\n"
588         "\n"
589         "pass\n";
590     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
591         .Times(1);
592     preprocess(input, expected);
593 }
594 
TEST_F(DefineTest,ChainObjToFuncCompose)595 TEST_F(DefineTest, ChainObjToFuncCompose)
596 {
597     const char *input =
598         "#define baz(fail) fail\n"
599         "#define bar(fail) fail\n"
600         "#define foo bar(baz(pass))\n"
601         "foo\n";
602     const char *expected =
603         "\n"
604         "\n"
605         "\n"
606         "pass\n";
607     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
608         .Times(1);
609     preprocess(input, expected);
610 }
611 
TEST_F(DefineTest,ChainObjToFuncParensInText1)612 TEST_F(DefineTest, ChainObjToFuncParensInText1)
613 {
614     const char *input =
615         "#define fail() pass\n"
616         "#define foo fail\n"
617         "foo()\n";
618     const char *expected =
619         "\n"
620         "\n"
621         "pass\n";
622     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
623         .Times(1);
624     preprocess(input, expected);
625 }
626 
TEST_F(DefineTest,ChainObjToFuncParensInText2)627 TEST_F(DefineTest, ChainObjToFuncParensInText2)
628 {
629     const char *input =
630         "#define bar with,embedded,commas\n"
631         "#define func(x) pass\n"
632         "#define foo func\n"
633         "foo(bar)\n";
634     const char *expected =
635         "\n"
636         "\n"
637         "\n"
638         "pass\n";
639     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
640         .Times(1);
641     preprocess(input, expected);
642 }
643 
TEST_F(DefineTest,ChainObjToFuncMultiLevel)644 TEST_F(DefineTest, ChainObjToFuncMultiLevel)
645 {
646     const char *input =
647         "#define foo(x) pass\n"
648         "#define bar foo\n"
649         "#define baz bar\n"
650         "#define joe baz\n"
651         "joe (fail)\n";
652     const char *expected =
653         "\n"
654         "\n"
655         "\n"
656         "\n"
657         "pass\n";
658     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
659         .Times(1);
660     preprocess(input, expected);
661 }
662 
TEST_F(DefineTest,ObjToFuncRecursive)663 TEST_F(DefineTest, ObjToFuncRecursive)
664 {
665     const char *input =
666         "#define A(a,b) B(a,b)\n"
667         "#define C A(0,C)\n"
668         "C\n";
669     const char *expected =
670         "\n"
671         "\n"
672         "B(0,C)\n";
673     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
674         .Times(1);
675     preprocess(input, expected);
676 }
677 
TEST_F(DefineTest,ChainFuncToFuncCompose)678 TEST_F(DefineTest, ChainFuncToFuncCompose)
679 {
680     const char *input =
681         "#define baz(fail) fail\n"
682         "#define bar(fail) fail\n"
683         "#define foo() bar(baz(pass))\n"
684         "foo()\n";
685     const char *expected =
686         "\n"
687         "\n"
688         "\n"
689         "pass\n";
690     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
691         .Times(1);
692     preprocess(input, expected);
693 }
694 
TEST_F(DefineTest,FuncSelfRecursive)695 TEST_F(DefineTest, FuncSelfRecursive)
696 {
697     const char *input =
698         "#define foo(a) foo(2*(a))\n"
699         "foo(3)\n";
700     const char *expected =
701         "\n"
702         "foo(2*(3))\n";
703     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
704         .Times(1);
705     preprocess(input, expected);
706 }
707 
TEST_F(DefineTest,FuncSelfCompose)708 TEST_F(DefineTest, FuncSelfCompose)
709 {
710     const char *input =
711         "#define foo(a) foo(2*(a))\n"
712         "foo(foo(3))\n";
713     const char *expected =
714         "\n"
715         "foo(2*(foo(2*(3))))\n";
716     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
717         .Times(1);
718     preprocess(input, expected);
719 }
720 
TEST_F(DefineTest,FuncSelfComposeNonFunc)721 TEST_F(DefineTest, FuncSelfComposeNonFunc)
722 {
723     const char *input =
724         "#define foo(bar) bar\n"
725         "foo(foo)\n";
726     const char *expected =
727         "\n"
728         "foo\n";
729     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
730         .Times(1);
731     preprocess(input, expected);
732 }
733 
TEST_F(DefineTest,FuncSelfComposeNonFuncMultiTokenArg)734 TEST_F(DefineTest, FuncSelfComposeNonFuncMultiTokenArg)
735 {
736     const char *input =
737         "#define foo(bar) bar\n"
738         "foo(1+foo)\n";
739     const char *expected =
740         "\n"
741         "1+foo\n";
742     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
743         .Times(1);
744     preprocess(input, expected);
745 }
746 
TEST_F(DefineTest,FinalizeUnexpandedMacro)747 TEST_F(DefineTest, FinalizeUnexpandedMacro)
748 {
749     const char *input =
750         "#define expand(x) expand(x once)\n"
751         "#define foo(x) x\n"
752         "foo(expand(just))\n";
753     const char *expected =
754         "\n"
755         "\n"
756         "expand(just once)\n";
757     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
758         .Times(1);
759     preprocess(input, expected);
760 }
761 
TEST_F(DefineTest,FuncArgWithCommas)762 TEST_F(DefineTest, FuncArgWithCommas)
763 {
764     const char *input =
765         "#define foo(x) pass\n"
766         "foo(argument (with,embedded, commas) -- baz)\n";
767     const char *expected =
768         "\n"
769         "pass\n";
770     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
771         .Times(1);
772     preprocess(input, expected);
773 }
774 
TEST_F(DefineTest,FuncArgObjMaroWithComma)775 TEST_F(DefineTest, FuncArgObjMaroWithComma)
776 {
777     const char *input =
778         "#define foo(a) (a)\n"
779         "#define bar two,words\n"
780         "foo(bar)\n";
781     const char *expected =
782         "\n"
783         "\n"
784         "(two,words)\n";
785     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
786         .Times(1);
787     preprocess(input, expected);
788 }
789 
TEST_F(DefineTest,FuncLeftParenInMacroRightParenInText)790 TEST_F(DefineTest, FuncLeftParenInMacroRightParenInText)
791 {
792     const char *input =
793         "#define bar(a) a*2\n"
794         "#define foo bar(\n"
795         "foo b)\n";
796     const char *expected =
797         "\n"
798         "\n"
799         "b*2\n";
800     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
801         .Times(1);
802     preprocess(input, expected);
803 }
804 
TEST_F(DefineTest,RepeatedArg)805 TEST_F(DefineTest, RepeatedArg)
806 {
807     const char *input =
808         "#define double(x) x x\n"
809         "double(1)\n";
810     const char *expected =
811         "\n"
812         "1 1\n";
813     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
814         .Times(1);
815     preprocess(input, expected);
816 }
817 
TEST_F(DefineTest,FuncMissingRightParen)818 TEST_F(DefineTest, FuncMissingRightParen)
819 {
820     const char *input =
821         "#define foo(x) (2*(x))\n"
822         "foo(3\n";
823     const char *expected =
824         "\n"
825         "\n";
826     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
827         .Times(1);
828     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_UNTERMINATED_INVOCATION,
829                                     pp::SourceLocation(0, 2), "foo"));
830 
831     preprocess(input, expected);
832 }
833 
TEST_F(DefineTest,FuncIncorrectArgCount)834 TEST_F(DefineTest, FuncIncorrectArgCount)
835 {
836     const char *input =
837         "#define foo(x,y) ((x)+(y))\n"
838         "foo()\n"
839         "foo(1)\n"
840         "foo(1,2,3)\n";
841     const char *expected =
842         "\n"
843         "\n"
844         "\n"
845         "\n";
846     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
847         .Times(1);
848     EXPECT_CALL(mDiagnostics,
849                 print(pp::Diagnostics::PP_MACRO_TOO_FEW_ARGS, pp::SourceLocation(0, 2), "foo"));
850     EXPECT_CALL(mDiagnostics,
851                 print(pp::Diagnostics::PP_MACRO_TOO_FEW_ARGS, pp::SourceLocation(0, 3), "foo"));
852     EXPECT_CALL(mDiagnostics,
853                 print(pp::Diagnostics::PP_MACRO_TOO_MANY_ARGS, pp::SourceLocation(0, 4), "foo"));
854 
855     preprocess(input, expected);
856 }
857 
TEST_F(DefineTest,Undef)858 TEST_F(DefineTest, Undef)
859 {
860     const char *input =
861         "#define foo 1\n"
862         "foo\n"
863         "#undef foo\n"
864         "foo\n";
865     const char *expected =
866         "\n"
867         "1\n"
868         "\n"
869         "foo\n";
870     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
871         .Times(1);
872     preprocess(input, expected);
873 }
874 
TEST_F(DefineTest,UndefPredefined)875 TEST_F(DefineTest, UndefPredefined)
876 {
877     const char *input =
878         "#undef __LINE__\n"
879         "__LINE__\n"
880         "#undef __FILE__\n"
881         "__FILE__\n"
882         "#undef __VERSION__\n"
883         "__VERSION__\n"
884         "#undef GL_ES\n"
885         "GL_ES\n";
886     const char *expected =
887         "\n"
888         "2\n"
889         "\n"
890         "0\n"
891         "\n"
892         "100\n"
893         "\n"
894         "1\n";
895     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
896         .Times(1);
897     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_PREDEFINED_UNDEFINED,
898                                     pp::SourceLocation(0, 1), "__LINE__"));
899     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_PREDEFINED_UNDEFINED,
900                                     pp::SourceLocation(0, 3), "__FILE__"));
901     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_PREDEFINED_UNDEFINED,
902                                     pp::SourceLocation(0, 5), "__VERSION__"));
903     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_PREDEFINED_UNDEFINED,
904                                     pp::SourceLocation(0, 7), "GL_ES"));
905 
906     preprocess(input, expected);
907 }
908 
TEST_F(DefineTest,UndefRedefine)909 TEST_F(DefineTest, UndefRedefine)
910 {
911     const char *input =
912         "#define foo 1\n"
913         "foo\n"
914         "#undef foo\n"
915         "foo\n"
916         "#define foo 2\n"
917         "foo\n";
918     const char *expected =
919         "\n"
920         "1\n"
921         "\n"
922         "foo\n"
923         "\n"
924         "2\n";
925     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
926         .Times(1);
927 
928     preprocess(input, expected);
929 }
930 
931 // Example from C99 standard section 6.10.3.5 Scope of macro definitions
TEST_F(DefineTest,C99Example)932 TEST_F(DefineTest, C99Example)
933 {
934     const char *input =
935         "#define x    3          \n"
936         "#define f(a) f(x * (a)) \n"
937         "#undef  x               \n"
938         "#define x    2          \n"
939         "#define g    f          \n"
940         "#define z    z[0]       \n"
941         "#define h    g(~        \n"
942         "#define m(a) a(w)       \n"
943         "#define w    0,1        \n"
944         "#define t(a) a          \n"
945         "#define p()  int        \n"
946         "#define q(x) x          \n"
947         "                        \n"
948         "f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);\n"
949         "g(x+(3,4)-w) | h 5) & m\n"
950         "    (f)^m(m);\n"
951         "p() i[q()] = { q(1), 23, 4, 5, };\n";
952     const char *expected =
953         "\n"
954         "\n"
955         "\n"
956         "\n"
957         "\n"
958         "\n"
959         "\n"
960         "\n"
961         "\n"
962         "\n"
963         "\n"
964         "\n"
965         "\n"
966         "f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);\n"
967         "f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) &\n"
968         " f(2 * (0,1))^m(0,1);\n"
969         "int i[] = { 1, 23, 4, 5, };\n";
970     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
971         .Times(1);
972 
973     preprocess(input, expected);
974 }
975 
TEST_F(DefineTest,Predefined_GL_ES)976 TEST_F(DefineTest, Predefined_GL_ES)
977 {
978     const char *input    = "GL_ES\n";
979     const char *expected = "1\n";
980     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
981         .Times(1);
982 
983     preprocess(input, expected);
984 }
985 
TEST_F(DefineTest,Predefined_VERSION)986 TEST_F(DefineTest, Predefined_VERSION)
987 {
988     const char *input    = "__VERSION__\n";
989     const char *expected = "100\n";
990     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
991         .Times(1);
992 
993     preprocess(input, expected);
994 }
995 
TEST_F(DefineTest,Predefined_LINE1)996 TEST_F(DefineTest, Predefined_LINE1)
997 {
998     const char *str = "\n\n__LINE__";
999     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 3), 100, SH_GLES2_SPEC, _))
1000         .Times(1);
1001 
1002     pp::Token token;
1003     lexSingleToken(str, &token);
1004     EXPECT_EQ(pp::Token::CONST_INT, token.type);
1005     EXPECT_EQ("3", token.text);
1006 }
1007 
TEST_F(DefineTest,Predefined_LINE2)1008 TEST_F(DefineTest, Predefined_LINE2)
1009 {
1010     const char *str =
1011         "#line 10\n"
1012         "__LINE__\n";
1013     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
1014         .Times(1);
1015 
1016     pp::Token token;
1017     lexSingleToken(str, &token);
1018     EXPECT_EQ(pp::Token::CONST_INT, token.type);
1019     EXPECT_EQ("10", token.text);
1020 }
1021 
TEST_F(DefineTest,Predefined_FILE1)1022 TEST_F(DefineTest, Predefined_FILE1)
1023 {
1024     const char *const str[] = {"", "", "__FILE__"};
1025     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(2, 1), 100, SH_GLES2_SPEC, _))
1026         .Times(1);
1027 
1028     pp::Token token;
1029     lexSingleToken(3, str, &token);
1030     EXPECT_EQ(pp::Token::CONST_INT, token.type);
1031     EXPECT_EQ("2", token.text);
1032 }
1033 
TEST_F(DefineTest,Predefined_FILE2)1034 TEST_F(DefineTest, Predefined_FILE2)
1035 {
1036     const char *const str[] = {"#line 10 20\n", "__FILE__"};
1037     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
1038         .Times(1);
1039 
1040     pp::Token token;
1041     lexSingleToken(2, str, &token);
1042     EXPECT_EQ(pp::Token::CONST_INT, token.type);
1043     EXPECT_EQ("21", token.text);
1044 }
1045 
1046 // Defined operator produced by macro expansion should be parsed inside #if directives
TEST_F(DefineTest,ExpandedDefinedParsedInsideIf)1047 TEST_F(DefineTest, ExpandedDefinedParsedInsideIf)
1048 {
1049     const char *input =
1050         "#define bar 1\n"
1051         "#define foo defined(bar)\n"
1052         "#if foo\n"
1053         "bar\n"
1054         "#endif\n";
1055     const char *expected =
1056         "\n"
1057         "\n"
1058         "\n"
1059         "1\n"
1060         "\n";
1061     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
1062         .Times(1);
1063     preprocess(input, expected);
1064 }
1065 
1066 // Defined operator produced by macro expansion should not be parsed outside #if directives
TEST_F(DefineTest,ExpandedDefinedNotParsedOutsideIf)1067 TEST_F(DefineTest, ExpandedDefinedNotParsedOutsideIf)
1068 {
1069     const char *input =
1070         "#define foo defined(bar)\n"
1071         "foo\n";
1072     const char *expected =
1073         "\n"
1074         "defined(bar)\n";
1075     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
1076         .Times(1);
1077     preprocess(input, expected);
1078 }
1079 
1080 // Test that line directive expressions give errors on negative or undefined shifts.
TEST_F(DefineTest,NegativeShiftInLineDirective)1081 TEST_F(DefineTest, NegativeShiftInLineDirective)
1082 {
1083     const char *input =
1084         "#line 1 << -1\n"
1085         "#line 1 >> -1\n"
1086         "#line 1 << x\n"
1087         "#line 1 >> x\n";
1088     const char *expected =
1089         "\n"
1090         "\n"
1091         "\n"
1092         "\n";
1093     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
1094         .Times(1);
1095     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_UNDEFINED_SHIFT, _, _)).Times(4);
1096     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_INVALID_LINE_NUMBER, _, _)).Times(2);
1097     preprocess(input, expected);
1098 }
1099 
1100 // Undefining a macro in its invocation parameters produces and error
TEST_F(DefineTest,UndefineInInvocation)1101 TEST_F(DefineTest, UndefineInInvocation)
1102 {
1103     const char *input =
1104         "#define G(a, b) a b\n"
1105         "G(\n"
1106         "#undef G\n"
1107         "1, 2)\n";
1108     const char *expected = "\n\n\n1 2\n";
1109     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
1110         .Times(1);
1111     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_UNDEFINED_WHILE_INVOKED,
1112                                     pp::SourceLocation(0, 3), _));
1113 
1114     preprocess(input, expected);
1115 }
1116 
1117 // Undefining a macro before its invocation parameters produces and error
TEST_F(DefineTest,UndefineInInvocationPreLParen)1118 TEST_F(DefineTest, UndefineInInvocationPreLParen)
1119 {
1120     const char *input =
1121         "#define G(a, b) a b\n"
1122         "G\n"
1123         "#undef G\n"
1124         "(1, 2)\n";
1125     const char *expected = "\n\n\n1 2\n";
1126 
1127     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
1128         .Times(1);
1129     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_UNDEFINED_WHILE_INVOKED,
1130                                     pp::SourceLocation(0, 3), _));
1131 
1132     preprocess(input, expected);
1133 }
1134 
1135 // The name of the macro "a" is inside an incomplete macro invocation of macro "m()" in its own
1136 // expansion. This should not result in infinite recursion.
TEST_F(DefineTest,RecursiveMacroNameInsideIncompleteMacroInvocationInMacroExpansion)1137 TEST_F(DefineTest, RecursiveMacroNameInsideIncompleteMacroInvocationInMacroExpansion)
1138 {
1139     const char *input =
1140         "#define m(a)\n"
1141         "#define a m((a)\n"
1142         "a)\n";
1143     const char *expected =
1144         "\n"
1145         "\n"
1146         "\n";
1147     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
1148         .Times(1);
1149     preprocess(input, expected);
1150 }
1151 
1152 // The name of the macro "a" is inside an incomplete macro invocation of macro "m()" in its own
1153 // expansion. Then the macro "a" is undef'd. This is a regression test for a memory management bug
1154 // where macro "a" would be freed on undef even though cleaning up the recursive macro invocation
1155 // would still need to refer to macro "a".
TEST_F(DefineTest,UndefInsideRecursiveMacroInvocation)1156 TEST_F(DefineTest, UndefInsideRecursiveMacroInvocation)
1157 {
1158     const char *input =
1159         "#define m(a)\n"
1160         "#define a m((a)\n"
1161         "a\n"
1162         "#undef a\n"
1163         ")\n";
1164     const char *expected =
1165         "\n"
1166         "\n"
1167         "\n"
1168         "\n"
1169         "\n";
1170     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
1171         .Times(1);
1172     preprocess(input, expected);
1173 }
1174 
1175 // The macro invocations form a long chain. The macro expander should protect against stack overflow
1176 // and generate an error in this case.
TEST_F(DefineTest,LongMacroInvocationChain)1177 TEST_F(DefineTest, LongMacroInvocationChain)
1178 {
1179     std::stringstream inputStream;
1180     std::stringstream expectedStream;
1181 
1182     inputStream << "#define b(x) x\n";
1183     inputStream << "#define a0(x) foo x\n";
1184     for (int i = 1; i < 20; ++i)
1185     {
1186         inputStream << "#define a" << i << "(x) b(a" << (i - 1) << "(x))\n";
1187     }
1188     inputStream << "a19(y)\n";
1189 
1190     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_INVOCATION_CHAIN_TOO_DEEP,
1191                                     pp::SourceLocation(0, 22), _));
1192 
1193     pp::PreprocessorSettings settings(SH_GLES2_SPEC);
1194     settings.maxMacroExpansionDepth = 19;
1195     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 100, SH_GLES2_SPEC, _))
1196         .Times(1);
1197 
1198     preprocess(inputStream.str().c_str(), settings);
1199 }
1200 
1201 // Tests what happens when a line directive is between macro name and parenthesis and unterminated
1202 // argument list. Mainly to explain
1203 // LineDirectiveInvalidNumberWithParenthesisFromFunctionInvocationInMiddleOfUnterminatedFunctionInvocation
TEST_F(DefineTest,LineDirectiveInMiddleOfUnterminatedFunctionInvocation)1204 TEST_F(DefineTest, LineDirectiveInMiddleOfUnterminatedFunctionInvocation)
1205 {
1206     const char kInput[]    = R"(
1207 #define f() a
1208 f
1209 #line 6
1210 (x)";
1211     const char kExpected[] = R"(
1212 
1213 
1214 
1215 
1216 )";
1217     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_UNTERMINATED_INVOCATION,
1218                                     pp::SourceLocation(0, 3), "f"))
1219         .Times(1);
1220     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 2), 100, SH_GLES2_SPEC, _))
1221         .Times(1);
1222     preprocess(kInput, kExpected);
1223 }
1224 
1225 // Tests what happens when an invalid line directive is between macro name and parenthesis and
1226 // unterminated argument list. Mainly to explain
1227 // LineDirectiveInvalidNumberWithParenthesisFromFunctionInvocationInMiddleOfUnterminatedFunctionInvocation
TEST_F(DefineTest,LineDirectiveInvalidNumberInMiddleOfUnterminatedFunctionInvocation)1228 TEST_F(DefineTest, LineDirectiveInvalidNumberInMiddleOfUnterminatedFunctionInvocation)
1229 {
1230     const char kInput[]    = R"(
1231 #define f() a
1232 f
1233 #line a
1234 (x)";
1235     const char kExpected[] = R"(
1236 
1237 
1238 
1239 )";
1240     EXPECT_CALL(mDiagnostics,
1241                 print(pp::Diagnostics::PP_INVALID_LINE_NUMBER, pp::SourceLocation(0, 4), "a"))
1242         .Times(1);
1243     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_UNTERMINATED_INVOCATION,
1244                                     pp::SourceLocation(0, 3), "f"))
1245         .Times(1);
1246     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 2), 100, SH_GLES2_SPEC, _))
1247         .Times(1);
1248     preprocess(kInput, kExpected);
1249 }
1250 
1251 // Tests what happens when an invalid line directive with number coming from a function invocation
1252 // is between macro name and parenthesis and unterminated argument list. Mainly to explain
1253 // LineDirectiveInvalidNumberWithParenthesisFromFunctionInvocationInMiddleOfUnterminatedFunctionInvocation
TEST_F(DefineTest,LineDirectiveInvalidNumberFromFunctionInvocationInMiddleOfUnterminatedFunctionInvocation)1254 TEST_F(DefineTest,
1255        LineDirectiveInvalidNumberFromFunctionInvocationInMiddleOfUnterminatedFunctionInvocation)
1256 {
1257     const char kInput[]    = R"(
1258 #define f() a
1259 f
1260 #line f()
1261 (x)";
1262     const char kExpected[] = R"(
1263 
1264 
1265 
1266 )";
1267     EXPECT_CALL(mDiagnostics,
1268                 print(pp::Diagnostics::PP_INVALID_LINE_NUMBER, pp::SourceLocation(0, 4), "a"))
1269         .Times(1);
1270     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_UNTERMINATED_INVOCATION,
1271                                     pp::SourceLocation(0, 3), "f"))
1272         .Times(1);
1273     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 2), 100, SH_GLES2_SPEC, _))
1274         .Times(1);
1275     preprocess(kInput, kExpected);
1276 }
1277 
1278 // Tests what happens when an invalid line directive with number that has a parenthesis coming from
1279 // a function invocation is between macro name and parenthesis and unterminated argument list. This
1280 // used to assert.
TEST_F(DefineTest,LineDirectiveInvalidNumberWithParenthesisFromFunctionInvocationInMiddleOfUnterminatedFunctionInvocation)1281 TEST_F(
1282     DefineTest,
1283     LineDirectiveInvalidNumberWithParenthesisFromFunctionInvocationInMiddleOfUnterminatedFunctionInvocation)
1284 {
1285     const char kInput[]    = R"(
1286 #define f() a(
1287 f
1288 #line f()
1289 (x)";
1290     const char kExpected[] = R"(
1291 
1292 
1293 
1294 )";
1295     EXPECT_CALL(mDiagnostics,
1296                 print(pp::Diagnostics::PP_INVALID_LINE_NUMBER, pp::SourceLocation(0, 4), "a"))
1297         .Times(1);
1298     EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::PP_MACRO_UNTERMINATED_INVOCATION,
1299                                     pp::SourceLocation(0, 3), "f"))
1300         .Times(1);
1301     EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 2), 100, SH_GLES2_SPEC, _))
1302         .Times(1);
1303     preprocess(kInput, kExpected);
1304 }
1305 
1306 }  // namespace angle
1307