Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/include/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@
* The input/output table of forge_parse_args(); slots are indexed by
* key (filled inside the parse_args macro by the OPT / builtin_args
* macros with designated-initializer syntax):
* - `long_name == NULL` means the slot is unused (other fields are
* treated as 0);
* - `long_name == NULL` means the option has no long form; short
* options are selected by their character index independently;
* - when `takes_arg` is non-zero, the parsed argument is written to
* `arg` (pointing into argv, not written back; with `--jobs=8`, the
* '=' in argv is rewritten to '\0' as the split);
Expand All @@ -134,7 +134,7 @@
*/
typedef struct {
const char *long_name; /**< long option name (without --); NULL means */
/**< the slot is unused */
/**< the option has no long form */
int takes_arg; /**< whether the option takes an argument */
int flag_store; /**< number of hits (count/boolean for no-arg */
/**< options) */
Expand Down
2 changes: 1 addition & 1 deletion lib/src/build.c
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ static int needs_rebuild(const char *obj, const char *src, const char *cmd) {
goto out;
}
for (int i = 0; i < dc; i++) {
if (os_mtime_ms(deps[i]) > om) {
if (!os_path_is_file(deps[i]) || os_mtime_ms(deps[i]) > om) {
rebuild = 1;
break;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/src/strv.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ void strv_destroy(strv_t *strv) {
}
}
free(strv->strs);
strv->strs = NULL;
strv->count = 0;
strv->capacity = 0;
}

int strv_append(strv_t* strv, const char* str) {
Expand All @@ -62,7 +65,7 @@ int strv_append_non_copy(strv_t* strv, char* str) {

int strv_set(strv_t* strv, int idx, const char* str) {
if(strv == NULL || str == NULL) return -1;
if(idx < 0 || idx > strv->count) return -1;
if(idx < 0 || idx >= strv->count) return -1;
char* ptr = forge_strdup(str);
if(ptr == NULL) return -1;
free(strv->strs[idx]);
Expand Down
19 changes: 8 additions & 11 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,16 @@ static char *dup_n(const char *s, size_t n) {
return out;
}

/* If the line containing the key match has a '#' between its start
and the match, the match is inside a comment and is invalid (same
rule as conf_defines in forge.c: '#' starts a comment cut off at
the end of the line), preventing commented lines such as
`# compiler=MSVC` from silently taking effect as configuration. */
static int hash_before_key(const char *p, const char *buf) {
/* Keys must begin a line after optional indentation. Matches inside
comments, other key names, or values are not configuration keys. */
static int is_key_start(const char *p, const char *buf) {
const char *q = p;
while (q > buf && q[-1] != '\n')
q--;
for (; q < p; q++)
if (*q == '#')
return 1;
return 0;
if (*q != ' ' && *q != '\t')
return 0;
return 1;
}

static char *get_value(char *buf, const char *key) {
Expand All @@ -48,7 +45,7 @@ static char *get_value(char *buf, const char *key) {
if (klen == 0) return NULL;
char *p = buf;
while ((p = strstr(p, key)) != NULL) {
if (p[klen] == '=' && !hash_before_key(p, buf))
if (p[klen] == '=' && is_key_start(p, buf))
break;
p += klen;
}
Expand Down Expand Up @@ -100,4 +97,4 @@ char *get_compiler(char *buf) {
default:
return get_value(buf, "compiler");
}
}
}
10 changes: 9 additions & 1 deletion tests/edge/test_ed_flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,15 @@ int main(void) {
TEST(st != 0);
TEST(out_has(out, "not found") || out_has(out, "undefined"));

/* A deleted header must invalidate the cached object and report the
compiler error, rather than link a stale successful build. */
char *header = os_path_join(dir, "include/flow.h");
TEST(header && os_remove_r(header) == 0);
free(header);
TEST(edge_make(dir, "make", out, sizeof(out)) != 0);
TEST(out_has(out, "flow.h"));

free(dir);
PASS();
return 0;
}
}
15 changes: 15 additions & 0 deletions tests/sub/test_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
#include <string.h>

int main(void) {
/* Short options do not require a corresponding long option. */
opt_t opts[OPTS_COUNT] = {
['x'] = {.long_name = NULL},
['y'] = {.long_name = NULL},
['o'] = {.long_name = NULL, .takes_arg = 1},
};
char *args[] = {"test", "-x", "-xy", "-oattached", "-o", "split", NULL};
TEST(forge_parse_args(6, args, opts, OPTS_COUNT) == 6);
TEST(opts['x'].flag_store == 2);
TEST(opts['y'].flag_store == 1);
TEST(opts['o'].argc == 2);
TEST(strcmp(opts['o'].argv[0], "attached") == 0);
TEST(strcmp(opts['o'].argv[1], "split") == 0);
free(opts['o'].argv);

TEST(atoi("42") == 42);
TEST(atoi("-7") == -7);

Expand Down
41 changes: 41 additions & 0 deletions tests/test_parser.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Source src/parser.c
// Include include
// Include lib/include
#include <build.h>
#include "parser.h"

#include <stdlib.h>
#include <string.h>

int main(void) {
char conf[] = "other_compiler=MSVC\n"
"other_compiler_path=/missing/cc\n"
"note=compiler=CLANG\n"
"# compiler=MSVC\n"
" compiler=GCC # chosen compiler\r\n"
"\tcompiler_path=gcc\n";
char *value = get_compiler(conf);
TEST(value && strcmp(value, "GCC") == 0);
free(value);
value = get_compiler_path(conf);
TEST(value && strcmp(value, "gcc") == 0);
free(value);

char unrelated[] = "my_compiler=MSVC\nvalue=compiler=CLANG\n";
TEST(get_compiler(unrelated) == NULL);
TEST(get_compiler(NULL) == NULL);

char overrides[] = "compiler=GCC\ncompiler_linux=CLANG\n"
"compiler_windows=MSVC\ncompiler_mac_os=CLANG\n";
value = get_compiler(overrides);
#if defined(__linux__) || defined(__APPLE__)
TEST(value && strcmp(value, "CLANG") == 0);
#elif defined(_WIN32)
TEST(value && strcmp(value, "MSVC") == 0);
#else
TEST(value && strcmp(value, "GCC") == 0);
#endif
free(value);
PASS();
return 0;
}
32 changes: 32 additions & 0 deletions tests/test_strv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Source lib/src/strv.c
// Include lib/include
#include <build.h>
#include <forge_strv.h>

#include <string.h>

int main(void) {
strv_t v = {0};
TEST(strv_init(&v) == 0);
TEST(strv_append(&v, "first") == 0);
TEST(strv_set(&v, v.count, "outside") == -1);
TEST(v.strs[v.count] == NULL);
TEST(strv_set(&v, 0, "updated") == 0);
TEST(strcmp(strv_get(&v, 0), "updated") == 0);

strv_destroy(&v);
TEST(v.strs == NULL && v.count == 0 && v.capacity == 0);
strv_destroy(&v);

strv_t dest = {0};
TEST(strv_init(&dest) == 0);
TEST(strv_append(&dest, "old") == 0);
TEST(strv_copy(&dest, &v) == 0);
TEST(dest.strs == NULL && dest.count == 0 && dest.capacity == 0);
strv_destroy(&dest);
TEST(strv_init(&dest) == 0);
TEST(strv_append(&dest, "new") == 0);
strv_destroy(&dest);
PASS();
return 0;
}
Loading