예제 #1
0
void preproc_push(FILE *f, const char *fname, int is_sysh)
{
	if(file_stack_idx >= 0)
		file_stack[file_stack_idx].line_no = current_line; /* save state */


	file_stack_idx++;
	if(file_stack_idx == countof(file_stack))
		CPP_DIE("too many includes");

#ifdef DO_CHDIR
	char *wd;

	curwdfd = open(".", O_RDONLY);
	if(curwdfd == -1)
		ppdie(p, "open(\".\"): %s", strerror(errno));

	/* make sure everything is relative to the file */
	wd = udirname(p->fname);
	if(chdir(wd))
		ppdie(p, "chdir(\"%s\"): %s (for %s)", wd, strerror(errno), p->fname);
	free(wd);
#endif


	/* setup new state */
	set_current_fname(fname);

	file_stack[file_stack_idx].file    = f;
	file_stack[file_stack_idx].fname   = ustrdup(fname);
	file_stack[file_stack_idx].line_no = current_line = 1;
	file_stack[file_stack_idx].is_sysh = is_sysh;

	preproc_emit_line_info_top(LINEINFO_START_OF_FILE);
}
예제 #2
0
void preproc_push(FILE *f, const char *fname)
{
	if(file_stack_idx >= 0)
		file_stack[file_stack_idx].line_no = current_line; /* save state */


	file_stack_idx++;
	if(file_stack_idx == ARRAY_LEN(file_stack))
		die("too many includes");

#ifdef DO_CHDIR
	char *wd;

	curwdfd = open(".", O_RDONLY);
	if(curwdfd == -1)
		ppdie(p, "open(\".\"): %s", strerror(errno));

	/* make sure everything is relative to the file */
	wd = udirname(p->fname);
	if(chdir(wd))
		ppdie(p, "chdir(\"%s\"): %s (for %s)", wd, strerror(errno), p->fname);
	free(wd);
#endif


	/* setup new state */
	file_stack[file_stack_idx].file    = f;
	file_stack[file_stack_idx].fname   = ustrdup(fname);
	file_stack[file_stack_idx].line_no = current_line = 1;

	preproc_out_info();
}
예제 #3
0
파일: main.c 프로젝트: 8l/ucc-c-compiler
int main(int argc, char **argv)
{
	char *infname, *outfname;
	int ret = 0;
	enum { NONE, MACROS, MACROS_WHERE, STATS, DEPS } dump = NONE;
	int i;
	int platform_win32 = 0;
	int freestanding = 0;

	infname = outfname = NULL;

	current_line = 1;
	current_fname = FNAME_BUILTIN;

	macro_add_limits();

	for(i = 0; initial_defs[i].nam; i++){
		if(initial_defs[i].is_fn)
			macro_add_func(initial_defs[i].nam, initial_defs[i].val, NULL, 0, 1);
		else
			macro_add(initial_defs[i].nam, initial_defs[i].val, 0);
	}

	switch(platform_type()){
		case PLATFORM_x86_64:
			macro_add("__LP64__", "1", 0);
			macro_add("__x86_64__", "1", 0);
			/* TODO: __i386__ for 32 bit */
			break;

		case PLATFORM_mipsel_32:
			macro_add("__MIPS__", "1", 0);
	}

	if(platform_bigendian())
		macro_add("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__", 0);
	else
		macro_add("__BYTE_ORDER__", "__ORDER_LITTLE_ENDIAN__", 0);

	switch(platform_sys()){
#define MAP(t, s) case t: macro_add(s, "1", 0); break
		MAP(PLATFORM_LINUX,   "__linux__");
		MAP(PLATFORM_FREEBSD, "__FreeBSD__");
#undef MAP

		case PLATFORM_DARWIN:
			macro_add("__DARWIN__", "1", 0);
			macro_add("__MACH__", "1", 0); /* TODO: proper detection for these */
			macro_add("__APPLE__", "1", 0);
			break;

		case PLATFORM_CYGWIN:
			macro_add("__CYGWIN__", "1", 0);
			platform_win32 = 1;
			break;
	}

	macro_add("__WCHAR_TYPE__",
			platform_win32 ? "short" : "int", 0);

	macro_add_sprintf("__BIGGEST_ALIGNMENT__", "%u", platform_align_max());

	current_fname = FNAME_CMDLINE;

	for(i = 1; i < argc && *argv[i] == '-'; i++){
		if(!strcmp(argv[i]+1, "-"))
			break;

		switch(argv[i][1]){
			case 'I':
				if(argv[i][2])
					include_add_dir(argv[i]+2);
				else
					goto usage;
				break;

			case 'o':
				if(outfname)
					goto usage;

				if(argv[i][2])
					outfname = argv[i] + 2;
				else if(++i < argc)
					outfname = argv[i];
				else
					goto usage;
				break;

			case 'P':
				option_line_info = 0;
				break;

			case 'C':
				if(argv[i][2] == '\0')
					strip_comments = STRIP_EXCEPT_DIRECTIVE;
				else if(!strcmp(argv[i] + 2, "C"))
					strip_comments = STRIP_NONE;
				break;

			case 'M':
				if(!strcmp(argv[i] + 2, "M")){
					dump = DEPS;
					no_output = 1;
				}else{
					goto usage;
				}
				break;

			case 'D':
			{
				char *arg = argv[i] + 2;
				char *eq;
				char *directive;

				if(!*arg){
					/* allow "-D" "arg" */
					arg = argv[++i];
					if(!arg)
						goto usage;
				}

				/* -D'yo yo' means #define yo yo 1, that is,
				 * we literally generate the #define line */

				eq = strchr(arg, '=');
				if(eq)
					*eq = '\0';

				directive = ustrprintf(
						"define %s %s",
						arg, eq ? eq + 1 : "1");

				parse_internal_directive(directive);
				free(directive);
				break;
			}

			case 'U':
				if(!argv[i][2])
					goto usage;
				macro_remove(argv[i] + 2);
				break;

			case 'd':
				if(argv[i][2] && argv[i][3])
					goto defaul;
				switch(argv[i][2]){
					case 'M':
					case 'S':
					case 'W':
						/* list #defines */
						dump = (
								argv[i][2] == 'M' ? MACROS :
								argv[i][2] == 'S' ? STATS :
								MACROS_WHERE);

						no_output = 1;
						break;
					case '\0':
						option_trace = 1;
						break;
					default:
						goto usage;
				}
				break;

			case '\0':
				/* we've been passed "-" as a filename */
				break;

			case 'f':
				if(!strcmp(argv[i]+2, "freestanding")){
					freestanding = 1;
				}else if(!strncmp(argv[i]+2, "message-length=", 15)){
					const char *p = argv[i] + 17;
					warning_length = atoi(p);
				}else{
					goto usage;
				}
				break;

			case 'W':
			{
				int off;
				unsigned j;
				char *p = argv[i] + 2;

				off = !strncmp(p, "no-", 3);
				if(off)
					p += 3;


				ITER_WARNS(j){
					if(!strcmp(p, warns[j].warn)){
						if(off)
							wmode &= ~warns[j].or_mask;
						else
							wmode |= warns[j].or_mask;
						break;
					}
				}

				/* if not found, we ignore - it was intended for cc1 */
				break;
			}

			case 'O':
			{
				switch(argv[i][2]){
					case '0':
						break;
					case 's':
						macro_add("__OPTIMIZE_SIZE__",  "1", 0);
						/* fallthru */
					default:
						macro_add("__OPTIMIZE__",  "1", 0);
				}
				break;
			}

			case 'w':
				if(!argv[i][2]){
					wmode = 0;
					break;
				}
				/* fall */


			default:
defaul:
				if(std_from_str(argv[i], &cpp_std, NULL) == 0){
					/* we have an std */
				}else if(!strcmp(argv[i], "-trigraphs")){
					option_trigraphs = 1;
				}else if(!strcmp(argv[i], "-digraphs")){
					option_digraphs = 1;
				}else{
					fprintf(stderr, "unrecognised option \"%s\"\n", argv[i]);
					goto usage;
				}
		}
	}

	current_fname = FNAME_BUILTIN;

	macro_add("__STDC_HOSTED__",  freestanding ? "0" : "1", 0);
	switch(cpp_std){
		case STD_C89:
		case STD_C90:
			/* no */
			break;
		case STD_C99:
			macro_add("__STDC_VERSION__", "199901L", 0);
			break;
		case STD_C11:
			macro_add("__STDC_VERSION__", "201112L", 0);
	}

	if(i < argc){
		infname = argv[i++];
		if(i < argc){
			if(outfname)
				goto usage;
			outfname = argv[i++];
			if(i < argc)
				goto usage;
		}
	}

	calctime(infname);

#define CHECK_FILE(var, mode, target) \
	if(var && strcmp(var, "-")){ \
		if(!freopen(var, mode, target)){ \
			fprintf(stderr, "open: %s: ", var); \
			perror(NULL); \
			return 1; \
		} \
	}

	CHECK_FILE(outfname, "w", stdout)
	CHECK_FILE(infname,  "r", stdin)

	if(infname){
		dirname_push(udirname(infname));
	}else{
		infname = "<stdin>";
		dirname_push(ustrdup("."));
	}

	current_fname = infname;

	preprocess();

	if(wmode & WUNUSED)
		macros_warn_unused();

	switch(dump){
		case NONE:
			break;
		case MACROS:
		case MACROS_WHERE:
			macros_dump(dump == MACROS_WHERE);
			break;
		case STATS:
			macros_stats();
			break;
		case DEPS:
			deps_dump(infname);
			break;
	}

	free(dirname_pop());

	errno = 0;
	fclose(stdout);
	if(errno)
		die("close():");

	return ret;
usage:
	fprintf(stderr, "Usage: %s [options] files...\n", *argv);
	fputs(" Options:\n"
				"  -Idir: Add search directory\n"
				"  -Dxyz[=abc]: Define xyz (to equal abc)\n"
				"  -Uxyz: Undefine xyz\n"
				"  -o output: output file\n"
				"  -P: don't add #line directives\n"
				"  -dM: debug output\n"
				"  -dS: print macro usage stats\n"
				"  -MM: generate Makefile dependencies\n"
				"  -C: don't discard comments, except in macros\n"
				"  -CC: don't discard comments, even in macros\n"
				"  -trigraphs: enable trigraphs\n"
				"  -digraphs: enable digraphs\n"
				, stderr);

	{
		unsigned i;
		ITER_WARNS(i)
			fprintf(stderr, "  -W%s: %s\n", warns[i].warn, warns[i].desc);
	}

	return 1;
}