Skip to content

Commit 2adb759

Browse files
authored
cc, plusplus: add -pm option to dump predefined macros (#1592)
* cc: add -pm option to dump predefined macros Add a new -pm compiler option that dumps all predefined macros in #define format, similar to gcc/clang's -dM -E. Usage: wcc386 -pm /dev/null * plusplus: add -pm option to dump predefined macros Port the -pm option from the C compiler to the C++ compiler, allowing wpp/wpp386 to dump all predefined macros. * cc, plusplus: move -pm outside preprocessor option chain Move -pm before the :usagechain with :nochain/:usagenochain so it is not part of the -p preprocessor control group. Process -pm after the cpp_mode block so it has highest priority and does not set cpp_mode.
1 parent 41b1745 commit 2adb759

12 files changed

Lines changed: 231 additions & 50 deletions

File tree

bld/cc/c/ccmain.c

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,66 +1020,71 @@ static void DoCCompile( char **cmdline )
10201020
}
10211021
ParseInit();
10221022
if( ParseCmdLine( cmdline ) ) {
1023-
if( WholeFName == NULL ) {
1023+
if( CompFlags.cpp_dump_macros ) {
1024+
if( CppFile == NULL )
1025+
OpenCppFile();
1026+
DumpAllMacros();
1027+
MacroFini();
1028+
} else if( WholeFName == NULL ) {
10241029
CErr1( ERR_FILENAME_REQUIRED );
10251030
return;
1026-
}
1027-
MakePgmName();
1028-
DelErrFile(); /* delete old error file */
1029-
CPragmaInit(); /* memory model is known now */
1031+
} else {
1032+
MakePgmName();
1033+
DelErrFile(); /* delete old error file */
1034+
CPragmaInit(); /* memory model is known now */
10301035
#if _CPU == 370
1031-
ParseAuxFile();
1036+
ParseAuxFile();
10321037
#endif
1033-
if( CompFlags.cpp_mode ) {
1034-
PrintWhiteSpace = true;
1035-
if( ForcePreInclude != NULL ) {
1036-
if( CppFile == NULL )
1037-
OpenCppFile();
1038-
CompFlags.cpp_output = false;
1039-
if( openForcePreInclude() ) {
1040-
CurToken = T_NULL;
1041-
while( CurToken != T_EOF ) {
1042-
CurToken = GetNextToken();
1038+
if( CompFlags.cpp_mode ) {
1039+
PrintWhiteSpace = true;
1040+
if( ForcePreInclude != NULL ) {
1041+
if( CppFile == NULL )
1042+
OpenCppFile();
1043+
CompFlags.cpp_output = false;
1044+
if( openForcePreInclude() ) {
1045+
CurToken = T_NULL;
1046+
while( CurToken != T_EOF ) {
1047+
CurToken = GetNextToken();
1048+
}
10431049
}
1050+
CompFlags.cpp_output = true;
10441051
}
1045-
CompFlags.cpp_output = true;
1046-
}
1047-
OpenPgmFile();
1048-
CPP_Parse();
1049-
if( !CompFlags.quiet_mode ) {
1050-
PrintStats();
1051-
}
1052-
if( CompFlags.warnings_cause_bad_exit ) {
1053-
ErrCount += WngCount;
1054-
}
1055-
} else {
1056-
OpenPgmFile();
1057-
MacroAddComp(); // Add any compile time only macros
1058-
Parse();
1059-
if( !CompFlags.quiet_mode ) {
1060-
PrintStats();
1061-
}
1062-
if( CompFlags.warnings_cause_bad_exit ) {
1063-
ErrCount += WngCount;
1064-
}
1065-
if( ( ErrCount == 0 ) && ( !CompFlags.check_syntax ) ) {
1066-
if( CompFlags.emit_browser_info ) {
1067-
DwarfBrowseEmit();
1052+
OpenPgmFile();
1053+
CPP_Parse();
1054+
if( !CompFlags.quiet_mode ) {
1055+
PrintStats();
1056+
}
1057+
if( CompFlags.warnings_cause_bad_exit ) {
1058+
ErrCount += WngCount;
1059+
}
1060+
} else {
1061+
OpenPgmFile();
1062+
MacroAddComp(); // Add any compile time only macros
1063+
Parse();
1064+
if( !CompFlags.quiet_mode ) {
1065+
PrintStats();
1066+
}
1067+
if( CompFlags.warnings_cause_bad_exit ) {
1068+
ErrCount += WngCount;
1069+
}
1070+
if( ( ErrCount == 0 ) && ( !CompFlags.check_syntax ) ) {
1071+
if( CompFlags.emit_browser_info ) {
1072+
DwarfBrowseEmit();
1073+
}
1074+
FreeMacroSegments();
1075+
DoCompile();
1076+
} else {
1077+
FreeMacroSegments();
10681078
}
1069-
FreeMacroSegments();
1070-
DoCompile();
1079+
}
1080+
if( ErrCount == 0 ) {
1081+
DumpDepFile();
10711082
} else {
1072-
FreeMacroSegments();
1083+
DelDepFile();
10731084
}
1085+
SymFini();
1086+
CPragmaFini();
10741087
}
1075-
if( ErrCount == 0 ) {
1076-
DumpDepFile();
1077-
} else {
1078-
DelDepFile();
1079-
}
1080-
1081-
SymFini();
1082-
CPragmaFini();
10831088
} else {
10841089
ErrCount = 1;
10851090
}

bld/cc/c/cmac1.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,79 @@ void MacroPurge( void )
222222
#endif
223223
}
224224

225+
226+
static void DumpMacroDefn( const char *p )
227+
/****************************************/
228+
{
229+
unsigned char c;
230+
TOKEN token;
231+
232+
while( (token = MTOK( p )) != T_NULL ) {
233+
MTOKINC( p );
234+
switch( token ) {
235+
case T_CONSTANT:
236+
case T_PPNUMBER:
237+
case T_BAD_TOKEN:
238+
case T_ID:
239+
case T_UNEXPANDABLE_ID:
240+
for( ; (c = *p++) != '\0'; ) {
241+
fputc( c, CppFile );
242+
}
243+
break;
244+
case T_LSTRING:
245+
fputc( 'L', CppFile );
246+
/* fall through */
247+
case T_STRING:
248+
fputc( '"', CppFile );
249+
for( ; (c = *p++) != '\0'; ) {
250+
fputc( c, CppFile );
251+
}
252+
fputc( '"', CppFile );
253+
break;
254+
case T_WHITE_SPACE:
255+
fputc( ' ', CppFile );
256+
break;
257+
case T_BAD_CHAR:
258+
fputc( *p++, CppFile );
259+
break;
260+
case T_MACRO_PARM:
261+
case T_MACRO_VAR_PARM:
262+
MTOKPARMINC( p );
263+
break;
264+
default:
265+
fprintf( CppFile, "%s", Tokens[token] );
266+
break;
267+
}
268+
}
269+
}
270+
271+
272+
void DumpAllMacros( void )
273+
/************************/
274+
{
275+
mac_hash_idx hash;
276+
MEPTR mentry;
277+
const char *p;
278+
279+
for( hash = 0; hash < MACRO_HASH_SIZE; hash++ ) {
280+
for( mentry = MacHash[hash]; mentry != NULL; mentry = mentry->next_macro ) {
281+
if( mentry->macro_flags & MFLAG_HIDDEN )
282+
continue;
283+
if( MacroIsSpecial( mentry ) )
284+
continue;
285+
fprintf( CppFile, "#define %s", mentry->macro_name );
286+
p = (char *)mentry + mentry->macro_defn;
287+
if( MTOK( p ) != T_NULL ) {
288+
fputc( ' ', CppFile );
289+
DumpMacroDefn( p );
290+
}
291+
fputc( '\n', CppFile );
292+
}
293+
}
294+
fflush( CppFile );
295+
}
296+
297+
225298
static void DeleteNestedMacro( void )
226299
/***********************************/
227300
{

bld/cc/c/coptions.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,10 @@ static void AnalyseAnyTargetOptions( OPT_STORAGE *data )
742742
CompFlags.cpp_output = true;
743743
CompFlags.quiet_mode = true;
744744
}
745+
if( data->pm ) {
746+
CompFlags.cpp_dump_macros = true;
747+
CompFlags.quiet_mode = true;
748+
}
745749
if( data->rod ) {
746750
OPT_STRING *s;
747751
for( s = data->rod_value; s != NULL; s = s->next ) {

bld/cc/gml/options.gml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,13 @@
972972
:usage. preprocessor ignores #line directives
973973
:jusage.
974974
975+
:option. pm
976+
:target. any
977+
:nochain.
978+
:usagenochain.
979+
:usage. dump predefined macros to output
980+
:jusage.
981+
975982
:option. p
976983
:target. any
977984
:usage. preprocess source file

bld/cc/h/ctypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ typedef struct comp_flags {
609609
boolbit debug_info_some : 1; /* d1 + some typing info */
610610
boolbit emit_names : 1; /* /en switch used */
611611
boolbit cpp_mode : 1; /* compiler CPP mode */
612+
boolbit cpp_dump_macros : 1; /* dump predefined macros */
612613
boolbit warnings_cause_bad_exit : 1; /* warnings=>non-zero exit */
613614

614615
boolbit save_restore_segregs : 1; /* save/restore segregs */

bld/cc/h/cvars.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ extern void MacroInit( void );
598598
extern void MacroAddComp( void );
599599
extern void MacroFini( void );
600600
extern void MacroPurge( void );
601+
extern void DumpAllMacros( void );
601602
extern TOKEN GetMacroToken( void );
602603
extern TOKEN SpecialMacro( MEPTR );
603604
extern void DoMacroExpansion( MEPTR );

bld/plusplus/c/cmacadd.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,3 +777,75 @@ void MacroSegmentAddMem( // MacroSegment: ADD A SEQUENCE OF BYTES
777777
memcpy( MacroOffset + clen, buff, len );
778778
*mlen += len;
779779
}
780+
781+
782+
static void dumpMacroDefn( // DUMP TOKENIZED MACRO DEFINITION
783+
const char *p ) // - pointer to token stream
784+
{
785+
unsigned char c;
786+
TOKEN token;
787+
788+
while( (token = *(TOKEN *)p) != T_NULL ) {
789+
p += sizeof( TOKEN );
790+
switch( token ) {
791+
case T_CONSTANT:
792+
case T_PPNUMBER:
793+
case T_BAD_TOKEN:
794+
case T_ID:
795+
case T_UNEXPANDABLE_ID:
796+
for( ; (c = *p++) != '\0'; ) {
797+
fputc( c, CppFile );
798+
}
799+
break;
800+
case T_LSTRING:
801+
fputc( 'L', CppFile );
802+
/* fall through */
803+
case T_STRING:
804+
fputc( '"', CppFile );
805+
for( ; (c = *p++) != '\0'; ) {
806+
fputc( c, CppFile );
807+
}
808+
fputc( '"', CppFile );
809+
break;
810+
case T_WHITE_SPACE:
811+
fputc( ' ', CppFile );
812+
break;
813+
case T_BAD_CHAR:
814+
fputc( *p++, CppFile );
815+
break;
816+
case T_MACRO_PARM:
817+
case T_MACRO_VAR_PARM:
818+
++p;
819+
break;
820+
default:
821+
fprintf( CppFile, "%s", Tokens[token] );
822+
break;
823+
}
824+
}
825+
}
826+
827+
828+
void DumpAllMacros( // DUMP ALL PREDEFINED MACROS
829+
void )
830+
{
831+
unsigned hash;
832+
MEPTR mentry;
833+
const char *p;
834+
835+
for( hash = 0; hash < MACRO_HASH_SIZE; hash++ ) {
836+
RingIterBeg( macroHashTable[hash], mentry ) {
837+
if( mentry->macro_flags & MFLAG_HIDDEN )
838+
continue;
839+
if( MacroIsSpecial( mentry ) )
840+
continue;
841+
fprintf( CppFile, "#define %s", mentry->macro_name );
842+
p = (char *)mentry + mentry->macro_defn;
843+
if( *(TOKEN *)p != T_NULL ) {
844+
fputc( ' ', CppFile );
845+
dumpMacroDefn( p );
846+
}
847+
fputc( '\n', CppFile );
848+
} RingIterEnd( mentry )
849+
}
850+
fflush( CppFile );
851+
}

bld/plusplus/c/cmdlnany.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,10 @@ static void analyseAnyTargetOptions( OPT_STORAGE *data )
10591059
CompFlags.cpp_output = true;
10601060
CompFlags.quiet_mode = true;
10611061
}
1062+
if( data->pm ) {
1063+
CompFlags.cpp_dump_macros = true;
1064+
CompFlags.quiet_mode = true;
1065+
}
10621066
if( data->p_sharp ) {
10631067
PreProcChar = (char)data->p_sharp_value;
10641068
}

bld/plusplus/c/plusplus.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ static int doCCompile( // COMPILE C++ PROGRAM
269269
} else {
270270
exit_status |= WPP_BATCH_FILES;
271271
}
272+
} else if( CompFlags.cpp_dump_macros ) {
273+
PpOpen();
274+
DumpAllMacros();
272275
} else if( WholeFName == NULL ) {
273276
CErr1( ERR_FILENAME_REQUIRED );
274277
CompFlags.cmdline_error = true;

bld/plusplus/gml/options.gml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,13 @@
987987
:usage. preprocessor ignores #line directives
988988
:jusage. preprocessor ignores #line directives
989989

990+
:option. pm
991+
:target. any
992+
:nochain.
993+
:usagenochain.
994+
:usage. dump predefined macros to output
995+
:jusage.
996+
990997
:option. p
991998
:target. any
992999
:usage. preprocess source file

0 commit comments

Comments
 (0)