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 5 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
9 changes: 6 additions & 3 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1257,14 +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;
// TODO: print error when standard is unknown
bool unknown = false;
if (std::strncmp(std.c_str(), "c++", 3) == 0) {
mSettings.standards.cpp = Standards::getCPP(std);
mSettings.standards.cpp = Standards::getCPP(std, unknown);
}
else if (std::strncmp(std.c_str(), "c", 1) == 0) {
mSettings.standards.c = Standards::getC(std);
mSettings.standards.c = Standards::getC(std, unknown);
}
else {
unknown = true;
}
if (unknown) {
mLogger.printError("unknown --std value '" + std + "'");
return Result::Fail;
}
Expand Down
15 changes: 12 additions & 3 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 All @@ -289,7 +289,7 @@ void ImportProject::fsParseCommand(FileSettings& fs, const std::string& command)
while (pos < command.size() && command[pos] == ' ')
++pos;
}
const std::string fval = readUntil(command, &pos, " =");
const std::string fval = readUntil(command, &pos, " =:");
if (F=='D') {
std::string defval = readUntil(command, &pos, " ");
defs += fval;
Expand All @@ -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("unknown --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
12 changes: 12 additions & 0 deletions lib/standards.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ struct Standards {
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 @@ -83,6 +88,7 @@ struct Standards {
if (std == "c23") {
return Standards::C23;
}
unknown = true;
return Standards::CLatest;
}
bool setCPP(std::string str) {
Expand Down Expand Up @@ -114,6 +120,11 @@ struct Standards {
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 @@ -135,6 +146,7 @@ struct Standards {
if (std == "c++26") {
return Standards::CPP26;
}
unknown = true;
return Standards::CPPLatest;
}
};
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