History

tccpp: Allow computed include like 42.h

The include directive needs to be parsed as pp-tokens, not as token (i.e. no conversion to TOK_STR or TOK_NUM). Also fix parsing computed includes using quoted strings.

Michael Matz committed 10 years ago

Commit d0d25ec1 parent 03813873 files · +50 −29Browse files

Unified · Split

3 files
  1. tccpp.c +19 −26
  2. tests/42test.h +13 −0
  3. tests/tcctest.c +18 −3
tccpp.c+19−26
@@ -1737,34 +1737,27 @@ ST_FUNC void preprocess(int is_bof) · 1736 unmodified lines
17371737 inp();
17381738 #endif
17391739 } else {
1740− /* computed #include : either we have only strings or
1741− we have anything enclosed in '<>' */
1740+ int len;
1741+ /* computed #include : concatenate everything up to linefeed,
1742+ the result must be one of the two accepted forms.
1743+ Don't convert pp-tokens to tokens here. */
1744+ parse_flags = (PARSE_FLAG_PREPROCESS
1745+ | PARSE_FLAG_LINEFEED
1746+ | (parse_flags & PARSE_FLAG_ASM_FILE));
17421747 next();
17431748 buf[0] = '\0';
1744− if (tok == TOK_STR) {
1745− while (tok != TOK_LINEFEED) {
1746− if (tok != TOK_STR) {
1747− include_syntax:
1748− tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1749− }
1750− pstrcat(buf, sizeof(buf), (char *)tokc.str.data);
1751− next();
1752− }
1753− c = '\"';
1754− } else {
1755− int len;
1756− while (tok != TOK_LINEFEED) {
1757− pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1758− next();
1759− }
1760− len = strlen(buf);
1761− /* check syntax and remove '<>' */
1762− if (len < 2 || buf[0] != '<' || buf[len - 1] != '>')
1763− goto include_syntax;
1764− memmove(buf, buf + 1, len - 2);
1765− buf[len - 2] = '\0';
1766− c = '>';
1767− }
1749+ while (tok != TOK_LINEFEED) {
1750+ pstrcat(buf, sizeof(buf), get_tok_str(tok, &tokc));
1751+ next();
1752+ }
1753+ len = strlen(buf);
1754+ /* check syntax and remove '<>|""' */
1755+ if ((len < 2 || ((buf[0] != '"' || buf[len-1] != '"') &&
1756+ (buf[0] != '<' || buf[len-1] != '>'))))
1757+ tcc_error("'#include' expects \"FILENAME\" or <FILENAME>");
1758+ c = buf[len-1];
1759+ memmove(buf, buf + 1, len - 2);
1760+ buf[len - 2] = '\0';
17681761 }
17691762
17701763 if (s1->include_stack_ptr >= s1->include_stack + INCLUDE_STACK_SIZE)
tests/42test.h+13−0
@@ -0,0 +1,13 @@
1+/* This file is to test compute #include directives. It's named so
2+ that it starts with a pre-processing number which isn't a valid
3+ number (42test.h). Including this must work. */
4+#ifndef INC42_FIRST
5+int have_included_42test_h;
6+#define INC42_FIRST
7+#elif !defined INC42_SECOND
8+#define INC42_SECOND
9+int have_included_42test_h_second;
10+#else
11+#define INC42_THIRD
12+int have_included_42test_h_third;
13+#endif
tests/tcctest.c+18−3
@@ -44,7 +44,7 @@ · 43 unmodified lines
4444 #define TCCLIB_INC <tcclib.h>
4545 #define TCCLIB_INC1 <tcclib
4646 #define TCCLIB_INC2 h>
47−#define TCCLIB_INC3 "tcclib"
47+#define TCCLIB_INC3 "tcclib.h"
4848
4949 #include TCCLIB_INC
5050
@@ -52,8 +52,7 @@ · 1 unmodified line
5252
5353 #include TCCLIB_INC1.h>
5454
55−/* gcc 3.2 does not accept that (bug ?) */
56−//#include TCCLIB_INC3 ".h"
55+#include TCCLIB_INC3
5756
5857 #include <tcclib.h>
5958
@@ -61,6 +60,17 @@ · 1 unmodified line
6160
6261 #include "tcctest.h"
6362
63+/* Test two more ways to include a file named like a pp-number */
64+#define INC(name) <tests/name.h>
65+#define funnyname 42test.h
66+#define incdir tests/
67+#define incname < incdir funnyname >
68+#define __stringify(x) #x
69+#define stringify(x) __stringify(x)
70+#include INC(42test)
71+#include incname
72+#include stringify(funnyname)
73+
6474 void intdiv_test();
6575 void string_test();
6676 void expr_test();
@@ -396,6 +406,11 @@ comment · 329 unmodified lines
396406 printf("%s\n", __BASE_FILE__);
397407 printf("%s\n", get_file_from_header());
398408 printf("%s\n", __FILE__);
409+
410+ /* Check that funnily named include was in fact included */
411+ have_included_42test_h = 1;
412+ have_included_42test_h_second = 1;
413+ have_included_42test_h_third = 1;
399414 }
400415
401416

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t