merge master

This commit is contained in:
mrdudz
2025-06-17 22:51:25 +02:00
29 changed files with 1391 additions and 101 deletions

View File

@@ -496,6 +496,14 @@ void Compile (const char* FileName)
while (PreprocessNextLine ())
{ /* Nothing */ }
/* Output macros if requested by the user */
if (DumpPredefMacros) {
OutputPredefMacros ();
}
if (DumpUserMacros) {
OutputUserMacros ();
}
/* Close the output file */
CloseOutputFile ();

View File

@@ -39,6 +39,7 @@
/* common */
#include "coll.h"
#include "debugflag.h"
#include "print.h"
#include "strbuf.h"
@@ -183,11 +184,15 @@ static unsigned GetDiagnosticLineNum (void)
void Fatal (const char* Format, ...)
void Fatal_ (const char *file, int line, const char* Format, ...)
/* Print a message about a fatal error and die */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
fprintf (stderr, "%s:%u: Fatal: ", GetDiagnosticFileName (), GetDiagnosticLineNum ());
va_start (ap, Format);
@@ -203,11 +208,15 @@ void Fatal (const char* Format, ...)
void Internal (const char* Format, ...)
void Internal_ (const char *file, int line, const char* Format, ...)
/* Print a message about an internal compiler error and die */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
fprintf (stderr, "%s:%u: Internal compiler error:\n",
GetDiagnosticFileName (), GetDiagnosticLineNum ());
@@ -270,10 +279,15 @@ static void IntError (errcat_t EC, LineInfo* LI, const char* Msg, va_list ap)
void LIError (errcat_t EC, LineInfo* LI, const char* Format, ...)
void LIError_ (const char *file, int line, errcat_t EC, LineInfo* LI, const char* Format, ...)
/* Print an error message with the line info given explicitly */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
va_start (ap, Format);
IntError (EC, LI, Format, ap);
va_end (ap);
@@ -281,10 +295,15 @@ void LIError (errcat_t EC, LineInfo* LI, const char* Format, ...)
void Error (const char* Format, ...)
void Error_ (const char *file, int line, const char* Format, ...)
/* Print an error message */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
va_start (ap, Format);
IntError (EC_PARSER, GetDiagnosticLI (), Format, ap);
va_end (ap);
@@ -292,10 +311,15 @@ void Error (const char* Format, ...)
void PPError (const char* Format, ...)
void PPError_ (const char *file, int line, const char* Format, ...)
/* Print an error message. For use within the preprocessor */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
va_start (ap, Format);
IntError (EC_PP, GetCurLineInfo (), Format, ap);
va_end (ap);
@@ -346,10 +370,15 @@ static void IntWarning (errcat_t EC, LineInfo* LI, const char* Msg, va_list ap)
void LIWarning (errcat_t EC, LineInfo* LI, const char* Format, ...)
void LIWarning_ (const char *file, int line, errcat_t EC, LineInfo* LI, const char* Format, ...)
/* Print a warning message with the line info given explicitly */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
va_start (ap, Format);
IntWarning (EC, LI, Format, ap);
va_end (ap);
@@ -357,10 +386,15 @@ void LIWarning (errcat_t EC, LineInfo* LI, const char* Format, ...)
void Warning (const char* Format, ...)
void Warning_ (const char *file, int line, const char* Format, ...)
/* Print a warning message */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
va_start (ap, Format);
IntWarning (EC_PARSER, GetDiagnosticLI (), Format, ap);
va_end (ap);
@@ -368,10 +402,15 @@ void Warning (const char* Format, ...)
void PPWarning (const char* Format, ...)
void PPWarning_ (const char *file, int line, const char* Format, ...)
/* Print a warning message. For use within the preprocessor */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
va_start (ap, Format);
IntWarning (EC_PP, GetCurLineInfo (), Format, ap);
va_end (ap);
@@ -436,10 +475,15 @@ static void IntNote (const LineInfo* LI, const char* Msg, va_list ap)
void LINote (const LineInfo* LI, const char* Format, ...)
void LINote_ (const char *file, int line, const LineInfo* LI, const char* Format, ...)
/* Print a note message with the line info given explicitly */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
va_start (ap, Format);
IntNote (LI, Format, ap);
va_end (ap);
@@ -447,10 +491,15 @@ void LINote (const LineInfo* LI, const char* Format, ...)
void Note (const char* Format, ...)
void Note_ (const char *file, int line, const char* Format, ...)
/* Print a note message */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
va_start (ap, Format);
IntNote (GetDiagnosticLI (), Format, ap);
va_end (ap);
@@ -458,10 +507,15 @@ void Note (const char* Format, ...)
void PPNote (const char* Format, ...)
void PPNote_ (const char *file, int line, const char* Format, ...)
/* Print a note message. For use within the preprocessor */
{
va_list ap;
if (Debug) {
fprintf(stderr, "[%s:%d] ", file, line);
}
va_start (ap, Format);
IntNote (GetDiagnosticLI (), Format, ap);
va_end (ap);

View File

@@ -103,28 +103,36 @@ struct StrBuf;
void PrintFileInclusionInfo (const LineInfo* LI);
/* Print hierarchy of file inclusion */
void Fatal (const char* Format, ...) attribute ((noreturn, format (printf, 1, 2)));
void Fatal_ (const char *file, int line, const char* Format, ...) attribute ((noreturn, format (printf, 3, 4)));
#define Fatal(...) Fatal_(__FILE__, __LINE__, __VA_ARGS__)
/* Print a message about a fatal error and die */
void Internal (const char* Format, ...) attribute ((noreturn, format (printf, 1, 2)));
void Internal_ (const char *file, int line, const char* Format, ...) attribute ((noreturn, format (printf, 3, 4)));
#define Internal(...) Internal_(__FILE__, __LINE__, __VA_ARGS__)
/* Print a message about an internal compiler error and die */
void Error (const char* Format, ...) attribute ((format (printf, 1, 2)));
void Error_ (const char *file, int line, const char* Format, ...) attribute ((format (printf, 3, 4)));
#define Error(...) Error_(__FILE__, __LINE__, __VA_ARGS__)
/* Print an error message */
void LIError (errcat_t EC, LineInfo* LI, const char* Format, ...) attribute ((format (printf, 3, 4)));
void LIError_ (const char *file, int line, errcat_t EC, LineInfo* LI, const char* Format, ...) attribute ((format (printf, 5, 6)));
#define LIError(...) LIError_(__FILE__, __LINE__, __VA_ARGS__)
/* Print an error message with the line info given explicitly */
void PPError (const char* Format, ...) attribute ((format (printf, 1, 2)));
void PPError_ (const char *file, int line, const char* Format, ...) attribute ((format (printf, 3, 4)));
#define PPError(...) PPError_(__FILE__, __LINE__, __VA_ARGS__)
/* Print an error message. For use within the preprocessor */
void Warning (const char* Format, ...) attribute ((format (printf, 1, 2)));
void Warning_ (const char *file, int line, const char* Format, ...) attribute ((format (printf, 3, 4)));
#define Warning(...) Warning_(__FILE__, __LINE__, __VA_ARGS__)
/* Print a warning message */
void LIWarning (errcat_t EC, LineInfo* LI, const char* Format, ...) attribute ((format (printf, 3, 4)));
void LIWarning_ (const char *file, int line, errcat_t EC, LineInfo* LI, const char* Format, ...) attribute ((format (printf, 5, 6)));
#define LIWarning(...) LIWarning_(__FILE__, __LINE__, __VA_ARGS__)
/* Print a warning message with the line info given explicitly */
void PPWarning (const char* Format, ...) attribute ((format (printf, 1, 2)));
void PPWarning_ (const char *file, int line, const char* Format, ...) attribute ((format (printf, 3, 4)));
#define PPWarning(...) PPWarning_(__FILE__, __LINE__, __VA_ARGS__)
/* Print a warning message. For use within the preprocessor */
void UnreachableCodeWarning (void);
@@ -140,13 +148,16 @@ IntStack* FindWarning (const char* Name);
void ListWarnings (FILE* F);
/* Print a list of warning types/names to the given file */
void Note (const char* Format, ...) attribute ((format (printf, 1, 2)));
void Note_ (const char *file, int line, const char* Format, ...) attribute ((format (printf, 3, 4)));
#define Note(...) Note_(__FILE__, __LINE__, __VA_ARGS__)
/* Print a note message */
void LINote (const LineInfo* LI, const char* Format, ...) attribute ((format (printf, 2, 3)));
void LINote_ (const char *file, int line, const LineInfo* LI, const char* Format, ...) attribute ((format (printf, 4, 5)));
#define LINote(...) LINote_(__FILE__, __LINE__, __VA_ARGS__)
/* Print a note message with the line info given explicitly */
void PPNote (const char* Format, ...) attribute ((format (printf, 1, 2)));
void PPNote_ (const char *file, int line, const char* Format, ...) attribute ((format (printf, 3, 4)));
#define PPNote(...) PPNote_(__FILE__, __LINE__, __VA_ARGS__)
/* Print a note message. For use within the preprocessor */
unsigned GetTotalErrors (void);

View File

@@ -44,12 +44,14 @@
unsigned char AddSource = 0; /* Add source lines as comments */
unsigned char AllowNewComments = 0; /* Allow new style comments in C89 mode */
unsigned char AutoCDecl = 0; /* Make functions default to __cdecl__ */
unsigned char DebugInfo = 0; /* Add debug info to the obj */
unsigned char DumpPredefMacros = 0; /* Output predefined macros */
unsigned char DumpUserMacros = 0; /* Output user macros */
unsigned char PreprocessOnly = 0; /* Just preprocess the input */
unsigned char DebugOptOutput = 0; /* Output debug stuff */
unsigned RegisterSpace = 6; /* Space available for register vars */
unsigned AllowNewComments = 0; /* Allow new style comments in C89 mode */
/* Stackable options */
IntStack WritableStrings = INTSTACK(0); /* Literal strings are r/w */

View File

@@ -52,12 +52,14 @@
/* Options */
extern unsigned char AddSource; /* Add source lines as comments */
extern unsigned char AllowNewComments; /* Allow new style comments in C89 mode */
extern unsigned char AutoCDecl; /* Make functions default to __cdecl__ */
extern unsigned char DebugInfo; /* Add debug info to the obj */
extern unsigned char DumpPredefMacros; /* Output predefined macros */
extern unsigned char DumpUserMacros; /* Output user macros */
extern unsigned char PreprocessOnly; /* Just preprocess the input */
extern unsigned char DebugOptOutput; /* Output debug stuff */
extern unsigned RegisterSpace; /* Space available for register vars */
extern unsigned AllowNewComments; /* Allow new style comments in C89 mode */
/* Stackable options */
extern IntStack WritableStrings; /* Literal strings are r/w */

View File

@@ -1,4 +1,4 @@
/*****************************************************************************/
/* */
/* macrotab.h */
/* */
@@ -42,6 +42,7 @@
/* cc65 */
#include "error.h"
#include "output.h"
#include "preproc.h"
#include "macrotab.h"
@@ -60,6 +61,70 @@ static Macro* MacroTab[MACRO_TAB_SIZE];
/* The undefined macros list head */
static Macro* UndefinedMacrosListHead;
/* Some defines for better readability when calling OutputMacros() */
#define USER_MACROS 0
#define PREDEF_MACROS 1
#define NAME_ONLY 0
#define FULL_DEFINITION 1
/*****************************************************************************/
/* helpers */
/*****************************************************************************/
static void OutputMacro (const Macro* M, int Full)
/* Output one macro. If Full is true, the replacement is also output. */
{
WriteOutput ("#define %s", M->Name);
int ParamCount = M->ParamCount;
if (M->ParamCount >= 0) {
int I;
if (M->Variadic) {
CHECK (ParamCount > 0);
--ParamCount;
}
WriteOutput ("(");
for (I = 0; I < ParamCount; ++I) {
const char* Name = CollConstAt (&M->Params, I);
WriteOutput ("%s%s", (I == 0)? "" : ",", Name);
}
if (M->Variadic) {
WriteOutput ("%s...", (ParamCount == 0)? "" : ",");
}
WriteOutput (")");
}
WriteOutput (" ");
if (Full) {
WriteOutput ("%.*s",
SB_GetLen (&M->Replacement),
SB_GetConstBuf (&M->Replacement));
}
WriteOutput ("\n");
}
static void OutputMacros (int Predefined, int Full)
/* Output macros to the output file depending on the flags given. */
{
/* Note: The Full flag is currently not used by any callers but is left in
** place for possible future changes.
*/
unsigned I;
for (I = 0; I < MACRO_TAB_SIZE; ++I) {
const Macro* M = MacroTab [I];
while (M) {
if ((Predefined != 0) == (M->Predefined != 0)) {
OutputMacro (M, Full);
}
M = M->Next;
}
}
}
/*****************************************************************************/
@@ -68,7 +133,7 @@ static Macro* UndefinedMacrosListHead;
Macro* NewMacro (const char* Name)
Macro* NewMacro (const char* Name, unsigned char Predefined)
/* Allocate a macro structure with the given name. The structure is not
** inserted into the macro table.
*/
@@ -84,6 +149,7 @@ Macro* NewMacro (const char* Name)
M->ParamCount = -1; /* Flag: Not a function-like macro */
InitCollection (&M->Params);
SB_Init (&M->Replacement);
M->Predefined = Predefined;
M->Variadic = 0;
memcpy (M->Name, Name, Len+1);
@@ -116,7 +182,7 @@ Macro* CloneMacro (const Macro* M)
** Use FreeMacro for that.
*/
{
Macro* New = NewMacro (M->Name);
Macro* New = NewMacro (M->Name, M->Predefined);
unsigned I;
for (I = 0; I < CollCount (&M->Params); ++I) {
@@ -134,7 +200,7 @@ Macro* CloneMacro (const Macro* M)
void DefineNumericMacro (const char* Name, long Val)
/* Define a macro for a numeric constant */
/* Define a predefined macro for a numeric constant */
{
char Buf[64];
@@ -148,10 +214,10 @@ void DefineNumericMacro (const char* Name, long Val)
void DefineTextMacro (const char* Name, const char* Val)
/* Define a macro for a textual constant */
/* Define a predefined macro for a textual constant */
{
/* Create a new macro */
Macro* M = NewMacro (Name);
Macro* M = NewMacro (Name, 1);
/* Set the value as replacement text */
SB_CopyStr (&M->Replacement, Val);
@@ -350,3 +416,19 @@ void PrintMacroStats (FILE* F)
}
}
}
void OutputPredefMacros (void)
/* Output all predefined macros to the output file */
{
OutputMacros (PREDEF_MACROS, FULL_DEFINITION);
}
void OutputUserMacros (void)
/* Output all user defined macros to the output file */
{
OutputMacros (USER_MACROS, FULL_DEFINITION);
}

View File

@@ -58,6 +58,7 @@ struct Macro {
int ParamCount; /* Number of parameters, -1 = no parens */
Collection Params; /* Parameter list (char*) */
StrBuf Replacement; /* Replacement text */
unsigned char Predefined; /* True if this is a predefined macro */
unsigned char Variadic; /* C99 variadic macro */
char Name[1]; /* Name, dynamically allocated */
};
@@ -70,7 +71,7 @@ struct Macro {
Macro* NewMacro (const char* Name);
Macro* NewMacro (const char* Name, unsigned char Predefined);
/* Allocate a macro structure with the given name. The structure is not
** inserted into the macro table.
*/
@@ -87,10 +88,10 @@ Macro* CloneMacro (const Macro* M);
*/
void DefineNumericMacro (const char* Name, long Val);
/* Define a macro for a numeric constant */
/* Define a predefined macro for a numeric constant */
void DefineTextMacro (const char* Name, const char* Val);
/* Define a macro for a textual constant */
/* Define a predefined macro for a textual constant */
void InsertMacro (Macro* M);
/* Insert the given macro into the macro table. */
@@ -132,6 +133,12 @@ int MacroCmp (const Macro* M1, const Macro* M2);
void PrintMacroStats (FILE* F);
/* Print macro statistics to the given text file. */
void OutputPredefMacros (void);
/* Output all predefined macros to the output file */
void OutputUserMacros (void);
/* Output all user defined macros to the output file */
/* End of macrotab.h */

View File

@@ -93,6 +93,8 @@ static void Usage (void)
" -V\t\t\t\tPrint the compiler version number\n"
" -W [-+]warning[,...]\t\tControl warnings ('-' disables, '+' enables)\n"
" -d\t\t\t\tDebug mode\n"
" -dM\t\t\t\tOutput all user macros (needs -E)\n"
" -dP\t\t\t\tOutput all predefined macros (needs -E)\n"
" -g\t\t\t\tAdd debug info to object file\n"
" -h\t\t\t\tHelp (this text)\n"
" -j\t\t\t\tDefault characters are signed\n"
@@ -1026,7 +1028,25 @@ int main (int argc, char* argv[])
break;
case 'd':
OptDebug (Arg, 0);
P = Arg + 2;
if (*P == '\0') {
OptDebug (Arg, 0);
} else {
while (*P) {
switch (*P) {
case 'M':
DumpUserMacros = 1;
break;
case 'P':
DumpPredefMacros = 1;
break;
default:
UnknownOption (Arg);
break;
}
++P;
}
}
break;
case 'h':
@@ -1138,6 +1158,11 @@ int main (int argc, char* argv[])
AbEnd ("No input files");
}
/* The options to output macros can only be used with -E */
if ((DumpPredefMacros || DumpUserMacros) && !PreprocessOnly) {
AbEnd ("Preprocessor macro output can only be used together with -E");
}
/* Add the default include search paths. */
FinishIncludePaths ();

View File

@@ -2575,7 +2575,7 @@ static void DoDefine (void)
CheckForBadIdent (Ident, Std, 0);
/* Create a new macro definition */
M = NewMacro (Ident);
M = NewMacro (Ident, 0);
/* Check if this is a function-like macro */
if (CurC == '(') {