Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #13021: cmdline: validate option --std #6723

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1257,12 +1257,21 @@ 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;
// TODO: print error when standard is unknown
if (std::strncmp(std.c_str(), "c++", 3) == 0) {
mSettings.standards.cpp = Standards::getCPP(std);
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;
}
else if (std::strncmp(std.c_str(), "c", 1) == 0) {
mSettings.standards.c = Standards::getC(std);
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;
}
else {
mLogger.printError("unknown --std value '" + std + "'");
Expand Down
8 changes: 8 additions & 0 deletions lib/keywords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ 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 @@ -203,6 +205,8 @@ 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 @@ -222,6 +226,8 @@ 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 @@ -245,6 +251,8 @@ 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
12 changes: 8 additions & 4 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 } c = CLatest;
enum cstd_t : std::uint8_t { C89, C99, C11, C17, C23, CLatest = C23, CInvalid } c = CLatest;

/** C++ code standard */
enum cppstd_t : std::uint8_t { CPP03, CPP11, CPP14, CPP17, CPP20, CPP23, CPP26, CPPLatest = CPP26 } cpp = CPPLatest;
enum cppstd_t : std::uint8_t { CPP03, CPP11, CPP14, CPP17, CPP20, CPP23, CPP26, CPPLatest = CPP26, CPPInvalid } cpp = CPPLatest;
danmar marked this conversation as resolved.
Show resolved Hide resolved

/** --std value given on command line */
std::string stdValue;
Expand All @@ -64,6 +64,8 @@ struct Standards {
return "c17";
case C23:
return "c23";
case CInvalid:
return "";
}
return "";
}
Expand All @@ -83,7 +85,7 @@ struct Standards {
if (std == "c23") {
return Standards::C23;
}
return Standards::CLatest;
return Standards::CInvalid;
}
bool setCPP(std::string str) {
stdValue = str;
Expand All @@ -110,6 +112,8 @@ struct Standards {
return "c++23";
case CPP26:
return "c++26";
case CPPInvalid:
return "";
}
return "";
}
Expand All @@ -135,7 +139,7 @@ struct Standards {
if (std == "c++26") {
return Standards::CPP26;
}
return Standards::CPPLatest;
return Standards::CPPInvalid;
}
};

Expand Down
4 changes: 2 additions & 2 deletions test/testcmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1369,8 +1369,8 @@ class TestCmdlineParser : public TestFixture {
void stdunknown2() {
REDIRECT;
const char *const argv[] = {"cppcheck", "--std=cplusplus11", "file.cpp"};
TODO_ASSERT_EQUALS(static_cast<int>(CmdLineParser::Result::Fail), static_cast<int>(CmdLineParser::Result::Success), static_cast<int>(parser->parseFromArgs(3, argv)));
TODO_ASSERT_EQUALS("cppcheck: error: unknown --std value 'cplusplus11'\n", "", logger->str());
ASSERT_EQUALS(static_cast<int>(CmdLineParser::Result::Fail), static_cast<int>(parser->parseFromArgs(3, argv)));
ASSERT_EQUALS("cppcheck: error: unknown --std value 'cplusplus11'\n", logger->str());
}

void platformWin64() {
Expand Down
Loading