mirror of
https://github.com/Ed94/refactor.git
synced 2024-12-21 14:34:44 -08:00
Fix for namespace swapping
This commit is contained in:
parent
5efd14322e
commit
4d5cbd3e8d
6
.vscode/launch.json
vendored
6
.vscode/launch.json
vendored
@ -47,10 +47,10 @@
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/build/refactor.exe",
|
||||
"args": [
|
||||
"-source=./refactor.cpp",
|
||||
"-destination=./Test/refactor.cpp",
|
||||
"-src=./project/refactor.cpp",
|
||||
"-dst=./Test/refactor.cpp",
|
||||
|
||||
"-specification=./Test/zpl.refactor"
|
||||
"-spec=./Test/zpl.refactor"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceRoot}",
|
||||
|
@ -79,8 +79,6 @@ With the refactors applied a meson configuraiton is setup (`meson.build` in test
|
||||
* Can have a heavy memory cost, so most likely do not want on by default.
|
||||
* Make comments ignored by default, and just have ability to specify custom comments.
|
||||
* Would need a directive to add refactors to comments.
|
||||
* Directive to add cpp namespaces on specific lines of a file, or near specific signatures.
|
||||
* This can honestly be done also with placing words on specific lines..
|
||||
* Provide a GUI build.
|
||||
* Provide as a single-header library.
|
||||
* Could add a test case where this library is refactored into pure C (most likely c99 or c11).
|
||||
|
@ -34,8 +34,8 @@ namespace zpl_random_, rng_
|
||||
namespace zpl_pr, process_
|
||||
namespace zpl__, __
|
||||
namespace ZPL_ADT_, EADT
|
||||
namespace ZPL_ALLOCATION_, EAllocation
|
||||
namespace ZPL_CSV_ERROR, ECSV_Error
|
||||
namespace ZPL_ALLOCATION_, EAllocation_
|
||||
namespace ZPL_CSV_ERROR, ECSV_Error_
|
||||
namespace ZPL_FILE_MODE_, EFileMode_
|
||||
namespace ZPL_FILE_ERROR_, EFileError_
|
||||
namespace ZPL_SEEK_WHENCE_, ESeekWhence_
|
||||
|
@ -9,7 +9,7 @@ namespace Global
|
||||
namespace Memory
|
||||
{
|
||||
zpl_arena Global_Arena {};
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
zpl_arena_init_from_allocator( & Global_Arena, zpl_heap(), Initial_Reserve );
|
||||
@ -19,16 +19,16 @@ namespace Memory
|
||||
zpl_assert_crash( "Failed to reserve memory for Tests:: Global_Arena" );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void resize( uw new_size )
|
||||
{
|
||||
void* new_memory = zpl_resize( zpl_heap(), Global_Arena.physical_start, Global_Arena.total_size, new_size );
|
||||
|
||||
void* new_memory = zpl_resize( zpl_heap(), Global_Arena.physical_start, Global_Arena.total_size, new_size );
|
||||
|
||||
if ( new_memory == nullptr )
|
||||
{
|
||||
fatal("Failed to resize global arena!");
|
||||
}
|
||||
|
||||
|
||||
Global_Arena.physical_start = new_memory;
|
||||
Global_Arena.total_size = new_size;
|
||||
}
|
||||
@ -36,7 +36,7 @@ namespace Memory
|
||||
void cleanup()
|
||||
{
|
||||
zpl_arena_free( & Global_Arena);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -44,7 +44,7 @@ bool opts_custom_add(zpl_opts* opts, zpl_opts_entry *t, char* b)
|
||||
{
|
||||
if (t->type != ZPL_OPTS_STRING)
|
||||
{
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
t->text = zpl_string_append_length(t->text, " ", 1);
|
||||
@ -53,23 +53,23 @@ bool opts_custom_add(zpl_opts* opts, zpl_opts_entry *t, char* b)
|
||||
return true;
|
||||
}
|
||||
|
||||
b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
|
||||
b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
|
||||
{
|
||||
zpl_b32 had_errors = false;
|
||||
|
||||
for (int i = 1; i < argc; ++i)
|
||||
for (int i = 1; i < argc; ++i)
|
||||
{
|
||||
char* arg = argv[i];
|
||||
|
||||
if (*arg)
|
||||
|
||||
if (*arg)
|
||||
{
|
||||
arg = (char*)zpl_str_trim(arg, false);
|
||||
|
||||
if (*arg == '-')
|
||||
if (*arg == '-')
|
||||
{
|
||||
zpl_opts_entry* entry = 0;
|
||||
zpl_b32 checkln = false;
|
||||
if ( *(arg + 1) == '-')
|
||||
if ( *(arg + 1) == '-')
|
||||
{
|
||||
checkln = true;
|
||||
++arg;
|
||||
@ -83,15 +83,15 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
|
||||
|
||||
entry = zpl__opts_find(opts, b, (e - b), checkln);
|
||||
|
||||
if (entry)
|
||||
if (entry)
|
||||
{
|
||||
char *ob = b;
|
||||
b = e;
|
||||
|
||||
/**/
|
||||
if (*e == '=')
|
||||
/**/
|
||||
if (*e == '=')
|
||||
{
|
||||
if (entry->type == ZPL_OPTS_FLAG)
|
||||
if (entry->type == ZPL_OPTS_FLAG)
|
||||
{
|
||||
*e = '\0';
|
||||
zpl__opts_push_error(opts, ob, ZPL_OPTS_ERR_EXTRA_VALUE);
|
||||
@ -101,14 +101,14 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
|
||||
}
|
||||
|
||||
b = e = e + 1;
|
||||
}
|
||||
else if (*e == '\0')
|
||||
}
|
||||
else if (*e == '\0')
|
||||
{
|
||||
char *sp = argv[i+1];
|
||||
|
||||
if (sp && *sp != '-' && (zpl_array_count(opts->positioned) < 1 || entry->type != ZPL_OPTS_FLAG))
|
||||
if (sp && *sp != '-' && (zpl_array_count(opts->positioned) < 1 || entry->type != ZPL_OPTS_FLAG))
|
||||
{
|
||||
if (entry->type == ZPL_OPTS_FLAG)
|
||||
if (entry->type == ZPL_OPTS_FLAG)
|
||||
{
|
||||
zpl__opts_push_error(opts, b, ZPL_OPTS_ERR_EXTRA_VALUE);
|
||||
had_errors = true;
|
||||
@ -119,10 +119,10 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
|
||||
arg = sp;
|
||||
b = e = sp;
|
||||
++i;
|
||||
}
|
||||
else
|
||||
}
|
||||
else
|
||||
{
|
||||
if (entry->type != ZPL_OPTS_FLAG)
|
||||
if (entry->type != ZPL_OPTS_FLAG)
|
||||
{
|
||||
zpl__opts_push_error(opts, ob, ZPL_OPTS_ERR_MISSING_VALUE);
|
||||
had_errors = true;
|
||||
@ -145,20 +145,20 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
|
||||
opts_custom_add(opts, entry, b );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
}
|
||||
else
|
||||
{
|
||||
zpl__opts_push_error(opts, b, ZPL_OPTS_ERR_OPTION);
|
||||
had_errors = true;
|
||||
}
|
||||
}
|
||||
else if (zpl_array_count(opts->positioned))
|
||||
}
|
||||
else if (zpl_array_count(opts->positioned))
|
||||
{
|
||||
zpl_opts_entry *l = zpl_array_back(opts->positioned);
|
||||
zpl_array_pop(opts->positioned);
|
||||
zpl__opts_set_value(opts, l, arg);
|
||||
}
|
||||
else
|
||||
}
|
||||
else
|
||||
{
|
||||
zpl__opts_push_error(opts, arg, ZPL_OPTS_ERR_VALUE);
|
||||
had_errors = true;
|
||||
|
@ -2,7 +2,7 @@
|
||||
BLOAT.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#pragma once
|
||||
|
||||
#ifdef BLOAT_IMPL
|
||||
# define ZPL_IMPLEMENTATION
|
||||
@ -10,7 +10,7 @@
|
||||
|
||||
#pragma region ZPL INCLUDE
|
||||
#if __clang__
|
||||
# pragma clang diagnostic push
|
||||
# pragma clang diagnostic push
|
||||
# pragma clang diagnostic ignored "-Wmissing-braces"
|
||||
# pragma clang diagnostic ignored "-Wbraced-scalar-init"
|
||||
#endif
|
||||
@ -111,25 +111,25 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv);
|
||||
|
||||
|
||||
inline
|
||||
sw log_fmt(char const *fmt, ...)
|
||||
sw log_fmt(char const *fmt, ...)
|
||||
{
|
||||
if ( Global::ShouldShowDebug == false )
|
||||
return 0;
|
||||
|
||||
sw res;
|
||||
va_list va;
|
||||
|
||||
|
||||
va_start(va, fmt);
|
||||
res = zpl_printf_va(fmt, va);
|
||||
va_end(va);
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
inline
|
||||
void fatal(char const *fmt, ...)
|
||||
void fatal(char const *fmt, ...)
|
||||
{
|
||||
zpl_local_persist zpl_thread_local
|
||||
zpl_local_persist zpl_thread_local
|
||||
char buf[ZPL_PRINTF_MAXLEN] = { 0 };
|
||||
|
||||
va_list va;
|
||||
|
@ -4,13 +4,13 @@
|
||||
namespace IO
|
||||
{
|
||||
using array_string = zpl_array( zpl_string );
|
||||
|
||||
|
||||
namespace StaticData
|
||||
{
|
||||
array_string Sources = nullptr;
|
||||
array_string Destinations = nullptr;
|
||||
zpl_string Specification = nullptr;
|
||||
|
||||
|
||||
// Current source and destination index.
|
||||
// Used to keep track of which file get_next_source or write refer to.
|
||||
sw Current = -1;
|
||||
@ -22,12 +22,12 @@ namespace IO
|
||||
zpl_arena MemSrc;
|
||||
}
|
||||
using namespace StaticData;
|
||||
|
||||
|
||||
|
||||
|
||||
void prepare()
|
||||
{
|
||||
const sw num_srcs = zpl_array_count( Sources );
|
||||
|
||||
|
||||
// Determine the largest content size.
|
||||
sw left = num_srcs;
|
||||
zpl_string* path = Sources;
|
||||
@ -35,14 +35,14 @@ namespace IO
|
||||
{
|
||||
zpl_file src = {};
|
||||
zpl_file_error error = zpl_file_open( & src, *path );
|
||||
|
||||
|
||||
if ( error != ZPL_FILE_ERROR_NONE )
|
||||
{
|
||||
fatal("IO::Prepare - Could not open source file: %s", *path );
|
||||
}
|
||||
|
||||
|
||||
const sw fsize = zpl_file_size( & src );
|
||||
|
||||
|
||||
if ( fsize > Largest_Src_Size )
|
||||
{
|
||||
Largest_Src_Size = fsize;
|
||||
@ -51,12 +51,12 @@ namespace IO
|
||||
zpl_file_close( & src );
|
||||
}
|
||||
while ( path++, left--, left > 0 );
|
||||
|
||||
|
||||
uw persist_size = Largest_Src_Size * 2 + 8;
|
||||
|
||||
|
||||
zpl_arena_init_from_allocator( & MemSrc, zpl_heap(), persist_size );
|
||||
}
|
||||
|
||||
|
||||
void cleanup()
|
||||
{
|
||||
zpl_arena_free( & MemSpec );
|
||||
|
@ -10,16 +10,16 @@ namespace IO
|
||||
// Preps the IO by loading all the files and checking to see what the largest size is.
|
||||
// The file with the largest size is used to determine the size of the persistent memory.
|
||||
void prepare();
|
||||
|
||||
|
||||
// Frees the persistent and transient memory arenas.
|
||||
void cleanup();
|
||||
void cleanup();
|
||||
|
||||
// Provides the content of the specification.
|
||||
Array_Line get_specification();
|
||||
|
||||
|
||||
// Provides the content of the next source, broken up as a series of lines.
|
||||
char* get_next_source();
|
||||
|
||||
// Writes the refactored content ot the current corresponding destination.
|
||||
void write( zpl_string refactored );
|
||||
}
|
||||
}
|
||||
|
@ -195,7 +195,7 @@ namespace Spec
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
find_next_token( type, token, line, length );
|
||||
|
||||
// First argument is signature.
|
||||
@ -211,7 +211,7 @@ namespace Spec
|
||||
case Tok::Word:
|
||||
if ( ignore)
|
||||
zpl_array_append( Ignore_Words, entry );
|
||||
|
||||
|
||||
else
|
||||
zpl_array_append( Words, entry );
|
||||
break;
|
||||
@ -248,7 +248,7 @@ namespace Spec
|
||||
{
|
||||
case Tok::Word:
|
||||
zpl_array_append( Words, entry );
|
||||
break;
|
||||
break;
|
||||
|
||||
case Tok::Namespace:
|
||||
zpl_array_append( Namespaces, entry );
|
||||
@ -288,7 +288,7 @@ namespace Spec
|
||||
{
|
||||
case Tok::Word:
|
||||
zpl_array_append( Words, entry );
|
||||
break;
|
||||
break;
|
||||
|
||||
case Tok::Namespace:
|
||||
zpl_array_append( Namespaces, entry );
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
namespace Spec
|
||||
{
|
||||
enum Tok
|
||||
enum Tok
|
||||
{
|
||||
Not,
|
||||
Comment,
|
||||
@ -19,7 +19,7 @@ namespace Spec
|
||||
char const* str_tok( Tok tok )
|
||||
{
|
||||
static
|
||||
char const* tok_to_str[ Tok::Num_Tok ] =
|
||||
char const* tok_to_str[ Tok::Num_Tok ] =
|
||||
{
|
||||
"not",
|
||||
"comments",
|
||||
@ -35,7 +35,7 @@ namespace Spec
|
||||
char strlen_tok( Tok tok )
|
||||
{
|
||||
static
|
||||
const u8 tok_to_len[ Tok::Num_Tok ] =
|
||||
const u8 tok_to_len[ Tok::Num_Tok ] =
|
||||
{
|
||||
3,
|
||||
8,
|
||||
|
@ -28,32 +28,32 @@ void parse_options( int num, char** arguments )
|
||||
|
||||
Global::ShouldShowDebug = true;
|
||||
}
|
||||
|
||||
|
||||
if ( zpl_opts_has_arg( & opts, "num" ) )
|
||||
{
|
||||
num = zpl_opts_integer( & opts, "num", -1 );
|
||||
uw global_reserve = num * sizeof(zpl_string) * IO::Path_Size_Largest * 2 + 8;
|
||||
|
||||
|
||||
if ( global_reserve > zpl_megabytes(1) )
|
||||
{
|
||||
Memory::resize( global_reserve + zpl_megabytes(2) );
|
||||
}
|
||||
|
||||
|
||||
zpl_array_init_reserve( IO::Sources, g_allocator, num );
|
||||
zpl_array_init_reserve( IO::Destinations, g_allocator, num );
|
||||
zpl_array_init_reserve( IO::Destinations, g_allocator, num );
|
||||
}
|
||||
else
|
||||
{
|
||||
num = 1;
|
||||
|
||||
zpl_array_init_reserve( IO::Sources, g_allocator, 1 );
|
||||
zpl_array_init_reserve( IO::Destinations, g_allocator, 1 );
|
||||
zpl_array_init_reserve( IO::Destinations, g_allocator, 1 );
|
||||
}
|
||||
|
||||
if ( zpl_opts_has_arg( & opts, "src" ) )
|
||||
{
|
||||
zpl_string opt = zpl_opts_string( & opts, "src", "INVALID SRC ARGUMENT" );
|
||||
|
||||
|
||||
if ( num == 1 )
|
||||
{
|
||||
zpl_string path = zpl_string_make_length( g_allocator, opt, zpl_string_length( opt ));
|
||||
@ -66,15 +66,15 @@ void parse_options( int num, char** arguments )
|
||||
uw left = num;
|
||||
do
|
||||
{
|
||||
char* path = buffer;
|
||||
sw length = 0;
|
||||
char* path = buffer;
|
||||
sw length = 0;
|
||||
|
||||
do
|
||||
{
|
||||
path[length] = *opt;
|
||||
}
|
||||
while ( length++, opt++, *opt != ' ' && *opt != '\0' );
|
||||
|
||||
|
||||
zpl_string path_string = zpl_string_make_length( g_allocator, path, length );
|
||||
zpl_array_append( IO::Sources, path_string );
|
||||
|
||||
@ -110,14 +110,14 @@ void parse_options( int num, char** arguments )
|
||||
do
|
||||
{
|
||||
path[length] = *opt;
|
||||
}
|
||||
}
|
||||
while ( length++, opt++, *opt != ' ' && *opt != '\0' );
|
||||
|
||||
|
||||
zpl_string path_string = zpl_string_make_length( g_allocator, path, length );
|
||||
zpl_array_append( IO::Destinations, path_string );
|
||||
|
||||
opt++;
|
||||
}
|
||||
}
|
||||
while ( --left );
|
||||
|
||||
if ( zpl_array_count(IO::Destinations) != zpl_array_count( IO::Sources ) )
|
||||
@ -336,7 +336,7 @@ void refactor()
|
||||
}
|
||||
|
||||
restore( backup );
|
||||
}
|
||||
}
|
||||
while (false);
|
||||
|
||||
// Word Ignores
|
||||
@ -390,7 +390,7 @@ void refactor()
|
||||
|
||||
u32 sig_length = zpl_string_length( ignore->Sig );
|
||||
current = zpl_string_append_length( current, src, sig_length );
|
||||
|
||||
|
||||
if ( zpl_string_are_equal( ignore->Sig, current ) )
|
||||
{
|
||||
u32 length = sig_length;
|
||||
@ -568,7 +568,7 @@ void refactor()
|
||||
Token entry {};
|
||||
|
||||
entry.Start = pos;
|
||||
entry.End = pos + length;
|
||||
entry.End = pos + sig_length;
|
||||
entry.Sig = nspace->Sig;
|
||||
|
||||
buffer_size += sig_length;
|
||||
@ -576,7 +576,7 @@ void refactor()
|
||||
if ( nspace->Sub != nullptr )
|
||||
{
|
||||
entry.Sub = nspace->Sub;
|
||||
buffer_size += zpl_string_length( entry.Sub ) - length;
|
||||
buffer_size += zpl_string_length( entry.Sub ) - sig_length;
|
||||
}
|
||||
|
||||
zpl_array_append( tokens, entry );
|
||||
@ -660,7 +660,7 @@ End_Search:
|
||||
int main( int num, char** arguments )
|
||||
{
|
||||
Memory::setup();
|
||||
|
||||
|
||||
parse_options( num, arguments);
|
||||
|
||||
IO::prepare();
|
||||
|
Loading…
Reference in New Issue
Block a user