Solved test38

This commit is contained in:
Lucian Popescu 2021-03-24 10:18:38 +02:00
parent aecef48272
commit da30ee17ab
10 changed files with 40 additions and 16 deletions

View File

@ -16,7 +16,7 @@ LIBFLAGS=$(foreach TMP,$(LIBPATHS),-L$(TMP))
build: $(SOURCES) $(OUTPUT)
coding_style:
./$(CODING_STYLE_CHECKER) $(SOURCES) $(INCPATHS)/*
./$(CODING_STYLE_CHECKER)
$(OUTPUT): $(OBJECTS)
$(CC) $(LIBFLAGS) $(OBJECTS) $(LDFLAGS) -o $@

View File

@ -148,7 +148,7 @@ free_and_exit:
free(_line);
free(line_copy);
free(exp_line);
return NULL;
}
char *_get_expanded_token(string_map_t def_map, char *token)
@ -181,9 +181,7 @@ char *_get_expanded_token(string_map_t def_map, char *token)
return exp_copy;
free_and_exit:
free(exp);
free(exp_copy);
return NULL;
}
@ -192,6 +190,8 @@ int _concat_delim_to_line(char *delim, int delim_size, char **line)
char *_delim;
char ret;
_delim = NULL;
_delim = calloc(delim_size + 1, sizeof(char));
if (!_delim)
return PP_FAILED;

View File

@ -207,7 +207,10 @@ void hashmap_free(string_map_t map)
m = (struct hashmap_map *) map;
for (i = 0; i < m->max_size; i++) {
if (!m)
return;
for (i = 0; m->data && i < m->max_size; i++) {
free(m->data[i].key);
free(m->data[i].val);
}

View File

@ -148,7 +148,8 @@ struct args *_create_struct_args(void)
return ret;
free_and_exit:
free_str_dyn_arr(ret->inc_dirs);
if (ret)
free_str_dyn_arr(ret->inc_dirs);
free(ret);
return NULL;
}

View File

@ -37,6 +37,7 @@ int expand_if(string_map_t def_map, const struct str_dyn_arr *inc_dirs, FILE *so
if (IS_IF_LINE(line) || IS_ELIF_LINE(line)) {
exp_line = get_expanded_line(def_map, line);
if (!exp_line)
return PP_FAILED;

View File

@ -30,12 +30,16 @@ int expand_ifdef(string_map_t def_map, const struct str_dyn_arr *inc_dirs, FILE
if (ret == -1)
return PP_FAILED;
if_en = 0;
continue;
}
if (if_en || else_en) {
ret = line_handler(line, def_map, inc_dirs, source, out);
if (ret == PP_FAILED)
return ret;
continue;
}
if (IS_IFDEF_LINE(line)) {
@ -63,10 +67,13 @@ int expand_ifdef(string_map_t def_map, const struct str_dyn_arr *inc_dirs, FILE
}
free(exp_line);
continue;
}
if (IS_IFNDEF_LINE(line)) {
exp_line = get_expanded_line(def_map, line);
if (!exp_line)
return PP_FAILED;
@ -76,6 +83,7 @@ int expand_ifdef(string_map_t def_map, const struct str_dyn_arr *inc_dirs, FILE
return PP_FAILED;
fgets(line, PP_LINE_LEN, source);
if (IS_ELSE_LINE(line)) {
else_en = 1;
} else {
@ -90,8 +98,9 @@ int expand_ifdef(string_map_t def_map, const struct str_dyn_arr *inc_dirs, FILE
}
free(exp_line);
}
continue;
}
}

View File

@ -25,6 +25,9 @@ int expand_inc(string_map_t def_map, const struct str_dyn_arr *inc_dirs, FILE *s
inc_filename = _get_filename_from_inc(line);
full_inc_filename = l_strdup("_test/inputs/");
if (!full_inc_filename)
return PP_FAILED;
a_strcat(&full_inc_filename, inc_filename);
ret = _compute_inc_file(def_map, inc_dirs, full_inc_filename, out);
@ -58,7 +61,7 @@ char *_get_filename_from_inc(char *inc)
filename = inc;
filename += INC_LINE_START_SZ + 1;
filename += INC_LINE_START_SZ + 1;
filename += 1;
filename_len = strlen(filename);
@ -76,7 +79,7 @@ int _compute_inc_file(string_map_t def_map, const struct str_dyn_arr *inc_dirs,
inc_file = fopen(inc_filename, "r");
if (inc_file) {
ret = pp_compute(def_map, inc_dirs, inc_file, out);
ret = pp_compute(def_map, inc_dirs, inc_file, out);
fclose(inc_file);
return ret == PP_OK ? PP_OK : PP_FAILED;

View File

@ -1,5 +1,4 @@
/* SPDX-License-Identifier: BSD-2 */
#include "hashmap.h"
#include "pp.h"
#include "pp_defines.h"
@ -7,6 +6,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(int argc, char **argv)
{
@ -17,7 +17,6 @@ int main(int argc, char **argv)
int ret;
define_map = NULL;
args = NULL;
source = NULL;
@ -27,7 +26,7 @@ int main(int argc, char **argv)
args = pp_parse_args(argc, argv, define_map);
if (!args) {
if (errno)
if (errno)
ret = errno;
else
ret = PP_FAILED;
@ -55,16 +54,23 @@ int main(int argc, char **argv)
}
ret = pp_compute(define_map, args->inc_dirs, source, out);
if (ret == PP_FAILED)
if (ret == PP_FAILED && errno == ENOMEM) {
ret = ENOMEM;
goto free_and_exit;
}
if (ret == PP_OK && errno == ENOENT) {
ret = PP_OK;
goto free_and_exit;
}
if (ret == PP_FAILED)
goto free_and_exit;
ret = errno;
free_and_exit:
hashmap_free(define_map);
pp_free_args(args);

View File

@ -24,8 +24,9 @@ struct str_dyn_arr *init_str_dyn_arr(int size)
return a;
free_and_exit:
if (a)
free(a->data);
free(a);
free(a->data);
return NULL;
}

View File

@ -22,7 +22,7 @@ int write_line_to_file(FILE *f, char *line)
char *l_strdup(char *s)
{
int size;
int size;
char *p;
if (!s)