Skip to content

Commit

Permalink
fix #13021: cmdline: validate option --std
Browse files Browse the repository at this point in the history
  • Loading branch information
ludviggunne committed Aug 26, 2024
1 parent 5b5e43e commit ad0f103
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
14 changes: 12 additions & 2 deletions cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1259,10 +1259,20 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
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;

/** --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

0 comments on commit ad0f103

Please sign in to comment.