Skip to content

JSONGenerator.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "JSONGenerator.hpp"

void JSONGenerator::generate(const Entity& root) {
  /* JSON header */
  std::cout << "{" << std::endl;
  std::cout << "  \"format_version:\": \"2\"," << std::endl;
  std::cout << "  \"gcovr/format_version\": \"0.6\"," << std::endl;
  std::cout << "  \"files\": [" << std::endl;

  /* JSON body */
  generate(root, 0);

  /* JSON footer */
  std::cout << std::endl;
  std::cout << "  ]" << std::endl;
  std::cout << "}" << std::endl;
}

int JSONGenerator::generate(const Entity& root, int nfiles) {
  for (auto& dir : root.dirs) {
    nfiles = generate(dir, nfiles);
  }
  for (auto& file : root.files) {
    if (nfiles > 0) {
      std::cout << "," << std::endl;
    }      
    std::cout << "    {" << std::endl;
    std::cout << "      \"file\": \"" << file.name << "\"," << std::endl;
    std::cout << "      \"functions\": []," << std::endl;
    std::cout << "      \"lines\": [" << std::endl;

    /* line information; note lines are stored zero-based, but must be
      * output 1-based */
    uint32_t nlines = 0;
    for (uint32_t line = 0; line < file.line_counts.size(); ++line) {
      if (file.line_counts[line] >= 0) {
        if (nlines > 0) {
          std::cout << "," << std::endl;
        }
        ++nlines;
        std::cout << "        {" << std::endl;
        std::cout << "          \"line_number\": " << (line + 1) << "," << std::endl;
        std::cout << "          \"branches\": []," << std::endl;
        std::cout << "          \"count\": " << file.line_counts[line] << std::endl;
        std::cout << "        }";
      }
    }
    std::cout << std::endl;
    std::cout << "      ]" << std::endl;
    std::cout << "    }";
    ++nfiles;
  }
  return nfiles;
}