Skip to content

Commit

Permalink
Introduce "jou --valgrind"
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Dec 3, 2023
1 parent 602bfd9 commit c15b232
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
8 changes: 7 additions & 1 deletion runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ function run_test()
# We don't do this for all files, because I like relative paths in error messages.
command="cd $(dirname $joufile) && $(printf "$command_template" $(basename $joufile))"
else
command="$(printf "$command_template" $joufile)"
# For non-aoc files we can valgrind the compiled Jou executables.
# Aoc solutions can be really slow --> valgrind only the compilation.
if [ $valgrind = yes ]; then
command="$(printf "$command_template" "--valgrind $joufile")"
else
command="$(printf "$command_template" $joufile)"
fi
fi

local diffpath
Expand Down
3 changes: 2 additions & 1 deletion src/jou_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ typedef struct CfInstruction CfInstruction;
extern struct CommandLineArgs {
const char *argv0; // Program name
int verbosity; // How much debug/progress info to print, how many times -v/--verbose passed
bool valgrind; // true --> Use valgrind when runnning user's jou program
bool tokenize_only; // If true, tokenize the file passed on command line and don't actually compile anything
bool parse_only; // If true, parse the file passed on command line and don't actually compile anything
int optlevel; // Optimization level (0 don't optimize, 3 optimize a lot)
Expand Down Expand Up @@ -620,7 +621,7 @@ LLVMModuleRef codegen(const CfGraphFile *cfgfile, const FileTypes *ft);
char *compile_to_object_file(LLVMModuleRef module);
char *get_default_exe_path(void);
void run_linker(const char *const *objpaths, const char *exepath);
int run_exe(const char *exepath);
int run_exe(const char *exepath, bool valgrind);

/*
Use these to clean up return values of compiling functions.
Expand Down
6 changes: 5 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static const char help_fmt[] =
" -O0/-O1/-O2/-O3 set optimization level (0 = default, 3 = runs fastest)\n"
" -v / --verbose display some progress information\n"
" -vv display a lot of information about all compilation steps\n"
" --valgrind use valgrind when running the code\n"
" --tokenize-only display only the output of the tokenizer, don't do anything else\n"
" --parse-only display only the AST (parse tree), don't do anything else\n"
" --linker-flags appended to the linker command, so you can use external libraries\n"
Expand Down Expand Up @@ -88,6 +89,9 @@ void parse_arguments(int argc, char **argv)
} else if (strncmp(argv[i], "-v", 2) == 0 && strspn(argv[i] + 1, "v") == strlen(argv[i])-1) {
command_line_args.verbosity += strlen(argv[i]) - 1;
i++;
} else if (!strcmp(argv[i], "--valgrind")) {
command_line_args.valgrind = true;
i++;
} else if (!strcmp(argv[i], "--tokenize-only")) {
if (argc > 3) {
fprintf(stderr, "%s: --tokenize-only cannot be used together with other flags", argv[0]);
Expand Down Expand Up @@ -490,7 +494,7 @@ int main(int argc, char **argv)
if (!command_line_args.outfile) {
if(command_line_args.verbosity >= 1)
printf("Run: %s\n", exepath);
ret = run_exe(exepath);
ret = run_exe(exepath, command_line_args.valgrind);
}

free(exepath);
Expand Down
9 changes: 6 additions & 3 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,19 @@ char *compile_to_object_file(LLVMModuleRef module)
return path;
}

int run_exe(const char *exepath)
int run_exe(const char *exepath, bool valgrind)
{
char *command = malloc(strlen(exepath) + 50);
char *command = malloc(strlen(exepath) + 1000);
#ifdef _WIN32
sprintf(command, "\"%s\"", exepath);
char *p;
while ((p = strchr(command, '/')))
*p = '\\';
#else
sprintf(command, "'%s'", exepath);
if (valgrind)
sprintf(command, "valgrind -q --leak-check=full --show-leak-kinds=all --error-exitcode=1 '%s'", exepath);
else
sprintf(command, "'%s'", exepath);
#endif

// Make sure that everything else shows up before the user's prints.
Expand Down

0 comments on commit c15b232

Please sign in to comment.