src/parser_shared_ast.h

filter: Statementstmt 58.6% 34 / 58 Branchbran 72.7% 16 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 62.5% 50 / 80 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/vm_runtime.h ↑ Summary src/validation.h »
1 /*
2 * Shared parser AST helpers.
3 *
4 * Responsibility: small AST node constructors and sorted hash-key helpers
5 * used by parser/IR/schema code paths. These are not graphqljs compatibility
6 * transforms by themselves; they are common utilities that remain in use even
7 * as compatibility layers shrink.
8 */
9  
10 static HV *
11 0 gql_parser_new_node_hv_sized(const char *kind, I32 keys) {
12 0 HV *hv = newHV();
13 0 ▶ if (keys > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys > 1) { 0 0 0
14 0 hv_ksplit(hv, keys);
15 }
16 0 gql_store_sv(hv, "kind", newSVpv(kind, 0));
17 0 return hv;
18 }
19  
20 static HV *
21 0 gql_parser_new_node_hv(const char *kind) {
22 0 return gql_parser_new_node_hv_sized(kind, 1);
23 }
24  
25 static SV *
26 0 gql_parser_new_node_ref(const char *kind) {
27 0 return newRV_noinc((SV *)gql_parser_new_node_hv(kind));
28 }
29  
30 static SV *
31 0 gql_parser_new_name_node_sv(pTHX_ SV *value_sv) {
32 0 HV *hv = gql_parser_new_node_hv_sized("Name", 2);
33 0 gql_store_sv(hv, "value", SvREFCNT_inc_simple_NN(value_sv));
34 0 return newRV_noinc((SV *)hv);
35 }
36  
37 static SV *
38 0 gql_parser_new_named_type_node_sv(pTHX_ SV *value_sv) {
39 0 HV *hv = gql_parser_new_node_hv_sized("NamedType", 2);
40 0 hv_stores(hv, "name", gql_parser_new_name_node_sv(aTHX_ value_sv));
41 0 return newRV_noinc((SV *)hv);
42 }
43  
44 static SV *
45 0 gql_parser_new_variable_node_sv(pTHX_ SV *value_sv) {
46 0 HV *hv = gql_parser_new_node_hv_sized("Variable", 2);
47 0 hv_stores(hv, "name", gql_parser_new_name_node_sv(aTHX_ value_sv));
48 0 return newRV_noinc((SV *)hv);
49 }
50  
51 static int
52 210155 gql_parser_cmp_sv_ptrs(const void *a, const void *b) {
53 210155 SV *const *left = (SV *const *)a;
54 210155 SV *const *right = (SV *const *)b;
55 STRLEN left_len, right_len;
56 210155 const char *left_str = SvPV(*left, left_len);
57 210155 ▶ const char *right_str = SvPV(*right, right_len);
Branch: 1 branch
BranchTrueFalseTotal
const char *right_str = SvPV(*right, right_len); 210155 0 210155
58 210155 STRLEN min_len = left_len < right_len ? left_len : right_len;
59 210155 int cmp = memcmp(left_str, right_str, min_len);
60 210155 ▶ if (cmp != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cmp != 0) { 205572 4583 210155
61 205572 return cmp;
62 }
63 4583 ▶ if (left_len < right_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (left_len < right_len) { 2291 2292 4583
64 2291 return -1;
65 }
66 2292 ▶ if (left_len > right_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (left_len > right_len) { 2292 0 2292
67 2292 return 1;
68 }
69 0 return 0;
70 }
71  
72 static SV **
73 35243 gql_parser_sorted_hash_keys(pTHX_ HV *hv, I32 *count_out) {
74 I32 count;
75 35243 I32 i = 0;
76 HE *he;
77 SV **keys;
78  
79 35243 *count_out = 0;
80 35243 ▶ if (!hv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!hv) { 0 35243 35243
81 0 return NULL;
82 }
83  
84 35243 count = hv_iterinit(hv);
85 35243 ▶ if (count <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count <= 0) { 10404 24839 35243
86 10404 return NULL;
87 }
88  
89 24839 Newxz(keys, count, SV *);
90 24839 hv_iterinit(hv);
91 131656 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 106817 24839 131656
92 106817 keys[i++] = newSVsv(hv_iterkeysv(he));
93 }
94 24839 qsort(keys, count, sizeof(SV *), gql_parser_cmp_sv_ptrs);
95 24839 *count_out = count;
96 24839 return keys;
97 }
98  
99 static void
100 24842 gql_parser_free_sorted_hash_keys(SV **keys, I32 count) {
101 I32 i;
102 24842 ▶ if (!keys) {
Branch: 1 branch
BranchTrueFalseTotal
if (!keys) { 3 24839 24842
103 3 return;
104 }
105 131656 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 106817 24839 131656
106 106817 ▶ if (keys[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys[i]) { 106817 0 106817
107 106817 SvREFCNT_dec(keys[i]);
108 }
109 }
110 24839 Safefree(keys);
111 }
« src/vm_runtime.h ↑ Summary src/validation.h »