2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* C pre-processor functions */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2001-01-05 19:24:47 +00:00
|
|
|
/* common */
|
|
|
|
#include "chartype.h"
|
2001-09-07 09:54:33 +00:00
|
|
|
#include "check.h"
|
2002-09-29 21:09:47 +00:00
|
|
|
#include "inline.h"
|
2001-03-10 10:21:03 +00:00
|
|
|
#include "print.h"
|
2001-01-05 19:24:47 +00:00
|
|
|
#include "xmalloc.h"
|
2000-06-14 21:01:37 +00:00
|
|
|
|
2001-01-05 19:24:47 +00:00
|
|
|
/* cc65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "codegen.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "ident.h"
|
2000-06-14 08:18:19 +00:00
|
|
|
#include "incpath.h"
|
2000-06-14 21:01:37 +00:00
|
|
|
#include "input.h"
|
2001-07-14 15:56:17 +00:00
|
|
|
#include "lineinfo.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "macrotab.h"
|
|
|
|
#include "scanner.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "preproc.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Forwards */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int Pass1 (const char* From, char* To);
|
|
|
|
/* Preprocessor pass 1. Remove whitespace and comments. */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-07 09:54:33 +00:00
|
|
|
/* Set when the preprocessor calls expr() recursively */
|
2000-05-28 13:40:48 +00:00
|
|
|
unsigned char Preprocessing = 0;
|
|
|
|
|
|
|
|
/* Management data for #if */
|
2001-09-07 09:54:33 +00:00
|
|
|
#define MAX_IFS 64
|
|
|
|
#define IFCOND_NONE 0x00U
|
|
|
|
#define IFCOND_SKIP 0x01U
|
|
|
|
#define IFCOND_ELSE 0x02U
|
|
|
|
#define IFCOND_NEEDTERM 0x04U
|
|
|
|
static unsigned char IfStack[MAX_IFS];
|
|
|
|
static int IfIndex = -1;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Buffer for macro expansion */
|
|
|
|
static char mlinebuf [LINESIZE];
|
|
|
|
static char* mline = mlinebuf;
|
|
|
|
static char* mptr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Low level preprocessor token handling */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Types of preprocessor tokens */
|
|
|
|
typedef enum {
|
|
|
|
PP_DEFINE,
|
2001-09-07 09:54:33 +00:00
|
|
|
PP_ELIF,
|
2001-09-06 10:17:52 +00:00
|
|
|
PP_ELSE,
|
|
|
|
PP_ENDIF,
|
|
|
|
PP_ERROR,
|
|
|
|
PP_IF,
|
|
|
|
PP_IFDEF,
|
|
|
|
PP_IFNDEF,
|
|
|
|
PP_INCLUDE,
|
|
|
|
PP_LINE,
|
|
|
|
PP_PRAGMA,
|
|
|
|
PP_UNDEF,
|
|
|
|
PP_ILLEGAL
|
|
|
|
} pptoken_t;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Preprocessor keyword to token mapping table */
|
|
|
|
static const struct PPToken {
|
|
|
|
const char* Key; /* Keyword */
|
|
|
|
pptoken_t Tok; /* Token */
|
|
|
|
} PPTokens[] = {
|
|
|
|
{ "define", PP_DEFINE },
|
2001-09-07 09:54:33 +00:00
|
|
|
{ "elif", PP_ELIF },
|
2001-09-06 10:17:52 +00:00
|
|
|
{ "else", PP_ELSE },
|
|
|
|
{ "endif", PP_ENDIF },
|
|
|
|
{ "error", PP_ERROR },
|
|
|
|
{ "if", PP_IF },
|
|
|
|
{ "ifdef", PP_IFDEF },
|
|
|
|
{ "ifndef", PP_IFNDEF },
|
|
|
|
{ "include", PP_INCLUDE },
|
|
|
|
{ "line", PP_LINE },
|
|
|
|
{ "pragma", PP_PRAGMA },
|
|
|
|
{ "undef", PP_UNDEF },
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Number of preprocessor tokens */
|
|
|
|
#define PPTOKEN_COUNT (sizeof(PPTokens) / sizeof(PPTokens[0]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int CmpToken (const void* Key, const void* Elem)
|
|
|
|
/* Compare function for bsearch */
|
|
|
|
{
|
|
|
|
return strcmp ((const char*) Key, ((const struct PPToken*) Elem)->Key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static pptoken_t FindPPToken (const char* Ident)
|
|
|
|
/* Find a preprocessor token and return ut. Return PP_ILLEGAL if the identifier
|
|
|
|
* is not a valid preprocessor token.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
struct PPToken* P;
|
|
|
|
P = bsearch (Ident, PPTokens, PPTOKEN_COUNT, sizeof (PPTokens[0]), CmpToken);
|
|
|
|
return P? P->Tok : PP_ILLEGAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Code */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
#ifdef HAVE_INLINE
|
|
|
|
INLINE void KeepChar (char c)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Put character c into translation buffer. */
|
|
|
|
{
|
2001-09-07 09:54:33 +00:00
|
|
|
*mptr++ = c;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2002-09-29 21:09:47 +00:00
|
|
|
#else
|
|
|
|
#define KeepChar(c) *mptr++ = (c)
|
|
|
|
#endif
|
2002-02-18 08:40:11 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
static void KeepStr (const char* S)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Put string str into translation buffer. */
|
|
|
|
{
|
2001-09-07 09:54:33 +00:00
|
|
|
unsigned Len = strlen (S);
|
|
|
|
memcpy (mptr, S, Len);
|
|
|
|
mptr += Len;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
static void Stringize (const char* S)
|
|
|
|
/* Stringize the given string: Add double quotes at start and end and preceed
|
|
|
|
* each occurance of " and \ by a backslash.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
KeepChar ('\"');
|
|
|
|
/* Replace any characters inside the string may not be part of a string
|
|
|
|
* unescaped.
|
|
|
|
*/
|
|
|
|
while (*S) {
|
|
|
|
switch (*S) {
|
|
|
|
case '\"':
|
|
|
|
case '\\':
|
|
|
|
KeepChar ('\\');
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
KeepChar (*S);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++S;
|
|
|
|
}
|
|
|
|
KeepChar ('\"');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void SwapLineBuffers (void)
|
|
|
|
/* Swap both line buffers */
|
|
|
|
{
|
|
|
|
/* Swap mline and line */
|
|
|
|
char* p = line;
|
|
|
|
line = mline;
|
|
|
|
mline = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-12-12 21:45:05 +00:00
|
|
|
static void OldStyleComment (void)
|
|
|
|
/* Remove an old style C comment from line. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Remember the current line number, so we can output better error
|
|
|
|
* messages if the comment is not terminated in the current file.
|
|
|
|
*/
|
2000-06-14 21:01:37 +00:00
|
|
|
unsigned StartingLine = GetCurrentLine();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Skip the start of comment chars */
|
|
|
|
NextChar ();
|
|
|
|
NextChar ();
|
|
|
|
|
|
|
|
/* Skip the comment */
|
|
|
|
while (CurC != '*' || NextC != '/') {
|
|
|
|
if (CurC == '\0') {
|
2000-06-14 08:18:19 +00:00
|
|
|
if (NextLine () == 0) {
|
2001-12-12 21:45:05 +00:00
|
|
|
PPError ("End-of-file reached in comment starting at line %u",
|
|
|
|
StartingLine);
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC == '/' && NextC == '*') {
|
2001-12-12 21:45:05 +00:00
|
|
|
PPWarning ("`/*' found inside a comment");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
|
|
|
|
/* Skip the end of comment chars */
|
|
|
|
NextChar ();
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-12-12 21:45:05 +00:00
|
|
|
static void NewStyleComment (void)
|
|
|
|
/* Remove a new style C comment from line. */
|
|
|
|
{
|
|
|
|
/* Beware: Because line continuation chars are handled when reading
|
|
|
|
* lines, we may only skip til the end of the source line, which
|
|
|
|
* may not be the same as the end of the input line. The end of the
|
|
|
|
* source line is denoted by a lf (\n) character.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
NextChar ();
|
|
|
|
} while (CurC != '\n' && CurC != '\0');
|
|
|
|
if (CurC == '\n') {
|
|
|
|
NextChar ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static void SkipBlank (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Skip blanks and tabs in the input stream. */
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
while (IsBlank (CurC)) {
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static char* CopyQuotedString (char* Target)
|
|
|
|
/* Copy a single or double quoted string from the input to Target. Return the
|
2000-05-28 13:40:48 +00:00
|
|
|
* new target pointer. Target will not be terminated after the copy.
|
|
|
|
*/
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Remember the quote character, copy it to the target buffer and skip it */
|
|
|
|
char Quote = CurC;
|
|
|
|
*Target++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Copy the characters inside the string */
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC != '\0' && CurC != Quote) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Keep an escaped char */
|
2002-09-29 21:09:47 +00:00
|
|
|
if (CurC == '\\') {
|
2000-06-21 21:02:44 +00:00
|
|
|
*Target++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
/* Copy the character */
|
2000-06-21 21:02:44 +00:00
|
|
|
*Target++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we had a terminating quote, copy it */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC != '\0') {
|
|
|
|
*Target++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the new target pointer */
|
|
|
|
return Target;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Macro stuff */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int MacName (char* Ident)
|
2001-09-07 09:54:33 +00:00
|
|
|
/* Get a macro symbol name into Ident. If we have an error, print a
|
|
|
|
* diagnostic message and clear the line.
|
2001-09-06 10:17:52 +00:00
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
if (IsSym (Ident) == 0) {
|
2001-09-06 10:17:52 +00:00
|
|
|
PPError ("Identifier expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
ClearLine ();
|
2001-09-06 10:17:52 +00:00
|
|
|
return 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ExpandMacroArgs (Macro* M)
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Expand the arguments of a macro */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
|
|
|
ident Ident;
|
|
|
|
const char* Replacement;
|
2000-06-21 21:02:44 +00:00
|
|
|
const char* SavePtr;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Save the current line pointer and setup the new ones */
|
|
|
|
SavePtr = lptr;
|
2000-06-21 21:02:44 +00:00
|
|
|
InitLine (M->Replacement);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Copy the macro replacement checking for parameters to replace */
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC != '\0') {
|
2001-09-06 10:17:52 +00:00
|
|
|
/* If the next token is an identifier, check for a macro arg */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (IsIdent (CurC)) {
|
|
|
|
SymName (Ident);
|
2001-09-06 10:17:52 +00:00
|
|
|
Replacement = FindMacroArg (M, Ident);
|
|
|
|
if (Replacement) {
|
|
|
|
/* Macro arg, keep the replacement */
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepStr (Replacement);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-09-06 10:17:52 +00:00
|
|
|
/* No macro argument, keep the original identifier */
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepStr (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
} else if (CurC == '#' && IsIdent (NextC)) {
|
|
|
|
NextChar ();
|
|
|
|
SymName (Ident);
|
2001-09-06 10:17:52 +00:00
|
|
|
Replacement = FindMacroArg (M, Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
if (Replacement) {
|
2002-09-29 21:09:47 +00:00
|
|
|
/* Make a valid string from Replacement */
|
|
|
|
Stringize (Replacement);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2002-09-29 21:09:47 +00:00
|
|
|
/* No replacement - keep the input */
|
|
|
|
KeepChar ('#');
|
|
|
|
KeepStr (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2001-01-05 19:26:25 +00:00
|
|
|
} else if (IsQuote (CurC)) {
|
2000-06-21 21:02:44 +00:00
|
|
|
mptr = CopyQuotedString (mptr);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepChar (CurC);
|
2001-09-06 10:17:52 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the line pointer */
|
2000-06-21 21:02:44 +00:00
|
|
|
InitLine (SavePtr);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int MacroCall (Macro* M)
|
|
|
|
/* Process a function like macro */
|
|
|
|
{
|
2001-09-15 12:13:33 +00:00
|
|
|
int ArgCount; /* Macro argument count */
|
2000-05-28 13:40:48 +00:00
|
|
|
unsigned ParCount; /* Number of open parenthesis */
|
|
|
|
char Buf[LINESIZE]; /* Argument buffer */
|
|
|
|
const char* ArgStart;
|
|
|
|
char* B;
|
|
|
|
|
|
|
|
/* Expect an argument list */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (CurC != '(') {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Illegal macro call");
|
2000-05-28 13:40:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Eat the left paren */
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Read the actual macro arguments and store pointers to these arguments
|
|
|
|
* into the array of actual arguments in the macro definition.
|
|
|
|
*/
|
|
|
|
ArgCount = 0;
|
|
|
|
ParCount = 0;
|
|
|
|
ArgStart = Buf;
|
|
|
|
B = Buf;
|
|
|
|
while (1) {
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC == '(') {
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Nested parenthesis */
|
2000-06-21 21:02:44 +00:00
|
|
|
*B++ = CurC;
|
2001-09-06 10:17:52 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
++ParCount;
|
2001-01-05 19:26:25 +00:00
|
|
|
} else if (IsQuote (CurC)) {
|
2000-06-21 21:02:44 +00:00
|
|
|
B = CopyQuotedString (B);
|
|
|
|
} else if (CurC == ',' || CurC == ')') {
|
2000-05-28 13:40:48 +00:00
|
|
|
if (ParCount == 0) {
|
2001-09-06 10:17:52 +00:00
|
|
|
/* End of actual argument */
|
2000-05-28 13:40:48 +00:00
|
|
|
*B++ = '\0';
|
2001-09-06 10:17:52 +00:00
|
|
|
while (IsBlank(*ArgStart)) {
|
|
|
|
++ArgStart;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
if (ArgCount < M->ArgCount) {
|
|
|
|
M->ActualArgs[ArgCount++] = ArgStart;
|
2000-06-21 21:02:44 +00:00
|
|
|
} else if (CurC != ')' || *ArgStart != '\0' || M->ArgCount > 0) {
|
2001-09-15 12:13:33 +00:00
|
|
|
/* Be sure not to count the single empty argument for a
|
2001-09-06 10:17:52 +00:00
|
|
|
* macro that does not have arguments.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
++ArgCount;
|
2001-09-06 10:17:52 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
|
2001-12-12 21:45:05 +00:00
|
|
|
/* Check for end of macro param list */
|
|
|
|
if (CurC == ')') {
|
|
|
|
NextChar ();
|
|
|
|
break;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Start the next param */
|
2000-05-28 13:40:48 +00:00
|
|
|
ArgStart = B;
|
2001-12-12 21:45:05 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Comma or right paren inside nested parenthesis */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC == ')') {
|
2000-05-28 13:40:48 +00:00
|
|
|
--ParCount;
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
*B++ = CurC;
|
2002-09-29 21:09:47 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
} else if (IsBlank (CurC)) {
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Squeeze runs of blanks */
|
2000-05-28 13:40:48 +00:00
|
|
|
*B++ = ' ';
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
2001-12-12 21:45:05 +00:00
|
|
|
} else if (CurC == '/' && NextC == '*') {
|
|
|
|
*B++ = ' ';
|
|
|
|
OldStyleComment ();
|
|
|
|
} else if (ANSI == 0 && CurC == '/' && NextC == '/') {
|
|
|
|
*B++ = ' ';
|
|
|
|
NewStyleComment ();
|
2000-06-21 21:02:44 +00:00
|
|
|
} else if (CurC == '\0') {
|
2001-12-12 21:45:05 +00:00
|
|
|
/* End of line inside macro argument list - read next line */
|
2000-06-14 08:18:19 +00:00
|
|
|
if (NextLine () == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2001-12-12 21:45:05 +00:00
|
|
|
/* Just copy the character */
|
2000-06-21 21:02:44 +00:00
|
|
|
*B++ = CurC;
|
2001-12-12 21:45:05 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare formal argument count with actual */
|
|
|
|
if (M->ArgCount != ArgCount) {
|
2001-12-12 21:45:05 +00:00
|
|
|
PPError ("Macro argument count mismatch");
|
|
|
|
/* Be sure to make enough empty arguments available */
|
|
|
|
while (ArgCount < M->ArgCount) {
|
|
|
|
M->ActualArgs [ArgCount++] = "";
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Preprocess the line, replacing macro parameters */
|
|
|
|
ExpandMacroArgs (M);
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ExpandMacro (Macro* M)
|
|
|
|
/* Expand a macro */
|
|
|
|
{
|
|
|
|
/* Check if this is a function like macro */
|
|
|
|
if (M->ArgCount >= 0) {
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Function like macro */
|
2000-05-28 13:40:48 +00:00
|
|
|
if (MacroCall (M) == 0) {
|
2000-06-15 19:03:01 +00:00
|
|
|
ClearLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Just copy the replacement text */
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepStr (M->Replacement);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-06 10:17:52 +00:00
|
|
|
static void DefineMacro (void)
|
|
|
|
/* Handle a macro definition. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
char* saveptr;
|
|
|
|
ident Ident;
|
|
|
|
char Buf[LINESIZE];
|
|
|
|
Macro* M;
|
2000-10-10 21:20:34 +00:00
|
|
|
Macro* Existing;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Read the macro name */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (!MacName (Ident)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-10-10 21:20:34 +00:00
|
|
|
/* Get an existing macro definition with this name */
|
|
|
|
Existing = FindMacro (Ident);
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Create a new macro definition */
|
|
|
|
M = NewMacro (Ident);
|
|
|
|
|
|
|
|
/* Check if this is a function like macro */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC == '(') {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Skip the left paren */
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Set the marker that this is a function like macro */
|
2001-09-06 10:17:52 +00:00
|
|
|
M->ArgCount = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Read the formal parameter list */
|
2000-05-28 13:40:48 +00:00
|
|
|
while (1) {
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (CurC == ')')
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
2000-06-21 21:02:44 +00:00
|
|
|
if (MacName (Ident) == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-09-06 10:17:52 +00:00
|
|
|
AddMacroArg (M, Ident);
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (CurC != ',')
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
|
2001-09-06 10:17:52 +00:00
|
|
|
/* Check for a right paren and eat it if we find one */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC != ')') {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("`)' expected");
|
2000-06-15 19:03:01 +00:00
|
|
|
ClearLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert the macro into the macro table and allocate the ActualArgs array */
|
|
|
|
InsertMacro (M);
|
|
|
|
|
|
|
|
/* Remove whitespace and comments from the line, store the preprocessed
|
|
|
|
* line into Buf.
|
|
|
|
*/
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
2000-05-28 13:40:48 +00:00
|
|
|
saveptr = mptr;
|
|
|
|
Pass1 (lptr, Buf);
|
|
|
|
mptr = saveptr;
|
|
|
|
|
|
|
|
/* Create a copy of the replacement */
|
|
|
|
M->Replacement = xstrdup (Buf);
|
2000-10-10 21:20:34 +00:00
|
|
|
|
|
|
|
/* If we have an existing macro, check if the redefinition is identical.
|
|
|
|
* Print a diagnostic if not.
|
|
|
|
*/
|
|
|
|
if (Existing) {
|
2001-09-06 10:17:52 +00:00
|
|
|
if (MacroCmp (M, Existing) != 0) {
|
|
|
|
PPError ("Macro redefinition is not identical");
|
|
|
|
}
|
2000-10-10 21:20:34 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2001-09-07 09:54:33 +00:00
|
|
|
/* Preprocessing */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int Pass1 (const char* From, char* To)
|
|
|
|
/* Preprocessor pass 1. Remove whitespace and comments. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
int done;
|
|
|
|
ident Ident;
|
|
|
|
int HaveParen;
|
|
|
|
|
|
|
|
/* Initialize reading from "From" */
|
|
|
|
InitLine (From);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Target is "To" */
|
|
|
|
mptr = To;
|
|
|
|
|
|
|
|
/* Loop removing ws and comments */
|
2000-05-28 13:40:48 +00:00
|
|
|
done = 1;
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC != '\0') {
|
|
|
|
if (IsBlank (CurC)) {
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepChar (' ');
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
} else if (IsIdent (CurC)) {
|
|
|
|
SymName (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
if (Preprocessing && strcmp(Ident, "defined") == 0) {
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Handle the "defined" operator */
|
|
|
|
SkipBlank();
|
|
|
|
HaveParen = 0;
|
|
|
|
if (CurC == '(') {
|
|
|
|
HaveParen = 1;
|
|
|
|
NextChar ();
|
|
|
|
SkipBlank();
|
|
|
|
}
|
|
|
|
if (!IsIdent (CurC)) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Identifier expected");
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepChar ('0');
|
2000-06-21 21:02:44 +00:00
|
|
|
} else {
|
|
|
|
SymName (Ident);
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepChar (IsMacro (Ident)? '1' : '0');
|
2000-06-21 21:02:44 +00:00
|
|
|
if (HaveParen) {
|
|
|
|
SkipBlank();
|
|
|
|
if (CurC != ')') {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("`)' expected");
|
2002-09-29 21:09:47 +00:00
|
|
|
} else {
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-06-21 21:02:44 +00:00
|
|
|
if (MaybeMacro (Ident[0])) {
|
|
|
|
done = 0;
|
|
|
|
}
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepStr (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2001-01-05 19:26:25 +00:00
|
|
|
} else if (IsQuote (CurC)) {
|
2000-06-21 21:02:44 +00:00
|
|
|
mptr = CopyQuotedString (mptr);
|
|
|
|
} else if (CurC == '/' && NextC == '*') {
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepChar (' ');
|
2001-12-12 21:45:05 +00:00
|
|
|
OldStyleComment ();
|
2000-06-21 21:02:44 +00:00
|
|
|
} else if (ANSI == 0 && CurC == '/' && NextC == '/') {
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepChar (' ');
|
2001-12-12 21:45:05 +00:00
|
|
|
NewStyleComment ();
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepChar (CurC);
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepChar ('\0');
|
2000-05-28 13:40:48 +00:00
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int Pass2 (const char* From, char* To)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Preprocessor pass 2. Perform macro substitution. */
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
int no_chg;
|
|
|
|
ident Ident;
|
|
|
|
Macro* M;
|
|
|
|
|
|
|
|
/* Initialize reading from "From" */
|
|
|
|
InitLine (From);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Target is "To" */
|
|
|
|
mptr = To;
|
|
|
|
|
|
|
|
/* Loop substituting macros */
|
2000-05-28 13:40:48 +00:00
|
|
|
no_chg = 1;
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC != '\0') {
|
2002-09-29 21:09:47 +00:00
|
|
|
/* If we have an identifier, check if it's a macro */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (IsIdent (CurC)) {
|
|
|
|
SymName (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
M = FindMacro (Ident);
|
|
|
|
if (M) {
|
2000-06-21 21:02:44 +00:00
|
|
|
ExpandMacro (M);
|
|
|
|
no_chg = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepStr (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2001-01-05 19:26:25 +00:00
|
|
|
} else if (IsQuote (CurC)) {
|
2000-06-21 21:02:44 +00:00
|
|
|
mptr = CopyQuotedString (mptr);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepChar (CurC);
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return no_chg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
static void PreprocessLine (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Translate one line. */
|
|
|
|
{
|
2002-09-29 21:09:47 +00:00
|
|
|
unsigned I;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
/* Trim whitespace and remove comments. The function returns false if no
|
|
|
|
* identifiers were found that may be macros. If this is the case, no
|
|
|
|
* macro substitution is performed.
|
|
|
|
*/
|
|
|
|
int Done = Pass1 (line, mline);
|
|
|
|
|
|
|
|
/* Repeatedly expand macros in the line */
|
|
|
|
for (I = 0; I < 5; ++I) {
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Swap mline and line */
|
2002-09-29 21:09:47 +00:00
|
|
|
SwapLineBuffers ();
|
|
|
|
if (Done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Perform macro expansion */
|
|
|
|
Done = Pass2 (line, mline);
|
|
|
|
KeepChar ('\0');
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
|
|
|
|
/* Reinitialize line parsing */
|
|
|
|
InitLine (line);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-07 09:54:33 +00:00
|
|
|
static void DoUndef (void)
|
|
|
|
/* Process the #undef directive */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
|
|
|
ident Ident;
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (MacName (Ident)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
UndefineMacro (Ident);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-07 09:54:33 +00:00
|
|
|
static int PushIf (int Skip, int Invert, int Cond)
|
|
|
|
/* Push a new if level onto the if stack */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2001-09-07 09:54:33 +00:00
|
|
|
/* Check for an overflow of the if stack */
|
|
|
|
if (IfIndex >= MAX_IFS-1) {
|
|
|
|
PPError ("Too many nested #if clauses");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push the #if condition */
|
|
|
|
++IfIndex;
|
|
|
|
if (Skip) {
|
|
|
|
IfStack[IfIndex] = IFCOND_SKIP | IFCOND_NEEDTERM;
|
|
|
|
return 1;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-09-07 09:54:33 +00:00
|
|
|
IfStack[IfIndex] = IFCOND_NONE | IFCOND_NEEDTERM;
|
|
|
|
return (Invert ^ Cond);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-07 09:54:33 +00:00
|
|
|
static int DoIf (int Skip)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Process #if directive */
|
|
|
|
{
|
2001-07-17 21:24:19 +00:00
|
|
|
ExprDesc lval;
|
2000-05-28 13:40:48 +00:00
|
|
|
char* S;
|
|
|
|
|
|
|
|
/* We're about to abuse the compiler expression parser to evaluate the
|
|
|
|
* #if expression. Save the current tokens to come back here later.
|
2001-07-14 15:56:17 +00:00
|
|
|
* NOTE: Yes, this is a hack, but it saves a complete separate expression
|
|
|
|
* evaluation for the preprocessor.
|
2000-05-28 13:40:48 +00:00
|
|
|
*/
|
|
|
|
Token sv1 = CurTok;
|
|
|
|
Token sv2 = NextTok;
|
|
|
|
|
2001-07-14 15:56:17 +00:00
|
|
|
/* Make sure the line infos for the tokens won't get removed */
|
|
|
|
if (sv1.LI) {
|
2002-09-29 21:09:47 +00:00
|
|
|
UseLineInfo (sv1.LI);
|
2001-07-14 15:56:17 +00:00
|
|
|
}
|
|
|
|
if (sv2.LI) {
|
2002-09-29 21:09:47 +00:00
|
|
|
UseLineInfo (sv2.LI);
|
2001-07-14 15:56:17 +00:00
|
|
|
}
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Remove the #if from the line and add two semicolons as sentinels */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
2000-05-28 13:40:48 +00:00
|
|
|
S = line;
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC != '\0') {
|
2002-09-29 21:09:47 +00:00
|
|
|
*S++ = CurC;
|
|
|
|
NextChar ();
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
*S++ = ';';
|
|
|
|
*S++ = ';';
|
|
|
|
*S = '\0';
|
|
|
|
|
|
|
|
/* Start over parsing from line */
|
|
|
|
InitLine (line);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Switch into special preprocessing mode */
|
|
|
|
Preprocessing = 1;
|
|
|
|
|
|
|
|
/* Expand macros in this line */
|
2002-09-29 21:09:47 +00:00
|
|
|
PreprocessLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Prime the token pump (remove old tokens from the stream) */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Call the expression parser */
|
2002-02-18 08:40:11 +00:00
|
|
|
ConstExpr (&lval);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* End preprocessing mode */
|
|
|
|
Preprocessing = 0;
|
|
|
|
|
|
|
|
/* Reset the old tokens */
|
|
|
|
CurTok = sv1;
|
|
|
|
NextTok = sv2;
|
|
|
|
|
|
|
|
/* Set the #if condition according to the expression result */
|
2001-09-07 09:54:33 +00:00
|
|
|
return PushIf (Skip, 1, lval.ConstVal != 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-07 09:54:33 +00:00
|
|
|
static int DoIfDef (int skip, int flag)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Process #ifdef if flag == 1, or #ifndef if flag == 0. */
|
|
|
|
{
|
|
|
|
ident Ident;
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (MacName (Ident) == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2001-09-07 09:54:33 +00:00
|
|
|
return PushIf (skip, flag, IsMacro(Ident));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-07 09:54:33 +00:00
|
|
|
static void DoInclude (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Open an include file. */
|
|
|
|
{
|
2000-06-14 21:01:37 +00:00
|
|
|
char RTerm;
|
|
|
|
unsigned DirSpec;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-14 21:01:37 +00:00
|
|
|
|
|
|
|
/* Skip blanks */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-14 21:01:37 +00:00
|
|
|
/* Get the next char and check for a valid file name terminator. Setup
|
|
|
|
* the include directory spec (SYS/USR) by looking at the terminator.
|
|
|
|
*/
|
2000-06-21 21:02:44 +00:00
|
|
|
switch (CurC) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
case '\"':
|
|
|
|
RTerm = '\"';
|
|
|
|
DirSpec = INC_USER;
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
case '<':
|
|
|
|
RTerm = '>';
|
|
|
|
DirSpec = INC_SYS;
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
default:
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("`\"' or `<' expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
goto Done;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Copy the filename into mline. Since mline has the same size as the
|
|
|
|
* input line, we don't need to check for an overflow here.
|
|
|
|
*/
|
|
|
|
mptr = mline;
|
|
|
|
while (CurC != '\0' && CurC != RTerm) {
|
2002-09-29 21:09:47 +00:00
|
|
|
KeepChar (CurC);
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
*mptr = '\0';
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Check if we got a terminator */
|
|
|
|
if (CurC != RTerm) {
|
|
|
|
/* No terminator found */
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Missing terminator or file name too long");
|
2000-06-21 21:02:44 +00:00
|
|
|
goto Done;
|
|
|
|
}
|
2000-06-14 21:01:37 +00:00
|
|
|
|
|
|
|
/* Open the include file */
|
2000-06-21 21:02:44 +00:00
|
|
|
OpenIncludeFile (mline, DirSpec);
|
2000-06-14 21:01:37 +00:00
|
|
|
|
|
|
|
Done:
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Clear the remaining line so the next input will come from the new
|
|
|
|
* file (if open)
|
2000-06-15 19:03:01 +00:00
|
|
|
*/
|
|
|
|
ClearLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-07 09:54:33 +00:00
|
|
|
static void DoError (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Print an error */
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (CurC == '\0') {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Invalid #error directive");
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("#error: %s", lptr);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2001-09-07 09:54:33 +00:00
|
|
|
/* Clear the rest of line */
|
2000-06-15 19:03:01 +00:00
|
|
|
ClearLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
static void DoPragma (void)
|
|
|
|
/* Handle a #pragma line by converting the #pragma preprocessor directive into
|
|
|
|
* the _Pragma() compiler operator.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* Skip blanks following the #pragma directive */
|
|
|
|
SkipBlank ();
|
|
|
|
|
|
|
|
/* Copy the remainder of the line into mline removing comments and ws */
|
|
|
|
Pass1 (lptr, mline);
|
|
|
|
|
|
|
|
/* Convert the directive into the operator */
|
|
|
|
mptr = line;
|
|
|
|
KeepStr ("_Pragma (");
|
|
|
|
Stringize (mline);
|
|
|
|
KeepChar (')');
|
|
|
|
*mptr = '\0';
|
|
|
|
|
|
|
|
/* Initialize reading from line */
|
|
|
|
InitLine (line);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
void Preprocess (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Preprocess a line */
|
|
|
|
{
|
2002-09-29 21:09:47 +00:00
|
|
|
int Skip;
|
|
|
|
ident Directive;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Skip white space at the beginning of the line */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check for stuff to skip */
|
|
|
|
Skip = 0;
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC == '\0' || CurC == '#' || Skip) {
|
|
|
|
|
|
|
|
/* Check for preprocessor lines lines */
|
|
|
|
if (CurC == '#') {
|
|
|
|
NextChar ();
|
|
|
|
SkipBlank ();
|
|
|
|
if (CurC == '\0') {
|
|
|
|
/* Ignore the empty preprocessor directive */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!IsSym (Directive)) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Preprocessor directive expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
ClearLine ();
|
|
|
|
} else {
|
2001-09-06 10:17:52 +00:00
|
|
|
switch (FindPPToken (Directive)) {
|
2000-06-21 21:02:44 +00:00
|
|
|
|
|
|
|
case PP_DEFINE:
|
|
|
|
if (!Skip) {
|
2001-09-06 10:17:52 +00:00
|
|
|
DefineMacro ();
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
case PP_ELIF:
|
|
|
|
if (IfIndex >= 0) {
|
|
|
|
if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) {
|
|
|
|
|
|
|
|
/* Handle as #else/#if combination */
|
|
|
|
if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) {
|
|
|
|
Skip = !Skip;
|
|
|
|
}
|
|
|
|
IfStack[IfIndex] |= IFCOND_ELSE;
|
|
|
|
Skip = DoIf (Skip);
|
|
|
|
|
|
|
|
/* #elif doesn't need a terminator */
|
|
|
|
IfStack[IfIndex] &= ~IFCOND_NEEDTERM;
|
|
|
|
} else {
|
|
|
|
PPError ("Duplicate #else/#elif");
|
|
|
|
}
|
2001-09-07 09:54:33 +00:00
|
|
|
} else {
|
|
|
|
PPError ("Unexpected #elif");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
case PP_ELSE:
|
2001-09-07 09:54:33 +00:00
|
|
|
if (IfIndex >= 0) {
|
2001-12-12 21:45:05 +00:00
|
|
|
if ((IfStack[IfIndex] & IFCOND_ELSE) == 0) {
|
2001-09-07 09:54:33 +00:00
|
|
|
if ((IfStack[IfIndex] & IFCOND_SKIP) == 0) {
|
|
|
|
Skip = !Skip;
|
|
|
|
}
|
|
|
|
IfStack[IfIndex] |= IFCOND_ELSE;
|
|
|
|
} else {
|
|
|
|
PPError ("Duplicate #else");
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
} else {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Unexpected `#else'");
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_ENDIF:
|
2001-09-07 09:54:33 +00:00
|
|
|
if (IfIndex >= 0) {
|
|
|
|
/* Remove any clauses on top of stack that do not
|
|
|
|
* need a terminating #endif.
|
|
|
|
*/
|
2002-09-29 21:09:47 +00:00
|
|
|
while (IfIndex >= 0 && (IfStack[IfIndex] & IFCOND_NEEDTERM) == 0) {
|
2001-09-07 09:54:33 +00:00
|
|
|
--IfIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stack may not be empty here or something is wrong */
|
|
|
|
CHECK (IfIndex >= 0);
|
|
|
|
|
|
|
|
/* Remove the clause that needs a terminator */
|
|
|
|
Skip = (IfStack[IfIndex--] & IFCOND_SKIP) != 0;
|
2000-06-21 21:02:44 +00:00
|
|
|
} else {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Unexpected `#endif'");
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_ERROR:
|
|
|
|
if (!Skip) {
|
2001-09-07 09:54:33 +00:00
|
|
|
DoError ();
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_IF:
|
2001-09-07 09:54:33 +00:00
|
|
|
Skip = DoIf (Skip);
|
2000-06-21 21:02:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_IFDEF:
|
2001-09-07 09:54:33 +00:00
|
|
|
Skip = DoIfDef (Skip, 1);
|
2000-06-21 21:02:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_IFNDEF:
|
2001-09-07 09:54:33 +00:00
|
|
|
Skip = DoIfDef (Skip, 0);
|
2000-06-21 21:02:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_INCLUDE:
|
|
|
|
if (!Skip) {
|
2001-09-07 09:54:33 +00:00
|
|
|
DoInclude ();
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_LINE:
|
2002-09-29 21:09:47 +00:00
|
|
|
/* Not allowed in strict ANSI mode */
|
2001-09-07 09:54:33 +00:00
|
|
|
if (!Skip && ANSI) {
|
|
|
|
PPError ("Preprocessor directive expected");
|
|
|
|
ClearLine ();
|
2002-09-29 21:09:47 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-06-21 21:02:44 +00:00
|
|
|
|
|
|
|
case PP_PRAGMA:
|
2002-09-29 21:09:47 +00:00
|
|
|
if (!Skip) {
|
|
|
|
DoPragma ();
|
|
|
|
goto Done;
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_UNDEF:
|
|
|
|
if (!Skip) {
|
2001-09-07 09:54:33 +00:00
|
|
|
DoUndef ();
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Preprocessor directive expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
ClearLine ();
|
|
|
|
}
|
2002-09-29 21:09:47 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
}
|
2000-06-14 08:18:19 +00:00
|
|
|
if (NextLine () == 0) {
|
2001-09-07 09:54:33 +00:00
|
|
|
if (IfIndex >= 0) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("`#endif' expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2002-09-29 21:09:47 +00:00
|
|
|
SkipBlank ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
PreprocessLine ();
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
Done:
|
2001-03-10 10:21:03 +00:00
|
|
|
Print (stdout, 2, "line: %s\n", line);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|