Skip to content

Commit

Permalink
use bool reference instead of extra enum variant to indicate unknown …
Browse files Browse the repository at this point in the history
…'--std' option
  • Loading branch information
ludviggunne committed Aug 27, 2024
1 parent 3202520 commit 0b2c852
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 31 deletions.
18 changes: 6 additions & 12 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 11 additions & 2 deletions lib/importproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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, " ");
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<std::string, std::string, cppcheck::stricmp> variables;
fsSetIncludePaths(fs, directory, fs.includePaths, variables);
fileSettings.push_back(std::move(fs));
Expand Down
2 changes: 1 addition & 1 deletion lib/importproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> &in, std::map<std::string, std::string, cppcheck::stricmp> &variables);

Expand Down
8 changes: 0 additions & 8 deletions lib/keywords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,6 @@ const std::unordered_set<std::string>& 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();
}
Expand All @@ -205,8 +203,6 @@ const std::unordered_set<std::string>& 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();
}
Expand All @@ -226,8 +222,6 @@ const std::unordered_set<std::string>& 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();
}
Expand All @@ -251,8 +245,6 @@ const std::unordered_set<std::string>& 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();
}
Expand Down
24 changes: 16 additions & 8 deletions lib/standards.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
Expand All @@ -139,7 +146,8 @@ struct Standards {
if (std == "c++26") {
return Standards::CPP26;
}
return Standards::CPPInvalid;
unknown = true;
return Standards::CPPLatest;
}
};

Expand Down

0 comments on commit 0b2c852

Please sign in to comment.