2017-01-29 00:03:23 +00:00
|
|
|
/*
|
|
|
|
4coder_function_list.cpp - Command for listing all functions in a C/C++ file in a jump list.
|
|
|
|
*/
|
2017-01-23 06:19:43 +00:00
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
|
|
|
// NOTE(allen|a4.0.14): This turned out to be a nasty little routine. There might
|
|
|
|
// be a better way to do it with just tokens that I didn't see the first time
|
|
|
|
// through. Once I build a real parser this should become almost just as easy as
|
|
|
|
// iterating tokens is now.
|
|
|
|
//
|
|
|
|
// This version can be dropped anywhere underneath 4coder_default_include.cpp and
|
|
|
|
// will then provide the "list_all_functions_current_buffer" command.
|
|
|
|
//
|
|
|
|
|
2017-03-29 16:32:06 +00:00
|
|
|
static Get_Positions_Results
|
|
|
|
get_function_positions(Application_Links *app, Buffer_Summary *buffer, int32_t token_index, Function_Positions *positions_array, int32_t positions_max){
|
2018-11-20 08:18:54 +00:00
|
|
|
Get_Positions_Results result = {};
|
2017-01-23 06:19:43 +00:00
|
|
|
|
|
|
|
static const int32_t token_chunk_size = 512;
|
|
|
|
Cpp_Token token_chunk[token_chunk_size];
|
2018-11-20 08:18:54 +00:00
|
|
|
Stream_Tokens token_stream = {};
|
2017-01-23 06:19:43 +00:00
|
|
|
|
2017-03-29 16:32:06 +00:00
|
|
|
if (init_stream_tokens(&token_stream, app, buffer, token_index, token_chunk, token_chunk_size)){
|
2017-01-23 06:19:43 +00:00
|
|
|
int32_t nest_level = 0;
|
|
|
|
int32_t paren_nest_level = 0;
|
|
|
|
|
|
|
|
int32_t first_paren_index = 0;
|
|
|
|
int32_t first_paren_position = 0;
|
|
|
|
int32_t last_paren_index = 0;
|
|
|
|
|
|
|
|
bool32 still_looping = false;
|
|
|
|
|
|
|
|
// Look for the next token at global scope that might need to be printed.
|
2017-03-29 16:32:06 +00:00
|
|
|
mode1:
|
|
|
|
Assert(nest_level == 0);
|
|
|
|
Assert(paren_nest_level == 0);
|
|
|
|
first_paren_index = 0;
|
|
|
|
first_paren_position = 0;
|
|
|
|
last_paren_index = 0;
|
|
|
|
|
|
|
|
do{
|
2017-01-23 06:19:43 +00:00
|
|
|
for (; token_index < token_stream.end; ++token_index){
|
|
|
|
Cpp_Token *token = &token_stream.tokens[token_index];
|
|
|
|
|
|
|
|
if (!(token->flags & CPP_TFLAG_PP_BODY)){
|
|
|
|
switch (token->type){
|
|
|
|
case CPP_TOKEN_BRACE_OPEN:
|
|
|
|
{
|
|
|
|
++nest_level;
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case CPP_TOKEN_BRACE_CLOSE:
|
|
|
|
{
|
|
|
|
if (nest_level > 0){
|
|
|
|
--nest_level;
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case CPP_TOKEN_PARENTHESE_OPEN:
|
|
|
|
{
|
|
|
|
if (nest_level == 0){
|
|
|
|
first_paren_index = token_index;
|
|
|
|
first_paren_position = token->start;
|
|
|
|
goto paren_mode1;
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
still_looping = forward_stream_tokens(&token_stream);
|
|
|
|
}while(still_looping);
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
// Look for a closing parenthese to mark the end of a function signature.
|
|
|
|
paren_mode1:
|
|
|
|
paren_nest_level = 0;
|
|
|
|
do{
|
|
|
|
for (; token_index < token_stream.end; ++token_index){
|
|
|
|
Cpp_Token *token = &token_stream.tokens[token_index];
|
|
|
|
|
|
|
|
if (!(token->flags & CPP_TFLAG_PP_BODY)){
|
|
|
|
switch (token->type){
|
|
|
|
case CPP_TOKEN_PARENTHESE_OPEN:
|
|
|
|
{
|
|
|
|
++paren_nest_level;
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case CPP_TOKEN_PARENTHESE_CLOSE:
|
|
|
|
{
|
|
|
|
--paren_nest_level;
|
|
|
|
if (paren_nest_level == 0){
|
|
|
|
last_paren_index = token_index;
|
|
|
|
goto paren_mode2;
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
still_looping = forward_stream_tokens(&token_stream);
|
|
|
|
}while(still_looping);
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
// Look backwards from an open parenthese to find the start of a function signature.
|
|
|
|
paren_mode2: {
|
|
|
|
Stream_Tokens backward_stream_temp = begin_temp_stream_token(&token_stream);
|
|
|
|
int32_t local_index = first_paren_index;
|
|
|
|
int32_t signature_start_index = 0;
|
|
|
|
|
|
|
|
do{
|
|
|
|
for (; local_index >= token_stream.start; --local_index){
|
|
|
|
Cpp_Token *token = &token_stream.tokens[local_index];
|
|
|
|
if ((token->flags & CPP_TFLAG_PP_BODY) || (token->flags & CPP_TFLAG_PP_DIRECTIVE) || token->type == CPP_TOKEN_BRACE_CLOSE || token->type == CPP_TOKEN_SEMICOLON || token->type == CPP_TOKEN_PARENTHESE_CLOSE){
|
|
|
|
++local_index;
|
|
|
|
signature_start_index = local_index;
|
|
|
|
goto paren_mode2_done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
still_looping = backward_stream_tokens(&token_stream);
|
|
|
|
}while(still_looping);
|
|
|
|
// When this loop ends by going all the way back to the beginning set the signature start to 0 and fall through to the printing phase.
|
|
|
|
signature_start_index = 0;
|
|
|
|
|
|
|
|
paren_mode2_done:;
|
|
|
|
{
|
|
|
|
Function_Positions positions;
|
|
|
|
positions.sig_start_index = signature_start_index;
|
|
|
|
positions.sig_end_index = last_paren_index;
|
|
|
|
positions.open_paren_pos = first_paren_position;
|
2017-03-29 16:32:06 +00:00
|
|
|
positions_array[result.positions_count++] = positions;
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
end_temp_stream_token(&token_stream, backward_stream_temp);
|
2017-03-29 16:32:06 +00:00
|
|
|
if (result.positions_count >= positions_max){
|
|
|
|
result.next_token_index = token_index;
|
|
|
|
result.still_looping = true;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
goto mode1;
|
|
|
|
}
|
|
|
|
end:;
|
2017-03-29 16:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
print_positions(Application_Links *app, Buffer_Summary *buffer, Function_Positions *positions_array, int32_t positions_count, Buffer_Summary *output_buffer, Partition *part){
|
|
|
|
|
|
|
|
Temp_Memory temp = begin_temp_memory(part);
|
|
|
|
|
|
|
|
Partition extra_memory_ = partition_sub_part(part, (4<<10));
|
|
|
|
Partition *extra_memory = &extra_memory_;
|
|
|
|
|
|
|
|
char *str_ptr = (char*)partition_current(part);
|
|
|
|
int32_t part_size = 0;
|
|
|
|
|
|
|
|
String buffer_name = make_string(buffer->buffer_name, buffer->buffer_name_len);
|
|
|
|
int32_t size = output_buffer->size;
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < positions_count; ++i){
|
|
|
|
Function_Positions *positions = &positions_array[i];
|
|
|
|
Temp_Memory extra_temp = begin_temp_memory(extra_memory);
|
|
|
|
|
|
|
|
int32_t local_index = positions->sig_start_index;
|
|
|
|
int32_t end_index = positions->sig_end_index;
|
|
|
|
int32_t open_paren_pos = positions->open_paren_pos;
|
|
|
|
|
|
|
|
Assert(end_index > local_index);
|
|
|
|
|
|
|
|
static const int32_t sig_chunk_size = 64;
|
|
|
|
Cpp_Token sig_chunk[sig_chunk_size];
|
2018-11-20 08:18:54 +00:00
|
|
|
Stream_Tokens sig_stream = {};
|
2017-03-29 16:32:06 +00:00
|
|
|
if (init_stream_tokens(&sig_stream, app, buffer, local_index, sig_chunk, sig_chunk_size)){
|
|
|
|
bool32 still_looping = false;
|
2017-01-23 06:19:43 +00:00
|
|
|
do{
|
2017-03-29 16:32:06 +00:00
|
|
|
for (; local_index < sig_stream.end; ++local_index){
|
|
|
|
Cpp_Token *token = &sig_stream.tokens[local_index];
|
|
|
|
if (!(token->flags & CPP_TFLAG_PP_BODY) && token->type != CPP_TOKEN_COMMENT){
|
|
|
|
bool32 delete_space_before = false;
|
|
|
|
bool32 space_after = false;
|
|
|
|
|
|
|
|
switch (token->type){
|
|
|
|
case CPP_TOKEN_PARENTHESE_OPEN:
|
|
|
|
case CPP_TOKEN_PARENTHESE_CLOSE:
|
|
|
|
{
|
|
|
|
delete_space_before = true;
|
|
|
|
}break;
|
2017-01-23 06:19:43 +00:00
|
|
|
|
2017-03-29 16:32:06 +00:00
|
|
|
case CPP_TOKEN_IDENTIFIER:
|
|
|
|
case CPP_TOKEN_STAR:
|
|
|
|
{
|
2017-01-23 06:19:43 +00:00
|
|
|
space_after = true;
|
2017-03-29 16:32:06 +00:00
|
|
|
}break;
|
2017-01-23 06:19:43 +00:00
|
|
|
|
2017-03-29 16:32:06 +00:00
|
|
|
case CPP_TOKEN_COMMA:
|
|
|
|
{
|
|
|
|
delete_space_before = true;
|
|
|
|
space_after = true;
|
|
|
|
}break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token->flags & CPP_TFLAG_IS_KEYWORD){
|
|
|
|
space_after = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delete_space_before){
|
|
|
|
int32_t pos = extra_memory->pos - 1;
|
|
|
|
char *base = ((char*)(extra_memory->base));
|
|
|
|
if (pos >= 0 && base[pos] == ' '){
|
|
|
|
extra_memory->pos = pos;
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
2017-03-29 16:32:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *token_str = push_array(extra_memory, char, token->size + space_after);
|
|
|
|
if (token_str != 0){
|
2017-01-23 06:19:43 +00:00
|
|
|
buffer_read_range(app, buffer, token->start, token->start + token->size, token_str);
|
|
|
|
if (space_after){
|
|
|
|
token_str[token->size] = ' ';
|
|
|
|
}
|
|
|
|
}
|
2017-03-29 16:32:06 +00:00
|
|
|
else{
|
|
|
|
goto finish_print;
|
|
|
|
}
|
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (local_index == end_index){
|
|
|
|
goto finish_print;
|
|
|
|
}
|
|
|
|
}
|
2017-03-29 16:32:06 +00:00
|
|
|
still_looping = forward_stream_tokens(&sig_stream);
|
2017-01-23 06:19:43 +00:00
|
|
|
}while(still_looping);
|
|
|
|
|
|
|
|
finish_print:;
|
|
|
|
{
|
2017-03-23 19:16:39 +00:00
|
|
|
int32_t sig_size = extra_memory->pos;
|
|
|
|
String sig = make_string(extra_memory->base, sig_size);
|
2017-01-23 06:19:43 +00:00
|
|
|
|
2017-11-28 20:16:53 +00:00
|
|
|
int32_t line_number = buffer_get_line_number(app, buffer, open_paren_pos);
|
2017-03-23 19:15:33 +00:00
|
|
|
int32_t line_number_len = int_to_str_size(line_number);
|
2017-01-23 06:19:43 +00:00
|
|
|
|
2017-03-23 19:16:39 +00:00
|
|
|
int32_t append_len = buffer_name.size + 1 + line_number_len + 1 + 1 + sig_size + 1;
|
2017-01-23 06:19:43 +00:00
|
|
|
|
|
|
|
char *out_space = push_array(part, char, append_len);
|
|
|
|
if (out_space == 0){
|
2017-03-29 16:32:06 +00:00
|
|
|
buffer_replace_range(app, output_buffer, size, size, str_ptr, part_size);
|
2017-01-23 06:19:43 +00:00
|
|
|
size += part_size;
|
|
|
|
|
|
|
|
end_temp_memory(temp);
|
|
|
|
temp = begin_temp_memory(part);
|
|
|
|
|
|
|
|
part_size = 0;
|
|
|
|
out_space = push_array(part, char, append_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
part_size += append_len;
|
2017-03-23 19:16:39 +00:00
|
|
|
String out = make_string(out_space, 0, append_len);
|
2017-01-23 06:19:43 +00:00
|
|
|
append(&out, buffer_name);
|
|
|
|
append(&out, ':');
|
2017-03-23 19:15:33 +00:00
|
|
|
append_int_to_str(&out, line_number);
|
2017-01-23 06:19:43 +00:00
|
|
|
append(&out, ':');
|
|
|
|
append(&out, ' ');
|
|
|
|
append(&out, sig);
|
|
|
|
append(&out, '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:32:06 +00:00
|
|
|
end_temp_memory(extra_temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_replace_range(app, output_buffer, size, size, str_ptr, part_size);
|
|
|
|
end_temp_memory(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-09-15 23:48:02 +00:00
|
|
|
list_all_functions(Application_Links *app, Partition *part, Buffer_Summary *optional_target_buffer){
|
2017-03-29 16:32:06 +00:00
|
|
|
String search_name = make_lit_string("*decls*");
|
|
|
|
Buffer_Summary decls_buffer = get_buffer_by_name(app, search_name.str, search_name.size, AccessAll);
|
|
|
|
if (!decls_buffer.exists){
|
|
|
|
decls_buffer = create_buffer(app, search_name.str, search_name.size, BufferCreate_AlwaysNew);
|
|
|
|
buffer_set_setting(app, &decls_buffer, BufferSetting_Unimportant, true);
|
|
|
|
buffer_set_setting(app, &decls_buffer, BufferSetting_ReadOnly, true);
|
|
|
|
buffer_set_setting(app, &decls_buffer, BufferSetting_WrapLine, false);
|
|
|
|
}
|
|
|
|
else{
|
2017-04-23 00:53:19 +00:00
|
|
|
buffer_send_end_signal(app, &decls_buffer);
|
2017-03-29 16:32:06 +00:00
|
|
|
buffer_replace_range(app, &decls_buffer, 0, decls_buffer.size, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Temp_Memory temp = begin_temp_memory(part);
|
|
|
|
|
|
|
|
int32_t positions_max = (4<<10)/sizeof(Function_Positions);
|
|
|
|
Function_Positions *positions_array = push_array(part, Function_Positions, positions_max);
|
|
|
|
|
2018-09-15 23:48:02 +00:00
|
|
|
for (Buffer_Summary buffer_it = get_buffer_first(app, AccessAll);
|
|
|
|
buffer_it.exists;
|
|
|
|
get_buffer_next(app, &buffer_it, AccessAll)){
|
|
|
|
Buffer_Summary buffer = buffer_it;
|
|
|
|
if (optional_target_buffer != 0){
|
|
|
|
buffer = *optional_target_buffer;
|
|
|
|
}
|
2017-01-23 06:19:43 +00:00
|
|
|
|
2018-09-15 23:48:02 +00:00
|
|
|
if (!buffer.tokens_are_ready){
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-23 06:19:43 +00:00
|
|
|
|
2018-09-15 23:48:02 +00:00
|
|
|
int32_t token_index = 0;
|
|
|
|
bool32 still_looping = false;
|
|
|
|
do{
|
|
|
|
Get_Positions_Results get_positions_results = get_function_positions(app, &buffer, token_index, positions_array, positions_max);
|
|
|
|
|
|
|
|
int32_t positions_count = get_positions_results.positions_count;
|
|
|
|
token_index = get_positions_results.next_token_index;
|
|
|
|
still_looping = get_positions_results.still_looping;
|
|
|
|
|
|
|
|
print_positions(app, &buffer, positions_array, positions_count, &decls_buffer, part);
|
|
|
|
}while(still_looping);
|
|
|
|
|
|
|
|
if (optional_target_buffer != 0){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-29 16:32:06 +00:00
|
|
|
|
|
|
|
View_Summary view = get_active_view(app, AccessAll);
|
|
|
|
view_set_buffer(app, &view, decls_buffer.buffer_id, 0);
|
|
|
|
|
|
|
|
lock_jump_buffer(search_name.str, search_name.size);
|
|
|
|
|
|
|
|
end_temp_memory(temp);
|
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 23:57:21 +00:00
|
|
|
CUSTOM_COMMAND_SIG(list_all_functions_current_buffer)
|
|
|
|
CUSTOM_DOC("Creates a jump list of lines of the current buffer that appear to define or declare functions.")
|
|
|
|
{
|
2018-09-15 23:48:02 +00:00
|
|
|
View_Summary view = get_active_view(app, AccessProtected);
|
|
|
|
Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessProtected);
|
|
|
|
if (buffer.exists){
|
|
|
|
list_all_functions(app, &global_part, &buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(list_all_functions_current_buffer_lister)
|
|
|
|
CUSTOM_DOC("Creates a lister of locations that look like function definitions and declarations in the buffer.")
|
|
|
|
{
|
|
|
|
View_Summary view = get_active_view(app, AccessProtected);
|
|
|
|
Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessProtected);
|
|
|
|
if (buffer.exists){
|
|
|
|
list_all_functions(app, &global_part, &buffer);
|
|
|
|
view = get_active_view(app, AccessAll);
|
|
|
|
open_jump_lister(app, &global_part, &global_heap,
|
|
|
|
&view, view.buffer_id, JumpListerActivation_OpenInUIView, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(list_all_functions_all_buffers)
|
|
|
|
CUSTOM_DOC("Creates a jump list of lines from all buffers that appear to define or declare functions.")
|
|
|
|
{
|
|
|
|
list_all_functions(app, &global_part, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_COMMAND_SIG(list_all_functions_all_buffers_lister)
|
|
|
|
CUSTOM_DOC("Creates a lister of locations that look like function definitions and declarations all buffers.")
|
|
|
|
{
|
|
|
|
list_all_functions(app, &global_part, 0);
|
|
|
|
View_Summary view = get_active_view(app, AccessAll);
|
|
|
|
open_jump_lister(app, &global_part, &global_heap,
|
|
|
|
&view, view.buffer_id, JumpListerActivation_OpenInUIView, 0);
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BOTTOM
|
|
|
|
|