Implement -dD, -dM and -dN command line switches to output macro definitions.

This commit is contained in:
Kugel Fuhr
2025-06-12 21:50:20 +02:00
parent 0a1df881b3
commit a6b94ebba2
8 changed files with 179 additions and 13 deletions

View File

@@ -63,6 +63,9 @@ Short options:
-V Print the compiler version number
-W [-+]warning[,...] Control warnings ('-' disables, '+' enables)
-d Debug mode
-dD Output all macro definitions (needs -E)
-dM Output user defined macros (needs -E)
-dN Output user defined macro names (needs -E)
-g Add debug info to object file
-h Help (this text)
-j Default characters are signed
@@ -199,6 +202,26 @@ Here is a description of all the command line options:
Enables debug mode, for debugging the behavior of cc65.
<label id="option-dD">
<tag><tt>-dD</tt></tag>
Like <tt/<ref id="option-dM" name="-dM">/ but does not include the predefined
macros.
<label id="option-dM">
<tag><tt>-dM</tt></tag>
When used with -E, will output <tt>#define</tt> directives for all the macros
defined during execution of the preprocessor, including predefined macros.
<tag><tt>-dN</tt></tag>
Like <tt/<ref id="option-dD" name="-dD">/ but will only output the macro names,
not their definitions.
<tag><tt>--debug-tables name</tt></tag>
Writes symbol table information to a file, which includes details on structs, unions

View File

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

View File

@@ -44,12 +44,15 @@
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 DumpAllMacrosFull = 0; /* Output all macro defs */
unsigned char DumpUserMacros = 0; /* Output user macro names */
unsigned char DumpUserMacrosFull= 0; /* Output user macro defs */
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,15 @@
/* 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 DumpAllMacrosFull; /* Output all macro defs */
extern unsigned char DumpUserMacros; /* Output user macro names */
extern unsigned char DumpUserMacrosFull; /* Output user macro defs */
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,67 @@ static Macro* MacroTab[MACRO_TAB_SIZE];
/* The undefined macros list head */
static Macro* UndefinedMacrosListHead;
/* Some defines for better readability when calling OutputMacros() */
#define ALL_MACROS 0
#define USER_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 UserOnly, int Full)
/* Output macros to the output file depending on the flags given */
{
unsigned I;
for (I = 0; I < MACRO_TAB_SIZE; ++I) {
const Macro* M = MacroTab [I];
while (M) {
if (!UserOnly || !M->Predefined) {
OutputMacro (M, Full);
}
M = M->Next;
}
}
}
/*****************************************************************************/
@@ -68,7 +130,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 +146,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 +179,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 +197,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 +211,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 +413,27 @@ void PrintMacroStats (FILE* F)
}
}
}
void OutputAllMacrosFull (void)
/* Output all macros to the output file */
{
OutputMacros (ALL_MACROS, FULL_DEFINITION);
}
void OutputUserMacros (void)
/* Output the names of all user defined macros to the output file */
{
OutputMacros (USER_MACROS, NAME_ONLY);
}
void OutputUserMacrosFull (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,15 @@ int MacroCmp (const Macro* M1, const Macro* M2);
void PrintMacroStats (FILE* F);
/* Print macro statistics to the given text file. */
void OutputAllMacrosFull (void);
/* Output all macros to the output file */
void OutputUserMacros (void);
/* Output the names of all user defined macros to the output file */
void OutputUserMacrosFull (void);
/* Output all user defined macros to the output file */
/* End of macrotab.h */

View File

@@ -93,6 +93,9 @@ 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"
" -dD\t\t\t\tOutput all macro definitions (needs -E)\n"
" -dM\t\t\t\tOutput user defined macros (needs -E)\n"
" -dN\t\t\t\tOutput user defined macro names (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"
@@ -1022,7 +1025,26 @@ int main (int argc, char* argv[])
break;
case 'd':
OptDebug (Arg, 0);
switch (Arg[2]) {
case '\0':
OptDebug (Arg, 0);
break;
case 'D':
DumpUserMacrosFull = 1;
break;
case 'M':
DumpAllMacrosFull = 1;
break;
case 'N':
DumpUserMacros = 1;
break;
default:
UnknownOption (Arg);
break;
}
if (Arg[2] && Arg[3]) {
UnknownOption (Arg);
}
break;
case 'h':
@@ -1134,6 +1156,13 @@ int main (int argc, char* argv[])
AbEnd ("No input files");
}
/* The options to output macros can only be used with -E */
if (DumpAllMacrosFull || DumpUserMacros || DumpUserMacrosFull) {
if (!PreprocessOnly) {
AbEnd ("Preprocessor macro output can only be used together with -E");
}
}
/* Add the default include search paths. */
FinishIncludePaths ();

View File

@@ -2573,7 +2573,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 == '(') {