From 0b2c852a7ce62a9c7a378e0bc68be320c92cff3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Tue, 27 Aug 2024 09:48:46 +0200 Subject: [PATCH] use bool reference instead of extra enum variant to indicate unknown '--std' option --- cli/cmdlineparser.cpp | 18 ++++++------------ lib/importproject.cpp | 13 +++++++++++-- lib/importproject.h | 2 +- lib/keywords.cpp | 8 -------- lib/standards.h | 24 ++++++++++++++++-------- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 6b9eaac0b6c..de814f05a37 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -1257,23 +1257,17 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a // --std else if (std::strncmp(argv[i], "--std=", 6) == 0) { const std::string std = argv[i] + 6; + bool unknown = false; if (std::strncmp(std.c_str(), "c++", 3) == 0) { - const Standards::cppstd_t cppstd = Standards::getCPP(std); - if (cppstd == Standards::CPPInvalid) { - mLogger.printError("unknown --std value '" + std + "'"); - return Result::Fail; - } - mSettings.standards.cpp = cppstd; + mSettings.standards.cpp = Standards::getCPP(std, unknown); } else if (std::strncmp(std.c_str(), "c", 1) == 0) { - const Standards::cstd_t cstd = Standards::getC(std); - if (cstd == Standards::CInvalid) { - mLogger.printError("unknown --std value '" + std + "'"); - return Result::Fail; - } - mSettings.standards.c = cstd; + mSettings.standards.c = Standards::getC(std, unknown); } else { + unknown = true; + } + if (unknown) { mLogger.printError("unknown --std value '" + std + "'"); return Result::Fail; } diff --git a/lib/importproject.cpp b/lib/importproject.cpp index 336e25744c8..31088d80c64 100644 --- a/lib/importproject.cpp +++ b/lib/importproject.cpp @@ -268,7 +268,7 @@ static std::string unescape(const std::string &in) return out; } -void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command) +bool ImportProject::fsParseCommand(FileSettings& fs, const std::string& command) { std::string defs; @@ -311,6 +311,13 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command) } else if (F=='s' && startsWith(fval,"td")) { ++pos; fs.standard = readUntil(command, &pos, " "); + bool unknown_std = false; + (void)Standards::getCPP(fs.standard, unknown_std); + if (unknown_std) (void)Standards::getC(fs.standard, unknown_std); + if (unknown_std) { + printError("unkown --std value '" + fs.standard + "'"); + return false; + } } else if (F == 'i' && fval == "system") { ++pos; std::string isystem = readUntil(command, &pos, " "); @@ -339,6 +346,7 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command) } } fsSetDefines(fs, std::move(defs)); + return true; } bool ImportProject::importCompileCommands(std::istream &istr) @@ -416,7 +424,8 @@ bool ImportProject::importCompileCommands(std::istream &istr) return false; } FileSettings fs{std::move(path)}; - fsParseCommand(fs, command); // read settings; -D, -I, -U, -std, -m*, -f* + // read settings; -D, -I, -U, -std, -m*, -f* + if (!fsParseCommand(fs, command)) return false; std::map variables; fsSetIncludePaths(fs, directory, fs.includePaths, variables); fileSettings.push_back(std::move(fs)); diff --git a/lib/importproject.h b/lib/importproject.h index 7382b56eb8d..c112a0e068a 100644 --- a/lib/importproject.h +++ b/lib/importproject.h @@ -64,7 +64,7 @@ class CPPCHECKLIB WARN_UNUSED ImportProject { CPPCHECK_GUI }; - static void fsParseCommand(FileSettings& fs, const std::string& command); + static bool fsParseCommand(FileSettings& fs, const std::string& command); static void fsSetDefines(FileSettings& fs, std::string defs); static void fsSetIncludePaths(FileSettings& fs, const std::string &basepath, const std::list &in, std::map &variables); diff --git a/lib/keywords.cpp b/lib/keywords.cpp index 8ef2454922f..a26fc5bd786 100644 --- a/lib/keywords.cpp +++ b/lib/keywords.cpp @@ -181,8 +181,6 @@ const std::unordered_set& Keywords::getAll(Standards::cstd_t cStd) return c17_keywords_all; case Standards::cstd_t::C23: return c23_keywords_all; - case Standards::cstd_t::CInvalid: - cppcheck::unreachable(); } cppcheck::unreachable(); } @@ -205,8 +203,6 @@ const std::unordered_set& Keywords::getAll(Standards::cppstd_t cppS return cpp23_keywords_all; case Standards::cppstd_t::CPP26: return cpp26_keywords_all; - case Standards::cppstd_t::CPPInvalid: - cppcheck::unreachable(); } cppcheck::unreachable(); } @@ -226,8 +222,6 @@ const std::unordered_set& Keywords::getOnly(Standards::cstd_t cStd) return c17_keywords; case Standards::cstd_t::C23: return c23_keywords; - case Standards::cstd_t::CInvalid: - cppcheck::unreachable(); } cppcheck::unreachable(); } @@ -251,8 +245,6 @@ const std::unordered_set& Keywords::getOnly(Standards::cppstd_t cpp return cpp23_keywords; case Standards::cppstd_t::CPP26: return cpp26_keywords; - case Standards::cppstd_t::CPPInvalid: - cppcheck::unreachable(); } cppcheck::unreachable(); } diff --git a/lib/standards.h b/lib/standards.h index 1efe3ce7056..51f46633e18 100644 --- a/lib/standards.h +++ b/lib/standards.h @@ -38,10 +38,10 @@ struct Standards { enum Language : std::uint8_t { None, C, CPP }; /** C code standard */ - enum cstd_t : std::uint8_t { C89, C99, C11, C17, C23, CLatest = C23, CInvalid } c = CLatest; + enum cstd_t : std::uint8_t { C89, C99, C11, C17, C23, CLatest = C23 } c = CLatest; /** C++ code standard */ - enum cppstd_t : std::uint8_t { CPP03, CPP11, CPP14, CPP17, CPP20, CPP23, CPP26, CPPLatest = CPP26, CPPInvalid } cpp = CPPLatest; + enum cppstd_t : std::uint8_t { CPP03, CPP11, CPP14, CPP17, CPP20, CPP23, CPP26, CPPLatest = CPP26 } cpp = CPPLatest; /** --std value given on command line */ std::string stdValue; @@ -64,12 +64,15 @@ struct Standards { return "c17"; case C23: return "c23"; - case CInvalid: - return ""; } return ""; } static cstd_t getC(const std::string &std) { + bool _unused; + return getC(std, _unused); + } + static cstd_t getC(const std::string &std, bool &unknown) { + unknown = false; if (std == "c89") { return Standards::C89; } @@ -85,7 +88,8 @@ struct Standards { if (std == "c23") { return Standards::C23; } - return Standards::CInvalid; + unknown = true; + return Standards::CLatest; } bool setCPP(std::string str) { stdValue = str; @@ -112,12 +116,15 @@ struct Standards { return "c++23"; case CPP26: return "c++26"; - case CPPInvalid: - return ""; } return ""; } static cppstd_t getCPP(const std::string &std) { + bool _unused; + return getCPP(std, _unused); + } + static cppstd_t getCPP(const std::string &std, bool &unknown) { + unknown = false; if (std == "c++03") { return Standards::CPP03; } @@ -139,7 +146,8 @@ struct Standards { if (std == "c++26") { return Standards::CPP26; } - return Standards::CPPInvalid; + unknown = true; + return Standards::CPPLatest; } };