]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/tests_pass_valgrind.c
ccanlint: fix uninitialized variables in tests_pass_valgrind.
[ccan] / tools / ccanlint / tests / tests_pass_valgrind.c
index d248aaa85fea4c86782057b99ef286c7bf436bc9..bf914856845f2a7a42e9cec97d210c9b7c3ef06a 100644 (file)
@@ -5,6 +5,7 @@
 #include <ccan/foreach/foreach.h>
 #include <ccan/grab_file/grab_file.h>
 #include <ccan/str_talloc/str_talloc.h>
+#include "tests_pass.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <ctype.h>
 
 REGISTER_TEST(tests_pass_valgrind);
+REGISTER_TEST(tests_pass_valgrind_noleaks);
 
 /* Note: we already test safe_mode in run_tests.c */
 static const char *can_run_vg(struct manifest *m)
 {
-       unsigned int timeleft = default_timeout_ms;
-       char *output;
-
-       if (!run_command(m, &timeleft, &output,
-                        "valgrind -q --error-exitcode=0 true"))
-               return talloc_asprintf(m, "No valgrind support: %s", output);
+       if (!do_valgrind)
+               return talloc_asprintf(m, "No valgrind support");
        return NULL;
 }
 
@@ -148,55 +146,37 @@ static const char *concat(struct score *score, char *bits[])
 
 /* FIXME: Run examples, too! */
 static void do_run_tests_vg(struct manifest *m,
-                            bool keep,
+                           bool keep,
                            unsigned int *timeleft,
                            struct score *score)
 {
        struct ccan_file *i;
        struct list_head *list;
-       char *cmdout;
 
        /* This is slow, so we run once but grab leak info. */
        score->total = 0;
        score->pass = true;
        foreach_ptr(list, &m->run_tests, &m->api_tests) {
                list_for_each(list, i, list) {
-                       char *output, *err, *log;
-                       bool pass;
+                       char *err, *output;
                        const char *options;
 
                        score->total++;
                        options = concat(score,
                                         per_file_options(&tests_pass_valgrind,
                                                          i));
-                       if (streq(options, "FAIL"))
+                       if (streq(options, "FAIL")) {
+                               i->leak_info = NULL;
                                continue;
+                       }
+
+                       if (keep)
+                               talloc_set_destructor(i->valgrind_log, NULL);
 
-                       /* FIXME: Valgrind's output sucks.  XML is unreadable by
-                        * humans *and* doesn't support children reporting. */
-                       log = talloc_asprintf(score,
-                                             "%s.valgrind-log", i->compiled);
-                       if (!keep)
-                               talloc_set_destructor(log,
-                                                     unlink_file_destructor);
-
-                       pass = run_command(score, timeleft, &cmdout,
-                                        "valgrind -q --error-exitcode=101"
-                                          " --leak-check=full"
-                                          " --log-fd=3 %s %s"
-                                          " 3> %s",
-                                          options,
-                                          i->compiled, log);
-                       output = grab_file(i, log, NULL);
-                       /* No valgrind errors?  Expect it to pass... */
+                       output = grab_file(i, i->valgrind_log, NULL);
+                       /* No valgrind errors? */
                        if (!output || output[0] == '\0') {
-                               if (!pass) {
-                                       err = talloc_asprintf(score,
-                                                             "Test failed:\n"
-                                                             "%s",
-                                                             cmdout);
-                               } else
-                                       err = NULL;
+                               err = NULL;
                                i->leak_info = NULL;
                        } else {
                                i->leak_info = analyze_output(output, &err);
@@ -217,10 +197,22 @@ static void do_leakcheck_vg(struct manifest *m,
 {
        struct ccan_file *i;
        struct list_head *list;
+       char **options;
        bool leaks = false;
 
        foreach_ptr(list, &m->run_tests, &m->api_tests) {
                list_for_each(list, i, list) {
+                       options = per_file_options(&tests_pass_valgrind_noleaks,
+                                                  i);
+                       if (options[0]) {
+                               if (streq(options[0], "FAIL")) {
+                                       leaks = true;
+                                       continue;
+                               }
+                               errx(1, "Unknown leakcheck options '%s'",
+                                    options[0]);
+                       }
+
                        if (i->leak_info) {
                                score_file_error(score, i, 0, "%s",
                                                 i->leak_info);
@@ -256,7 +248,7 @@ static void run_under_debugger_vg(struct manifest *m, struct score *score)
                                  concat(score,
                                         per_file_options(&tests_pass_valgrind,
                                                          first->file)),
-                                 first->file->compiled);
+                                 first->file->compiled[COMPILE_NORMAL]);
        if (system(command))
                doesnt_matter();
 }
@@ -275,7 +267,7 @@ struct ccanlint tests_pass_valgrind_noleaks = {
        .key = "tests_pass_valgrind_noleaks",
        .name = "Module's run and api tests have no memory leaks",
        .check = do_leakcheck_vg,
+       .takes_options = true,
        .needs = "tests_pass_valgrind"
 };
 
-REGISTER_TEST(tests_pass_valgrind_noleaks);