Replace non-identifier characters with '_' when generating prefixes

This commit is contained in:
Bruce Hill 2024-06-13 22:56:45 -04:00
parent ce66358d06
commit 6f11f5cbb4
2 changed files with 19 additions and 9 deletions

10
tomo.c
View File

@ -182,6 +182,12 @@ int main(int argc, char *argv[])
// For shared objects, link up all the object files into one .so file:
if (mode == MODE_COMPILE_SHARED_OBJ) {
char *libname_id = heap_str(libname);
for (char *p = libname_id; *p; p++) {
if (!isalnum(*p) && *p != '_' && *p != '$')
*p = '_';
}
// Build a "libwhatever.h" header that loads all the headers:
const char *h_filename = heap_strf("lib%s.h", libname);
FILE *header_prog = CORD_RUN(autofmt ? autofmt : "cat", " 2>/dev/null >", h_filename);
@ -192,7 +198,7 @@ int main(int argc, char *argv[])
if (!f) errx(1, "No such file: %s", filename);
ast_t *ast = parse_file(f, NULL);
if (!ast) errx(1, "Could not parse %s", f);
env->file_prefix = heap_strf("%s$%s$", libname, file_base_name(filename));
env->file_prefix = heap_strf("%s$%s$", libname_id, file_base_name(filename));
for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) {
if (stmt->ast->tag == Import || (stmt->ast->tag == Declare && Match(stmt->ast, Declare)->value->tag == Import))
@ -218,7 +224,7 @@ int main(int argc, char *argv[])
unlink("symbol_renames.txt");
FILE *prog;
for (int i = after_flags; i < argc; i++) {
prog = CORD_RUN("nm -U -fjust-symbols ", argv[i], ".o | sed 's/.*/\\0 ", libname, "$\\0/' >>symbol_renames.txt");
prog = CORD_RUN("nm -U -fjust-symbols ", argv[i], ".o | sed 's/.*/\\0 ", libname_id, "$\\0/' >>symbol_renames.txt");
status = pclose(prog);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
errx(WEXITSTATUS(status), "Failed to create symbol rename table with `nm` and `sed`");

View File

@ -123,11 +123,11 @@ static env_t *load_module(env_t *env, ast_t *module_ast)
if (!ast) errx(1, "Could not compile!");
return load_module_env(env, heap_strf("%s$", name), ast);
} else if (module_ast->tag == Use) {
const char *name = Match(module_ast, Use)->name;
const char *files_filename = heap_strf("%s/lib%s.files", name, name);
const char *libname = Match(module_ast, Use)->name;
const char *files_filename = heap_strf("%s/lib%s.files", libname, libname);
const char *resolved_path = resolve_path(files_filename, module_ast->file->filename, getenv("TOMO_IMPORT_PATH"));
if (!resolved_path)
code_err(module_ast, "No such library exists: \"lib%s.files\"", name);
code_err(module_ast, "No such library exists: \"lib%s.files\"", libname);
file_t *files_f = load_file(resolved_path);
if (!files_f) errx(1, "Couldn't open file: %s", resolved_path);
@ -137,18 +137,22 @@ static env_t *load_module(env_t *env, ast_t *module_ast)
line = heap_strn(line, strcspn(line, "\r\n"));
if (!line || line[0] == '\0') continue;
const char *tm_path = resolve_path(line, resolved_path, ".");
if (!tm_path) errx(1, "Couldn't find library %s dependency: %s", name, line);
if (!tm_path) errx(1, "Couldn't find library %s dependency: %s", libname, line);
file_t *tm_f = load_file(tm_path);
if (!tm_f) errx(1, "No such file: %s", tm_path);
ast_t *ast = parse_file(tm_f, NULL);
if (!ast) errx(1, "Could not compile!");
const char *prefix = file_base_name(line);
env_t *tmp_env = fresh_scope(env);
tmp_env->file_prefix = heap_strf("%s$", name);
char *prefix = heap_strf("%s$%s$", libname, file_base_name(line));
for (char *p = prefix; *p; p++) {
if (!isalnum(*p) && *p != '_' && *p != '$')
*p = '_';
}
tmp_env->file_prefix = prefix;
tmp_env->imports = new(table_t);
env_t *subenv = load_module_env(tmp_env, heap_strf("%s$%s$", name, prefix), ast);
env_t *subenv = load_module_env(tmp_env, prefix, ast);
for (int64_t j = 0; j < subenv->locals->entries.length; j++) {
struct {
const char *name; binding_t *binding;