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

Commit 2522892

Browse files
committed
Added function definition-declaration matching
1 parent afde447 commit 2522892

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
function(match_function_declaration _definition_signature _included_headers _return_var)
3+
4+
# Get function name and list of argument-types
5+
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+
12+
list(GET original_stripped_function 0 original_function_name)
13+
14+
foreach (included_header ${_included_headers})
15+
16+
file(STRINGS "${included_header}" header_lines)
17+
18+
foreach (line ${header_lines})
19+
20+
# Search for function declarations
21+
if ("${line}" MATCHES "${ARDUINO_CMAKE_FUNCTION_DECLARATION_REGEX_PATTERN}")
22+
23+
# Get function name and list of argument-types
24+
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+
31+
list(GET iterated_stripped_function 0 iterated_function_name)
32+
33+
if ("${original_function_name}" STREQUAL "${iterated_function_name}")
34+
if (${orig_func_list_len} EQUAL ${iter_func_list_len})
35+
36+
set(arg_types_match TRUE)
37+
38+
if (${iterated_function_args_length} GREATER 0)
39+
foreach (arg_type_index RANGE 1 ${iterated_function_args_length})
40+
41+
list(GET original_stripped_function ${arg_type_index} orig_arg_type)
42+
list(GET iterated_stripped_function ${arg_type_index} iter_arg_type)
43+
44+
if (NOT ${orig_arg_type} EQUAL ${iter_arg_type})
45+
set(arg_types_match FALSE)
46+
break()
47+
endif ()
48+
49+
endforeach ()
50+
endif ()
51+
52+
if (${arg_types_match}) # Signature has been matched
53+
set(${_return_var} ${line} PARENT_SCOPE)
54+
return()
55+
endif ()
56+
57+
endif ()
58+
endif ()
59+
60+
endif ()
61+
62+
endforeach ()
63+
64+
endforeach ()
65+
66+
set(${_return_var} "NOTFOUND" PARENT_SCOPE)
67+
68+
endfunction()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#=============================================================================#
2+
# Gets the types of a function's arguments as a list.
3+
# _signature - String representing a full function signature, e.g. 'int main(int argc, char **argv)'
4+
# _return_var - Name of variable in parent-scope holding the return value.
5+
# Returns - List of types, matching the given function-argument string.
6+
#=============================================================================#
7+
function(_get_function_arguments_types _signature _return_var)
8+
9+
string(REGEX MATCH ${ARDUINO_CMAKE_FUNCTION_ARGS_REGEX_PATTERN} function_args_string "${_signature}")
10+
string(REGEX MATCHALL ${ARDUINO_CMAKE_FUNCTION_SINGLE_ARG_REGEX_PATTERN}
11+
function_arg_list "${_function_args_string}")
12+
13+
# Iterate through all arguments to extract only their type
14+
foreach (arg ${function_arg_list})
15+
string(REGEX MATCH ${ARDUINO_CMAKE_FUNCTION_ARG_TYPE_REGEX_PATTERN} arg_type "${arg}")
16+
string(STRIP "${arg_type}" arg_type) # Strip remaining whitespaces
17+
list(APPEND function_args ${arg_type})
18+
endforeach ()
19+
20+
set(${_return_var} ${function_args} PARENT_SCOPE)
21+
22+
endfunction()
23+
24+
#=============================================================================#
25+
# Strips a given function signature to its name and its arguments' types.
26+
# _signature - String representing a full function signature, e.g. 'int main(int argc, char **argv)'
27+
# _return_var - Name of variable in parent-scope holding the return value.
28+
# Returns - List consisting of the function's name and arguments types.
29+
#=============================================================================#
30+
function(strip_function_signature _signature _return_var)
31+
32+
# Strip function's name
33+
string(REGEX MATCH ${ARDUINO_CMAKE_FUNCTION_NAME_REGEX_PATTERN} function_name_match "${_signature}")
34+
set(function_name ${CMAKE_MATCH_1})
35+
list(APPEND stripped_signature ${function_name})
36+
37+
# Strip arguments types , i.e. without names
38+
_get_function_arguments_types("${_signature}" function_args_types)
39+
list(APPEND stripped_signature ${function_args_types})
40+
41+
set(${_return_var} ${stripped_signature} PARENT_SCOPE)
42+
43+
endfunction()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function(get_source_function_definitions _source_file _return_var)
2+
3+
file(STRINGS "${_source_file}" source_lines)
4+
5+
foreach (line ${source_lines})
6+
if ("${line}" MATCHES "${ARDUINO_CMAKE_FUNCTION_DEFINITION_REGEX_PATTERN}")
7+
list(APPEND definitions "${line}")
8+
endif ()
9+
endforeach ()
10+
11+
set(${_return_var} ${definitions} PARENT_SCOPE)
12+
13+
endfunction()

cmake/Platform/Sources/Sources.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
include(FunctionSignatureStripper)
2+
include(FunctionDeclarationMatcher)
3+
include(SourceFunctionsRetriever)
14
include(SourceSeeker)
25
include(ExampleSourcesSeeker)
36
include(ArduinoLibrarySourcesSeeker)

0 commit comments

Comments
 (0)