mirror of
https://github.com/mirror/tinycc.git
synced 2025-03-10 08:50:07 +08:00
Allow different names for pragma once
pragma once can now be used with test.h ./test.h and other references to the same file just like gcc/clang. On linux we can use stat and st_ino to check for the same file. On windows the st_ino does not work so we calculate a file hash if the size of the file differs and then compare the hash.
This commit is contained in:
parent
5077d4c915
commit
30fd24abd4
5
tcc.h
5
tcc.h
@ -38,6 +38,7 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
@ -716,6 +717,10 @@ typedef struct InlineFunc {
|
|||||||
/* include file cache, used to find files faster and also to eliminate
|
/* include file cache, used to find files faster and also to eliminate
|
||||||
inclusion if the include file is protected by #ifndef ... #endif */
|
inclusion if the include file is protected by #ifndef ... #endif */
|
||||||
typedef struct CachedInclude {
|
typedef struct CachedInclude {
|
||||||
|
struct stat st;
|
||||||
|
#ifdef _WIN32
|
||||||
|
unsigned long long hash;
|
||||||
|
#endif
|
||||||
int ifndef_macro;
|
int ifndef_macro;
|
||||||
int once;
|
int once;
|
||||||
int hash_next; /* -1 if none */
|
int hash_next; /* -1 if none */
|
||||||
|
74
tccpp.c
74
tccpp.c
@ -1607,38 +1607,88 @@ bad_twosharp:
|
|||||||
define_push(v, t, tok_str_dup(&tokstr_buf), first);
|
define_push(v, t, tok_str_dup(&tokstr_buf), first);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
static unsigned long long calc_file_hash(const char *filename)
|
||||||
|
{
|
||||||
|
unsigned long long hash = 14695981039346656037ull; // FNV_offset_basis;
|
||||||
|
FILE *fp = fopen (filename, "rb");
|
||||||
|
|
||||||
|
if (fp == NULL)
|
||||||
|
return 0;
|
||||||
|
for (;;) {
|
||||||
|
unsigned char temp[IO_BUF_SIZE];
|
||||||
|
int i, n = fread(temp, 1, sizeof(temp), fp);
|
||||||
|
|
||||||
|
if (n <= 0)
|
||||||
|
break;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
hash = hash * 1099511628211ull + temp[i]; // FNV_prime
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
if (hash == 0)
|
||||||
|
hash = 1;
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)
|
static CachedInclude *search_cached_include(TCCState *s1, const char *filename, int add)
|
||||||
{
|
{
|
||||||
const unsigned char *s;
|
unsigned int h = 0;
|
||||||
unsigned int h;
|
|
||||||
CachedInclude *e;
|
CachedInclude *e;
|
||||||
int i;
|
int i;
|
||||||
|
struct stat st;
|
||||||
h = TOK_HASH_INIT;
|
|
||||||
s = (unsigned char *) filename;
|
|
||||||
while (*s) {
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
h = TOK_HASH_FUNC(h, toup(*s));
|
unsigned long long hash = 0;
|
||||||
#else
|
|
||||||
h = TOK_HASH_FUNC(h, *s);
|
|
||||||
#endif
|
#endif
|
||||||
s++;
|
|
||||||
|
/* This is needed for #pragmae once
|
||||||
|
* We cannot use stat on windows because st_ino is not set correctly
|
||||||
|
* so we calculate a hash of file contents.
|
||||||
|
* This also works for hard/soft links as in gcc/clang.
|
||||||
|
*/
|
||||||
|
memset (&st, 0, sizeof(st));
|
||||||
|
if (stat (filename, &st))
|
||||||
|
goto skip;
|
||||||
|
h = st.st_size & (CACHED_INCLUDES_HASH_SIZE - 1);
|
||||||
|
#ifdef _WIN32
|
||||||
|
/* Only calculate file hash if file size same. */
|
||||||
|
i = s1->cached_includes_hash[h];
|
||||||
|
for(;;) {
|
||||||
|
if (i == 0)
|
||||||
|
break;
|
||||||
|
e = s1->cached_includes[i - 1];
|
||||||
|
if (e->st.st_size == st.st_size) {
|
||||||
|
if (e->hash == 0)
|
||||||
|
e->hash = calc_file_hash(e->filename);
|
||||||
|
hash = calc_file_hash(filename);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
h &= (CACHED_INCLUDES_HASH_SIZE - 1);
|
i = e->hash_next;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
i = s1->cached_includes_hash[h];
|
i = s1->cached_includes_hash[h];
|
||||||
for(;;) {
|
for(;;) {
|
||||||
if (i == 0)
|
if (i == 0)
|
||||||
break;
|
break;
|
||||||
e = s1->cached_includes[i - 1];
|
e = s1->cached_includes[i - 1];
|
||||||
if (0 == PATHCMP(e->filename, filename))
|
#ifdef _WIN32
|
||||||
|
if (e->st.st_size == st.st_size && e->hash == hash)
|
||||||
|
#else
|
||||||
|
if (st.st_dev == e->st.st_dev && st.st_ino == e->st.st_ino)
|
||||||
|
#endif
|
||||||
return e;
|
return e;
|
||||||
i = e->hash_next;
|
i = e->hash_next;
|
||||||
}
|
}
|
||||||
|
skip:
|
||||||
if (!add)
|
if (!add)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
|
e = tcc_malloc(sizeof(CachedInclude) + strlen(filename));
|
||||||
|
e->st = st;
|
||||||
|
#ifdef _WIN32
|
||||||
|
e->hash = hash;
|
||||||
|
#endif
|
||||||
strcpy(e->filename, filename);
|
strcpy(e->filename, filename);
|
||||||
e->ifndef_macro = e->once = 0;
|
e->ifndef_macro = e->once = 0;
|
||||||
dynarray_add(&s1->cached_includes, &s1->nb_cached_includes, e);
|
dynarray_add(&s1->cached_includes, &s1->nb_cached_includes, e);
|
||||||
|
@ -37,6 +37,10 @@ int main()
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "18_include2.h"
|
||||||
|
#include "./18_include2.h"
|
||||||
|
#include "../tests2/18_include2.h"
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,3 +5,4 @@ has_include
|
|||||||
has_include
|
has_include
|
||||||
has_include_next
|
has_include_next
|
||||||
has_include_next
|
has_include_next
|
||||||
|
counter 0
|
||||||
|
2
tests/tests2/18_include2.h
Normal file
2
tests/tests2/18_include2.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#pragma once
|
||||||
|
printf ("counter %d\n", __COUNTER__);
|
Loading…
Reference in New Issue
Block a user