# HG changeset patch # User Joerg Sonnenberger # Date 1364710000 -7200 # Node ID 8eee66a255338c722445ddb62a02c3d653f9d0db # Parent dd3aa9b8fed1a39827131bdb826684cd27168a83# Parent 737ffe27b4bdefdbcf8b5366dd47b84f2ae6fefd Merge diff -r 737ffe27b4bd -r 8eee66a25533 directive.c --- a/directive.c Sun Mar 31 07:19:49 2013 +0200 +++ b/directive.c Sun Mar 31 08:06:40 2013 +0200 @@ -57,6 +57,41 @@ static void +uncomment(char *buf) +{ + char *s, *t, *u = NULL; + bool incomment = false; + + for (s = t = buf; *s; s++) { + if (incomment) { + if (s[0] == '*' && s[1] == '/') { + s++; + incomment = false; + } + } else { + if (s[0] == '/' && s[1] == '*') { + incomment = true; + } else { + if (t != s) { + *t = *s; + } + if (!strchr(ws, *t)) { + u = t; + } + t++; + } + } + } + if (u) { + /* end string after last non-whitespace char */ + u[1] = '\0'; + } else { + *t = '\0'; + } +} + +static +void oneword(const char *what, struct place *p2, char *line) { size_t pos; @@ -119,13 +154,14 @@ static void -d_if(struct place *p, struct place *p2, char *line, size_t len) +d_if(struct place *p, struct place *p2, char *line) { char *expr; bool val; struct place p3 = *p2; - expr = macroexpand(p2, line, len, true); + uncomment(line); + expr = macroexpand(p2, line, strlen(line), true); val = eval(&p3, expr); ifstate_push(p, val); dostrfree(expr); @@ -133,23 +169,25 @@ static void -d_ifdef(struct place *p, struct place *p2, char *line, size_t len) +d_ifdef(struct place *p, struct place *p2, char *line) { + uncomment(line); oneword("#ifdef", p2, line); ifstate_push(p, macro_isdefined(line)); } static void -d_ifndef(struct place *p, struct place *p2, char *line, size_t len) +d_ifndef(struct place *p, struct place *p2, char *line) { + uncomment(line); oneword("#ifndef", p2, line); ifstate_push(p, !macro_isdefined(line)); } static void -d_elif(struct place *p, struct place *p2, char *line, size_t len) +d_elif(struct place *p, struct place *p2, char *line) { char *expr; struct place p3 = *p2; @@ -162,7 +200,8 @@ if (ifstate->evertrue) { ifstate->curtrue = false; } else { - expr = macroexpand(p2, line, len, true); + uncomment(line); + expr = macroexpand(p2, line, strlen(line), true); ifstate->curtrue = eval(&p3, expr); ifstate->evertrue = ifstate->curtrue; dostrfree(expr); @@ -171,7 +210,7 @@ static void -d_else(struct place *p, struct place *p2, char *line, size_t len) +d_else(struct place *p, struct place *p2, char *line) { if (ifstate->seenelse) { complain(p, "Multiple #else directives in one conditional"); @@ -185,7 +224,7 @@ static void -d_endif(struct place *p, struct place *p2, char *line, size_t len) +d_endif(struct place *p, struct place *p2, char *line) { if (ifstate->prev == NULL) { complain(p, "Unmatched #endif"); @@ -200,7 +239,7 @@ static void -d_define(struct place *p, struct place *p2, char *line, size_t len) +d_define(struct place *p, struct place *p2, char *line) { size_t pos, argpos; struct place p3, p4; @@ -263,8 +302,9 @@ static void -d_undef(struct place *p, struct place *p2, char *line, size_t len) +d_undef(struct place *p, struct place *p2, char *line) { + uncomment(line); oneword("#undef", p2, line); macro_undef(line); } @@ -274,16 +314,21 @@ static bool -tryinclude(struct place *p, char *line, size_t len) +tryinclude(struct place *p, char *line) { + size_t len; + + len = strlen(line); if (len > 2 && line[0] == '"' && line[len-1] == '"') { line[len-1] = '\0'; file_readquote(p, line+1); + line[len-1] = '"'; return true; } if (len > 2 && line[0] == '<' && line[len-1] == '>') { line[len-1] = '\0'; file_readbracket(p, line+1); + line[len-1] = '>'; return true; } return false; @@ -291,15 +336,16 @@ static void -d_include(struct place *p, struct place *p2, char *line, size_t len) +d_include(struct place *p, struct place *p2, char *line) { char *text; - if (tryinclude(p, line, len)) { + uncomment(line); + if (tryinclude(p, line)) { return; } - text = macroexpand(p2, line, len, false); - if (tryinclude(p, text, strlen(text))) { + text = macroexpand(p2, line, strlen(line), false); + if (tryinclude(p, text)) { dostrfree(text); return; } @@ -310,7 +356,7 @@ static void -d_line(struct place *p, struct place *p2, char *line, size_t len) +d_line(struct place *p, struct place *p2, char *line) { /* XXX */ complain(p, "Sorry, no #line yet"); @@ -321,11 +367,11 @@ static void -d_warning(struct place *p, struct place *p2, char *line, size_t len) +d_warning(struct place *p, struct place *p2, char *line) { char *msg; - msg = macroexpand(p2, line, len, false); + msg = macroexpand(p2, line, strlen(line), false); complain(p, "#warning: %s", msg); if (mode.werror) { complain_fail(); @@ -335,11 +381,11 @@ static void -d_error(struct place *p, struct place *p2, char *line, size_t len) +d_error(struct place *p, struct place *p2, char *line) { char *msg; - msg = macroexpand(p2, line, len, false); + msg = macroexpand(p2, line, strlen(line), false); complain(p, "#error: %s", msg); complain_fail(); dostrfree(msg); @@ -350,7 +396,7 @@ static void -d_pragma(struct place *p, struct place *p2, char *line, size_t len) +d_pragma(struct place *p, struct place *p2, char *line) { complain(p, "#pragma %s", line); complain_fail(); @@ -362,7 +408,7 @@ static const struct { const char *name; bool ifskip; - void (*func)(struct place *, struct place *, char *line, size_t len); + void (*func)(struct place *, struct place *, char *line); } directives[] = { { "define", true, d_define }, { "elif", false, d_elif }, @@ -382,7 +428,7 @@ static void -directive_gotdirective(struct place *p, char *line, size_t linelen) +directive_gotdirective(struct place *p, char *line) { struct place p2; size_t len, skip; @@ -399,9 +445,13 @@ skip = len + strspn(line+len, ws); p2.column += skip; line += skip; - linelen -= skip; - linelen = notrailingws(line, linelen); - directives[i].func(p, &p2, line, linelen); + + len = strlen(line); + len = notrailingws(line, len); + if (len < strlen(line)) { + line[len] = '\0'; + } + directives[i].func(p, &p2, line); return; } } @@ -524,10 +574,19 @@ /* check if we have a directive line */ skip = strspn(line + acomm, ws); if (acomm == 0 && line[skip] == '#') { + char ch; + skip = skip + 1 + strspn(line + skip + 1, ws); assert(skip <= text); p->column += skip; - directive_gotdirective(p, line+skip, text-skip); + assert(line[len] == '\0'); + /* ensure null termination for directives */ + ch = line[text]; + if (ch != '\0') { + line[text] = '\0'; + } + directive_gotdirective(p, line+skip /*, length = text-skip */); + line[text] = ch; p->column += text-skip; } else if (ifstate->curtrue) { macro_sendline(p, line + acomm, text); diff -r 737ffe27b4bd -r 8eee66a25533 eval.c --- a/eval.c Sun Mar 31 07:19:49 2013 +0200 +++ b/eval.c Sun Mar 31 08:06:40 2013 +0200 @@ -647,6 +647,8 @@ len = strspn(expr+pos, ws); pos += len; p->column += len; + /* trailing whitespace is supposed to have been pruned */ + assert(expr[pos] != '\0'); if (check_word(p, expr, pos, &len)) { pos += len; p->column += len; diff -r 737ffe27b4bd -r 8eee66a25533 tests/Makefile --- a/tests/Makefile Sun Mar 31 07:19:49 2013 +0200 +++ b/tests/Makefile Sun Mar 31 08:06:40 2013 +0200 @@ -1,19 +1,12 @@ -# $NetBSD$ +TRADCPP_OBJDIR!= ${MAKE} -C ${.CURDIR}/.. -V .OBJDIR +TRADCPP= ${TRADCPP_OBJDIR}/tradcpp + +TESTS=\ + t01 t02 t03 t04 t05 t06 t07 t08 t09 t10 t11 t12 t13 t14 t15 t16 \ + t17 t18 t19 all: run-tests .WAIT show-diffs -TESTDIR= ${TESTSBASE}/usr.bin/tradcpp - -TESTS_SH+= tradcpp - -.include - -TRADCPP_OBJDIR!= ${MAKE} -C .. -V .OBJDIR -TRADCPP= ${TRADCPP_OBJDIR}/tradcpp - -TESTS=t01 t02 t03 t04 t05 t06 t07 t08 t09 t10 t11 t12 t13 t14 t15 t16 \ - t17 t18 t19 - .for T in $(TESTS) run-tests: $(T).diff @@ -41,3 +34,11 @@ .endfor .PHONY: all run-tests show-diffs clean good + +############################################################ + +.if defined(ALLOW_BROKEN_ATF_POLLUTION) +TESTDIR= ${TESTSBASE}/usr.bin/tradcpp +TESTS_SH+= tradcpp +.include +.endif diff -r 737ffe27b4bd -r 8eee66a25533 tests/t16.c --- a/tests/t16.c Sun Mar 31 07:19:49 2013 +0200 +++ b/tests/t16.c Sun Mar 31 08:06:40 2013 +0200 @@ -1,3 +1,11 @@ -#define foo /* comment continues - into the next line */ baz -baz +#define a() x +a() +a () +#define b(p) p +x/**/b(1)/**/x +x/**/b (1)/**/x +x/**/b()/**/x +#define c(p,q) p/**/q +x/**/c(1,2)/**/x +x/**/c(1)/**/x +x/**/c()/**/x diff -r 737ffe27b4bd -r 8eee66a25533 tests/t16.good --- a/tests/t16.good Sun Mar 31 07:19:49 2013 +0200 +++ b/tests/t16.good Sun Mar 31 08:06:40 2013 +0200 @@ -1,1 +1,11 @@ -baz +x +x +x1x +x1x +xx +x12x +t16.c:10:1: Wrong number of arguments for macro c; found 1, expected 2 +x1x +t16.c:11:1: Wrong number of arguments for macro c; found 0, expected 2 +xx +FAILED diff -r 737ffe27b4bd -r 8eee66a25533 tests/t17.c --- a/tests/t17.c Sun Mar 31 07:19:49 2013 +0200 +++ b/tests/t17.c Sun Mar 31 08:06:40 2013 +0200 @@ -1,11 +1,2 @@ -#define a() x -a() -a () -#define b(p) p -x/**/b(1)/**/x -x/**/b (1)/**/x -x/**/b()/**/x -#define c(p,q) p/**/q -x/**/c(1,2)/**/x -x/**/c(1)/**/x -x/**/c()/**/x +#define file "subdir/test.h" +#include file diff -r 737ffe27b4bd -r 8eee66a25533 tests/t17.good --- a/tests/t17.good Sun Mar 31 07:19:49 2013 +0200 +++ b/tests/t17.good Sun Mar 31 08:06:40 2013 +0200 @@ -1,11 +1,1 @@ -x -x -x1x -x1x -xx -x12x -t17.c:10:1: Wrong number of arguments for macro c; found 1, expected 2 -x1x -t17.c:11:1: Wrong number of arguments for macro c; found 0, expected 2 -xx -FAILED +hello diff -r 737ffe27b4bd -r 8eee66a25533 tests/t18.c --- a/tests/t18.c Sun Mar 31 07:19:49 2013 +0200 +++ b/tests/t18.c Sun Mar 31 08:06:40 2013 +0200 @@ -1,2 +1,2 @@ -#define file "subdir/test.h" -#include file +#if FOO /* ignore me */ +#endif diff -r 737ffe27b4bd -r 8eee66a25533 tests/t18.good --- a/tests/t18.good Sun Mar 31 07:19:49 2013 +0200 +++ b/tests/t18.good Sun Mar 31 08:06:40 2013 +0200 @@ -1,1 +0,0 @@ -hello diff -r 737ffe27b4bd -r 8eee66a25533 tests/t19.c --- a/tests/t19.c Sun Mar 31 07:19:49 2013 +0200 +++ b/tests/t19.c Sun Mar 31 08:06:40 2013 +0200 @@ -1,2 +1,3 @@ -#if FOO /* ignore me */ -#endif +#define foo /* comment continues + into the next line */ baz +baz diff -r 737ffe27b4bd -r 8eee66a25533 tests/t19.good --- a/tests/t19.good Sun Mar 31 07:19:49 2013 +0200 +++ b/tests/t19.good Sun Mar 31 08:06:40 2013 +0200 @@ -1,2 +1,1 @@ - - +baz