scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 8776d6e2e93ea4a1d3be9ed3ac3a8c2f99d2a684
parent f1689426d892911f774884c79ecdc2e639522448
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 19 Jan 2017 13:16:59 +0100

[cc1] Fix relative inclusion

"" is used to include a file from the current directory, but
cc1 was not controlling that the current directory depends
of the source being processed at the moment. This patch
gets the current work directory (cwd) from the name of
the current file.

Diffstat:
Mcc1/cpp.c | 19+++++++++++++++++--
Mtests/execute/0062-include.c | 4++--
Dtests/execute/0062-include.h | 3---
Mtests/execute/0064-sysinclude.c | 2+-
Mtests/execute/chktest.sh | 2+-
Atests/execute/include/0062-include.h | 5+++++
Atests/execute/include/0062-include2.h | 2++
Dtests/execute/include/0064-sysinclude.h | 2--
Atests/execute/sysinclude/0064-sysinclude.h | 4++++
Atests/execute/sysinclude/0064-sysinclude2.h | 2++
10 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -444,10 +444,25 @@ includefile(char *dir, char *file, size_t filelen) return addinput(path, NULL, NULL); } +static char * +cwd(char *buf) +{ + char *p, *s = input->fname; + size_t len; + + if ((p = strrchr(s, '/')) == NULL) + return NULL; + if ((len = p - s) >= FILENAME_MAX) + die("current work directory too long"); + memcpy(buf, s, len); + buf[len] = '\0'; + return buf; +} + static void include(void) { - char file[FILENAME_MAX], *p, **bp; + char dir[FILENAME_MAX], file[FILENAME_MAX], *p, **bp; size_t filelen; static char *sysinclude[] = { PREFIX "/include/scc/" ARCH "/", @@ -490,7 +505,7 @@ include(void) if (next() != '\n') goto trailing_characters; - if (includefile(NULL, file, filelen)) + if (includefile(cwd(dir), file, filelen)) goto its_done; break; default: diff --git a/tests/execute/0062-include.c b/tests/execute/0062-include.c @@ -1,4 +1,4 @@ #include \ -"0062-include.h" - return 0; +"include/0062-include.h" + return x; } diff --git a/tests/execute/0062-include.h b/tests/execute/0062-include.h @@ -1,3 +0,0 @@ -int -main() -{ diff --git a/tests/execute/0064-sysinclude.c b/tests/execute/0064-sysinclude.c @@ -3,5 +3,5 @@ int main() { - return x; + return x - y; } diff --git a/tests/execute/chktest.sh b/tests/execute/chktest.sh @@ -8,5 +8,5 @@ for i in $@ do printf "%s\t" $i rm -f a.out - (scc -Iinclude -m qbe "$i" && ./a.out) 2>/dev/null && echo [OK] || echo [FAILED] + (scc -Isysinclude -m qbe "$i" && ./a.out) 2>/dev/null && echo [OK] || echo [FAILED] done diff --git a/tests/execute/include/0062-include.h b/tests/execute/include/0062-include.h @@ -0,0 +1,5 @@ +#include "0062-include2.h" + +int +main() +{ diff --git a/tests/execute/include/0062-include2.h b/tests/execute/include/0062-include2.h @@ -0,0 +1,2 @@ +int x; + diff --git a/tests/execute/include/0064-sysinclude.h b/tests/execute/include/0064-sysinclude.h @@ -1,2 +0,0 @@ -int x = 0; - diff --git a/tests/execute/sysinclude/0064-sysinclude.h b/tests/execute/sysinclude/0064-sysinclude.h @@ -0,0 +1,4 @@ +#include "0064-sysinclude2.h" + +int x = 2; + diff --git a/tests/execute/sysinclude/0064-sysinclude2.h b/tests/execute/sysinclude/0064-sysinclude2.h @@ -0,0 +1,2 @@ + +int y = 2;