Commit 2c835b4c authored by heretik's avatar heretik
Browse files

handle warnings

parent 5db9e19e
......@@ -6,7 +6,7 @@ set( CMAKE_CXX_STANDARD 17 )
set( CXX_STANDARD_REQUIRED ON )
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
add_compile_options(-Wall -Wextra -pedantic -Werror -Weffc++)
add_compile_options(-Wall -Wextra -pedantic -Weffc++)
endif()
find_package( Boost )
......
#include "block_data.hpp"
BlockData::BlockData( std::string msg )
BlockData::BlockData( std::string msg, bool has_errors )
: msg{ std::move( msg ) }
, has_errors{ has_errors }
{
// null
}
......
......@@ -5,13 +5,14 @@
#include <QTextBlockUserData>
class BlockData
: public QTextBlockUserData
: public QTextBlockUserData
{
public:
std::string msg = {};
bool has_errors = false;
public:
explicit BlockData( std::string msg );
BlockData( std::string msg, bool has_errors );
~BlockData() override;
};
......
......@@ -2,11 +2,11 @@
#include "block_data.hpp"
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include "utils/algorithm.hpp"
#include <QSettings>
#include <QStandardPaths>
#include <QToolTip>
#include <QUuid>
#include <filesystem>
#include <fstream>
#include <regex>
......@@ -19,7 +19,7 @@ MainWindow::MainWindow( QWidget *parent )
, process_{ parent }
, plain_text_{ nullptr }
, commands_{}
, line_and_errors_{}
, line_and_messages_{}
, syntax_highlighter_ptr_{}
{
// prepare qt things
......@@ -195,21 +195,24 @@ void MainWindow::update_blocks()
{
auto &document = *ui_ptr_->textEdit_code->document();
for( auto i = 0; i < document.blockCount(); ++i ) {
auto it = line_and_errors_.find( i + 1 );
auto it = line_and_messages_.find( i + 1 );
auto block = document.findBlockByNumber( i );
if( it != line_and_errors_.end() ) {
// transform std::vector<std::string> into one string
if( it != line_and_messages_.end() ) {
auto result = std::string{ "" };
auto first = true;
for( const auto &str : it->second ) {
if( not first ) result += "\n";
result += str;
first = false;
auto has_errors = false;
const auto &errors = it->second.errors;
if( not errors.empty() ) {
has_errors = true;
result = tr( "error(s):\n" ).toStdString() + utils::join( errors.begin(), errors.end(), "\n" );
}
const auto &warnings = it->second.warnings;
if( not warnings.empty() ) {
if( has_errors ) result += '\n';
result += tr( "warning(s):\n" ).toStdString() + utils::join( warnings.begin(), warnings.end(), "\n" );
}
// store message into the corresponding block
auto block_data_ptr = std::make_unique<BlockData>( result );
auto block_data_ptr = std::make_unique<BlockData>( result, has_errors );
block.setUserData( block_data_ptr.release() );
} else {
block.setUserData( nullptr );
......@@ -238,7 +241,7 @@ void MainWindow::disable()
ui_ptr_->plainTextEdit_compilation->clear();
ui_ptr_->plainTextEdit_io->clear();
plain_text_.reset( ui_ptr_->plainTextEdit_compilation );
line_and_errors_.clear();
line_and_messages_.clear();
update_blocks();
syntax_highlighter_ptr_->rehighlight();
}
......@@ -262,10 +265,14 @@ void MainWindow::append_text( const std::string &text, const QColor &color )
plain_text_->moveCursor( QTextCursor::End );
// analyse and save errors or warnings
static const std::regex error_regex{ R"(\.\.\/main\.cpp\:(\d+)\:\d+: error\: (.*))" };
static const std::regex error_regex{ R"(\.\.\/main\.cpp\:(\d+)\:\d+: (error|warning)\: (.*))" };
auto begin_it = std::sregex_iterator( text.begin(), text.end(), error_regex );
for( auto it = begin_it; it != std::sregex_iterator{}; ++it ) {
auto match = *it;
line_and_errors_[std::stoi( match.str( 1 ) )].push_back( match.str( 2 ) );
if( match.str( 2 ) == "error" ) {
line_and_messages_[std::stoi( match.str( 1 ) )].errors.push_back( match.str( 3 ) );
} else {
line_and_messages_[std::stoi( match.str( 1 ) )].warnings.push_back( match.str( 3 ) );
}
}
}
......@@ -47,6 +47,12 @@ private:
void restore_settings();
void update_blocks();
private:
struct Messages {
std::vector<std::string> warnings = {};
std::vector<std::string> errors = {};
};
private:
std::unique_ptr<Ui::MainWindow> ui_ptr_;
bool is_prepared_ = false;
......@@ -55,7 +61,7 @@ private:
std::experimental::observer_ptr<QPlainTextEdit> plain_text_;
std::vector<Command> commands_;
unsigned current_command_index_ = 0;
std::map<int, std::vector<std::string>> line_and_errors_;
std::map<int, Messages> line_and_messages_;
std::unique_ptr<SyntaxHighlighter> syntax_highlighter_ptr_;
};
......
......@@ -23,7 +23,7 @@ const auto keywords = {
};
const std::vector<std::pair<const char *, QColor>> regex_and_colors = {
{ "^#.*", Qt::darkYellow }, // standard types
{ "^#.*", Qt::darkYellow }, // preprocessor
{ "\\bstd::[a-z0-9]+\\b", Qt::blue }, // standard types
{ "\\b[A-Z][a-z0-9_]+", Qt::blue }, // types
{ "(\".*\")|(\\b\\d+\\b)", Qt::darkGreen }, // string literals
......@@ -35,6 +35,7 @@ const std::vector<std::pair<const char *, QColor>> regex_and_colors = {
SyntaxHighlighter::SyntaxHighlighter( QTextDocument *parent )
: QSyntaxHighlighter( parent )
, format_error_{}
, format_warning_{}
, format_multiline_comment_{}
, regex_comment_start_{ R"(/\*)" }
, regex_comment_end_{ R"(\*/)" }
......@@ -43,6 +44,8 @@ SyntaxHighlighter::SyntaxHighlighter( QTextDocument *parent )
// formats
format_error_.setUnderlineStyle( QTextCharFormat::WaveUnderline );
format_error_.setUnderlineColor( Qt::red );
format_warning_.setUnderlineStyle( QTextCharFormat::WaveUnderline );
format_warning_.setUnderlineColor( Qt::darkYellow );
format_multiline_comment_.setForeground( Qt::gray );
// constructs keyword regex
......@@ -78,8 +81,9 @@ void SyntaxHighlighter::highlightBlock( const QString &text )
// on errors, underline
auto *data_block_ptr = static_cast<BlockData*>( currentBlock().userData() );
if( data_block_ptr != nullptr ) {
setFormat( 0, text.length(), format_error_ );
format_to_merge = format_error_;
auto format = data_block_ptr->has_errors ? format_error_ : format_warning_;
setFormat( 0, text.length(), format );
format_to_merge = format;
}
// regex
......
......@@ -7,7 +7,7 @@
#include <vector>
class SyntaxHighlighter
: public QSyntaxHighlighter
: public QSyntaxHighlighter
{
Q_OBJECT
......@@ -19,6 +19,7 @@ protected:
private:
QTextCharFormat format_error_;
QTextCharFormat format_warning_;
QTextCharFormat format_multiline_comment_;
QRegularExpression regex_comment_start_;
QRegularExpression regex_comment_end_;
......
#ifndef UTILS_ALGORITHM_HPP
#define UTILS_ALGORITHM_HPP
namespace utils {
template<typename Iterator_>
auto join( Iterator_ begin, Iterator_ end, typename Iterator_::value_type separator )
-> typename Iterator_::value_type
{
typename Iterator_::value_type result = {};
auto first = true;
for( auto it = begin; it != end; ++it ) {
const auto &elt = *it;
if( not first ) result += separator;
result += elt;
first = false;
}
return result;
}
}
#endif
......@@ -24,12 +24,12 @@
<translation></translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.ui" line="91"/>
<location filename="../src/prototypator/mainwindow.ui" line="88"/>
<source>Compilation</source>
<translation></translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.ui" line="156"/>
<location filename="../src/prototypator/mainwindow.ui" line="153"/>
<source>Input/output</source>
<translation></translation>
</message>
......@@ -63,7 +63,19 @@
<translation></translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.cpp" line="227"/>
<location filename="../src/prototypator/mainwindow.cpp" line="206"/>
<source>error(s):
</source>
<translation></translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.cpp" line="211"/>
<source>warning(s):
</source>
<translation></translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.cpp" line="230"/>
<source>failed to start &apos;</source>
<translation></translation>
</message>
......
......@@ -24,12 +24,12 @@
<translation>Ctrl+D</translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.ui" line="91"/>
<location filename="../src/prototypator/mainwindow.ui" line="88"/>
<source>Compilation</source>
<translation>Compilation</translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.ui" line="156"/>
<location filename="../src/prototypator/mainwindow.ui" line="153"/>
<source>Input/output</source>
<translation>Entrée/sortie</translation>
</message>
......@@ -67,7 +67,21 @@
<translation> s&apos;est terminé avec le code </translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.cpp" line="227"/>
<location filename="../src/prototypator/mainwindow.cpp" line="206"/>
<source>error(s):
</source>
<translation>erreur(s) :
</translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.cpp" line="211"/>
<source>warning(s):
</source>
<translation>avertissement(s) :
</translation>
</message>
<message>
<location filename="../src/prototypator/mainwindow.cpp" line="230"/>
<source>failed to start &apos;</source>
<translation>impossible de démarrer &apos;</translation>
</message>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment