<commit_before>/* See LICENSE file for copyright and license details. */
static char sccsid[] = "@(#) ./cc1/lex.c";
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cstd.h>
#include "../inc/cc.h"
#include "cc1.h"

unsigned yytoken;
struct yystype yylval;
char yytext[STRINGSIZ+3];
unsigned short yylen;
int lexmode = CCMODE;
unsigned lineno;
char filenam[FILENAME_MAX];

int namespace = NS_IDEN;
static int safe;
Input *input;

void
ilex(void)
{
	static struct keyword keys[] = {
		{"auto", SCLASS, AUTO},
		{"break", BREAK, BREAK},
		{"_Bool", TYPE, BOOL},
		{"__builtin_va_list", TYPE, VA_LIST},
		{"case", CASE, CASE},
		{"char", TYPE, CHAR},
		{"const", TQUALIFIER, CONST},
		{"continue", CONTINUE, CONTINUE},
		{"default", DEFAULT, DEFAULT},
		{"do", DO, DO},
		{"double", TYPE, DOUBLE},
		{"else", ELSE, ELSE},
		{"enum", TYPE, ENUM},
		{"extern", SCLASS, EXTERN},
		{"float", TYPE, FLOAT},
		{"for", FOR, FOR},
		{"goto", GOTO, GOTO},
		{"if", IF, IF},
		{"inline", TQUALIFIER, INLINE},
		{"int", TYPE, INT},
		{"long", TYPE, LONG},
		{"register", SCLASS, REGISTER},
		{"restrict", TQUALIFIER, RESTRICT},
		{"return", RETURN, RETURN},
		{"short", TYPE, SHORT},
		{"signed", TYPE, SIGNED},
		{"sizeof", SIZEOF, SIZEOF},
		{"static", SCLASS, STATIC},
		{"struct", TYPE, STRUCT},
		{"switch", SWITCH, SWITCH},
		{"typedef", SCLASS, TYPEDEF},
		{"union", TYPE, UNION},
		{"unsigned", TYPE, UNSIGNED},
		{"void", TYPE, VOID},
		{"volatile", TQUALIFIER, VOLATILE},
		{"while", WHILE, WHILE},
		{NULL, 0, 0},
	};
	keywords(keys, NS_KEYWORD);
}

int
setloc(char *fname, unsigned line)
{
	size_t len;

	if ((len = strlen(fname)) >= FILENAME_MAX)
		die("file name too long: '%s'", fname);
	memcpy(filenam, fname, len);
	filenam[len] = '\0';

	free(input->filenam);
	input->filenam = xstrdup(fname);
	lineno = input->lineno = line;
	return 1;
}

int
addinput(char *fname, Symbol *hide, char *buffer)
{
	FILE *fp;
	unsigned flags;
	Input *newip, *curip = input;

	if (hide) {
		/* this is a macro expansion */
		fp = NULL;
		if (hide->hide == UCHAR_MAX)
			die("Too many macro expansions");
		++hide->hide;
		flags = IMACRO;
	} else  if (fname) {
		/* a new file */
		if ((fp = fopen(fname, "r")) == NULL)
			die("Error opening '%s': %s", fname, strerror(errno));
		flags = IFILE;
		if (curip && onlyheader)
			printf("%s: %s\n", infile, fname);
	} else {
		/* reading from stdin */
		fp = stdin;
		fname = "<stdin>";
		flags = ISTDIN;
	}

	newip = xmalloc(sizeof(*newip));

	if (!buffer) {<commit_msg>[cc1] Return error in addinput() when a file cannot be openend<commit_after>/* See LICENSE file for copyright and license details. */
static char sccsid[] = "@(#) ./cc1/lex.c";
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <cstd.h>
#include "../inc/cc.h"
#include "cc1.h"

unsigned yytoken;
struct yystype yylval;
char yytext[STRINGSIZ+3];
unsigned short yylen;
int lexmode = CCMODE;
unsigned lineno;
char filenam[FILENAME_MAX];

int namespace = NS_IDEN;
static int safe;
Input *input;

void
ilex(void)
{
	static struct keyword keys[] = {
		{"auto", SCLASS, AUTO},
		{"break", BREAK, BREAK},
		{"_Bool", TYPE, BOOL},
		{"__builtin_va_list", TYPE, VA_LIST},
		{"case", CASE, CASE},
		{"char", TYPE, CHAR},
		{"const", TQUALIFIER, CONST},
		{"continue", CONTINUE, CONTINUE},
		{"default", DEFAULT, DEFAULT},
		{"do", DO, DO},
		{"double", TYPE, DOUBLE},
		{"else", ELSE, ELSE},
		{"enum", TYPE, ENUM},
		{"extern", SCLASS, EXTERN},
		{"float", TYPE, FLOAT},
		{"for", FOR, FOR},
		{"goto", GOTO, GOTO},
		{"if", IF, IF},
		{"inline", TQUALIFIER, INLINE},
		{"int", TYPE, INT},
		{"long", TYPE, LONG},
		{"register", SCLASS, REGISTER},
		{"restrict", TQUALIFIER, RESTRICT},
		{"return", RETURN, RETURN},
		{"short", TYPE, SHORT},
		{"signed", TYPE, SIGNED},
		{"sizeof", SIZEOF, SIZEOF},
		{"static", SCLASS, STATIC},
		{"struct", TYPE, STRUCT},
		{"switch", SWITCH, SWITCH},
		{"typedef", SCLASS, TYPEDEF},
		{"union", TYPE, UNION},
		{"unsigned", TYPE, UNSIGNED},
		{"void", TYPE, VOID},
		{"volatile", TQUALIFIER, VOLATILE},
		{"while", WHILE, WHILE},
		{NULL, 0, 0},
	};
	keywords(keys, NS_KEYWORD);
}

int
setloc(char *fname, unsigned line)
{
	size_t len;

	if ((len = strlen(fname)) >= FILENAME_MAX)
		die("file name too long: '%s'", fname);
	memcpy(filenam, fname, len);
	filenam[len] = '\0';

	free(input->filenam);
	input->filenam = xstrdup(fname);
	lineno = input->lineno = line;
	return 1;
}

int
addinput(char *fname, Symbol *hide, char *buffer)
{
	FILE *fp;
	unsigned flags;
	Input *newip, *curip = input;

	if (hide) {
		/* this is a macro expansion */
		fp = NULL;
		if (hide->hide == UCHAR_MAX)
			die("Too many macro expansions");
		++hide->hide;
		flags = IMACRO;
	} else  if (fname) {
		/* a new file */
		if ((fp = fopen(fname, "r")) == NULL)
			return 0;
		flags = IFILE;
		if (curip && onlyheader)
			printf("%s: %s\n", infile, fname);
	} else {
		/* reading from stdin */
		fp = stdin;
		fname = "<stdin>";
		flags = ISTDIN;
	}

	newip = xmalloc(sizeof(*newip));

	if (!buffer) {
{'fraction_non_alphanumeric': 0.2030175706646295, 'fraction_numerical': 0.0030557677616501145, 'mean_word_length': 4.15960591133005, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '159936', 'n_tokens_mistral': 2263, 'n_tokens_neox': 2064, 'n_words': 573}