Skip to content

Replace runtime C++ version detection with compile-time extraction from argv #349

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

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 14 additions & 25 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,20 @@ namespace xcpp
xeus::register_interpreter(this);
}

static std::string get_stdopt()
static std::string get_stdopt(int argc, const char* const* argv)
{
// We need to find what's the C++ version the interpreter runs with.
const char* code = R"(
int __get_cxx_version () {
#if __cplusplus > 202302L
return 26;
#elif __cplusplus > 202002L
return 23;
#elif __cplusplus > 201703L
return 20;
#elif __cplusplus > 201402L
return 17;
#elif __cplusplus > 201103L || (defined(_WIN32) && _MSC_VER >= 1900)
return 14;
#elif __cplusplus >= 201103L
return 11;
#else
return 0;
#endif
}
__get_cxx_version ()
)";
auto cxx_version = Cpp::Evaluate(code);
return std::to_string(cxx_version);
std::string res = "14";
for (int i = 0; i < argc; ++i)
{
std::string arg(argv[i]);
auto pos = arg.find("-std=c++");
if (pos != std::string::npos)
{
res = arg.substr(pos + 8);
break;
}
}
return res;
}

interpreter::interpreter(int argc, const char* const* argv) :
Expand All @@ -109,7 +98,7 @@ __get_cxx_version ()
{
//NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic)
createInterpreter(Args(argv ? argv + 1 : argv, argv + argc));
m_version = get_stdopt();
m_version = get_stdopt(argc, argv);
redirect_output();
init_preamble();
init_magic();
Expand Down
5 changes: 4 additions & 1 deletion test/test_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ TEST_SUITE("kernel_info_request")
{
TEST_CASE("good_status")
{
std::vector<const char*> Args = {/*"-v", "resource-dir", "....."*/};
std::vector<const char*> Args = {
"-v", "-std=c++23" // test input for get_stdopt
};
xcpp::interpreter interpreter((int)Args.size(), Args.data());

nl::json result = interpreter.kernel_info_request();
Expand All @@ -264,6 +266,7 @@ TEST_SUITE("kernel_info_request")
REQUIRE(result["language_info"]["mimetype"] == "text/x-c++src");
REQUIRE(result["language_info"]["codemirror_mode"] == "text/x-c++src");
REQUIRE(result["language_info"]["file_extension"] == ".cpp");
REQUIRE(result["language_info"]["version"] == "23");
REQUIRE(result["status"] == "ok");
}

Expand Down