Run through clang-format before editing.

This commit is contained in:
David Given 2018-09-02 12:04:33 +02:00
parent 274ed3cb6a
commit 0e9736fdca

View file

@ -5,77 +5,88 @@
/* $Id$ */ /* $Id$ */
/* PREPROCESSOR DRIVER */ /* PREPROCESSOR DRIVER */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <system.h> #include <system.h>
#include <alloc.h> #include <alloc.h>
#include "input.h" #include "input.h"
#include "parameters.h" #include "parameters.h"
#include "arith.h" #include "arith.h"
#include "LLlex.h" #include "LLlex.h"
#include "class.h" #include "class.h"
#include "macro.h" #include "macro.h"
#include "idf.h" #include "idf.h"
#include "bits.h" #include "bits.h"
char _obuf[OBUFSIZE]; char _obuf[OBUFSIZE];
#ifdef DOBITS #ifdef DOBITS
char bits[128]; char bits[128];
#endif #endif
extern int InputLevel; extern int InputLevel;
extern char *sprint(); extern char* sprint();
Xflush() Xflush()
{ {
sys_write(STDOUT, _obuf, OBUFSIZE); sys_write(STDOUT, _obuf, OBUFSIZE);
} }
static char *SkipComment(); static char* SkipComment();
extern char options[]; extern char options[];
/* #pragma directives are saved here and passed to the compiler later on. /* #pragma directives are saved here and passed to the compiler later on.
*/ */
struct prag_info { struct prag_info
int pr_linnr; {
char *pr_fil; int pr_linnr;
char *pr_text; char* pr_fil;
char* pr_text;
}; };
static struct prag_info *pragma_tab; static struct prag_info* pragma_tab;
static int pragma_nr; static int pragma_nr;
do_pragma() do_pragma()
{ {
register int size = ITEXTSIZE; register int size = ITEXTSIZE;
char *cur_line = Malloc((unsigned)size); char* cur_line = Malloc((unsigned)size);
register char *c_ptr = cur_line; register char* c_ptr = cur_line;
register int c = GetChar(); register int c = GetChar();
register int delim = 0; register int delim = 0;
while(c != '\n') { while (c != '\n')
if (c_ptr + 1 - cur_line == size) { {
if (c_ptr + 1 - cur_line == size)
{
cur_line = Realloc(cur_line, (unsigned)(size + ITEXTSIZE)); cur_line = Realloc(cur_line, (unsigned)(size + ITEXTSIZE));
c_ptr = cur_line + size - 1; c_ptr = cur_line + size - 1;
size += ITEXTSIZE; size += ITEXTSIZE;
} }
if (delim) { if (delim)
if (c == delim) { {
if (c == delim)
{
delim = 0; delim = 0;
} }
else if (c == '\\') { else if (c == '\\')
{
*c_ptr++ = c; *c_ptr++ = c;
c = GetChar(); c = GetChar();
if (c == '\n') break; if (c == '\n')
break;
} }
} }
else if (c == '\'' || c == '"') { else if (c == '\'' || c == '"')
{
delim = c; delim = c;
} }
else if (c == '/') { else if (c == '/')
if ((c = GetChar()) != '*' || InputLevel) { {
if ((c = GetChar()) != '*' || InputLevel)
{
*c_ptr++ = '/'; *c_ptr++ = '/';
} }
else { else
{
skipcomment(); skipcomment();
continue; continue;
} }
@ -84,13 +95,17 @@ do_pragma()
c = GetChar(); c = GetChar();
} }
*c_ptr = '\0'; *c_ptr = '\0';
if (!pragma_nr) { if (!pragma_nr)
pragma_tab = (struct prag_info *)Malloc(sizeof(struct prag_info)); {
} else { pragma_tab = (struct prag_info*)Malloc(sizeof(struct prag_info));
pragma_tab = (struct prag_info *)Realloc((char *)pragma_tab
, (unsigned)(sizeof(struct prag_info) * (pragma_nr+1)));
} }
if (delim) { else
{
pragma_tab = (struct prag_info*)Realloc(
(char*)pragma_tab, (unsigned)(sizeof(struct prag_info) * (pragma_nr + 1)));
}
if (delim)
{
error("unclosed opening %c", delim); error("unclosed opening %c", delim);
} }
pragma_tab[pragma_nr].pr_linnr = LineNumber; pragma_tab[pragma_nr].pr_linnr = LineNumber;
@ -102,138 +117,176 @@ do_pragma()
char Xbuf[256]; char Xbuf[256];
void void preprocess(fn) char* fn;
preprocess(fn)
char *fn;
{ {
register int c; register int c;
register char *op = _obuf; register char* op = _obuf;
register char *ob = &_obuf[OBUFSIZE]; register char* ob = &_obuf[OBUFSIZE];
int lineno = 0; int lineno = 0;
int startline; int startline;
#define flush(X) (sys_write(STDOUT,_obuf,X)) #define flush(X) (sys_write(STDOUT, _obuf, X))
#define echo(ch) if (op == ob) { Xflush(); op = _obuf; } *op++ = (ch); #define echo(ch) \
#define newline() op--; while (op >= _obuf && (*op == ' ' || *op == '\t')) op--; op++; echo('\n') if (op == ob) \
{ \
Xflush(); \
op = _obuf; \
} \
*op++ = (ch);
#define newline() \
op--; \
while (op >= _obuf && (*op == ' ' || *op == '\t')) \
op--; \
op++; \
echo('\n')
if (!options['P']) { if (!options['P'])
{
/* Generate a line directive communicating the /* Generate a line directive communicating the
source filename source filename
*/ */
register char *p = Xbuf; register char* p = Xbuf;
sprint(p, "%s 1 \"%s\"\n", sprint(p, "%s 1 \"%s\"\n", LINE_PREFIX, FileName);
LINE_PREFIX, while (*p)
FileName); {
while (*p) {
echo(*p++); echo(*p++);
} }
} }
#define do_line_dir(lineno, fn) \ #define do_line_dir(lineno, fn) \
if (lineno != LineNumber || fn != FileName) { \ if (lineno != LineNumber || fn != FileName) \
fn = FileName; \ { \
lineno = LineNumber; \ fn = FileName; \
if (! options['P']) { \ lineno = LineNumber; \
register char *p = Xbuf; \ if (!options['P']) \
sprint(Xbuf, "%s %d \"%s\"\n", \ { \
LINE_PREFIX, \ register char* p = Xbuf; \
(int)LineNumber, \ sprint(Xbuf, "%s %d \"%s\"\n", LINE_PREFIX, (int)LineNumber, FileName); \
FileName); \ op--; \
op--; \ while (op >= _obuf && (class(*op) == STSKIP || *op == '\n')) \
while (op >= _obuf \ op--; \
&& (class(*op) == STSKIP \ op++; \
|| *op == '\n')) op--; \ newline(); \
op++; \ while (*p) \
newline(); \ { \
while (*p) { \ echo(*p++); \
echo(*p++); \ } \
} \ } \
} \ }
}
for (;;) { for (;;)
{
LineNumber++; LineNumber++;
lineno++; lineno++;
startline = 1; startline = 1;
c = GetChar(); c = GetChar();
while (startline) { while (startline)
/* first flush the saved pragma's */ {
if (pragma_nr) { /* first flush the saved pragma's */
if (pragma_nr)
{
register int i = 0; register int i = 0;
int LiNo = LineNumber; int LiNo = LineNumber;
char *FiNam = FileName; char* FiNam = FileName;
while (i < pragma_nr) { while (i < pragma_nr)
register char *c_ptr = "#pragma"; {
register char* c_ptr = "#pragma";
LineNumber = pragma_tab[i].pr_linnr;
FileName = pragma_tab[i].pr_fil; LineNumber = pragma_tab[i].pr_linnr;
do_line_dir(lineno, fn); FileName = pragma_tab[i].pr_fil;
while (*c_ptr) { echo(*c_ptr++); } do_line_dir(lineno, fn);
c_ptr = pragma_tab[i].pr_text; while (*c_ptr)
while (*c_ptr) { echo(*c_ptr++); } {
newline(); lineno++; echo(*c_ptr++);
free(pragma_tab[i].pr_text); }
i++; c_ptr = pragma_tab[i].pr_text;
while (*c_ptr)
{
echo(*c_ptr++);
}
newline();
lineno++;
free(pragma_tab[i].pr_text);
i++;
} }
free((char *) pragma_tab); free((char*)pragma_tab);
pragma_tab = (struct prag_info *)0; pragma_tab = (struct prag_info*)0;
pragma_nr = 0; pragma_nr = 0;
LineNumber = LiNo; LineNumber = LiNo;
FileName = FiNam; FileName = FiNam;
do_line_dir(lineno, fn); do_line_dir(lineno, fn);
} }
while (class(c) == STSKIP || c == '/')
while (class(c) == STSKIP || c == '/') { {
if (c == '/') { if (c == '/')
if (!InputLevel) { {
c = GetChar(); if (!InputLevel)
if (c == '*') { {
op = SkipComment(op, &lineno); c = GetChar();
if (!op) return; if (c == '*')
if (!options['C']) { echo(' '); } {
c = GetChar(); op = SkipComment(op, &lineno);
continue; if (!op)
return;
if (!options['C'])
{
echo(' ');
}
c = GetChar();
continue;
}
UnGetChar();
c = '/';
} }
UnGetChar(); break;
c = '/';
}
break;
} }
echo(c); echo(c);
c = GetChar(); c = GetChar();
} }
if (c == '#') { if (c == '#')
{
domacro(); domacro();
lineno++; lineno++;
newline(); newline();
do_line_dir(lineno, fn); do_line_dir(lineno, fn);
c = GetChar(); c = GetChar();
} else startline = 0; }
else
startline = 0;
} }
do_line_dir(lineno, fn); do_line_dir(lineno, fn);
for (;;) { for (;;)
{
/* illegal character */ /* illegal character */
if (c & 0200) { if (c & 0200)
if (c == EOI) { {
if (c == EOI)
{
newline(); newline();
flush((int)(op-_obuf)); flush((int)(op - _obuf));
return; return;
} }
fatal("non-ascii character read"); fatal("non-ascii character read");
} }
/* comments */ /* comments */
if (c == '/' && !InputLevel) { if (c == '/' && !InputLevel)
{
c = GetChar(); c = GetChar();
if (c == '*') { if (c == '*')
{
op = SkipComment(op, &lineno); op = SkipComment(op, &lineno);
if (!op) return; if (!op)
if (!options['C']) { echo(' '); } return;
if (!options['C'])
{
echo(' ');
}
c = GetChar(); c = GetChar();
continue; continue;
} }
@ -242,164 +295,221 @@ preprocess(fn)
} }
/* switch on character */ /* switch on character */
switch(class(c)) { switch (class(c))
case STNL: {
newline(); case STNL:
break; newline();
case STSTR: break;
case STCHAR: case STSTR:
case STCHAR:
{ {
register int stopc = c; register int stopc = c;
int escaped; int escaped;
do { do
escaped = 0; {
echo(c); escaped = 0;
c = GetChar();
if (c == '\n') {
/* the compiler will complain */
break;
}
else if (c == EOI) {
newline();
flush((int)(op-_obuf));
return;
}
if (c == '\\') {
echo(c); echo(c);
c = GetChar(); c = GetChar();
if (c == '\n') { if (c == '\n')
++LineNumber; {
lineno++; /* the compiler will complain */
} else escaped = 1; break;
} }
} while (escaped || c != stopc); else if (c == EOI)
echo(c); {
if (c == '\n') newline();
break; /* Don't eat # */ flush((int)(op - _obuf));
c = GetChar(); return;
continue; }
} if (c == '\\')
case STNUM: {
/* The following code is quit ugly. This because echo(c);
* ..3 == . .3 , whereas ...3 == ... 3 c = GetChar();
*/ if (c == '\n')
echo(c); {
if (c == '.') { ++LineNumber;
lineno++;
}
else
escaped = 1;
}
} while (escaped || c != stopc);
echo(c);
if (c == '\n')
break; /* Don't eat # */
c = GetChar(); c = GetChar();
if (c == '.') { continue;
if ((c = GetChar()) == '.') { }
echo('.'); echo('.'); case STNUM:
/* The following code is quit ugly. This because
* ..3 == . .3 , whereas ...3 == ... 3
*/
echo(c);
if (c == '.')
{
c = GetChar();
if (c == '.')
{
if ((c = GetChar()) == '.')
{
echo('.');
echo('.');
c = GetChar();
continue;
}
UnGetChar();
c = '.';
continue;
}
else if (!is_dig(c))
{
continue;
}
else
{
echo(c);
}
}
c = GetChar();
while (in_idf(c) || c == '.')
{
echo(c);
if (c == 'e' || c == 'E')
{
c = GetChar();
if (c == '+' || c == '-')
{
echo(c);
c = GetChar();
}
}
else
c = GetChar();
}
continue;
case STELL:
c = GetChar();
UnGetChar();
if (c == '"' || c == '\'')
{
echo('L');
continue;
}
c = 'L';
case STIDF:
{
extern int idfsize; /* ??? */
char buf[IDFSIZE + 1];
register char* tg = &buf[0];
register char* maxpos = &buf[idfsize];
register struct idf* idef;
int NoExpandNext = 0;
#define tstmac(bx) \
if (!(bits[c] & bx)) \
goto nomac
#define cpy *tg++ = c
#define load \
c = GetChar(); \
if (!in_idf(c)) \
goto endidf
/* unstack macro's when allowed. */
if (Unstacked)
EnableMacros();
if (c == NOEXPM)
{
NoExpandNext = 1;
c = GetChar();
}
#ifdef DOBITS
cpy;
tstmac(bit0);
load;
cpy;
tstmac(bit1);
load;
cpy;
tstmac(bit2);
load;
cpy;
tstmac(bit3);
load;
cpy;
tstmac(bit4);
load;
cpy;
tstmac(bit5);
load;
cpy;
tstmac(bit6);
load;
cpy;
tstmac(bit7);
load;
#endif
for (;;)
{
if (tg < maxpos)
{
cpy;
}
load;
}
endidf:
if (c != EOF)
UnGetChar();
*tg = '\0'; /* mark the end of the identifier */
if ((idef = findidf(buf)) && idef->id_macro && ReplaceMacros && !NoExpandNext)
{
if (replace(idef))
{
echo(' ');
c = GetChar(); c = GetChar();
continue; continue;
} }
UnGetChar(); tg = buf;
c = '.'; while (*tg)
continue; {
} else if (!is_dig(c)) { echo(*tg++);
continue;
} else { echo(c); }
}
c = GetChar();
while (in_idf(c) || c == '.') {
echo(c);
if (c == 'e' || c == 'E') {
c = GetChar();
if (c == '+' || c == '-') {
echo(c);
c = GetChar();
} }
} else c = GetChar();
}
continue;
case STELL:
c = GetChar();
UnGetChar();
if (c == '"' || c == '\'') {
echo('L');
continue;
}
c = 'L';
case STIDF: {
extern int idfsize; /* ??? */
char buf[IDFSIZE + 1];
register char *tg = &buf[0];
register char *maxpos = &buf[idfsize];
register struct idf *idef;
int NoExpandNext = 0;
#define tstmac(bx) if (!(bits[c] & bx)) goto nomac
#define cpy *tg++ = c
#define load c = GetChar(); if (!in_idf(c)) goto endidf
/* unstack macro's when allowed. */
if (Unstacked)
EnableMacros();
if (c == NOEXPM) {
NoExpandNext = 1;
c = GetChar();
}
#ifdef DOBITS
cpy; tstmac(bit0); load;
cpy; tstmac(bit1); load;
cpy; tstmac(bit2); load;
cpy; tstmac(bit3); load;
cpy; tstmac(bit4); load;
cpy; tstmac(bit5); load;
cpy; tstmac(bit6); load;
cpy; tstmac(bit7); load;
#endif
for(;;) {
if (tg < maxpos) {
cpy;
}
load;
}
endidf:
if (c != EOF) UnGetChar();
*tg = '\0'; /* mark the end of the identifier */
if ((idef = findidf(buf))
&& idef->id_macro
&& ReplaceMacros && !NoExpandNext) {
if (replace(idef)) {
echo(' ');
c = GetChar(); c = GetChar();
if (in_idf(c))
{
echo(' ');
}
continue; continue;
} }
nomac:
*tg = '\0';
tg = buf; tg = buf;
while (*tg) { while (*tg)
{
echo(*tg++); echo(*tg++);
} }
c = GetChar(); c = GetChar();
if (in_idf(c)) { echo(' '); } while (in_idf(c))
{
echo(c);
c = GetChar();
}
continue; continue;
} }
nomac: case STMSPEC:
*tg = '\0'; if (InputLevel)
tg = buf; {
while (*tg) { echo(' '); /* seperate tokens */
echo(*tg++); c = GetChar();
} continue;
c = GetChar(); }
while (in_idf(c)) { /* else fallthrough */
default:
echo(c); echo(c);
c = GetChar(); c = GetChar();
}
continue;
}
case STMSPEC:
if (InputLevel) {
echo(' '); /* seperate tokens */
c = GetChar();
continue; continue;
}
/* else fallthrough */
default:
echo(c);
c = GetChar();
continue;
} }
break; break;
} }
@ -407,49 +517,60 @@ preprocess(fn)
/*NOTREACHED*/ /*NOTREACHED*/
} }
static char * static char* SkipComment(op, lineno) char* op;
SkipComment(op, lineno) int* lineno;
char *op;
int *lineno;
{ {
char *ob = &_obuf[OBUFSIZE]; char* ob = &_obuf[OBUFSIZE];
register int c, oldc = '\0'; register int c, oldc = '\0';
NoUnstack++; NoUnstack++;
if (options['C']) { if (options['C'])
{
echo('/'); echo('/');
echo('*'); echo('*');
} }
c = GetChar(); c = GetChar();
for(;;) { for (;;)
if (c == EOI) { {
if (c == EOI)
{
newline(); newline();
flush((int)(op - _obuf)); flush((int)(op - _obuf));
op = 0; op = 0;
break; break;
} }
if (options['C']) { if (options['C'])
{
echo(c); echo(c);
} }
if (c == '\n') { if (c == '\n')
{
++LineNumber; ++LineNumber;
++*lineno; ++*lineno;
if (!options['C']) { if (!options['C'])
{
echo(c); echo(c);
} }
} }
if (c == '*') { if (c == '*')
{
c = GetChar(); c = GetChar();
if (c == '/') { if (c == '/')
if (options['C']) { {
if (options['C'])
{
echo(c); echo(c);
} }
break; /* for(;;) */ break; /* for(;;) */
} else if (oldc == '/') { }
else if (oldc == '/')
{
warning("comment inside comment ?"); warning("comment inside comment ?");
} }
oldc = '*'; oldc = '*';
} else { }
else
{
oldc = c; oldc = c;
c = GetChar(); c = GetChar();
} }