Skip to content

Commit

Permalink
use new formatter for ERROR_PRINT, added ERROR_PRINTLN (#595)
Browse files Browse the repository at this point in the history
* use new formatter for ERROR_PRINT, added ERROR_PRINTLN

* remove old log define

* fix comments
  • Loading branch information
turleypol committed Jan 12, 2024
1 parent 43b776b commit 0e675c5
Show file tree
Hide file tree
Showing 219 changed files with 1,229 additions and 1,151 deletions.
4 changes: 2 additions & 2 deletions pol-core/bscript/StoredToken.cpp
Expand Up @@ -14,8 +14,8 @@ StoredToken::StoredToken( unsigned char aModule, int aID, BTokenType aType, unsi
{
if ( offset != aOffset )
{
ERROR_PRINT << "Data segment overflowed.\n"
<< "Flog the programmer for using 2-byte offsets in datafiles.\n";
ERROR_PRINTLN(
"Data segment overflowed.\nFlog the programmer for using 2-byte offsets in datafiles." );
throw std::runtime_error( "Data segment overflowed" );
}
}
Expand Down
15 changes: 6 additions & 9 deletions pol-core/bscript/compctx.cpp
Expand Up @@ -156,15 +156,12 @@ void CompilerContext::printOn( fmt::Writer& writer ) const
{
writer << "File: " << filename << ", Line " << line << "\n";
}
} // namespace Bscript
} // namespace Pol

void CompilerContext::printOnShort( std::ostream& os ) const
{
os << filename << ", Line " << line << std::endl;
}

void CompilerContext::printOnShort( fmt::Writer& writer ) const
fmt::format_context::iterator fmt::formatter<Pol::Bscript::CompilerContext>::format(
const Pol::Bscript::CompilerContext& c, fmt::format_context& ctx ) const
{
writer << filename << ", Line " << line << "\n";
return fmt::formatter<std::string>::format(
fmt::format( "File: {}, Line {}", c.filename, c.line ), ctx );
}
} // namespace Bscript
} // namespace Pol
42 changes: 17 additions & 25 deletions pol-core/bscript/compctx.h
Expand Up @@ -10,8 +10,10 @@
#include "compilercfg.h"

#include <iosfwd>
#include <iterator>
#include <string>

#include <fmt/format.h>
#include <format/format.h>

namespace Pol
Expand All @@ -34,8 +36,6 @@ class CompilerContext

void printOn( std::ostream& os ) const;
void printOn( fmt::Writer& writer ) const;
void printOnShort( std::ostream& os ) const;
void printOnShort( fmt::Writer& writer ) const;

void skipws();
int skipcomments();
Expand Down Expand Up @@ -67,43 +67,35 @@ inline fmt::Writer& operator<<( fmt::Writer& writer, const CompilerContext& ctx
return writer;
}

namespace
{
inline void rec_write( fmt::Writer& /*w*/ ) {}
template <typename T, typename... Targs>
inline void rec_write( fmt::Writer& w, T&& value, Targs&&... Fargs )
{
w << value;
rec_write( w, std::forward<Targs>( Fargs )... );
}
} // namespace

template <typename... Args>
inline void compiler_warning( CompilerContext* ctx, Args&&... args )
template <typename Str, typename... Args>
inline void compiler_warning( CompilerContext* ctx, Str const& format, Args&&... args )
{
if ( compilercfg.DisplayWarnings || compilercfg.ErrorOnWarning )
{
fmt::Writer w;
rec_write( w, std::forward<Args>( args )... );
std::string out = fmt::format( format, args... );

if ( compilercfg.ErrorOnWarning )
throw std::runtime_error( w.str() );
throw std::runtime_error( out );
else
{
if ( ctx != nullptr )
w << *ctx;
ERROR_PRINT << w.c_str();
fmt::format_to( std::back_inserter( out ), "{}", *ctx );
ERROR_PRINTLN( out );
}
}
}

template <typename... Args>
inline void compiler_error( Args&&... args )
template <typename Str, typename... Args>
inline void compiler_error( Str const& format, Args&&... args )
{
fmt::Writer w;
rec_write( w, std::forward<Args>( args )... );
ERROR_PRINT << w.str();
ERROR_PRINTLN( format, args... );
}
} // namespace Bscript
} // namespace Pol
template <>
struct fmt::formatter<Pol::Bscript::CompilerContext> : fmt::formatter<std::string>
{
fmt::format_context::iterator format( const Pol::Bscript::CompilerContext& c,
fmt::format_context& ctx ) const;
};
#endif
2 changes: 1 addition & 1 deletion pol-core/bscript/compiler/Compiler.cpp
Expand Up @@ -96,7 +96,7 @@ bool Compiler::compile_file( const std::string& filename )
}
catch ( std::exception& ex )
{
ERROR_PRINT << ex.what() << '\n';
ERROR_PRINTLN( ex.what() );
success = false;
}
return success;
Expand Down
14 changes: 8 additions & 6 deletions pol-core/bscript/compiler/Report.cpp
@@ -1,12 +1,12 @@
#include "Report.h"

#include "clib/logfacility.h"
#include "bscript/compiler/file/SourceLocation.h"
#include "clib/logfacility.h"

namespace Pol::Bscript::Compiler
{
Report::Report( bool display_warnings )
: display_warnings( display_warnings ), errors( 0 ), warnings( 0 )
: display_warnings( display_warnings ), errors( 0 ), warnings( 0 )
{
}

Expand All @@ -15,8 +15,9 @@ void Report::report_error( const SourceLocation& source_location, const char* ms
++errors;
try
{
ERROR_PRINT << source_location << ": error: " << msg;
} catch (...)
ERROR_PRINTLN( "{}: error: {}", source_location, msg );
}
catch ( ... )
{
}
}
Expand All @@ -26,8 +27,9 @@ void Report::report_warning( const SourceLocation& source_location, const char*
++warnings;
try
{
ERROR_PRINT << source_location << ": warning: " << msg;
} catch (...)
ERROR_PRINTLN( "{}: warning: {}", source_location, msg );
}
catch ( ... )
{
}
}
Expand Down
67 changes: 23 additions & 44 deletions pol-core/bscript/compiler/Report.h
Expand Up @@ -16,59 +16,51 @@ class Report
Report( const Report& ) = delete;
Report& operator=( const Report& ) = delete;

// Always put a newline at the end of the message.
template <typename... Args>
inline void error( const SourceLocation& source_location, Args&&... args )
template <typename Str, typename... Args>
inline void error( const SourceLocation& source_location, Str const& format, Args&&... args )
{
fmt::Writer w;
rec_write( w, std::forward<Args>( args )... );
report_error( source_location, w.c_str() );
auto msg = fmt::format( format, args... );
report_error( source_location, msg.c_str() );
}

// Always put a newline at the end of the message.
template <typename... Args>
inline void error( const SourceFileIdentifier& ident, Args&&... args )
template <typename Str, typename... Args>
inline void error( const SourceFileIdentifier& ident, Str const& format, Args&&... args )
{
SourceLocation loc( &ident, 0, 0 );
error( loc, args... );
error( loc, format, args... );
}

// Always put a newline at the end of the message.
template <typename... Args>
inline void error( const Node& node, Args&&... args )
template <typename Str, typename... Args>
inline void error( const Node& node, Str const& format, Args&&... args )
{
error( node.source_location, args... );
error( node.source_location, format, args... );
}

// Report.fatal: use this when it's not possible to continue after a user-facing error.
//
// Always put a newline at the end of the message.
template <typename... Args>
[[noreturn]] inline void fatal( const SourceLocation& source_location, Args&&... args )
template <typename Str, typename... Args>
[[noreturn]] inline void fatal( const SourceLocation& source_location, Str const& format,
Args&&... args )
{
fmt::Writer w;
rec_write( w, std::forward<Args>( args )... );
report_error( source_location, w.c_str() );
throw std::runtime_error( w.c_str() );
auto msg = fmt::format( format, args... );
report_error( source_location, msg.c_str() );
throw std::runtime_error( msg.c_str() );
}

// Always put a newline at the end of the message.
template <typename... Args>
inline void warning( const SourceLocation& source_location, Args&&... args )
template <typename Str, typename... Args>
inline void warning( const SourceLocation& source_location, Str const& format, Args&&... args )
{
if ( display_warnings )
{
fmt::Writer w;
rec_write( w, std::forward<Args>( args )... );
report_warning( source_location, w.c_str() );
auto msg = fmt::format( format, args... );
report_warning( source_location, msg.c_str() );
}
}

// Always put a newline at the end of the message.
template <typename... Args>
inline void warning( const Node& node, Args&&... args )
template <typename Str, typename... Args>
inline void warning( const Node& node, Str const& format, Args&&... args )
{
warning( node.source_location, args... );
warning( node.source_location, format, args... );
}

[[nodiscard]] unsigned error_count() const;
Expand All @@ -78,19 +70,6 @@ class Report
void report_error( const SourceLocation&, const char* msg );
void report_warning( const SourceLocation&, const char* msg );

inline void rec_write( fmt::Writer& /*w*/ ) {}
template <typename T, typename... Targs>
inline void rec_write( fmt::Writer& w, T&& value, Targs&&... Fargs )
{
try
{
w << value;
} catch(...)
{
}
rec_write( w, std::forward<Targs>( Fargs )... );
}

const bool display_warnings;
unsigned errors;
unsigned warnings;
Expand Down
15 changes: 8 additions & 7 deletions pol-core/bscript/compiler/analyzer/Constants.cpp
Expand Up @@ -23,15 +23,16 @@ void Constants::create( ConstDeclaration& constant )
// The OG compiler ignores enum members with the same name as a
// constant or enum that was defined earlier.

report.warning( constant, "Constant '", constant.identifier,
"' definition ignored due to an earlier definition.\n",
" Previous definition at: ",
( *itr ).second->source_location, "\n" );
report.warning( constant,
"Constant '{}' definition ignored due to an earlier definition.\n"
" Previous definition at: {}",
constant.identifier, ( *itr ).second->source_location );
return;
}
report.error( constant, "Constant '", constant.identifier, "' defined more than once.\n",
" Previous definition at: ",
( *itr ).second->source_location, "\n" );
report.error( constant,
"Constant '{}' defined more than once.\n"
" Previous definition at: {}",
constant.identifier, ( *itr ).second->source_location );
}

constants[constant.identifier] = &constant;
Expand Down
12 changes: 6 additions & 6 deletions pol-core/bscript/compiler/analyzer/FlowControlScope.cpp
Expand Up @@ -9,12 +9,12 @@ namespace Pol::Bscript::Compiler
FlowControlScope::FlowControlScope( FlowControlScopes& flow_control_scopes,
const SourceLocation& source_location, std::string name,
std::shared_ptr<FlowControlLabel> flow_control_label )
: source_location( source_location ),
name( std::move( name ) ),
flow_control_label( std::move( flow_control_label ) ),
local_variables_size( flow_control_scopes.local_variables.count() ),
parent_scope( flow_control_scopes.current_unnamed_scope ),
flow_control_scopes( flow_control_scopes )
: source_location( source_location ),
name( std::move( name ) ),
flow_control_label( std::move( flow_control_label ) ),
local_variables_size( flow_control_scopes.local_variables.count() ),
parent_scope( flow_control_scopes.current_unnamed_scope ),
flow_control_scopes( flow_control_scopes )
{
flow_control_scopes.push( this );
}
Expand Down
17 changes: 9 additions & 8 deletions pol-core/bscript/compiler/analyzer/FlowControlScopes.cpp
@@ -1,15 +1,15 @@
#include "FlowControlScopes.h"

#include "bscript/compiler/analyzer/FlowControlScope.h"
#include "bscript/compiler/Report.h"
#include "bscript/compiler/analyzer/FlowControlScope.h"

namespace Pol::Bscript::Compiler
{
FlowControlScopes::FlowControlScopes( const Variables& local_variables, Report& report )
: current_unnamed_scope( nullptr ),
named_scopes(),
local_variables( local_variables ),
report( report )
: current_unnamed_scope( nullptr ),
named_scopes(),
local_variables( local_variables ),
report( report )
{
}

Expand Down Expand Up @@ -43,9 +43,10 @@ void FlowControlScopes::push( const FlowControlScope* scope )
auto ins = named_scopes.insert( { scope->name, scope } );
if ( !ins.second )
{
report.error( scope->source_location, "A statement with label '", scope->name,
"' is already in scope.\n",
" See also: ", ( *ins.first ).second->source_location, "\n" );
report.error( scope->source_location,
"A statement with label '{}' is already in scope.\n"
" See also: {}",
scope->name, ( *ins.first ).second->source_location );
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions pol-core/bscript/compiler/analyzer/LocalVariableScope.cpp
Expand Up @@ -40,15 +40,15 @@ std::shared_ptr<Variable> LocalVariableScope::create( const std::string& name, W
{
if ( existing->block_depth == block_depth )
{
report.error( source_location, "Variable '", name, "' is already in scope.\n",
" See previous definition at: ",
existing->source_location, "\n" );
report.error( source_location,
"Variable '{}' is already in scope.\n"
" See previous definition at: {}",
name, existing->source_location );
return existing;
}
shadowing.push_back( existing );
}
auto local = scopes.local_variables.create( name, block_depth, warn_on,
source_location );
auto local = scopes.local_variables.create( name, block_depth, warn_on, source_location );

local_variable_scope_info.variables.push_back( local );

Expand Down

0 comments on commit 0e675c5

Please sign in to comment.