-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrcComplexity.cpp
38 lines (28 loc) · 930 Bytes
/
srcComplexity.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
/*
srcComplexity.cpp
Calculates the cyclomatic complexity of source code.
Source code must be in srcML format
*/
#include <iostream>
#include <string>
#include "srcMLXPathCount.hpp"
int main(int argc, const char* argv[]) {
// number of arguments is 0..1
if (argc > 2) {
std::cerr << "Usage: " << argv[0] << " <srcML_filename>" << '\n'
<< " " << argv[0] << '\n';
return 1;
}
// input is filename, or stdin as "-"
std::string filename = argc == 2 ? argv[1] : "-";
// use the number of conditions to calculate cyclomatic complexity
int conditionCount = srcMLXPathCount(argv[1], "count(//src:condition)");
if (conditionCount < 0) {
std::cerr << "Error in applying xpath\n";
return 1;
}
int complexity = conditionCount + 1;
// output the cyclomatic complexity
std::cout << complexity << '\n';
return 0;
}