Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an issue with the C preprocessor and files which don't end in newlines. #311

Merged
merged 12 commits into from
Oct 18, 2024
2 changes: 1 addition & 1 deletion .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
run: |
make ack-setup.exe
- name: upload setup
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: ${{ github.event.repository.name }}.${{ github.sha }}
path: ack-setup.exe
44 changes: 34 additions & 10 deletions lang/cem/cpp.ansi/LLlex.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "parameters.h"

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <alloc.h>
#include "input.h"
Expand All @@ -19,8 +21,9 @@
#include "class.h"
#include "error.h"
#include "bits.h"
#include "domacro.h"

#define BUFSIZ 1024
#define BUFFERSIZE 1024

struct token dot;

Expand All @@ -32,20 +35,17 @@ int AccFileSpecifier = 0; /* return filespecifier <...> */
int LexSave = 0; /* last character read by GetChar */
extern int InputLevel; /* # of current macro expansions */


#define FLG_ESEEN 0x01 /* possibly a floating point number */
#define FLG_DOTSEEN 0x02 /* certainly a floating point number */


/* Private forward definitions */

static arith char_constant(char*);
static char* string_token(char *, int);
static char* string_token(char*, int);
static int quoted(register int);
static int val_in_base(register int, int);
static int trigraph(void);


int LLlex(void)
{
return (DOT != EOF) ? GetToken(&dot) : EOF;
Expand All @@ -58,7 +58,7 @@ int GetToken(register struct token* ptok)
combination. Macro replacement is also performed if it is
needed.
*/
char buf[BUFSIZ];
char buf[BUFFERSIZE];
register int ch, nch;

again: /* rescan the input after an error or replacement */
Expand Down Expand Up @@ -433,9 +433,10 @@ void skiplinecomment(void)
{
/* The last character read has been the '/' of '//'. We read
and discard all characters up to but not including the next
NL. */

for (;;) {
NL. */

for (;;)
{
int c = GetChar();
if ((class(c) == STNL) || (c == EOI))
{
Expand Down Expand Up @@ -479,7 +480,7 @@ static arith char_constant(char* nm)
return val;
}

static char* string_token(char *nm, int stop_char)
static char* string_token(char* nm, int stop_char)
{
register int ch;
register int str_size;
Expand Down Expand Up @@ -597,9 +598,32 @@ int GetChar(void)
sequences and removes occurences of \\\n.
*/
register int ch;
static bool atnewline = true;

again:
LoadChar(ch);
if (ch == EOI)
{
if (!atnewline)
{
PushBack();
ch = '\n';
}
else
{
if (nestcount > 0)
{
nestlevel = svnestlevel[nestcount--];
goto again;
}

if (nestlevel > svnestlevel[nestcount])
warning("missing #endif");
else if (NoUnstack)
warning("unexpected EOF");
}
}
atnewline = (ch == '\n');

/* possible trigraph sequence */
if (ch == '?')
Expand Down
25 changes: 11 additions & 14 deletions lang/cem/cpp.ansi/domacro.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ extern int do_preprocess;

/* Internal declarations */


static void do_define(void);
static void do_elif(void);
static void do_else(void);
Expand All @@ -49,21 +48,20 @@ static void do_if(void);
static void do_ifdef(int);
static void do_include(void);
static void do_line(unsigned int);
static int find_name(char* , char* []);
static char* get_text(char* [], int* );
static int getparams(char* [], char []);
static int find_name(char*, char*[]);
static char* get_text(char*[], int*);
static int getparams(char*[], char[]);
static int ifexpr(void);
static int macroeq(register char*, register char *);
static int macroeq(register char*, register char*);
static void skip_block(int);
static void do_error(void);

/* External dependencies to C files with no include files */
extern void If_expr(void);
extern void add_dependency(char *);

extern void add_dependency(char*);

char* GetIdentifier(int skiponerr /* skip the rest of the line on error */
)
)
{
/* Returns a pointer to the identifier that is read from the
input stream. When the input does not contain an
Expand All @@ -89,7 +87,6 @@ char* GetIdentifier(int skiponerr /* skip the rest of the line on error */
return tk.tk_str;
}


void domacro(void)
{
struct token tk; /* the token itself */
Expand Down Expand Up @@ -346,7 +343,7 @@ static int ifexpr(void)
static void do_include(void)
{
/* do_include() performs the inclusion of a file.
*/
*/
char* filenm;
char* result;
int tok;
Expand Down Expand Up @@ -390,7 +387,7 @@ static void do_include(void)
static void do_define(void)
{
/* do_define() interprets a #define control line.
*/
*/
register char* str; /* the #defined identifier's descriptor */
int nformals = -1; /* keep track of the number of formals */
char* formals[NPARAMS]; /* pointers to the names of the formals */
Expand Down Expand Up @@ -505,7 +502,7 @@ static void do_ifdef(int how)
register char* str;

/* how == 1 : ifdef; how == 0 : ifndef
*/
*/
push_if();
if (!(str = GetIdentifier(1)))
{
Expand Down Expand Up @@ -697,7 +694,7 @@ void macro_def(register struct idf* id, char* text, int nformals, int length, in
newdef->mc_flag = flags; /* special flags */
}

static int find_name(char* nm, char *index[])
static int find_name(char* nm, char* index[])
{
/* find_name() returns the index of "nm" in the namelist
"index" if it can be found there. 0 is returned if it is
Expand Down Expand Up @@ -888,7 +885,7 @@ static char* get_text(char* formals[], int* length)
as strings, without taking care of the leading and trailing
blanks (spaces and tabs).
*/
static int macroeq(register char* s, register char *t)
static int macroeq(register char* s, register char* t)
{

/* skip leading spaces */
Expand Down
16 changes: 10 additions & 6 deletions lang/cem/cpp.ansi/domacro.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* Copyright (c) 2019 ACK Project.
* See the copyright notice in the ACK home directory,
* See the copyright notice in the ACK home directory,
* in the file "Copyright".
*
* Created on: 2019-02-09
*
*
*/
#ifndef DOMACRO_H_
#define DOMACRO_H_

struct idf;

void macro_def(register struct idf* id, char* text, int nformals, int length, int flags);
void do_undef(char* argstr);
extern void macro_def(register struct idf* id, char* text, int nformals, int length, int flags);
extern void do_undef(char* argstr);
/* Control line interpreter. The '#' has already
been read by the lexical analyzer by which this function is called.
The token appearing directly after the '#' is obtained by calling
Expand All @@ -21,7 +21,11 @@ void do_undef(char* argstr);
Pragma's are handled by do_pragma(). They are passed on to the
compiler.
*/
void domacro(void);
char* GetIdentifier(int skiponerr);
extern void domacro(void);
extern char* GetIdentifier(int skiponerr);

extern int nestlevel;
extern int nestcount;
extern int svnestlevel[];

#endif /* DOMACRO_H_ */
34 changes: 15 additions & 19 deletions lang/cem/cpp.ansi/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,30 @@
#include "input.h"
#include "error.h"
#include "replace.h"
#include "domacro.h"

#define INP_PUSHBACK 3
#define INP_TYPE struct file_info
#define INP_VAR finfo
struct file_info finfo;
#define INP_NPUSHBACK 3
#define INP_TYPE struct file_info
#define INP_VAR finfo
struct file_info finfo;
#include <inp_pkg.body>
#include <alloc.h>

char *getwdir(register char *fn)
char* getwdir(register char* fn)
{
register char *p;
char *strrchr();
register char* p;

p = strrchr(fn, '/');
while (p && *(p + 1) == '\0') { /* remove trailing /'s */
while (p && *(p + 1) == '\0')
{ /* remove trailing /'s */
*p = '\0';
p = strrchr(fn, '/');
}

if (fn[0] == '\0' || (fn[0] == '/' && p == &fn[0])) /* absolute path */
return "";
if (p) {
if (p)
{
*p = '\0';
fn = Salloc(fn, (unsigned)(p - &fn[0] + 1));
*p = '/';
Expand All @@ -41,8 +43,8 @@ char *getwdir(register char *fn)
return ".";
}

int NoUnstack;
int InputLevel;
int NoUnstack;
int InputLevel;

int AtEoIT(void)
{
Expand All @@ -54,12 +56,6 @@ int AtEoIT(void)

int AtEoIF(void)
{
extern int nestlevel;
extern int nestcount;
extern int svnestlevel[];

if (nestlevel > svnestlevel[nestcount]) warning("missing #endif");
else if (NoUnstack) warning("unexpected EOF");
nestlevel = svnestlevel[nestcount--];
return 0;
/* Configure inp_pkg to always return EOIs at the end of source files. */
return 1;
}
1 change: 1 addition & 0 deletions lang/m2/comp/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "f_info.h"
struct f_info file_info;
#include "input.h"
#include "error.h"
#include <inp_pkg.body>


Expand Down
1 change: 1 addition & 0 deletions lang/pc/comp/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct f_info file_info;
#include "input.h"
#include <em_arith.h>
#include "idf.h"
#include "error.h"
#include <inp_pkg.body>


Expand Down
Loading
Loading