Skip to content

Commit 9f90164

Browse files
committed
Use Cpp::GetLanguageStandard to get language standard
1 parent 5e4a82b commit 9f90164

6 files changed

Lines changed: 98 additions & 29 deletions

File tree

notebooks/hello_world.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"file_extension": ".cpp",
7474
"mimetype": "text/x-c++src",
7575
"name": "C++",
76-
"version": "23"
76+
"version": "cxx23"
7777
}
7878
},
7979
"nbformat": 4,

notebooks/tinyraytracer.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"file_extension": ".cpp",
1111
"mimetype": "text/x-c++src",
1212
"name": "C++",
13-
"version": "23"
13+
"version": "cxx23"
1414
}
1515
},
1616
"nbformat_minor": 5,

notebooks/xeus-cpp-lite-demo.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"file_extension": ".cpp",
1111
"mimetype": "text/x-c++src",
1212
"name": "C++",
13-
"version": "23"
13+
"version": "cxx23"
1414
}
1515
},
1616
"nbformat_minor": 5,

notebooks/xeus-cpp.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@
886886
"file_extension": ".cpp",
887887
"mimetype": "text/x-c++src",
888888
"name": "C++",
889-
"version": "20"
889+
"version": "cxx20"
890890
}
891891
},
892892
"nbformat": 4,

src/xinterpreter.cpp

Lines changed: 93 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,33 +101,102 @@ namespace xcpp
101101
xeus::register_interpreter(this);
102102
}
103103

104+
// FIXME: This function should be upstreamed to CppInterOp. See
105+
// https://github.com/compiler-research/CppInterOp/issues/879
104106
static std::string get_stdopt()
105107
{
106-
// We need to find what's the C++ version the interpreter runs with.
107-
const char* code = R"(
108-
int __get_cxx_version () {
109-
#if __cplusplus > 202302L
110-
return 26;
111-
#elif __cplusplus > 202002L
112-
return 23;
113-
#elif __cplusplus > 201703L
114-
return 20;
115-
#elif __cplusplus > 201402L
116-
return 17;
117-
#elif __cplusplus > 201103L || (defined(_WIN32) && _MSC_VER >= 1900)
118-
return 14;
119-
#elif __cplusplus >= 201103L
120-
return 11;
121-
#else
122-
return 0;
123-
#endif
124-
}
125-
__get_cxx_version ()
126-
)";
127-
auto cxx_version = Cpp::Evaluate(code);
128-
return std::to_string(cxx_version);
129-
}
108+
switch (Cpp::GetLanguageStandard(nullptr))
109+
{
110+
case Cpp::InterpreterLanguageStandard::c89:
111+
return "c89";
112+
case Cpp::InterpreterLanguageStandard::c94:
113+
return "c94";
114+
case Cpp::InterpreterLanguageStandard::gnu89:
115+
return "gnu89";
116+
case Cpp::InterpreterLanguageStandard::c99:
117+
return "c99";
118+
case Cpp::InterpreterLanguageStandard::gnu99:
119+
return "gnu99";
120+
case Cpp::InterpreterLanguageStandard::c11:
121+
return "c11";
122+
case Cpp::InterpreterLanguageStandard::gnu11:
123+
return "gnu11";
124+
case Cpp::InterpreterLanguageStandard::c17:
125+
return "c17";
126+
case Cpp::InterpreterLanguageStandard::gnu17:
127+
return "gnu17";
128+
case Cpp::InterpreterLanguageStandard::c23:
129+
return "c23";
130+
case Cpp::InterpreterLanguageStandard::gnu23:
131+
return "gnu23";
132+
case Cpp::InterpreterLanguageStandard::c2y:
133+
return "c2y";
134+
case Cpp::InterpreterLanguageStandard::gnu2y:
135+
return "gnu2y";
136+
case Cpp::InterpreterLanguageStandard::cxx98:
137+
return "cxx98";
138+
case Cpp::InterpreterLanguageStandard::gnucxx98:
139+
return "gnucxx98";
140+
case Cpp::InterpreterLanguageStandard::cxx11:
141+
return "cxx11";
142+
case Cpp::InterpreterLanguageStandard::gnucxx11:
143+
return "gnucxx11";
144+
case Cpp::InterpreterLanguageStandard::cxx14:
145+
return "cxx14";
146+
case Cpp::InterpreterLanguageStandard::gnucxx14:
147+
return "gnucxx14";
148+
case Cpp::InterpreterLanguageStandard::cxx17:
149+
return "cxx17";
150+
case Cpp::InterpreterLanguageStandard::gnucxx17:
151+
return "gnucxx17";
152+
case Cpp::InterpreterLanguageStandard::cxx20:
153+
return "cxx20";
154+
case Cpp::InterpreterLanguageStandard::gnucxx20:
155+
return "gnucxx20";
156+
case Cpp::InterpreterLanguageStandard::cxx23:
157+
return "cxx23";
158+
case Cpp::InterpreterLanguageStandard::gnucxx23:
159+
return "gnucxx23";
160+
case Cpp::InterpreterLanguageStandard::cxx26:
161+
return "cxx26";
162+
case Cpp::InterpreterLanguageStandard::gnucxx26:
163+
return "gnucxx26";
164+
case Cpp::InterpreterLanguageStandard::opencl10:
165+
return "opencl10";
166+
case Cpp::InterpreterLanguageStandard::opencl11:
167+
return "opencl11";
168+
case Cpp::InterpreterLanguageStandard::opencl12:
169+
return "opencl12";
170+
case Cpp::InterpreterLanguageStandard::opencl20:
171+
return "opencl20";
172+
case Cpp::InterpreterLanguageStandard::opencl30:
173+
return "opencl30";
174+
case Cpp::InterpreterLanguageStandard::openclcpp10:
175+
return "openclcpp10";
176+
case Cpp::InterpreterLanguageStandard::openclcpp2021:
177+
return "openclcpp2021";
178+
case Cpp::InterpreterLanguageStandard::hlsl:
179+
return "hlsl";
180+
case Cpp::InterpreterLanguageStandard::hlsl2015:
181+
return "hlsl2015";
182+
case Cpp::InterpreterLanguageStandard::hlsl2016:
183+
return "hlsl2016";
184+
case Cpp::InterpreterLanguageStandard::hlsl2017:
185+
return "hlsl2017";
186+
case Cpp::InterpreterLanguageStandard::hlsl2018:
187+
return "hlsl2018";
188+
case Cpp::InterpreterLanguageStandard::hlsl2021:
189+
return "hlsl2021";
190+
case Cpp::InterpreterLanguageStandard::hlsl202x:
191+
return "hlsl202x";
192+
case Cpp::InterpreterLanguageStandard::hlsl202y:
193+
return "hlsl202y";
194+
case Cpp::InterpreterLanguageStandard::lang_unspecified:
195+
return "lang_unspecified";
196+
}
130197

198+
return "unknown";
199+
}
131200
interpreter::interpreter(int argc, const char* const* argv) :
132201
xmagics()
133202
, p_cout_strbuf(nullptr)

test/test_interpreter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ TEST_SUITE("kernel_info_request")
407407
REQUIRE(result["language_info"]["mimetype"] == "text/x-c++src");
408408
REQUIRE(result["language_info"]["codemirror_mode"] == "text/x-c++src");
409409
REQUIRE(result["language_info"]["file_extension"] == ".cpp");
410-
REQUIRE(result["language_info"]["version"] == "23");
410+
REQUIRE(result["language_info"]["version"] == "cxx23");
411411
REQUIRE(result["status"] == "ok");
412412
}
413413

0 commit comments

Comments
 (0)