Skip to content

Commit a43c7e6

Browse files
authored
C: Deprecate hb_array_size (#885)
This PR deprecates the `hb_array_size` function in favor of directly accessing `hb_array_T.size` As part of the deprecation, ast nodes now always have fully initialized array members.
1 parent 310b58e commit a43c7e6

File tree

23 files changed

+78
-74
lines changed

23 files changed

+78
-74
lines changed

ext/herb/extension_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ VALUE create_lex_result(hb_array_T* tokens, VALUE source) {
6363
VALUE warnings = rb_ary_new();
6464
VALUE errors = rb_ary_new();
6565

66-
for (size_t i = 0; i < hb_array_size(tokens); i++) {
66+
for (size_t i = 0; i < tokens->size; i++) {
6767
token_T* token = hb_array_get(tokens, i);
6868
if (token != NULL) { rb_ary_push(value, rb_token_from_c_struct(token)); }
6969
}

java/extension_helpers.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ jobject CreateLexResult(JNIEnv* env, hb_array_T* tokens, jstring source) {
6060
jmethodID arrayListConstructor = (*env)->GetMethodID(env, arrayListClass, "<init>", "(I)V");
6161
jmethodID addMethod = (*env)->GetMethodID(env, arrayListClass, "add", "(Ljava/lang/Object;)Z");
6262

63-
jobject tokensList = (*env)->NewObject(env, arrayListClass, arrayListConstructor, (jint) hb_array_size(tokens));
63+
jobject tokensList = (*env)->NewObject(env, arrayListClass, arrayListConstructor, (jint) tokens->size);
6464

65-
for (size_t i = 0; i < hb_array_size(tokens); i++) {
65+
for (size_t i = 0; i < tokens->size; i++) {
6666
token_T* token = (token_T*) hb_array_get(tokens, i);
6767
jobject tokenObj = CreateToken(env, token);
6868
(*env)->CallBooleanMethod(env, tokensList, addMethod, tokenObj);
@@ -85,7 +85,7 @@ jobject CreateParseResult(JNIEnv* env, AST_DOCUMENT_NODE_T* root, jstring source
8585
jobject errorsList = (*env)->NewObject(env, arrayListClass, arrayListConstructor);
8686

8787
if (root->base.errors) {
88-
for (size_t i = 0; i < hb_array_size(root->base.errors); i++) {
88+
for (size_t i = 0; i < root->base.errors->size; i++) {
8989
AST_NODE_T* error_node = (AST_NODE_T*) hb_array_get(root->base.errors, i);
9090
jobject errorObj = CreateErrorNode(env, error_node);
9191
(*env)->CallBooleanMethod(env, errorsList, addMethod, errorObj);

javascript/packages/node/extension/extension_helpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ napi_value CreateLexResult(napi_env env, hb_array_T* tokens, napi_value source)
153153

154154
// Add tokens to array
155155
if (tokens) {
156-
for (size_t i = 0; i < hb_array_size(tokens); i++) {
156+
for (size_t i = 0; i < tokens->size; i++) {
157157
token_T* token = (token_T*)hb_array_get(tokens, i);
158158
if (token) {
159159
napi_value token_obj = CreateToken(env, token);

src/analyze.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static AST_NODE_T* create_control_node(
287287

288288
if (end_node) {
289289
end_position = end_node->base.location.end;
290-
} else if (children && hb_array_size(children) > 0) {
290+
} else if (children && children->size > 0) {
291291
AST_NODE_T* last_child = hb_array_last(children);
292292
end_position = last_child->location.end;
293293
} else if (subsequent) {
@@ -329,7 +329,7 @@ static AST_NODE_T* create_control_node(
329329
hb_array_T* in_conditions = hb_array_init(8);
330330
hb_array_T* non_when_non_in_children = hb_array_init(8);
331331

332-
for (size_t i = 0; i < hb_array_size(children); i++) {
332+
for (size_t i = 0; i < children->size; i++) {
333333
AST_NODE_T* child = hb_array_get(children, i);
334334

335335
if (child && child->type == AST_ERB_WHEN_NODE) {
@@ -343,7 +343,7 @@ static AST_NODE_T* create_control_node(
343343

344344
hb_array_free(&children);
345345

346-
if (hb_array_size(in_conditions) > 0) {
346+
if (in_conditions->size > 0) {
347347
hb_array_free(&when_conditions);
348348

349349
return (AST_NODE_T*) ast_erb_case_match_node_init(
@@ -539,7 +539,7 @@ static size_t process_control_structure(
539539
hb_array_T* in_conditions = hb_array_init(8);
540540
hb_array_T* non_when_non_in_children = hb_array_init(8);
541541

542-
while (index < hb_array_size(array)) {
542+
while (index < array->size) {
543543
AST_NODE_T* next_node = hb_array_get(array, index);
544544

545545
if (!next_node) { break; }
@@ -555,7 +555,7 @@ static size_t process_control_structure(
555555
index++;
556556
}
557557

558-
while (index < hb_array_size(array)) {
558+
while (index < array->size) {
559559
AST_NODE_T* next_node = hb_array_get(array, index);
560560

561561
if (!next_node) { break; }
@@ -627,7 +627,7 @@ static size_t process_control_structure(
627627

628628
AST_ERB_ELSE_NODE_T* else_clause = NULL;
629629

630-
if (index < hb_array_size(array)) {
630+
if (index < array->size) {
631631
AST_NODE_T* next_node = hb_array_get(array, index);
632632

633633
if (next_node && next_node->type == AST_ERB_CONTENT_NODE) {
@@ -661,7 +661,7 @@ static size_t process_control_structure(
661661

662662
AST_ERB_END_NODE_T* end_node = NULL;
663663

664-
if (index < hb_array_size(array)) {
664+
if (index < array->size) {
665665
AST_NODE_T* potential_end = hb_array_get(array, index);
666666

667667
if (potential_end && potential_end->type == AST_ERB_CONTENT_NODE) {
@@ -694,15 +694,15 @@ static size_t process_control_structure(
694694
end_position = end_node->base.location.end;
695695
} else if (else_clause) {
696696
end_position = else_clause->base.location.end;
697-
} else if (hb_array_size(when_conditions) > 0) {
697+
} else if (when_conditions->size > 0) {
698698
AST_NODE_T* last_when = hb_array_last(when_conditions);
699699
end_position = last_when->location.end;
700-
} else if (hb_array_size(in_conditions) > 0) {
700+
} else if (in_conditions->size > 0) {
701701
AST_NODE_T* last_in = hb_array_last(in_conditions);
702702
end_position = last_in->location.end;
703703
}
704704

705-
if (hb_array_size(in_conditions) > 0) {
705+
if (in_conditions->size > 0) {
706706
hb_array_T* case_match_errors = erb_node->base.errors;
707707
erb_node->base.errors = NULL;
708708

@@ -760,7 +760,7 @@ static size_t process_control_structure(
760760
AST_ERB_ELSE_NODE_T* else_clause = NULL;
761761
AST_ERB_ENSURE_NODE_T* ensure_clause = NULL;
762762

763-
if (index < hb_array_size(array)) {
763+
if (index < array->size) {
764764
AST_NODE_T* next_node = hb_array_get(array, index);
765765

766766
if (next_node && next_node->type == AST_ERB_CONTENT_NODE) {
@@ -775,7 +775,7 @@ static size_t process_control_structure(
775775
}
776776
}
777777

778-
if (index < hb_array_size(array)) {
778+
if (index < array->size) {
779779
AST_NODE_T* next_node = hb_array_get(array, index);
780780

781781
if (next_node && next_node->type == AST_ERB_CONTENT_NODE) {
@@ -807,7 +807,7 @@ static size_t process_control_structure(
807807
}
808808
}
809809

810-
if (index < hb_array_size(array)) {
810+
if (index < array->size) {
811811
AST_NODE_T* next_node = hb_array_get(array, index);
812812

813813
if (next_node && next_node->type == AST_ERB_CONTENT_NODE) {
@@ -819,7 +819,7 @@ static size_t process_control_structure(
819819

820820
index++;
821821

822-
while (index < hb_array_size(array)) {
822+
while (index < array->size) {
823823
AST_NODE_T* child = hb_array_get(array, index);
824824

825825
if (!child) { break; }
@@ -855,7 +855,7 @@ static size_t process_control_structure(
855855

856856
AST_ERB_END_NODE_T* end_node = NULL;
857857

858-
if (index < hb_array_size(array)) {
858+
if (index < array->size) {
859859
AST_NODE_T* potential_end = hb_array_get(array, index);
860860

861861
if (potential_end && potential_end->type == AST_ERB_CONTENT_NODE) {
@@ -922,7 +922,7 @@ static size_t process_control_structure(
922922

923923
AST_ERB_END_NODE_T* end_node = NULL;
924924

925-
if (index < hb_array_size(array)) {
925+
if (index < array->size) {
926926
AST_NODE_T* potential_close = hb_array_get(array, index);
927927

928928
if (potential_close && potential_close->type == AST_ERB_CONTENT_NODE) {
@@ -954,7 +954,7 @@ static size_t process_control_structure(
954954

955955
if (end_node) {
956956
end_position = end_node->base.location.end;
957-
} else if (children && hb_array_size(children) > 0) {
957+
} else if (children && children->size > 0) {
958958
AST_NODE_T* last_child = hb_array_last(children);
959959
end_position = last_child->location.end;
960960
}
@@ -984,7 +984,7 @@ static size_t process_control_structure(
984984
AST_NODE_T* subsequent = NULL;
985985
AST_ERB_END_NODE_T* end_node = NULL;
986986

987-
if (index < hb_array_size(array)) {
987+
if (index < array->size) {
988988
AST_NODE_T* next_node = hb_array_get(array, index);
989989

990990
if (next_node && next_node->type == AST_ERB_CONTENT_NODE) {
@@ -997,7 +997,7 @@ static size_t process_control_structure(
997997
}
998998
}
999999

1000-
if (index < hb_array_size(array)) {
1000+
if (index < array->size) {
10011001
AST_NODE_T* potential_end = hb_array_get(array, index);
10021002

10031003
if (potential_end && potential_end->type == AST_ERB_CONTENT_NODE) {
@@ -1059,7 +1059,7 @@ static size_t process_subsequent_block(
10591059
hb_array_free(&children);
10601060
}
10611061

1062-
if (index < hb_array_size(array)) {
1062+
if (index < array->size) {
10631063
AST_NODE_T* next_node = hb_array_get(array, index);
10641064

10651065
if (next_node && next_node->type == AST_ERB_CONTENT_NODE) {
@@ -1117,7 +1117,7 @@ static size_t process_block_children(
11171117
analyze_ruby_context_T* context,
11181118
control_type_t parent_type
11191119
) {
1120-
while (index < hb_array_size(array)) {
1120+
while (index < array->size) {
11211121
AST_NODE_T* child = hb_array_get(array, index);
11221122

11231123
if (!child) { break; }
@@ -1139,7 +1139,7 @@ static size_t process_block_children(
11391139
hb_array_T* temp_array = hb_array_init(1);
11401140
size_t new_index = process_control_structure(node, array, index, temp_array, context, child_type);
11411141

1142-
if (hb_array_size(temp_array) > 0) { hb_array_append(children_array, hb_array_first(temp_array)); }
1142+
if (temp_array->size > 0) { hb_array_append(children_array, hb_array_first(temp_array)); }
11431143

11441144
hb_array_free(&temp_array);
11451145

@@ -1155,10 +1155,10 @@ static size_t process_block_children(
11551155
}
11561156

11571157
hb_array_T* rewrite_node_array(AST_NODE_T* node, hb_array_T* array, analyze_ruby_context_T* context) {
1158-
hb_array_T* new_array = hb_array_init(hb_array_size(array));
1158+
hb_array_T* new_array = hb_array_init(array->size);
11591159
size_t index = 0;
11601160

1161-
while (index < hb_array_size(array)) {
1161+
while (index < array->size) {
11621162
AST_NODE_T* item = hb_array_get(array, index);
11631163

11641164
if (!item) { break; }
@@ -1293,7 +1293,7 @@ static bool detect_invalid_erb_structures(const AST_NODE_T* node, void* data) {
12931293
if (if_node->end_node == NULL) { check_erb_node_for_missing_end(node); }
12941294

12951295
if (if_node->statements != NULL) {
1296-
for (size_t i = 0; i < hb_array_size(if_node->statements); i++) {
1296+
for (size_t i = 0; i < if_node->statements->size; i++) {
12971297
AST_NODE_T* statement = (AST_NODE_T*) hb_array_get(if_node->statements, i);
12981298

12991299
if (statement != NULL) { herb_visit_node(statement, detect_invalid_erb_structures, context); }
@@ -1325,7 +1325,7 @@ static bool detect_invalid_erb_structures(const AST_NODE_T* node, void* data) {
13251325
const AST_ERB_IF_NODE_T* elsif_node = (const AST_ERB_IF_NODE_T*) subsequent;
13261326

13271327
if (elsif_node->statements != NULL) {
1328-
for (size_t i = 0; i < hb_array_size(elsif_node->statements); i++) {
1328+
for (size_t i = 0; i < elsif_node->statements->size; i++) {
13291329
AST_NODE_T* statement = (AST_NODE_T*) hb_array_get(elsif_node->statements, i);
13301330

13311331
if (statement != NULL) { herb_visit_node(statement, detect_invalid_erb_structures, context); }
@@ -1337,7 +1337,7 @@ static bool detect_invalid_erb_structures(const AST_NODE_T* node, void* data) {
13371337
const AST_ERB_ELSE_NODE_T* else_node = (const AST_ERB_ELSE_NODE_T*) subsequent;
13381338

13391339
if (else_node->statements != NULL) {
1340-
for (size_t i = 0; i < hb_array_size(else_node->statements); i++) {
1340+
for (size_t i = 0; i < else_node->statements->size; i++) {
13411341
AST_NODE_T* statement = (AST_NODE_T*) hb_array_get(else_node->statements, i);
13421342

13431343
if (statement != NULL) { herb_visit_node(statement, detect_invalid_erb_structures, context); }

src/ast_node.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ ast_node_type_T ast_node_type(const AST_NODE_T* node) {
4343
}
4444

4545
size_t ast_node_errors_count(const AST_NODE_T* node) {
46-
return hb_array_size(node->errors);
46+
return node->errors->size;
4747
}
4848

4949
hb_array_T* ast_node_errors(const AST_NODE_T* node) {

src/extract.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void herb_extract_ruby_to_buffer(const char* source, hb_buffer_T* output) {
1212
bool skip_erb_content = false;
1313
bool is_comment_tag = false;
1414

15-
for (size_t i = 0; i < hb_array_size(tokens); i++) {
15+
for (size_t i = 0; i < tokens->size; i++) {
1616
const token_T* token = hb_array_get(tokens, i);
1717

1818
switch (token->type) {
@@ -94,7 +94,7 @@ void herb_extract_ruby_to_buffer(const char* source, hb_buffer_T* output) {
9494
void herb_extract_html_to_buffer(const char* source, hb_buffer_T* output) {
9595
hb_array_T* tokens = herb_lex(source);
9696

97-
for (size_t i = 0; i < hb_array_size(tokens); i++) {
97+
for (size_t i = 0; i < tokens->size; i++) {
9898
const token_T* token = hb_array_get(tokens, i);
9999

100100
switch (token->type) {

src/herb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ HERB_EXPORTED_FUNCTION hb_array_T* herb_lex_file(const char* path) {
5858
HERB_EXPORTED_FUNCTION void herb_lex_to_buffer(const char* source, hb_buffer_T* output) {
5959
hb_array_T* tokens = herb_lex(source);
6060

61-
for (size_t i = 0; i < hb_array_size(tokens); i++) {
61+
for (size_t i = 0; i < tokens->size; i++) {
6262
token_T* token = hb_array_get(tokens, i);
6363

6464
hb_string_T type = token_to_string(token);
@@ -74,7 +74,7 @@ HERB_EXPORTED_FUNCTION void herb_lex_to_buffer(const char* source, hb_buffer_T*
7474
HERB_EXPORTED_FUNCTION void herb_free_tokens(hb_array_T** tokens) {
7575
if (!tokens || !*tokens) { return; }
7676

77-
for (size_t i = 0; i < hb_array_size(*tokens); i++) {
77+
for (size_t i = 0; i < (*tokens)->size; i++) {
7878
token_T* token = hb_array_get(*tokens, i);
7979
if (token) { token_free(token); }
8080
}

src/parser.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ static void parser_parse_in_data_state(parser_T* parser, hb_array_T* children, h
11421142
static size_t find_matching_close_tag(hb_array_T* nodes, size_t start_idx, hb_string_T tag_name) {
11431143
int depth = 0;
11441144

1145-
for (size_t i = start_idx + 1; i < hb_array_size(nodes); i++) {
1145+
for (size_t i = start_idx + 1; i < nodes->size; i++) {
11461146
AST_NODE_T* node = (AST_NODE_T*) hb_array_get(nodes, i);
11471147
if (node == NULL) { continue; }
11481148

@@ -1166,9 +1166,9 @@ static size_t find_matching_close_tag(hb_array_T* nodes, size_t start_idx, hb_st
11661166
static hb_array_T* parser_build_elements_from_tags(hb_array_T* nodes, hb_array_T* errors);
11671167

11681168
static hb_array_T* parser_build_elements_from_tags(hb_array_T* nodes, hb_array_T* errors) {
1169-
hb_array_T* result = hb_array_init(hb_array_size(nodes));
1169+
hb_array_T* result = hb_array_init(nodes->size);
11701170

1171-
for (size_t index = 0; index < hb_array_size(nodes); index++) {
1171+
for (size_t index = 0; index < nodes->size; index++) {
11721172
AST_NODE_T* node = (AST_NODE_T*) hb_array_get(nodes, index);
11731173
if (node == NULL) { continue; }
11741174

@@ -1179,7 +1179,7 @@ static hb_array_T* parser_build_elements_from_tags(hb_array_T* nodes, hb_array_T
11791179
size_t close_index = find_matching_close_tag(nodes, index, tag_name);
11801180

11811181
if (close_index == (size_t) -1) {
1182-
if (hb_array_size(open_tag->base.errors) == 0) {
1182+
if (open_tag->base.errors->size == 0) {
11831183
append_missing_closing_tag_error(
11841184
open_tag->tag_name,
11851185
open_tag->base.location.start,
@@ -1223,7 +1223,7 @@ static hb_array_T* parser_build_elements_from_tags(hb_array_T* nodes, hb_array_T
12231223
AST_HTML_CLOSE_TAG_NODE_T* close_tag = (AST_HTML_CLOSE_TAG_NODE_T*) node;
12241224

12251225
if (!is_void_element(hb_string(close_tag->tag_name->value))) {
1226-
if (hb_array_size(close_tag->base.errors) == 0) {
1226+
if (close_tag->base.errors->size == 0) {
12271227
append_missing_opening_tag_error(
12281228
close_tag->tag_name,
12291229
close_tag->base.location.start,
@@ -1297,7 +1297,7 @@ void herb_parser_deinit(parser_T* parser) {
12971297
}
12981298

12991299
void match_tags_in_node_array(hb_array_T* nodes, hb_array_T* errors) {
1300-
if (nodes == NULL || hb_array_size(nodes) == 0) { return; }
1300+
if (nodes == NULL || nodes->size == 0) { return; }
13011301

13021302
hb_array_T* processed = parser_build_elements_from_tags(nodes, errors);
13031303

src/parser_helpers.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void parser_push_open_tag(const parser_T* parser, token_T* tag_name) {
1919
}
2020

2121
bool parser_check_matching_tag(const parser_T* parser, hb_string_T tag_name) {
22-
if (hb_array_size(parser->open_tags_stack) == 0) { return false; }
22+
if (parser->open_tags_stack->size == 0) { return false; }
2323

2424
token_T* top_token = hb_array_last(parser->open_tags_stack);
2525
if (top_token == NULL || top_token->value == NULL) { return false; };
@@ -28,7 +28,7 @@ bool parser_check_matching_tag(const parser_T* parser, hb_string_T tag_name) {
2828
}
2929

3030
token_T* parser_pop_open_tag(const parser_T* parser) {
31-
if (hb_array_size(parser->open_tags_stack) == 0) { return NULL; }
31+
if (parser->open_tags_stack->size == 0) { return NULL; }
3232

3333
return hb_array_pop(parser->open_tags_stack);
3434
}
@@ -42,7 +42,7 @@ token_T* parser_pop_open_tag(const parser_T* parser) {
4242
bool parser_in_svg_context(const parser_T* parser) {
4343
if (!parser || !parser->open_tags_stack) { return false; }
4444

45-
size_t stack_size = hb_array_size(parser->open_tags_stack);
45+
size_t stack_size = parser->open_tags_stack->size;
4646

4747
for (size_t i = 0; i < stack_size; i++) {
4848
token_T* tag = (token_T*) hb_array_get(parser->open_tags_stack, i);
@@ -191,7 +191,7 @@ void parser_handle_mismatched_tags(
191191
const AST_HTML_CLOSE_TAG_NODE_T* close_tag,
192192
hb_array_T* errors
193193
) {
194-
if (hb_array_size(parser->open_tags_stack) > 0) {
194+
if (parser->open_tags_stack->size > 0) {
195195
token_T* expected_tag = hb_array_last(parser->open_tags_stack);
196196
token_T* actual_tag = close_tag->tag_name;
197197

0 commit comments

Comments
 (0)