From 5cc2dbdbca2575ad5c88917cf9a5726971486d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Tue, 19 Apr 2022 13:18:01 +0200 Subject: [PATCH 01/12] abstract-parser: drop dynamic_cast in favour of AbstractParser::inputFormat --- src/abstract-parser.cc | 14 -------------- src/abstract-parser.hh | 8 +++++++- src/csparser.hh | 6 +++++- src/gcc-parser.hh | 6 +++++- src/json-parser.hh | 6 +++++- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/abstract-parser.cc b/src/abstract-parser.cc index cedfb970..fc27e94d 100644 --- a/src/abstract-parser.cc +++ b/src/abstract-parser.cc @@ -63,17 +63,3 @@ AbstractParserPtr createParser(InStream &input) // GCC return make_unique(input); } - -EFileFormat Parser::inputFormat() const -{ - if (dynamic_cast(parser_.get())) - return FF_JSON; - - if (dynamic_cast(parser_.get())) - return FF_COVERITY; - - if (dynamic_cast(parser_.get())) - return FF_GCC; - - return FF_INVALID; -} diff --git a/src/abstract-parser.hh b/src/abstract-parser.hh index d3972080..2cace5bc 100644 --- a/src/abstract-parser.hh +++ b/src/abstract-parser.hh @@ -50,6 +50,10 @@ class AbstractParser { return emptyProps_; } + virtual EFileFormat inputFormat() const { + return FF_INVALID; + } + protected: AbstractParser() = default; @@ -91,7 +95,9 @@ class Parser { return parser_->getScanProps(); } - EFileFormat inputFormat() const; + EFileFormat inputFormat() const { + return parser_->inputFormat(); + }; private: InStream &input_; diff --git a/src/csparser.hh b/src/csparser.hh index 4eceed05..2146a232 100644 --- a/src/csparser.hh +++ b/src/csparser.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011-2012 Red Hat, Inc. + * Copyright (C) 2011-2022 Red Hat, Inc. * * This file is part of csdiff. * @@ -30,6 +30,10 @@ class CovParser: public AbstractParser { bool getNext(Defect *) override; bool hasError() const override; + EFileFormat inputFormat() const override { + return FF_COVERITY; + } + private: CovParser(const Parser &); CovParser& operator=(const Parser &); diff --git a/src/gcc-parser.hh b/src/gcc-parser.hh index 559d063b..c6657632 100644 --- a/src/gcc-parser.hh +++ b/src/gcc-parser.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Red Hat, Inc. + * Copyright (C) 2013-2022 Red Hat, Inc. * * This file is part of csdiff. * @@ -30,6 +30,10 @@ class GccParser: public AbstractParser { bool getNext(Defect *) override; bool hasError() const override; + EFileFormat inputFormat() const override { + return FF_GCC; + } + private: GccParser(const Parser &); GccParser& operator=(const Parser &); diff --git a/src/json-parser.hh b/src/json-parser.hh index 44439f09..0f4ad3ac 100644 --- a/src/json-parser.hh +++ b/src/json-parser.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2012 Red Hat, Inc. + * Copyright (C) 2012-2022 Red Hat, Inc. * * This file is part of csdiff. * @@ -31,6 +31,10 @@ class JsonParser: public AbstractParser { bool hasError() const override; const TScanProps& getScanProps() const override; + EFileFormat inputFormat() const override { + return FF_JSON; + } + private: JsonParser(const Parser &); JsonParser& operator=(const Parser &); From 2fe057f2b2ca117adb7f59f4068aeaefad42bae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Thu, 26 May 2022 14:32:32 +0200 Subject: [PATCH 02/12] color: mark constant methods const --- src/color.cc | 6 +++--- src/color.hh | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/color.cc b/src/color.cc index 0951f57a..9a4ae7c1 100644 --- a/src/color.cc +++ b/src/color.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014 Red Hat, Inc. + * Copyright (C) 2014-2022 Red Hat, Inc. * * This file is part of csdiff. * @@ -39,7 +39,7 @@ ColorWriter::ColorWriter(const std::ostream &str, const EColorMode cm) } } -const char* ColorWriter::setColor(const EColor color) +const char* ColorWriter::setColor(const EColor color) const { if (!enabled_) return ""; @@ -55,7 +55,7 @@ const char* ColorWriter::setColor(const EColor color) return ""; } -const char* ColorWriter::setColorIf(bool cond, const EColor color) +const char* ColorWriter::setColorIf(bool cond, const EColor color) const { return (cond) ? this->setColor(color) : ""; } diff --git a/src/color.hh b/src/color.hh index dd648df3..c20f7f76 100644 --- a/src/color.hh +++ b/src/color.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014 Red Hat, Inc. + * Copyright (C) 2014-2022 Red Hat, Inc. * * This file is part of csdiff. * @@ -39,8 +39,8 @@ enum EColor { class ColorWriter { public: ColorWriter(const std::ostream &str, EColorMode); - const char* setColor(EColor); - const char* setColorIf(bool, EColor); + const char* setColor(EColor) const; + const char* setColorIf(bool, EColor) const; private: bool enabled_; From 1e7db3ca25d905ef2a492a4440767e43f30d1b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Wed, 3 Aug 2022 13:18:03 +0200 Subject: [PATCH 03/12] csdiff: use using instead of typedef --- src/csdiff.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/csdiff.cc b/src/csdiff.cc index 562883dd..052e73de 100644 --- a/src/csdiff.cc +++ b/src/csdiff.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Red Hat, Inc. + * Copyright (C) 2011-2022 Red Hat, Inc. * * This file is part of csdiff. * @@ -37,7 +37,7 @@ int main(int argc, char *argv[]) po::options_description desc(string("Usage: ") + name + " [options] old.err new.err, where options are"); - typedef std::vector TStringList; + using TStringList = std::vector; string mode; try { From 3953d31386f861a6a0ebd5b1308d434312a8281f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Wed, 3 Aug 2022 13:18:16 +0200 Subject: [PATCH 04/12] csdiff: fix typos --- src/csdiff.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/csdiff.cc b/src/csdiff.cc index 052e73de..a2de454a 100644 --- a/src/csdiff.cc +++ b/src/csdiff.cc @@ -124,7 +124,7 @@ int main(int argc, char *argv[]) const RE reSubst("([^,]+),(.*)"); for (const string &subst : substList) { if (!boost::regex_match(subst, sm, reSubst)) { - std::cerr << "bad substutution format: " << subst + std::cerr << "bad substitution format: " << subst << std::endl << "use: -s OLD,NEW" << std::endl; return 1; } From 796457479bda2c926391665e26d75b58cca8929f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Wed, 3 Aug 2022 13:29:48 +0200 Subject: [PATCH 05/12] csfilter: use using instead of typedef --- src/csfilter.cc | 2 +- src/csfilter.hh | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/csfilter.cc b/src/csfilter.cc index 3a149701..b5ed822f 100644 --- a/src/csfilter.cc +++ b/src/csfilter.cc @@ -58,7 +58,7 @@ struct MsgReplace { } }; -typedef std::vector TMsgReplaceList; +using TMsgReplaceList = std::vector; struct MsgFilter::Private { bool ignorePath = false; diff --git a/src/csfilter.hh b/src/csfilter.hh index 00389700..7cad8d61 100644 --- a/src/csfilter.hh +++ b/src/csfilter.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Red Hat, Inc. + * Copyright (C) 2011-2022 Red Hat, Inc. * * This file is part of csdiff. * @@ -26,8 +26,8 @@ #include #include -typedef std::vector TStringList; -typedef std::map TSubstMap; +using TStringList = std::vector; +using TSubstMap = std::map; class MsgFilter { public: From b39d716635e68846a0e256f344584566bc05c922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Wed, 3 Aug 2022 15:52:23 +0200 Subject: [PATCH 06/12] instream: use == instead of !s1.compare(s2) --- src/instream.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/instream.cc b/src/instream.cc index f73b8c27..9462177c 100644 --- a/src/instream.cc +++ b/src/instream.cc @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 - 2021 Red Hat, Inc. + * Copyright (C) 2011 - 2022 Red Hat, Inc. * * This file is part of csdiff. * @@ -23,7 +23,7 @@ InStream::InStream(const std::string &fileName, const bool silent): fileName_(fileName), silent_(silent), anyError_(false), - str_((!fileName_.compare("-")) + str_((fileName_ == "-") ? std::cin : fileStr_) { From 87082d19e86b50dc07410b42c6db4377b6e36887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Wed, 3 Aug 2022 15:54:34 +0200 Subject: [PATCH 07/12] instream: use std::ifstream instead of more generic std::fstream * The constructor call is more succinct. --- src/instream.cc | 2 +- src/instream.hh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/instream.cc b/src/instream.cc index 9462177c..3dae93ef 100644 --- a/src/instream.cc +++ b/src/instream.cc @@ -28,7 +28,7 @@ InStream::InStream(const std::string &fileName, const bool silent): : fileStr_) { if (&str_ == &fileStr_) - fileStr_.open(fileName_, std::ios::in); + fileStr_.open(fileName_); if (!fileStr_) throw InFileException(fileName_); diff --git a/src/instream.hh b/src/instream.hh index 48100e27..c15a167b 100644 --- a/src/instream.hh +++ b/src/instream.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 - 2021 Red Hat, Inc. + * Copyright (C) 2011 - 2022 Red Hat, Inc. * * This file is part of csdiff. * @@ -53,7 +53,7 @@ class InStream { const std::string fileName_; const bool silent_; bool anyError_; - std::fstream fileStr_; + std::ifstream fileStr_; std::istream &str_; }; From cd9e94b6edccfc7878e41da5c172e4ac2c1be3e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Wed, 3 Aug 2022 15:57:21 +0200 Subject: [PATCH 08/12] instream: use defaulted default destructor There is no point in closing std::ifstream in a destructor manually since that will be done either way in its own destructor. --- src/instream.cc | 6 ------ src/instream.hh | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/instream.cc b/src/instream.cc index 3dae93ef..de89fcb7 100644 --- a/src/instream.cc +++ b/src/instream.cc @@ -41,12 +41,6 @@ InStream::InStream(std::istringstream &str, const bool silent): { } -InStream::~InStream() -{ - if (&str_ == &fileStr_) - fileStr_.close(); -} - void InStream::handleError(const std::string msg, const long line) { anyError_ = true; diff --git a/src/instream.hh b/src/instream.hh index c15a167b..35d4d6fe 100644 --- a/src/instream.hh +++ b/src/instream.hh @@ -40,7 +40,7 @@ class InStream { public: InStream(const std::string &fileName, bool silent = false); InStream(std::istringstream &str, bool silent = false); - ~InStream(); + ~InStream() = default; const std::string& fileName() const { return fileName_; } std::istream& str() const { return str_; } From 80ea97fb47269c721b8b5bc813d023842e7e9ffd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Wed, 3 Aug 2022 17:09:14 +0200 Subject: [PATCH 09/12] instream: change type of line in handleError to unsigned long ... because negative line number does not make any sense and boost::file_parser_error::line also returns unsigned long.and pass msg by a const reference. --- src/instream.cc | 2 +- src/instream.hh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/instream.cc b/src/instream.cc index de89fcb7..412bceba 100644 --- a/src/instream.cc +++ b/src/instream.cc @@ -41,7 +41,7 @@ InStream::InStream(std::istringstream &str, const bool silent): { } -void InStream::handleError(const std::string msg, const long line) +void InStream::handleError(const std::string &msg, const unsigned long line) { anyError_ = true; if (silent_ || msg.empty()) diff --git a/src/instream.hh b/src/instream.hh index 35d4d6fe..c2ea4c5f 100644 --- a/src/instream.hh +++ b/src/instream.hh @@ -47,7 +47,7 @@ class InStream { bool silent() const { return silent_; } bool anyError() const { return anyError_; } - void handleError(std::string msg = std::string(), long line = 0L); + void handleError(const std::string &msg = "", unsigned long line = 0UL); private: const std::string fileName_; From 4c1ec90b8cc586f95725d3e0aedc25db2e730ba7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Wed, 3 Aug 2022 17:16:24 +0200 Subject: [PATCH 10/12] instream: pass std::string by value and then move it in constructors ... to avoid excessively copies of std::string rvalues. --- src/instream.cc | 4 ++-- src/instream.hh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/instream.cc b/src/instream.cc index 412bceba..9ca309fd 100644 --- a/src/instream.cc +++ b/src/instream.cc @@ -19,8 +19,8 @@ #include "instream.hh" -InStream::InStream(const std::string &fileName, const bool silent): - fileName_(fileName), +InStream::InStream(std::string fileName, const bool silent): + fileName_(std::move(fileName)), silent_(silent), anyError_(false), str_((fileName_ == "-") diff --git a/src/instream.hh b/src/instream.hh index c2ea4c5f..8a34d65f 100644 --- a/src/instream.hh +++ b/src/instream.hh @@ -38,7 +38,7 @@ struct InFileException { class InStream { public: - InStream(const std::string &fileName, bool silent = false); + InStream(std::string fileName, bool silent = false); InStream(std::istringstream &str, bool silent = false); ~InStream() = default; From 7472caf308fe305dfd724320b16303e321c5c69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Wed, 3 Aug 2022 17:23:39 +0200 Subject: [PATCH 11/12] instream: use default member initializers for literals --- src/instream.cc | 2 -- src/instream.hh | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/instream.cc b/src/instream.cc index 9ca309fd..576a661f 100644 --- a/src/instream.cc +++ b/src/instream.cc @@ -22,7 +22,6 @@ InStream::InStream(std::string fileName, const bool silent): fileName_(std::move(fileName)), silent_(silent), - anyError_(false), str_((fileName_ == "-") ? std::cin : fileStr_) @@ -36,7 +35,6 @@ InStream::InStream(std::string fileName, const bool silent): InStream::InStream(std::istringstream &str, const bool silent): silent_(silent), - anyError_(false), str_(str) { } diff --git a/src/instream.hh b/src/instream.hh index 8a34d65f..924bb058 100644 --- a/src/instream.hh +++ b/src/instream.hh @@ -52,7 +52,7 @@ class InStream { private: const std::string fileName_; const bool silent_; - bool anyError_; + bool anyError_ = false; std::ifstream fileStr_; std::istream &str_; }; From d9392907af92621c680334ba5a1cca70418ff1d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Wed, 3 Aug 2022 17:24:25 +0200 Subject: [PATCH 12/12] instream: make argument identifiers of InStreamLookAhead consistent ... between declaration and definition. --- src/instream.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/instream.hh b/src/instream.hh index 924bb058..8abf8229 100644 --- a/src/instream.hh +++ b/src/instream.hh @@ -60,7 +60,7 @@ class InStream { class InStreamLookAhead { public: InStreamLookAhead( - InStream &inStr, + InStream &input, unsigned size, bool skipWhiteSpaces = false);