Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit 4742857

Browse files
committed
Added utility function to get list's max/largest index
1 parent 89fec47 commit 4742857

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

cmake/Platform/Sketches/SketchPrototypesResolver.cmake

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
function(resolve_sketch_prototypes _sketch_file _resolved_sketch_headers _return_var)
1+
#=============================================================================#
2+
# Resolves the given sketch file's prototypes, which are just function declarations,
3+
# by matching all function definitions with their declaration. If a declaration can't be found,
4+
# the definition is added to a list of prototypes to generate, which is then returned.
5+
# _sketch_file - Path to the sketch file which its' libraries should be resolved.
6+
# _sketch_headers - List of headers files used by the sketch, directly or indirectly.
7+
# _return_var - Name of variable in parent-scope holding the return value.
8+
# Returns - List of prototypes to generate, i.e. function definitions without a matching declaration.
9+
#=============================================================================#
10+
function(resolve_sketch_prototypes _sketch_file _sketch_headers _return_var)
211

312
get_source_function_definitions(${_sketch_file} sketch_func_defines)
413
if (NOT sketch_func_defines) # Source has no function definitions at all
514
return()
615
endif ()
716

8-
list(APPEND _resolved_sketch_headers "${_sketch_file}")
17+
# Add the current file to the list of headers to search in as well - It's the functions' containing file
18+
list(APPEND _sketch_headers "${_sketch_file}")
919

1020
foreach (func_def ${sketch_func_defines})
1121

12-
match_function_declaration("${func_def}" "${_resolved_sketch_headers}" match)
22+
match_function_declaration("${func_def}" "${_sketch_headers}" match)
1323

1424
if (${match} MATCHES "NOTFOUND")
1525
# ToDo: Append signature to list of prototypes to create

cmake/Platform/Sources/FunctionDeclarationMatcher.cmake

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
1+
#=============================================================================#
2+
# Attempts to match a given function definition signature to its' declaration, searching in all given headers.
3+
# These headers are usually a list of all headers included by the function's file, recursively.
4+
# Given a match, the declaration is returned, otherwise - "NOTFOUND" string.
5+
# _definition_signature - String representing a full function signature, e.g. 'int main(int argc, char **argv)'
6+
# _included_headers - List of headers to search the declaration in.
7+
# Should include the function's containing file itself.
8+
# _return_var - Name of variable in parent-scope holding the return value.
9+
# Returns - Function's declaration signature if exists, otherwise "NOTFOUND".
10+
#=============================================================================#
211
function(match_function_declaration _definition_signature _included_headers _return_var)
312

413
# Get function name and list of argument-types
514
strip_function_signature("${_definition_signature}" original_stripped_function)
6-
7-
# ToDo: Consider writing a utility function
8-
list(LENGTH original_stripped_function orig_func_list_len)
9-
set(original_function_args_length ${orig_func_list_len})
10-
decrement_integer(original_function_args_length 1)
11-
1215
list(GET original_stripped_function 0 original_function_name)
16+
list_max_index("${original_stripped_function}" original_function_args_length)
1317

1418
foreach (included_header ${_included_headers})
1519

@@ -22,13 +26,8 @@ function(match_function_declaration _definition_signature _included_headers _ret
2226

2327
# Get function name and list of argument-types
2428
strip_function_signature("${line}" iterated_stripped_function)
25-
26-
# ToDo: Consider writing a utility function
27-
list(LENGTH iterated_stripped_function iter_func_list_len)
28-
set(iterated_function_args_length ${iter_func_list_len})
29-
decrement_integer(iterated_function_args_length 1)
30-
3129
list(GET iterated_stripped_function 0 iterated_function_name)
30+
list_max_index("${iterated_stripped_function}" iterated_function_args_length)
3231

3332
if ("${original_function_name}" STREQUAL "${iterated_function_name}")
3433
if (${orig_func_list_len} EQUAL ${iter_func_list_len})

cmake/Platform/Utilities/ListUtils.cmake

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
macro(list_replace _list _index _new_element)
88

99
list(REMOVE_AT ${_list} ${_index})
10-
1110
list(INSERT ${_list} ${_index} "${_new_element}")
1211

1312
endmacro()
@@ -25,3 +24,23 @@ macro(initialize_list _list)
2524
endif ()
2625

2726
endmacro()
27+
28+
#=============================================================================#
29+
# Gets the maximal index a given list can relate to, i.e. the largest index, zero-counted.
30+
# Usually it's just `length - 1`, but there are edge cases where special treatment must be applied.
31+
# _list - List to get its maximal index.
32+
# _return_var - Name of variable in parent-scope holding the return value.
33+
# Returns - Maximal index the list can relate to (usually `length - 1`).
34+
#=============================================================================#
35+
function(list_max_index _list _return_var)
36+
37+
list(LENGTH _list list_length)
38+
39+
set(index ${list_length})
40+
if (${index} GREATER 0)
41+
decrement_integer(${index} 1)
42+
endif ()
43+
44+
set(${_return_var} ${index} PARENT_SCOPE)
45+
46+
endfunction()

0 commit comments

Comments
 (0)