Fix for namespace swapping

This commit is contained in:
Edward R. Gonzalez 2023-07-20 01:03:31 -04:00
parent 5efd14322e
commit 4d5cbd3e8d
10 changed files with 81 additions and 83 deletions

6
.vscode/launch.json vendored
View File

@ -47,10 +47,10 @@
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/build/refactor.exe", "program": "${workspaceFolder}/build/refactor.exe",
"args": [ "args": [
"-source=./refactor.cpp", "-src=./project/refactor.cpp",
"-destination=./Test/refactor.cpp", "-dst=./Test/refactor.cpp",
"-specification=./Test/zpl.refactor" "-spec=./Test/zpl.refactor"
], ],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceRoot}", "cwd": "${workspaceRoot}",

View File

@ -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. * 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. * Make comments ignored by default, and just have ability to specify custom comments.
* Would need a directive to add refactors to 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 a GUI build.
* Provide as a single-header library. * Provide as a single-header library.
* Could add a test case where this library is refactored into pure C (most likely c99 or c11). * Could add a test case where this library is refactored into pure C (most likely c99 or c11).

View File

@ -34,8 +34,8 @@ namespace zpl_random_, rng_
namespace zpl_pr, process_ namespace zpl_pr, process_
namespace zpl__, __ namespace zpl__, __
namespace ZPL_ADT_, EADT namespace ZPL_ADT_, EADT
namespace ZPL_ALLOCATION_, EAllocation namespace ZPL_ALLOCATION_, EAllocation_
namespace ZPL_CSV_ERROR, ECSV_Error namespace ZPL_CSV_ERROR, ECSV_Error_
namespace ZPL_FILE_MODE_, EFileMode_ namespace ZPL_FILE_MODE_, EFileMode_
namespace ZPL_FILE_ERROR_, EFileError_ namespace ZPL_FILE_ERROR_, EFileError_
namespace ZPL_SEEK_WHENCE_, ESeekWhence_ namespace ZPL_SEEK_WHENCE_, ESeekWhence_

View File

@ -9,7 +9,7 @@ namespace Global
namespace Memory namespace Memory
{ {
zpl_arena Global_Arena {}; zpl_arena Global_Arena {};
void setup() void setup()
{ {
zpl_arena_init_from_allocator( & Global_Arena, zpl_heap(), Initial_Reserve ); 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" ); zpl_assert_crash( "Failed to reserve memory for Tests:: Global_Arena" );
} }
} }
void resize( uw new_size ) 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 ) if ( new_memory == nullptr )
{ {
fatal("Failed to resize global arena!"); fatal("Failed to resize global arena!");
} }
Global_Arena.physical_start = new_memory; Global_Arena.physical_start = new_memory;
Global_Arena.total_size = new_size; Global_Arena.total_size = new_size;
} }
@ -36,7 +36,7 @@ namespace Memory
void cleanup() void cleanup()
{ {
zpl_arena_free( & Global_Arena); 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) if (t->type != ZPL_OPTS_STRING)
{ {
return false; return false;
} }
t->text = zpl_string_append_length(t->text, " ", 1); 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; 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; zpl_b32 had_errors = false;
for (int i = 1; i < argc; ++i) for (int i = 1; i < argc; ++i)
{ {
char* arg = argv[i]; char* arg = argv[i];
if (*arg) if (*arg)
{ {
arg = (char*)zpl_str_trim(arg, false); arg = (char*)zpl_str_trim(arg, false);
if (*arg == '-') if (*arg == '-')
{ {
zpl_opts_entry* entry = 0; zpl_opts_entry* entry = 0;
zpl_b32 checkln = false; zpl_b32 checkln = false;
if ( *(arg + 1) == '-') if ( *(arg + 1) == '-')
{ {
checkln = true; checkln = true;
++arg; ++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); entry = zpl__opts_find(opts, b, (e - b), checkln);
if (entry) if (entry)
{ {
char *ob = b; char *ob = b;
b = e; b = e;
/**/ /**/
if (*e == '=') if (*e == '=')
{ {
if (entry->type == ZPL_OPTS_FLAG) if (entry->type == ZPL_OPTS_FLAG)
{ {
*e = '\0'; *e = '\0';
zpl__opts_push_error(opts, ob, ZPL_OPTS_ERR_EXTRA_VALUE); 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; b = e = e + 1;
} }
else if (*e == '\0') else if (*e == '\0')
{ {
char *sp = argv[i+1]; 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); zpl__opts_push_error(opts, b, ZPL_OPTS_ERR_EXTRA_VALUE);
had_errors = true; had_errors = true;
@ -119,10 +119,10 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
arg = sp; arg = sp;
b = e = sp; b = e = sp;
++i; ++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); zpl__opts_push_error(opts, ob, ZPL_OPTS_ERR_MISSING_VALUE);
had_errors = true; had_errors = true;
@ -145,20 +145,20 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv)
opts_custom_add(opts, entry, b ); opts_custom_add(opts, entry, b );
} }
} }
} }
else else
{ {
zpl__opts_push_error(opts, b, ZPL_OPTS_ERR_OPTION); zpl__opts_push_error(opts, b, ZPL_OPTS_ERR_OPTION);
had_errors = true; 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_opts_entry *l = zpl_array_back(opts->positioned);
zpl_array_pop(opts->positioned); zpl_array_pop(opts->positioned);
zpl__opts_set_value(opts, l, arg); zpl__opts_set_value(opts, l, arg);
} }
else else
{ {
zpl__opts_push_error(opts, arg, ZPL_OPTS_ERR_VALUE); zpl__opts_push_error(opts, arg, ZPL_OPTS_ERR_VALUE);
had_errors = true; had_errors = true;

View File

@ -2,7 +2,7 @@
BLOAT. BLOAT.
*/ */
#pragma once #pragma once
#ifdef BLOAT_IMPL #ifdef BLOAT_IMPL
# define ZPL_IMPLEMENTATION # define ZPL_IMPLEMENTATION
@ -10,7 +10,7 @@
#pragma region ZPL INCLUDE #pragma region ZPL INCLUDE
#if __clang__ #if __clang__
# pragma clang diagnostic push # pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wmissing-braces" # pragma clang diagnostic ignored "-Wmissing-braces"
# pragma clang diagnostic ignored "-Wbraced-scalar-init" # pragma clang diagnostic ignored "-Wbraced-scalar-init"
#endif #endif
@ -111,25 +111,25 @@ b32 opts_custom_compile(zpl_opts *opts, int argc, char **argv);
inline inline
sw log_fmt(char const *fmt, ...) sw log_fmt(char const *fmt, ...)
{ {
if ( Global::ShouldShowDebug == false ) if ( Global::ShouldShowDebug == false )
return 0; return 0;
sw res; sw res;
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
res = zpl_printf_va(fmt, va); res = zpl_printf_va(fmt, va);
va_end(va); va_end(va);
return res; return res;
} }
inline 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 }; char buf[ZPL_PRINTF_MAXLEN] = { 0 };
va_list va; va_list va;

View File

@ -4,13 +4,13 @@
namespace IO namespace IO
{ {
using array_string = zpl_array( zpl_string ); using array_string = zpl_array( zpl_string );
namespace StaticData namespace StaticData
{ {
array_string Sources = nullptr; array_string Sources = nullptr;
array_string Destinations = nullptr; array_string Destinations = nullptr;
zpl_string Specification = nullptr; zpl_string Specification = nullptr;
// Current source and destination index. // Current source and destination index.
// Used to keep track of which file get_next_source or write refer to. // Used to keep track of which file get_next_source or write refer to.
sw Current = -1; sw Current = -1;
@ -22,12 +22,12 @@ namespace IO
zpl_arena MemSrc; zpl_arena MemSrc;
} }
using namespace StaticData; using namespace StaticData;
void prepare() void prepare()
{ {
const sw num_srcs = zpl_array_count( Sources ); const sw num_srcs = zpl_array_count( Sources );
// Determine the largest content size. // Determine the largest content size.
sw left = num_srcs; sw left = num_srcs;
zpl_string* path = Sources; zpl_string* path = Sources;
@ -35,14 +35,14 @@ namespace IO
{ {
zpl_file src = {}; zpl_file src = {};
zpl_file_error error = zpl_file_open( & src, *path ); zpl_file_error error = zpl_file_open( & src, *path );
if ( error != ZPL_FILE_ERROR_NONE ) if ( error != ZPL_FILE_ERROR_NONE )
{ {
fatal("IO::Prepare - Could not open source file: %s", *path ); fatal("IO::Prepare - Could not open source file: %s", *path );
} }
const sw fsize = zpl_file_size( & src ); const sw fsize = zpl_file_size( & src );
if ( fsize > Largest_Src_Size ) if ( fsize > Largest_Src_Size )
{ {
Largest_Src_Size = fsize; Largest_Src_Size = fsize;
@ -51,12 +51,12 @@ namespace IO
zpl_file_close( & src ); zpl_file_close( & src );
} }
while ( path++, left--, left > 0 ); while ( path++, left--, left > 0 );
uw persist_size = Largest_Src_Size * 2 + 8; uw persist_size = Largest_Src_Size * 2 + 8;
zpl_arena_init_from_allocator( & MemSrc, zpl_heap(), persist_size ); zpl_arena_init_from_allocator( & MemSrc, zpl_heap(), persist_size );
} }
void cleanup() void cleanup()
{ {
zpl_arena_free( & MemSpec ); zpl_arena_free( & MemSpec );

View File

@ -10,16 +10,16 @@ namespace IO
// Preps the IO by loading all the files and checking to see what the largest size is. // 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. // The file with the largest size is used to determine the size of the persistent memory.
void prepare(); void prepare();
// Frees the persistent and transient memory arenas. // Frees the persistent and transient memory arenas.
void cleanup(); void cleanup();
// Provides the content of the specification. // Provides the content of the specification.
Array_Line get_specification(); Array_Line get_specification();
// Provides the content of the next source, broken up as a series of lines. // Provides the content of the next source, broken up as a series of lines.
char* get_next_source(); char* get_next_source();
// Writes the refactored content ot the current corresponding destination. // Writes the refactored content ot the current corresponding destination.
void write( zpl_string refactored ); void write( zpl_string refactored );
} }

View File

@ -195,7 +195,7 @@ namespace Spec
{ {
continue; continue;
} }
find_next_token( type, token, line, length ); find_next_token( type, token, line, length );
// First argument is signature. // First argument is signature.
@ -211,7 +211,7 @@ namespace Spec
case Tok::Word: case Tok::Word:
if ( ignore) if ( ignore)
zpl_array_append( Ignore_Words, entry ); zpl_array_append( Ignore_Words, entry );
else else
zpl_array_append( Words, entry ); zpl_array_append( Words, entry );
break; break;
@ -248,7 +248,7 @@ namespace Spec
{ {
case Tok::Word: case Tok::Word:
zpl_array_append( Words, entry ); zpl_array_append( Words, entry );
break; break;
case Tok::Namespace: case Tok::Namespace:
zpl_array_append( Namespaces, entry ); zpl_array_append( Namespaces, entry );
@ -288,7 +288,7 @@ namespace Spec
{ {
case Tok::Word: case Tok::Word:
zpl_array_append( Words, entry ); zpl_array_append( Words, entry );
break; break;
case Tok::Namespace: case Tok::Namespace:
zpl_array_append( Namespaces, entry ); zpl_array_append( Namespaces, entry );

View File

@ -4,7 +4,7 @@
namespace Spec namespace Spec
{ {
enum Tok enum Tok
{ {
Not, Not,
Comment, Comment,
@ -19,7 +19,7 @@ namespace Spec
char const* str_tok( Tok tok ) char const* str_tok( Tok tok )
{ {
static static
char const* tok_to_str[ Tok::Num_Tok ] = char const* tok_to_str[ Tok::Num_Tok ] =
{ {
"not", "not",
"comments", "comments",
@ -35,7 +35,7 @@ namespace Spec
char strlen_tok( Tok tok ) char strlen_tok( Tok tok )
{ {
static static
const u8 tok_to_len[ Tok::Num_Tok ] = const u8 tok_to_len[ Tok::Num_Tok ] =
{ {
3, 3,
8, 8,

View File

@ -28,32 +28,32 @@ void parse_options( int num, char** arguments )
Global::ShouldShowDebug = true; Global::ShouldShowDebug = true;
} }
if ( zpl_opts_has_arg( & opts, "num" ) ) if ( zpl_opts_has_arg( & opts, "num" ) )
{ {
num = zpl_opts_integer( & opts, "num", -1 ); num = zpl_opts_integer( & opts, "num", -1 );
uw global_reserve = num * sizeof(zpl_string) * IO::Path_Size_Largest * 2 + 8; uw global_reserve = num * sizeof(zpl_string) * IO::Path_Size_Largest * 2 + 8;
if ( global_reserve > zpl_megabytes(1) ) if ( global_reserve > zpl_megabytes(1) )
{ {
Memory::resize( global_reserve + zpl_megabytes(2) ); Memory::resize( global_reserve + zpl_megabytes(2) );
} }
zpl_array_init_reserve( IO::Sources, g_allocator, num ); 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 else
{ {
num = 1; num = 1;
zpl_array_init_reserve( IO::Sources, g_allocator, 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" ) ) if ( zpl_opts_has_arg( & opts, "src" ) )
{ {
zpl_string opt = zpl_opts_string( & opts, "src", "INVALID SRC ARGUMENT" ); zpl_string opt = zpl_opts_string( & opts, "src", "INVALID SRC ARGUMENT" );
if ( num == 1 ) if ( num == 1 )
{ {
zpl_string path = zpl_string_make_length( g_allocator, opt, zpl_string_length( opt )); 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; uw left = num;
do do
{ {
char* path = buffer; char* path = buffer;
sw length = 0; sw length = 0;
do do
{ {
path[length] = *opt; path[length] = *opt;
} }
while ( length++, opt++, *opt != ' ' && *opt != '\0' ); while ( length++, opt++, *opt != ' ' && *opt != '\0' );
zpl_string path_string = zpl_string_make_length( g_allocator, path, length ); zpl_string path_string = zpl_string_make_length( g_allocator, path, length );
zpl_array_append( IO::Sources, path_string ); zpl_array_append( IO::Sources, path_string );
@ -110,14 +110,14 @@ void parse_options( int num, char** arguments )
do do
{ {
path[length] = *opt; path[length] = *opt;
} }
while ( length++, opt++, *opt != ' ' && *opt != '\0' ); while ( length++, opt++, *opt != ' ' && *opt != '\0' );
zpl_string path_string = zpl_string_make_length( g_allocator, path, length ); zpl_string path_string = zpl_string_make_length( g_allocator, path, length );
zpl_array_append( IO::Destinations, path_string ); zpl_array_append( IO::Destinations, path_string );
opt++; opt++;
} }
while ( --left ); while ( --left );
if ( zpl_array_count(IO::Destinations) != zpl_array_count( IO::Sources ) ) if ( zpl_array_count(IO::Destinations) != zpl_array_count( IO::Sources ) )
@ -336,7 +336,7 @@ void refactor()
} }
restore( backup ); restore( backup );
} }
while (false); while (false);
// Word Ignores // Word Ignores
@ -390,7 +390,7 @@ void refactor()
u32 sig_length = zpl_string_length( ignore->Sig ); u32 sig_length = zpl_string_length( ignore->Sig );
current = zpl_string_append_length( current, src, sig_length ); current = zpl_string_append_length( current, src, sig_length );
if ( zpl_string_are_equal( ignore->Sig, current ) ) if ( zpl_string_are_equal( ignore->Sig, current ) )
{ {
u32 length = sig_length; u32 length = sig_length;
@ -568,7 +568,7 @@ void refactor()
Token entry {}; Token entry {};
entry.Start = pos; entry.Start = pos;
entry.End = pos + length; entry.End = pos + sig_length;
entry.Sig = nspace->Sig; entry.Sig = nspace->Sig;
buffer_size += sig_length; buffer_size += sig_length;
@ -576,7 +576,7 @@ void refactor()
if ( nspace->Sub != nullptr ) if ( nspace->Sub != nullptr )
{ {
entry.Sub = nspace->Sub; 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 ); zpl_array_append( tokens, entry );
@ -660,7 +660,7 @@ End_Search:
int main( int num, char** arguments ) int main( int num, char** arguments )
{ {
Memory::setup(); Memory::setup();
parse_options( num, arguments); parse_options( num, arguments);
IO::prepare(); IO::prepare();