From 9d32a84f6901fab559d425d426cb6f4556470f3c Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 10 Jan 2024 20:19:04 -0300 Subject: [PATCH 01/25] Add pragma result status enum Makes so that preprocessing a pragma yields a status flag with additional information on how to handle that particular #pragma. Mostly a prepraration for #pragma once. --- src/cc65/preproc.c | 51 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 0a9b94bf2..ba08bd47e 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -2963,14 +2963,34 @@ static void DoLine (void) MLine = InitLine (MLine); } +/* Possible outcomes of preprocessing a pragma */ +typedef enum { + /* the #pragma directive was preprocessed into _Pragma() */ + PREPROC_PRAGMA_EMITTED, + /* processing the #pragma directive did not result in any output */ + PREPROC_PRAGMA_NOTHING_EMITTED, -static void DoPragma (void) + /* the preprocessor should stop processing the current file */ + PREPROC_PRAGMA_HALT +} preproc_pragma_t; + +static preproc_pragma_t DoPragmaOnce (void) +/** + * Handle a pragma once directive, and determine if the current file + * should still be processed or not. + */ +{ + return PREPROC_PRAGMA_HALT; +} + +static preproc_pragma_t DoPragma (void) /* Handle a #pragma line by converting the #pragma preprocessor directive into ** the _Pragma() compiler operator. */ { - StrBuf* PragmaLine = OLine; + StrBuf* const PragmaLine = OLine; + preproc_pragma_t status; PRECONDITION (PragmaLine != 0); @@ -2981,14 +3001,22 @@ static void DoPragma (void) SB_Clear (MLine); PreprocessDirective (Line, MLine, MSM_NONE); - /* Convert #pragma to _Pragma () */ - SB_AppendStr (PragmaLine, "_Pragma ("); - SB_Reset (MLine); - Stringize (MLine, PragmaLine); - SB_AppendChar (PragmaLine, ')'); + if (SB_CompareStr(MLine, "once") == 0) { + status = DoPragmaOnce(); + } + else { + /* Convert #pragma to _Pragma () */ + SB_AppendStr (PragmaLine, "_Pragma ("); + SB_Reset (MLine); + Stringize (MLine, PragmaLine); + SB_AppendChar (PragmaLine, ')'); + status = PREPROC_PRAGMA_EMITTED; + } /* End this line */ SB_SetIndex (PragmaLine, SB_GetLen (PragmaLine)); + + return status; } @@ -3156,8 +3184,13 @@ static int ParseDirectives (unsigned ModeFlags) case PPD_PRAGMA: if (!PPSkip) { if ((ModeFlags & MSM_IN_ARG_LIST) == 0) { - DoPragma (); - return Whitespace; + preproc_pragma_t status = DoPragma(); + if (status == PREPROC_PRAGMA_EMITTED) { + return Whitespace; + } + else if (status == PREPROC_PRAGMA_HALT) { + PPSkip = 1; + } } else { PPError ("Embedded #pragma directive within macro arguments is unsupported"); } From 6a276ccac34f498cb7272575e089d1a0c096d34f Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Fri, 12 Jan 2024 10:05:59 -0300 Subject: [PATCH 02/25] Implement pragma once --- src/cc65/preproc.c | 60 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index ba08bd47e..ab330d08e 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -44,6 +44,7 @@ #include "inline.h" #include "print.h" #include "xmalloc.h" +#include "strpool.h" /* cc65 */ #include "codegen.h" @@ -113,6 +114,8 @@ static StrBuf* PLine; /* Buffer for macro expansion */ static StrBuf* MLine; /* Buffer for macro expansion in #pragma */ static StrBuf* OLine; /* Buffer for #pragma output */ +static StringPool* PragmaOnceSeenFiles; + /* Newlines to be added to preprocessed text */ static unsigned PendingNewLines; static unsigned ContinuedLines; @@ -2963,6 +2966,32 @@ static void DoLine (void) MLine = InitLine (MLine); } +/** + * Determines the absolute path of the given relative path. + * The absolute path for the file is stored in a malloced buffer. + * Returns NULL if some error occured. + */ +char *FindAbsolutePath(const char *path); + + +#if defined(_WIN32) + +char *FindAbsolutePath(const char *path) { + return _fullpath(NULL, path, PATH_MAX); +} + +#else + +extern char* realpath(const char* path, char* resolved_path); + +/* this uses the POSIX1.-2008 version of the function, + which solves the problem of finding a maximum path length for the file */ +char* FindAbsolutePath(const char* path) { + return realpath(path, NULL); +} + +#endif + /* Possible outcomes of preprocessing a pragma */ typedef enum { /* the #pragma directive was preprocessed into _Pragma() */ @@ -2981,7 +3010,32 @@ static preproc_pragma_t DoPragmaOnce (void) * should still be processed or not. */ { - return PREPROC_PRAGMA_HALT; + const char * const file_name = GetCurrentFilename(); + + char * const full_path = FindAbsolutePath(file_name); + + if (full_path == NULL) { + AbEnd ("Failed to find the full path for the file %s", file_name); + } + + /* considering StrPool does not have a way for checking if a string + is in the set or not, compare the counts to accomplish that */ + + unsigned previous_count = SP_GetCount(PragmaOnceSeenFiles); + + SP_AddStr(PragmaOnceSeenFiles, full_path); + + unsigned next_count = SP_GetCount(PragmaOnceSeenFiles); + + free(full_path); + + if (previous_count == next_count) { + /* file has been seen*/ + return PREPROC_PRAGMA_HALT; + } + else { + return PREPROC_PRAGMA_NOTHING_EMITTED; + } } static preproc_pragma_t DoPragma (void) @@ -3381,6 +3435,9 @@ void InitPreprocess (void) /* Create the output buffers */ MLine = NewStrBuf (); PLine = NewStrBuf (); + + /* 64 is a sensible number of slots for the hash table */ + PragmaOnceSeenFiles = NewStringPool(64); } @@ -3391,6 +3448,7 @@ void DonePreprocess (void) /* Done with the output buffers */ SB_Done (MLine); SB_Done (PLine); + FreeStringPool(PragmaOnceSeenFiles); } From c0285cadc85704cd20c65c2fd75f08c2be74e1b0 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Fri, 12 Jan 2024 11:45:59 -0300 Subject: [PATCH 03/25] Update style to conform to rules --- src/cc65/preproc.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index ab330d08e..885217d5b 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -45,6 +45,7 @@ #include "print.h" #include "xmalloc.h" #include "strpool.h" +#include "abend.h" /* cc65 */ #include "codegen.h" @@ -2971,23 +2972,23 @@ static void DoLine (void) * The absolute path for the file is stored in a malloced buffer. * Returns NULL if some error occured. */ -char *FindAbsolutePath(const char *path); +char *FindAbsolutePath (const char *path); #if defined(_WIN32) -char *FindAbsolutePath(const char *path) { - return _fullpath(NULL, path, PATH_MAX); +char *FindAbsolutePath (const char *path) { + return _fullpath (NULL, path, PATH_MAX); } #else -extern char* realpath(const char* path, char* resolved_path); +extern char* realpath (const char* path, char* resolved_path); /* this uses the POSIX1.-2008 version of the function, which solves the problem of finding a maximum path length for the file */ -char* FindAbsolutePath(const char* path) { - return realpath(path, NULL); +char* FindAbsolutePath (const char* path) { + return realpath (path, NULL); } #endif @@ -3010,9 +3011,11 @@ static preproc_pragma_t DoPragmaOnce (void) * should still be processed or not. */ { - const char * const file_name = GetCurrentFilename(); + const char * const file_name = GetCurrentFilename (); + char * const full_path = FindAbsolutePath (file_name); + unsigned previous_count; + unsigned next_count; - char * const full_path = FindAbsolutePath(file_name); if (full_path == NULL) { AbEnd ("Failed to find the full path for the file %s", file_name); @@ -3021,13 +3024,13 @@ static preproc_pragma_t DoPragmaOnce (void) /* considering StrPool does not have a way for checking if a string is in the set or not, compare the counts to accomplish that */ - unsigned previous_count = SP_GetCount(PragmaOnceSeenFiles); + previous_count = SP_GetCount (PragmaOnceSeenFiles); SP_AddStr(PragmaOnceSeenFiles, full_path); - unsigned next_count = SP_GetCount(PragmaOnceSeenFiles); + next_count = SP_GetCount (PragmaOnceSeenFiles); - free(full_path); + free (full_path); if (previous_count == next_count) { /* file has been seen*/ @@ -3056,7 +3059,7 @@ static preproc_pragma_t DoPragma (void) PreprocessDirective (Line, MLine, MSM_NONE); if (SB_CompareStr(MLine, "once") == 0) { - status = DoPragmaOnce(); + status = DoPragmaOnce (); } else { /* Convert #pragma to _Pragma () */ From 9d500907699e74764facbb94aaacc7b9d217e3b1 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Fri, 12 Jan 2024 18:29:54 -0300 Subject: [PATCH 04/25] Update windows build FindAbsolutePath bug --- src/cc65/preproc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 885217d5b..207d968bf 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -2978,7 +2978,7 @@ char *FindAbsolutePath (const char *path); #if defined(_WIN32) char *FindAbsolutePath (const char *path) { - return _fullpath (NULL, path, PATH_MAX); + return _fullpath (NULL, path, MAX_PATH); } #else From fc619573b420704299fed9a49d5201f38a05a2e0 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Fri, 12 Jan 2024 18:36:38 -0300 Subject: [PATCH 05/25] Add on windows --- src/cc65/preproc.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 207d968bf..288a4b289 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -37,6 +37,9 @@ #include #include #include +#if defined(_WIN32) +#include +#endif /* common */ #include "chartype.h" From 7798c7a47131b45d78509624de2ea093d8d1ffe7 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Fri, 12 Jan 2024 20:48:05 -0300 Subject: [PATCH 06/25] Include corret header for windows --- src/cc65/preproc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 288a4b289..5219c79f6 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -38,7 +38,7 @@ #include #include #if defined(_WIN32) -#include +#include #endif /* common */ From acf9676c0f736386c8f20bf9ea25c278edbfb47c Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:30:58 -0300 Subject: [PATCH 07/25] Move FindAbsolutePath to another .c --- src/cc65/preproc.c | 29 +--------------------- src/common.vcxproj | 6 +++-- src/common/pathutil.c | 25 +++++++++++++++++++ src/common/pathutil.h | 56 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 30 deletions(-) create mode 100644 src/common/pathutil.c create mode 100644 src/common/pathutil.h diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 5219c79f6..ed7a767a7 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -37,9 +37,6 @@ #include #include #include -#if defined(_WIN32) -#include -#endif /* common */ #include "chartype.h" @@ -49,6 +46,7 @@ #include "xmalloc.h" #include "strpool.h" #include "abend.h" +#include "pathutil.h" /* cc65 */ #include "codegen.h" @@ -2970,31 +2968,6 @@ static void DoLine (void) MLine = InitLine (MLine); } -/** - * Determines the absolute path of the given relative path. - * The absolute path for the file is stored in a malloced buffer. - * Returns NULL if some error occured. - */ -char *FindAbsolutePath (const char *path); - - -#if defined(_WIN32) - -char *FindAbsolutePath (const char *path) { - return _fullpath (NULL, path, MAX_PATH); -} - -#else - -extern char* realpath (const char* path, char* resolved_path); - -/* this uses the POSIX1.-2008 version of the function, - which solves the problem of finding a maximum path length for the file */ -char* FindAbsolutePath (const char* path) { - return realpath (path, NULL); -} - -#endif /* Possible outcomes of preprocessing a pragma */ typedef enum { diff --git a/src/common.vcxproj b/src/common.vcxproj index 6098c98a0..5a6855208 100644 --- a/src/common.vcxproj +++ b/src/common.vcxproj @@ -1,4 +1,4 @@ - + @@ -110,6 +110,7 @@ + @@ -155,6 +156,7 @@ + @@ -171,4 +173,4 @@ - \ No newline at end of file + diff --git a/src/common/pathutil.c b/src/common/pathutil.c new file mode 100644 index 000000000..ce4a63106 --- /dev/null +++ b/src/common/pathutil.c @@ -0,0 +1,25 @@ + +#include + + +#if defined(_WIN32) +# include +#endif + +#if defined(_WIN32) + +char *FindAbsolutePath (const char *path) { + return _fullpath (NULL, path, MAX_PATH); +} + +#else + +extern char* realpath (const char* path, char* resolved_path); + +/* this uses the POSIX1.-2008 version of the function, + which solves the problem of finding a maximum path length for the file */ +char* FindAbsolutePath (const char* path) { + return realpath (path, NULL); +} + +#endif diff --git a/src/common/pathutil.h b/src/common/pathutil.h new file mode 100644 index 000000000..0efd6490a --- /dev/null +++ b/src/common/pathutil.h @@ -0,0 +1,56 @@ +/*****************************************************************************/ +/* */ +/* pathutil.h */ +/* Path manipulation utilities */ +/* */ +/* */ +/* */ +/* (C) 2003-2008 Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef PATHUTIL_H +#define PATHUTIL_H + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + +/** + * Determines the absolute path of the given relative path. + * The absolute path for the file is stored in a malloced buffer. + * Returns NULL if some error occured. + * The returned path's separator is system specific. + */ +char *FindAbsolutePath (const char *path); + + + +/* End of pathutil.h */ + +#endif From 42a93f00d436828fd03592a1843266507b3098f0 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Sun, 14 Jan 2024 19:43:05 -0300 Subject: [PATCH 08/25] Add StrPool lookup --- src/common/strpool.c | 19 +++++++++++++++++++ src/common/strpool.h | 14 +++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/common/strpool.c b/src/common/strpool.c index 244681afd..e7fec1208 100644 --- a/src/common/strpool.c +++ b/src/common/strpool.c @@ -281,3 +281,22 @@ unsigned SP_GetCount (const StringPool* P) { return CollCount (&P->Entries); } + +unsigned SP_Lookup(StringPool *P, const StrBuf *S) +/* +** Determine whether the given string is in the pool. +** Returns 1 if the string is in the pool, 0 otherwise. +*/ +{ + return HT_Find (&P->Tab, S) != 0; +} + +unsigned SP_LookupStr(StringPool *P, const char *S) +/* +** Determine whether the given string is in the pool. +** Returns 1 if the string is in the pool, 0 otherwise. +*/ +{ + StrBuf Buf; + return SP_Lookup (P, SB_InitFromString (&Buf, S)); +} diff --git a/src/common/strpool.h b/src/common/strpool.h index 8a6a8faee..fefab27e1 100644 --- a/src/common/strpool.h +++ b/src/common/strpool.h @@ -84,7 +84,7 @@ const StrBuf* SP_Get (const StringPool* P, unsigned Index); unsigned SP_Add (StringPool* P, const StrBuf* S); /* Add a string buffer to the buffer and return the index. If the string does -** already exist in the pool, SP_AddBuf will just return the index of the +** already exist in the pool, SP_Add will just return the index of the ** existing string. */ @@ -96,6 +96,18 @@ unsigned SP_AddStr (StringPool* P, const char* S); unsigned SP_GetCount (const StringPool* P); /* Return the number of strings in the pool */ +unsigned SP_Lookup(StringPool *P, const StrBuf *S); +/* +** Determine whether the given string is in the pool. +** Returns 1 if the string is in the pool, 0 otherwise. +*/ + +unsigned SP_LookupStr(StringPool *P, const char *S); +/* +** Determine whether the given string is in the pool. +** Returns 1 if the string is in the pool, 0 otherwise. +*/ + /* End of strpool.h */ From d34a6df002b293946398016b301002f877033e88 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Sun, 14 Jan 2024 19:43:34 -0300 Subject: [PATCH 09/25] Improve preprocessor pragma once implementation Pragma once checking now happens in #include --- src/cc65/preproc.c | 90 +++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 57 deletions(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index ed7a767a7..974687899 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -2809,6 +2809,26 @@ static int DoIfDef (int skip, int flag) } +static void TryOpenIncludeFile (const char* Name, InputType IT) +/* Opens the given inclde file if it has not been marked with #pragma once. */ +{ + char *FullPath = FindAbsolutePath(Name); + + if (FullPath == NULL) { + PPError ("Failed to find the full path for the file %s", Name); + return; + } + + /* Has #pragma once been seen in this file already? */ + if (SP_LookupStr (PragmaOnceSeenFiles, FullPath)) { + /* Do not include it. */ + free(FullPath); + return; + } + + OpenIncludeFile (Name, IT); + free(FullPath); +} static void DoInclude (void) /* Open an include file. */ @@ -2860,7 +2880,7 @@ static void DoInclude (void) /* Check for extra tokens following the filename */ CheckExtraTokens ("include"); /* Open the include file */ - OpenIncludeFile (SB_GetConstBuf (&Filename), IT); + TryOpenIncludeFile (SB_GetConstBuf (&Filename), IT); } else { /* No terminator found */ PPError ("#include expects \"FILENAME\" or "); @@ -2968,62 +2988,25 @@ static void DoLine (void) MLine = InitLine (MLine); } - -/* Possible outcomes of preprocessing a pragma */ -typedef enum { - /* the #pragma directive was preprocessed into _Pragma() */ - PREPROC_PRAGMA_EMITTED, - - /* processing the #pragma directive did not result in any output */ - PREPROC_PRAGMA_NOTHING_EMITTED, - - /* the preprocessor should stop processing the current file */ - PREPROC_PRAGMA_HALT -} preproc_pragma_t; - -static preproc_pragma_t DoPragmaOnce (void) -/** - * Handle a pragma once directive, and determine if the current file - * should still be processed or not. - */ +static void DoPragmaOnce (void) +/* Marks the current file as seen by #pragma once. */ { - const char * const file_name = GetCurrentFilename (); - char * const full_path = FindAbsolutePath (file_name); - unsigned previous_count; - unsigned next_count; + const char * const Filename = GetCurrentFilename (); + char * const FullPath = FindAbsolutePath (Filename); - - if (full_path == NULL) { - AbEnd ("Failed to find the full path for the file %s", file_name); + if (FullPath == NULL) { + PPError ("Failed to find the full path for the file %s", Filename); } - /* considering StrPool does not have a way for checking if a string - is in the set or not, compare the counts to accomplish that */ - - previous_count = SP_GetCount (PragmaOnceSeenFiles); - - SP_AddStr(PragmaOnceSeenFiles, full_path); - - next_count = SP_GetCount (PragmaOnceSeenFiles); - - free (full_path); - - if (previous_count == next_count) { - /* file has been seen*/ - return PREPROC_PRAGMA_HALT; - } - else { - return PREPROC_PRAGMA_NOTHING_EMITTED; - } + SP_AddStr(PragmaOnceSeenFiles, FullPath); } -static preproc_pragma_t DoPragma (void) +static void DoPragma (void) /* Handle a #pragma line by converting the #pragma preprocessor directive into ** the _Pragma() compiler operator. */ { StrBuf* const PragmaLine = OLine; - preproc_pragma_t status; PRECONDITION (PragmaLine != 0); @@ -3035,7 +3018,7 @@ static preproc_pragma_t DoPragma (void) PreprocessDirective (Line, MLine, MSM_NONE); if (SB_CompareStr(MLine, "once") == 0) { - status = DoPragmaOnce (); + DoPragmaOnce (); } else { /* Convert #pragma to _Pragma () */ @@ -3043,13 +3026,10 @@ static preproc_pragma_t DoPragma (void) SB_Reset (MLine); Stringize (MLine, PragmaLine); SB_AppendChar (PragmaLine, ')'); - status = PREPROC_PRAGMA_EMITTED; } /* End this line */ SB_SetIndex (PragmaLine, SB_GetLen (PragmaLine)); - - return status; } @@ -3217,13 +3197,9 @@ static int ParseDirectives (unsigned ModeFlags) case PPD_PRAGMA: if (!PPSkip) { if ((ModeFlags & MSM_IN_ARG_LIST) == 0) { - preproc_pragma_t status = DoPragma(); - if (status == PREPROC_PRAGMA_EMITTED) { - return Whitespace; - } - else if (status == PREPROC_PRAGMA_HALT) { - PPSkip = 1; - } + DoPragma(); + + return Whitespace; } else { PPError ("Embedded #pragma directive within macro arguments is unsupported"); } From 085509c2e1e7622170683371931a34dc66ab28e6 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Tue, 16 Jan 2024 16:02:57 -0300 Subject: [PATCH 10/25] Improve FindAbsolutePath implementation --- src/common/pathutil.c | 55 ++++++++++++++++++++++++++++++++++++++----- src/common/pathutil.h | 14 +++++------ 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/common/pathutil.c b/src/common/pathutil.c index ce4a63106..acfea84ec 100644 --- a/src/common/pathutil.c +++ b/src/common/pathutil.c @@ -1,24 +1,67 @@ #include - #if defined(_WIN32) # include +# include "xmalloc.h" #endif #if defined(_WIN32) -char *FindAbsolutePath (const char *path) { - return _fullpath (NULL, path, MAX_PATH); +char *FindAbsolutePath (const char *Path) +/* +** Determines the absolute path of the given relative path. +** If the path points to a symlink, resolves such symlink. +** The absolute path for the file is stored in a malloced buffer. +** Returns NULL if some error occured. +** The returned path's separator is system specific. +*/ +{ + + HANDLE Handle = CreateFileA (Path, + FILE_READ_ATTRIBUTES, + FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + NULL, + OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, + NULL); + + if (Handle == INVALID_HANDLE_VALUE) { + return NULL; + } + + size_t BufferSize = MAX_PATH + 10; + char* Buffer = xmalloc(BufferSize); + + DWORD Status = GetFinalPathNameByHandleA (Handle, + Buffer, + BufferSize, + FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); + + if (Status == 0) { + free(Buffer); + CloseHandle(Handle); + return NULL; + } + + CloseHandle(Handle); + + return Buffer; } #else extern char* realpath (const char* path, char* resolved_path); -/* this uses the POSIX1.-2008 version of the function, - which solves the problem of finding a maximum path length for the file */ -char* FindAbsolutePath (const char* path) { +char* FindAbsolutePath (const char* path) +/* +** Determines the absolute path of the given relative path. +** If the path points to a symlink, resolves such symlink. +** The absolute path for the file is stored in a malloced buffer. +** Returns NULL if some error occured. +** The returned path's separator is system specific. +*/ +{ return realpath (path, NULL); } diff --git a/src/common/pathutil.h b/src/common/pathutil.h index 0efd6490a..50875330e 100644 --- a/src/common/pathutil.h +++ b/src/common/pathutil.h @@ -41,14 +41,14 @@ /*****************************************************************************/ -/** - * Determines the absolute path of the given relative path. - * The absolute path for the file is stored in a malloced buffer. - * Returns NULL if some error occured. - * The returned path's separator is system specific. - */ char *FindAbsolutePath (const char *path); - +/* +** Determines the absolute path of the given relative path. +** If the path points to a symlink, resolves such symlink. +** The absolute path for the file is stored in a malloced buffer. +** Returns NULL if some error occured. +** The returned path's separator is system specific. +*/ /* End of pathutil.h */ From aa47b6c1c7f4031ab8d01c9644a67585496e2df8 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Tue, 16 Jan 2024 19:49:47 -0300 Subject: [PATCH 11/25] Refactor include file searching --- src/cc65/input.c | 8 +++++++- src/cc65/input.h | 7 +++++-- src/cc65/preproc.c | 30 +++++------------------------- src/common/searchpath.c | 9 ++++++--- 4 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/cc65/input.c b/src/cc65/input.c index 89c471687..da93f0e19 100644 --- a/src/cc65/input.c +++ b/src/cc65/input.c @@ -293,7 +293,7 @@ void OpenMainFile (const char* Name) -void OpenIncludeFile (const char* Name, InputType IT) +void OpenIncludeFile (const char* Name, InputType IT, StringPool *FilesToIgnore) /* Open an include file and insert it into the tables. */ { char* N; @@ -314,6 +314,12 @@ void OpenIncludeFile (const char* Name, InputType IT) return; } + if (SP_LookupStr(FilesToIgnore, N) != 0) { + /* This file should not be included. */ + xfree (N); + return; + } + /* Search the list of all input files for this file. If we don't find ** it, create a new IFile object. */ diff --git a/src/cc65/input.h b/src/cc65/input.h index 9457bdf9b..c7e730f3d 100644 --- a/src/cc65/input.h +++ b/src/cc65/input.h @@ -43,6 +43,7 @@ /* common */ #include "coll.h" #include "strbuf.h" +#include "strpool.h" @@ -82,8 +83,10 @@ extern char NextC; void OpenMainFile (const char* Name); /* Open the main file. Will call Fatal() in case of failures. */ -void OpenIncludeFile (const char* Name, InputType IT); -/* Open an include file and insert it into the tables. */ +void OpenIncludeFile (const char* Name, InputType IT, StringPool* FilesToIgnore); +/* Open an include file and insert it into the tables. +** Does nothing if the resolved file is present in the pool. +*/ void CloseIncludeFile (void); /* Close an include file and switch to the higher level file. Set Input to diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 974687899..146ba452c 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -46,7 +46,8 @@ #include "xmalloc.h" #include "strpool.h" #include "abend.h" -#include "pathutil.h" +#include "searchpath.h" +#include "incpath.h" /* cc65 */ #include "codegen.h" @@ -2809,27 +2810,6 @@ static int DoIfDef (int skip, int flag) } -static void TryOpenIncludeFile (const char* Name, InputType IT) -/* Opens the given inclde file if it has not been marked with #pragma once. */ -{ - char *FullPath = FindAbsolutePath(Name); - - if (FullPath == NULL) { - PPError ("Failed to find the full path for the file %s", Name); - return; - } - - /* Has #pragma once been seen in this file already? */ - if (SP_LookupStr (PragmaOnceSeenFiles, FullPath)) { - /* Do not include it. */ - free(FullPath); - return; - } - - OpenIncludeFile (Name, IT); - free(FullPath); -} - static void DoInclude (void) /* Open an include file. */ { @@ -2879,8 +2859,8 @@ static void DoInclude (void) NextChar (); /* Check for extra tokens following the filename */ CheckExtraTokens ("include"); - /* Open the include file */ - TryOpenIncludeFile (SB_GetConstBuf (&Filename), IT); + /* Open the include file, if it is not marked with #pragma once */ + OpenIncludeFile (SB_GetConstBuf (&Filename), IT, PragmaOnceSeenFiles); } else { /* No terminator found */ PPError ("#include expects \"FILENAME\" or "); @@ -2992,7 +2972,7 @@ static void DoPragmaOnce (void) /* Marks the current file as seen by #pragma once. */ { const char * const Filename = GetCurrentFilename (); - char * const FullPath = FindAbsolutePath (Filename); + char * const FullPath = SearchFile(UsrIncSearchPath, Filename); if (FullPath == NULL) { PPError ("Failed to find the full path for the file %s", Filename); diff --git a/src/common/searchpath.c b/src/common/searchpath.c index 3628dab5c..4dd578f15 100644 --- a/src/common/searchpath.c +++ b/src/common/searchpath.c @@ -57,6 +57,7 @@ #include "searchpath.h" #include "strbuf.h" #include "xmalloc.h" +#include "pathutil.h" @@ -377,10 +378,12 @@ char* SearchFile (const SearchPaths* P, const char* File) SB_AppendStr (&PathName, File); SB_Terminate (&PathName); - /* Check if this file exists */ - if (access (SB_GetBuf (&PathName), 0) == 0) { + + /* Find real path of file, if it exists */ + Name = FindAbsolutePath(SB_GetBuf(&PathName)); + + if (Name != 0) { /* The file exists, we're done */ - Name = xstrdup (SB_GetBuf (&PathName)); break; } } From 346dd7df85438a3020e8a3e457e89b0fda6e7801 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:07:07 -0300 Subject: [PATCH 12/25] Remove symlink resolve from searchpath to maintain behaviour --- src/cc65/input.c | 17 +++++++++++++++-- src/cc65/preproc.c | 11 +++++++++-- src/common/searchpath.c | 8 +++----- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/cc65/input.c b/src/cc65/input.c index da93f0e19..a5a0c7e37 100644 --- a/src/cc65/input.c +++ b/src/cc65/input.c @@ -45,6 +45,7 @@ #include "print.h" #include "strbuf.h" #include "xmalloc.h" +#include "pathutil.h" /* cc65 */ #include "codegen.h" @@ -297,6 +298,7 @@ void OpenIncludeFile (const char* Name, InputType IT, StringPool *FilesToIgnore) /* Open an include file and insert it into the tables. */ { char* N; + char* M; FILE* F; IFile* IF; AFile* AF; @@ -314,12 +316,23 @@ void OpenIncludeFile (const char* Name, InputType IT, StringPool *FilesToIgnore) return; } - if (SP_LookupStr(FilesToIgnore, N) != 0) { - /* This file should not be included. */ + /* Resolve real path of file in case of a symlink */ + M = FindAbsolutePath(N); + if (M == 0) { + PPError ("Cannot resolve absolute path of '%s'", N); xfree (N); return; } + if (SP_LookupStr(FilesToIgnore, M) != 0) { + /* This file should not be included. */ + xfree (M); + xfree (N); + return; + } + + xfree (M); + /* Search the list of all input files for this file. If we don't find ** it, create a new IFile object. */ diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 146ba452c..4fdb65283 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -48,6 +48,7 @@ #include "abend.h" #include "searchpath.h" #include "incpath.h" +#include "pathutil.h" /* cc65 */ #include "codegen.h" @@ -2972,10 +2973,16 @@ static void DoPragmaOnce (void) /* Marks the current file as seen by #pragma once. */ { const char * const Filename = GetCurrentFilename (); - char * const FullPath = SearchFile(UsrIncSearchPath, Filename); + char * const IncludePath = SearchFile(UsrIncSearchPath, Filename); + + if (IncludePath == NULL) { + AbEnd ("Cannot find the full path for the file %s", Filename); + } + + const char * const FullPath = FindAbsolutePath(IncludePath); if (FullPath == NULL) { - PPError ("Failed to find the full path for the file %s", Filename); + AbEnd ("Failed to find the full path for the file %s", Filename); } SP_AddStr(PragmaOnceSeenFiles, FullPath); diff --git a/src/common/searchpath.c b/src/common/searchpath.c index 4dd578f15..592c10ccb 100644 --- a/src/common/searchpath.c +++ b/src/common/searchpath.c @@ -57,7 +57,6 @@ #include "searchpath.h" #include "strbuf.h" #include "xmalloc.h" -#include "pathutil.h" @@ -379,11 +378,10 @@ char* SearchFile (const SearchPaths* P, const char* File) SB_Terminate (&PathName); - /* Find real path of file, if it exists */ - Name = FindAbsolutePath(SB_GetBuf(&PathName)); - - if (Name != 0) { + /* Check if this file exists */ + if (access (SB_GetBuf (&PathName), 0) == 0) { /* The file exists, we're done */ + Name = xstrdup (SB_GetBuf (&PathName)); break; } } From a6f207a2a598e74516d996e63e40a0fcea562629 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:33:49 -0300 Subject: [PATCH 13/25] Improve FindAbsolutePath documentation and API --- src/cc65/input.c | 2 +- src/cc65/preproc.c | 2 +- src/common/pathutil.c | 9 +++++---- src/common/pathutil.h | 8 ++++---- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/cc65/input.c b/src/cc65/input.c index a5a0c7e37..a84d68405 100644 --- a/src/cc65/input.c +++ b/src/cc65/input.c @@ -317,7 +317,7 @@ void OpenIncludeFile (const char* Name, InputType IT, StringPool *FilesToIgnore) } /* Resolve real path of file in case of a symlink */ - M = FindAbsolutePath(N); + M = FindRealPath(N); if (M == 0) { PPError ("Cannot resolve absolute path of '%s'", N); xfree (N); diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 4fdb65283..69358bdc3 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -2979,7 +2979,7 @@ static void DoPragmaOnce (void) AbEnd ("Cannot find the full path for the file %s", Filename); } - const char * const FullPath = FindAbsolutePath(IncludePath); + const char * const FullPath = FindRealPath(IncludePath); if (FullPath == NULL) { AbEnd ("Failed to find the full path for the file %s", Filename); diff --git a/src/common/pathutil.c b/src/common/pathutil.c index acfea84ec..157e54964 100644 --- a/src/common/pathutil.c +++ b/src/common/pathutil.c @@ -3,17 +3,18 @@ #if defined(_WIN32) # include +# include # include "xmalloc.h" #endif #if defined(_WIN32) -char *FindAbsolutePath (const char *Path) +char *FindRealPath (const char *Path) /* -** Determines the absolute path of the given relative path. +** Determines the real path the given relative path of an existing file. ** If the path points to a symlink, resolves such symlink. -** The absolute path for the file is stored in a malloced buffer. -** Returns NULL if some error occured. +** The real path for the file is stored in a malloced buffer. +** Returns NULL if the file doesn't exist. ** The returned path's separator is system specific. */ { diff --git a/src/common/pathutil.h b/src/common/pathutil.h index 50875330e..e86a9c1a0 100644 --- a/src/common/pathutil.h +++ b/src/common/pathutil.h @@ -41,12 +41,12 @@ /*****************************************************************************/ -char *FindAbsolutePath (const char *path); +char *FindRealPath (const char *path); /* -** Determines the absolute path of the given relative path. +** Determines the real path the given relative path of an existing file. ** If the path points to a symlink, resolves such symlink. -** The absolute path for the file is stored in a malloced buffer. -** Returns NULL if some error occured. +** The real path for the file is stored in a malloced buffer. +** Returns NULL if the file doesn't exist. ** The returned path's separator is system specific. */ From 167c31c148ee1c94665200f5466ec88a428af321 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:47:53 -0300 Subject: [PATCH 14/25] Update FindRealPath function object name --- src/common/pathutil.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/pathutil.c b/src/common/pathutil.c index 157e54964..d1f9511d6 100644 --- a/src/common/pathutil.c +++ b/src/common/pathutil.c @@ -54,7 +54,7 @@ char *FindRealPath (const char *Path) extern char* realpath (const char* path, char* resolved_path); -char* FindAbsolutePath (const char* path) +char* FindRealPath (const char* path) /* ** Determines the absolute path of the given relative path. ** If the path points to a symlink, resolves such symlink. From 30ff2bdc6b9f65933369d0ddde1e5fba4d44bdce Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:05:27 -0300 Subject: [PATCH 15/25] Add pragma once tests --- test/val/pragma-once-sample-link.h | 1 + test/val/pragma-once-sample.h | 21 +++++++++++++++++++++ test/val/pragma-once-test.c | 27 +++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 120000 test/val/pragma-once-sample-link.h create mode 100644 test/val/pragma-once-sample.h create mode 100644 test/val/pragma-once-test.c diff --git a/test/val/pragma-once-sample-link.h b/test/val/pragma-once-sample-link.h new file mode 120000 index 000000000..d19d8c541 --- /dev/null +++ b/test/val/pragma-once-sample-link.h @@ -0,0 +1 @@ +pragma-once-sample.h \ No newline at end of file diff --git a/test/val/pragma-once-sample.h b/test/val/pragma-once-sample.h new file mode 100644 index 000000000..477c7c8bd --- /dev/null +++ b/test/val/pragma-once-sample.h @@ -0,0 +1,21 @@ +/* +** !!DESCRIPTION!! Simple #pragma once directive tests +** !!ORIGIN!! cc65 regression tests +** !!LICENCE!! Public Domain +*/ + +#ifdef FILE_INCLUDED + +#error "This file should not have been included twice" +#define INCLUDED_TWICE + +#else + +#define FILE_INCLUDED + +#endif + + +/* a pragma once directive should work regardless of where it is located in + the file, as long as it is seen by the preprocessor */ +#pragma once diff --git a/test/val/pragma-once-test.c b/test/val/pragma-once-test.c new file mode 100644 index 000000000..e85f19bbb --- /dev/null +++ b/test/val/pragma-once-test.c @@ -0,0 +1,27 @@ +/* +** !!DESCRIPTION!! Simple #pragma once directive tests +** !!ORIGIN!! cc65 regression tests +** !!LICENCE!! Public Domain +*/ + + +#include "pragma-once-sample.h" +#include "pragma-once-sample.h" +#include "pragma-once-sample-link.h" + +/* pragma-once-sample-link.h is a symlink to pragma-once-sample. */ + + +#include + + +int main() { + +#ifdef INCLUDED_TWICE + printf("pragma-once-sample.h included more than once\n"); + return 1; +#else + printf("pragma-once-sample included once\n"); + return 0; +#endif +} From 98208e7e95436dc6bee435d6b7e637b4aed7858a Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 16:49:47 -0300 Subject: [PATCH 16/25] Add pragma once to the documentation --- doc/cc65.sgml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/doc/cc65.sgml b/doc/cc65.sgml index efe48b61b..634195497 100644 --- a/doc/cc65.sgml +++ b/doc/cc65.sgml @@ -1681,6 +1681,16 @@ void somefunc2(int, char *); +#pragma once

+ + This pragma is used to prevent multiple inclusion of a header file. + If this pragma is preprocessed in a file, any posterior + #include which targets the current file will be ignored. + + Two files are considered to be the same by this directive if they + share the same absolute path. In the case of symbolic links, + the canonical, resolved path is considered instead. + Register variables

From 57e6c2a4f9feea1240d0ed83b3af9784df6a2d13 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 17:45:11 -0300 Subject: [PATCH 17/25] Make pragma once work even in .c files --- src/cc65/preproc.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 69358bdc3..846443f6b 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -46,8 +46,6 @@ #include "xmalloc.h" #include "strpool.h" #include "abend.h" -#include "searchpath.h" -#include "incpath.h" #include "pathutil.h" /* cc65 */ @@ -2973,19 +2971,16 @@ static void DoPragmaOnce (void) /* Marks the current file as seen by #pragma once. */ { const char * const Filename = GetCurrentFilename (); - char * const IncludePath = SearchFile(UsrIncSearchPath, Filename); - if (IncludePath == NULL) { - AbEnd ("Cannot find the full path for the file %s", Filename); - } - - const char * const FullPath = FindRealPath(IncludePath); + char * const FullPath = FindRealPath (Filename); if (FullPath == NULL) { - AbEnd ("Failed to find the full path for the file %s", Filename); + AbEnd ("Failed to find the real path for the file %s", Filename); } - SP_AddStr(PragmaOnceSeenFiles, FullPath); + SP_AddStr (PragmaOnceSeenFiles, FullPath); + + free (FullPath); } static void DoPragma (void) From 6e203ab2d2c8549928b800a221699f43576c8652 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 17:47:08 -0300 Subject: [PATCH 18/25] Improve pathutil.c style --- src/common/pathutil.c | 32 ++++++++++++++++---------------- src/common/pathutil.h | 9 ++++----- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/common/pathutil.c b/src/common/pathutil.c index d1f9511d6..d207f2bc5 100644 --- a/src/common/pathutil.c +++ b/src/common/pathutil.c @@ -11,17 +11,17 @@ char *FindRealPath (const char *Path) /* -** Determines the real path the given relative path of an existing file. -** If the path points to a symlink, resolves such symlink. -** The real path for the file is stored in a malloced buffer. -** Returns NULL if the file doesn't exist. -** The returned path's separator is system specific. +** Returns a malloced buffer containing the canonical path of the given path. +** If the path points to a non-existent file, or if any error occurs, NULL is returned. +** If the path points to a symlink, the resolved symlink path is returned. +** Note: The returned path's separator is system specific. */ { HANDLE Handle = CreateFileA (Path, FILE_READ_ATTRIBUTES, - FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, + FILE_SHARE_READ | + FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, @@ -32,20 +32,21 @@ char *FindRealPath (const char *Path) } size_t BufferSize = MAX_PATH + 10; - char* Buffer = xmalloc(BufferSize); + char* Buffer = xmalloc (BufferSize); DWORD Status = GetFinalPathNameByHandleA (Handle, Buffer, BufferSize, - FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); + FILE_NAME_NORMALIZED + | VOLUME_NAME_DOS); if (Status == 0) { - free(Buffer); - CloseHandle(Handle); + xfree (Buffer); + CloseHandle (Handle); return NULL; } - CloseHandle(Handle); + CloseHandle (Handle); return Buffer; } @@ -56,11 +57,10 @@ extern char* realpath (const char* path, char* resolved_path); char* FindRealPath (const char* path) /* -** Determines the absolute path of the given relative path. -** If the path points to a symlink, resolves such symlink. -** The absolute path for the file is stored in a malloced buffer. -** Returns NULL if some error occured. -** The returned path's separator is system specific. +** Returns a malloced buffer containing the canonical path of the given path. +** If the path points to a non-existent file, or if any error occurs, NULL is returned. +** If the path points to a symlink, the resolved symlink path is returned. +** Note: The returned path's separator is system specific. */ { return realpath (path, NULL); diff --git a/src/common/pathutil.h b/src/common/pathutil.h index e86a9c1a0..d29728ca2 100644 --- a/src/common/pathutil.h +++ b/src/common/pathutil.h @@ -43,11 +43,10 @@ char *FindRealPath (const char *path); /* -** Determines the real path the given relative path of an existing file. -** If the path points to a symlink, resolves such symlink. -** The real path for the file is stored in a malloced buffer. -** Returns NULL if the file doesn't exist. -** The returned path's separator is system specific. +** Returns a malloced buffer containing the canonical path of the given path. +** If the path points to a non-existent file, or if any error occurs, NULL is returned. +** If the path points to a symlink, the resolved symlink path is returned. +** Note: The returned path's separator is system specific. */ From dfbfe79c711f12e0340acaae062dbabc3b07a709 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 19:15:59 -0300 Subject: [PATCH 19/25] Add minimum version for pathutil.c --- src/common/pathutil.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/common/pathutil.c b/src/common/pathutil.c index d207f2bc5..7c70383f7 100644 --- a/src/common/pathutil.c +++ b/src/common/pathutil.c @@ -1,12 +1,21 @@ -#include - #if defined(_WIN32) -# include -# include -# include "xmalloc.h" + +/* Set minimum windows version for GetFinalPathNameByHandleA */ +/* NTDDI_VISTA */ +#define NTDDI_VERSION 0x06000000 + +/* _WIN32_WINNT_VISTA */ +#define _WIN32_WINNT 0x600 + +#include "xmalloc.h" +#include +#include + #endif +#include + #if defined(_WIN32) char *FindRealPath (const char *Path) From e10647dca6821c64667f993b9fd071d8c07333ec Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 19:23:09 -0300 Subject: [PATCH 20/25] Add pathutil.c comments and improve version macros --- src/common/pathutil.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/common/pathutil.h | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/common/pathutil.c b/src/common/pathutil.c index 7c70383f7..82dd0df22 100644 --- a/src/common/pathutil.c +++ b/src/common/pathutil.c @@ -1,6 +1,39 @@ +/*****************************************************************************/ +/* */ +/* pathutil.c */ +/* Path manipulation utilities */ +/* */ +/* */ +/* */ +/* (C) 2003-2008 Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ #if defined(_WIN32) +#if !defined(_WIN32_WINNT) && !defined(NTDDI_VERSION) + /* Set minimum windows version for GetFinalPathNameByHandleA */ /* NTDDI_VISTA */ #define NTDDI_VERSION 0x06000000 @@ -8,6 +41,8 @@ /* _WIN32_WINNT_VISTA */ #define _WIN32_WINNT 0x600 +#endif + #include "xmalloc.h" #include #include @@ -16,6 +51,14 @@ #include + + +/*****************************************************************************/ +/* code */ +/*****************************************************************************/ + + + #if defined(_WIN32) char *FindRealPath (const char *Path) diff --git a/src/common/pathutil.h b/src/common/pathutil.h index d29728ca2..2e2154102 100644 --- a/src/common/pathutil.h +++ b/src/common/pathutil.h @@ -1,7 +1,7 @@ /*****************************************************************************/ /* */ /* pathutil.h */ -/* Path manipulation utilities */ +/* Path manipulation utilities */ /* */ /* */ /* */ From 9c877fa5296ddbddf0b082e03773bfd7bcfd050b Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 19:36:25 -0300 Subject: [PATCH 21/25] Fix documentation label issue --- doc/cc65.sgml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/cc65.sgml b/doc/cc65.sgml index 634195497..d487ebc63 100644 --- a/doc/cc65.sgml +++ b/doc/cc65.sgml @@ -1681,15 +1681,15 @@ void somefunc2(int, char *); -#pragma once

+#pragma once

- This pragma is used to prevent multiple inclusion of a header file. - If this pragma is preprocessed in a file, any posterior - #include which targets the current file will be ignored. + This pragma is used to prevent multiple inclusion of a header file. + If this pragma is preprocessed in a file, any posterior + #include which targets the current file will be ignored. - Two files are considered to be the same by this directive if they - share the same absolute path. In the case of symbolic links, - the canonical, resolved path is considered instead. + Two files are considered to be the same by this directive if they + share the same absolute path. In the case of symbolic links, + the canonical, resolved path is considered instead. Register variables

From d4c7cba521da8119c4e25c804929463760fed7d7 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Wed, 17 Jan 2024 20:36:28 -0300 Subject: [PATCH 22/25] Update documentation tt tag --- doc/cc65.sgml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/cc65.sgml b/doc/cc65.sgml index d487ebc63..53b9d0773 100644 --- a/doc/cc65.sgml +++ b/doc/cc65.sgml @@ -1685,7 +1685,7 @@ void somefunc2(int, char *); This pragma is used to prevent multiple inclusion of a header file. If this pragma is preprocessed in a file, any posterior - #include which targets the current file will be ignored. + #include which targets the current file will be ignored. Two files are considered to be the same by this directive if they share the same absolute path. In the case of symbolic links, From 7a5526b19e0c7484e5a7eb09e30f143679b411d5 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Thu, 18 Jan 2024 20:13:47 -0300 Subject: [PATCH 23/25] Remove symlink from tests --- test/val/pragma-once-sample-2.h | 8 ++++++++ test/val/pragma-once-sample-link.h | 1 - test/val/pragma-once-test.c | 6 +++--- 3 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 test/val/pragma-once-sample-2.h delete mode 120000 test/val/pragma-once-sample-link.h diff --git a/test/val/pragma-once-sample-2.h b/test/val/pragma-once-sample-2.h new file mode 100644 index 000000000..86fdc2108 --- /dev/null +++ b/test/val/pragma-once-sample-2.h @@ -0,0 +1,8 @@ +/* +** !!DESCRIPTION!! Simple #pragma once directive tests +** !!ORIGIN!! cc65 regression tests +** !!LICENCE!! Public Domain +*/ + +#include "pragma-once-sample.h"; + diff --git a/test/val/pragma-once-sample-link.h b/test/val/pragma-once-sample-link.h deleted file mode 120000 index d19d8c541..000000000 --- a/test/val/pragma-once-sample-link.h +++ /dev/null @@ -1 +0,0 @@ -pragma-once-sample.h \ No newline at end of file diff --git a/test/val/pragma-once-test.c b/test/val/pragma-once-test.c index e85f19bbb..0816c7698 100644 --- a/test/val/pragma-once-test.c +++ b/test/val/pragma-once-test.c @@ -4,12 +4,12 @@ ** !!LICENCE!! Public Domain */ +#pragma once +#include "pragma-once-sample-2.h" +#include "pragma-once-sample-2.h" #include "pragma-once-sample.h" #include "pragma-once-sample.h" -#include "pragma-once-sample-link.h" - -/* pragma-once-sample-link.h is a symlink to pragma-once-sample. */ #include From 8e17cc2692ca83a2f232d7e8c9f25808784b0728 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Fri, 19 Jan 2024 13:06:11 -0300 Subject: [PATCH 24/25] Remove useless function prototype in pathutil.c --- src/common/pathutil.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/common/pathutil.c b/src/common/pathutil.c index 82dd0df22..a3b159a59 100644 --- a/src/common/pathutil.c +++ b/src/common/pathutil.c @@ -105,8 +105,6 @@ char *FindRealPath (const char *Path) #else -extern char* realpath (const char* path, char* resolved_path); - char* FindRealPath (const char* path) /* ** Returns a malloced buffer containing the canonical path of the given path. From e8e0c41f87de1af1ebf6f81282f85d81b7a3e0a9 Mon Sep 17 00:00:00 2001 From: cosineblast <55855728+cosineblast@users.noreply.github.com> Date: Fri, 19 Jan 2024 21:21:46 -0300 Subject: [PATCH 25/25] Fix style issue in preproc.c --- src/cc65/preproc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cc65/preproc.c b/src/cc65/preproc.c index 846443f6b..eaf82f0ca 100644 --- a/src/cc65/preproc.c +++ b/src/cc65/preproc.c @@ -3179,7 +3179,7 @@ static int ParseDirectives (unsigned ModeFlags) case PPD_PRAGMA: if (!PPSkip) { if ((ModeFlags & MSM_IN_ARG_LIST) == 0) { - DoPragma(); + DoPragma (); return Whitespace; } else {