Skip to content

Commit be93399

Browse files
authored
[clang][Basic] Add helper APIs to get language version codes from LangOptions (#163348)
Motivated by this discussion: #163208 (comment) We will soon want to emit language version codes into debug-info. Instead of replicating the `LangOptions -> version code` mapping we thought we'd try to share some of the logic with the Clang frontend. This patch teaches `LangStandard` about language versions (currently just C++ and C).
1 parent c4eaf56 commit be93399

File tree

10 files changed

+181
-91
lines changed

10 files changed

+181
-91
lines changed

clang/include/clang/Basic/LangOptions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,15 @@ class LangOptions : public LangOptionsBase {
756756
bool isTargetDevice() const {
757757
return OpenMPIsTargetDevice || CUDAIsDevice || SYCLIsDevice;
758758
}
759+
760+
/// Returns the most applicable C standard-compliant language version code.
761+
/// If none could be determined, returns \ref std::nullopt.
762+
std::optional<uint32_t> getCLangStd() const;
763+
764+
/// Returns the most applicable C++ standard-compliant language
765+
/// version code.
766+
/// If none could be determined, returns \ref std::nullopt.
767+
std::optional<uint32_t> getCPlusPlusLangStd() const;
759768
};
760769

761770
/// Floating point control options

clang/include/clang/Basic/LangStandard.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ enum LangFeatures {
7070
/// standard.
7171
struct LangStandard {
7272
enum Kind {
73-
#define LANGSTANDARD(id, name, lang, desc, features) \
74-
lang_##id,
73+
#define LANGSTANDARD(id, name, lang, desc, features, version) lang_##id,
7574
#include "clang/Basic/LangStandards.def"
7675
lang_unspecified
7776
};
@@ -80,6 +79,7 @@ struct LangStandard {
8079
const char *Description;
8180
unsigned Flags;
8281
clang::Language Language;
82+
std::optional<uint32_t> Version;
8383

8484
public:
8585
/// getName - Get the name of this standard.
@@ -91,6 +91,9 @@ struct LangStandard {
9191
/// Get the language that this standard describes.
9292
clang::Language getLanguage() const { return Language; }
9393

94+
/// Get the version code for this language standard.
95+
std::optional<uint32_t> getVersion() const { return Version; }
96+
9497
/// Language supports '//' comments.
9598
bool hasLineComments() const { return Flags & LineComment; }
9699

clang/include/clang/Basic/LangStandards.def

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
#error "LANGSTANDARD must be defined before including this file"
1111
#endif
1212

13-
/// LANGSTANDARD(IDENT, NAME, LANG, DESC, FEATURES)
13+
/// LANGSTANDARD(IDENT, NAME, LANG, DESC, FEATURES, VERSION)
1414
///
1515
/// \param IDENT - The name of the standard as a C++ identifier.
1616
/// \param NAME - The name of the standard.
1717
/// \param LANG - The Language for which this is a standard.
1818
/// \param DESC - A short description of the standard.
1919
/// \param FEATURES - The standard features as flags, these are enums from the
2020
/// clang::frontend namespace, which is assumed to be available.
21+
/// \param VERSION - The official version code for this standard.
22+
/// Has value 'std::nullopt' if no official version exists.
2123

2224
/// LANGSTANDARD_ALIAS(IDENT, ALIAS)
2325
/// \param IDENT - The name of the standard as a C++ identifier.
@@ -36,186 +38,188 @@
3638

3739
// C89-ish modes.
3840
LANGSTANDARD(c89, "c89",
39-
C, "ISO C 1990", 0)
41+
C, "ISO C 1990", 0, std::nullopt)
4042
LANGSTANDARD_ALIAS(c89, "c90")
4143
LANGSTANDARD_ALIAS(c89, "iso9899:1990")
4244

4345
LANGSTANDARD(c94, "iso9899:199409",
4446
C, "ISO C 1990 with amendment 1",
45-
Digraphs)
47+
Digraphs, 199409)
4648

4749
LANGSTANDARD(gnu89, "gnu89",
4850
C, "ISO C 1990 with GNU extensions",
49-
LineComment | Digraphs | GNUMode)
51+
LineComment | Digraphs | GNUMode, std::nullopt)
5052
LANGSTANDARD_ALIAS(gnu89, "gnu90")
5153

5254
// C99-ish modes
5355
LANGSTANDARD(c99, "c99",
5456
C, "ISO C 1999",
55-
LineComment | C99 | Digraphs | HexFloat)
57+
LineComment | C99 | Digraphs | HexFloat, 199901)
5658
LANGSTANDARD_ALIAS(c99, "iso9899:1999")
5759
LANGSTANDARD_ALIAS_DEPR(c99, "c9x")
5860
LANGSTANDARD_ALIAS_DEPR(c99, "iso9899:199x")
5961

6062
LANGSTANDARD(gnu99, "gnu99",
6163
C, "ISO C 1999 with GNU extensions",
62-
LineComment | C99 | Digraphs | GNUMode | HexFloat)
64+
LineComment | C99 | Digraphs | GNUMode | HexFloat, 199901)
6365
LANGSTANDARD_ALIAS_DEPR(gnu99, "gnu9x")
6466

6567
// C11 modes
6668
LANGSTANDARD(c11, "c11",
6769
C, "ISO C 2011",
68-
LineComment | C99 | C11 | Digraphs | HexFloat)
70+
LineComment | C99 | C11 | Digraphs | HexFloat, 201112)
6971
LANGSTANDARD_ALIAS(c11, "iso9899:2011")
7072
LANGSTANDARD_ALIAS_DEPR(c11, "c1x")
7173
LANGSTANDARD_ALIAS_DEPR(c11, "iso9899:201x")
7274

7375
LANGSTANDARD(gnu11, "gnu11",
7476
C, "ISO C 2011 with GNU extensions",
75-
LineComment | C99 | C11 | Digraphs | GNUMode | HexFloat)
77+
LineComment | C99 | C11 | Digraphs | GNUMode | HexFloat, 201112)
7678
LANGSTANDARD_ALIAS_DEPR(gnu11, "gnu1x")
7779

7880
// C17 modes
7981
LANGSTANDARD(c17, "c17",
8082
C, "ISO C 2017",
81-
LineComment | C99 | C11 | C17 | Digraphs | HexFloat)
83+
LineComment | C99 | C11 | C17 | Digraphs | HexFloat, 201710)
8284
LANGSTANDARD_ALIAS(c17, "iso9899:2017")
8385
LANGSTANDARD_ALIAS(c17, "c18")
8486
LANGSTANDARD_ALIAS(c17, "iso9899:2018")
8587
LANGSTANDARD(gnu17, "gnu17",
8688
C, "ISO C 2017 with GNU extensions",
87-
LineComment | C99 | C11 | C17 | Digraphs | GNUMode | HexFloat)
89+
LineComment | C99 | C11 | C17 | Digraphs | GNUMode | HexFloat, 201710)
8890
LANGSTANDARD_ALIAS(gnu17, "gnu18")
8991

9092
// C23 modes
9193
LANGSTANDARD(c23, "c23",
9294
C, "ISO C 2023",
93-
LineComment | C99 | C11 | C17 | C23 | Digraphs | HexFloat)
95+
LineComment | C99 | C11 | C17 | C23 | Digraphs | HexFloat, 202311)
9496
LANGSTANDARD_ALIAS(c23, "iso9899:2024")
9597
LANGSTANDARD_ALIAS_DEPR(c23, "c2x")
9698
LANGSTANDARD(gnu23, "gnu23",
9799
C, "ISO C 2023 with GNU extensions",
98-
LineComment | C99 | C11 | C17 | C23 | Digraphs | GNUMode | HexFloat)
100+
LineComment | C99 | C11 | C17 | C23 | Digraphs | GNUMode | HexFloat, 202311)
99101
LANGSTANDARD_ALIAS_DEPR(gnu23, "gnu2x")
100102

101103
// C2y modes
104+
// FIXME: Use correct version code for C2y once published.
102105
LANGSTANDARD(c2y, "c2y",
103106
C, "Working Draft for ISO C2y",
104-
LineComment | C99 | C11 | C17 | C23 | C2y | Digraphs | HexFloat)
107+
LineComment | C99 | C11 | C17 | C23 | C2y | Digraphs | HexFloat, 202400)
105108
LANGSTANDARD(gnu2y, "gnu2y",
106109
C, "Working Draft for ISO C2y with GNU extensions",
107-
LineComment | C99 | C11 | C17 | C23 | C2y | Digraphs | GNUMode | HexFloat)
110+
LineComment | C99 | C11 | C17 | C23 | C2y | Digraphs | GNUMode | HexFloat, 202400)
108111
// TODO: Add the iso9899:202y alias once ISO publishes the standard.
109112

110113
// C++ modes
111114
LANGSTANDARD(cxx98, "c++98",
112115
CXX, "ISO C++ 1998 with amendments",
113-
LineComment | CPlusPlus | Digraphs)
116+
LineComment | CPlusPlus | Digraphs, 199711)
114117
LANGSTANDARD_ALIAS(cxx98, "c++03")
115118

116119
LANGSTANDARD(gnucxx98, "gnu++98",
117120
CXX, "ISO C++ 1998 with amendments and GNU extensions",
118-
LineComment | CPlusPlus | Digraphs | GNUMode)
121+
LineComment | CPlusPlus | Digraphs | GNUMode, 199711)
119122
LANGSTANDARD_ALIAS(gnucxx98, "gnu++03")
120123

121124
LANGSTANDARD(cxx11, "c++11",
122125
CXX, "ISO C++ 2011 with amendments",
123-
LineComment | CPlusPlus | CPlusPlus11 | Digraphs)
126+
LineComment | CPlusPlus | CPlusPlus11 | Digraphs, 201103)
124127
LANGSTANDARD_ALIAS_DEPR(cxx11, "c++0x")
125128

126129
LANGSTANDARD(gnucxx11, "gnu++11", CXX,
127130
"ISO C++ 2011 with amendments and GNU extensions",
128-
LineComment | CPlusPlus | CPlusPlus11 | Digraphs | GNUMode)
131+
LineComment | CPlusPlus | CPlusPlus11 | Digraphs | GNUMode, 201103)
129132
LANGSTANDARD_ALIAS_DEPR(gnucxx11, "gnu++0x")
130133

131134
LANGSTANDARD(cxx14, "c++14",
132135
CXX, "ISO C++ 2014 with amendments",
133-
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | Digraphs)
136+
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | Digraphs, 201402)
134137
LANGSTANDARD_ALIAS_DEPR(cxx14, "c++1y")
135138

136139
LANGSTANDARD(gnucxx14, "gnu++14",
137140
CXX, "ISO C++ 2014 with amendments and GNU extensions",
138141
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | Digraphs |
139-
GNUMode)
142+
GNUMode, 201402)
140143
LANGSTANDARD_ALIAS_DEPR(gnucxx14, "gnu++1y")
141144

142145
LANGSTANDARD(cxx17, "c++17",
143146
CXX, "ISO C++ 2017 with amendments",
144147
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
145-
Digraphs | HexFloat)
148+
Digraphs | HexFloat, 201703)
146149
LANGSTANDARD_ALIAS_DEPR(cxx17, "c++1z")
147150

148151
LANGSTANDARD(gnucxx17, "gnu++17",
149152
CXX, "ISO C++ 2017 with amendments and GNU extensions",
150153
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
151-
Digraphs | HexFloat | GNUMode)
154+
Digraphs | HexFloat | GNUMode, 201703)
152155
LANGSTANDARD_ALIAS_DEPR(gnucxx17, "gnu++1z")
153156

154157
LANGSTANDARD(cxx20, "c++20",
155158
CXX, "ISO C++ 2020 DIS",
156159
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
157-
CPlusPlus20 | Digraphs | HexFloat)
160+
CPlusPlus20 | Digraphs | HexFloat, 202002)
158161
LANGSTANDARD_ALIAS_DEPR(cxx20, "c++2a")
159162

160163
LANGSTANDARD(gnucxx20, "gnu++20",
161164
CXX, "ISO C++ 2020 DIS with GNU extensions",
162165
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
163-
CPlusPlus20 | Digraphs | HexFloat | GNUMode)
166+
CPlusPlus20 | Digraphs | HexFloat | GNUMode, 202002)
164167
LANGSTANDARD_ALIAS_DEPR(gnucxx20, "gnu++2a")
165168

166169
LANGSTANDARD(cxx23, "c++23",
167170
CXX, "ISO C++ 2023 DIS",
168171
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
169-
CPlusPlus20 | CPlusPlus23 | Digraphs | HexFloat)
172+
CPlusPlus20 | CPlusPlus23 | Digraphs | HexFloat, 202302)
170173
LANGSTANDARD_ALIAS_DEPR(cxx23, "c++2b")
171174

172175
LANGSTANDARD(gnucxx23, "gnu++23",
173176
CXX, "ISO C++ 2023 DIS with GNU extensions",
174177
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
175-
CPlusPlus20 | CPlusPlus23 | Digraphs | HexFloat | GNUMode)
178+
CPlusPlus20 | CPlusPlus23 | Digraphs | HexFloat | GNUMode, 202302)
176179
LANGSTANDARD_ALIAS_DEPR(gnucxx23, "gnu++2b")
177180

181+
// FIXME: Use correct version code for C++26 once published.
178182
LANGSTANDARD(cxx26, "c++2c",
179183
CXX, "Working draft for C++2c",
180184
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
181-
CPlusPlus20 | CPlusPlus23 | CPlusPlus26 | Digraphs | HexFloat)
185+
CPlusPlus20 | CPlusPlus23 | CPlusPlus26 | Digraphs | HexFloat, 202400)
182186
LANGSTANDARD_ALIAS(cxx26, "c++26")
183187

184188
LANGSTANDARD(gnucxx26, "gnu++2c",
185189
CXX, "Working draft for C++2c with GNU extensions",
186190
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
187-
CPlusPlus20 | CPlusPlus23 | CPlusPlus26 | Digraphs | HexFloat | GNUMode)
191+
CPlusPlus20 | CPlusPlus23 | CPlusPlus26 | Digraphs | HexFloat | GNUMode, 202400)
188192
LANGSTANDARD_ALIAS(gnucxx26, "gnu++26")
189193

190194
// OpenCL
191195
LANGSTANDARD(opencl10, "cl1.0",
192196
OpenCL, "OpenCL 1.0",
193-
LineComment | C99 | Digraphs | HexFloat | OpenCL)
197+
LineComment | C99 | Digraphs | HexFloat | OpenCL, std::nullopt)
194198
LANGSTANDARD_ALIAS_DEPR(opencl10, "cl")
195199

196200
LANGSTANDARD(opencl11, "cl1.1",
197201
OpenCL, "OpenCL 1.1",
198-
LineComment | C99 | Digraphs | HexFloat | OpenCL)
202+
LineComment | C99 | Digraphs | HexFloat | OpenCL, std::nullopt)
199203
LANGSTANDARD(opencl12, "cl1.2",
200204
OpenCL, "OpenCL 1.2",
201-
LineComment | C99 | Digraphs | HexFloat | OpenCL)
205+
LineComment | C99 | Digraphs | HexFloat | OpenCL, std::nullopt)
202206
LANGSTANDARD(opencl20, "cl2.0",
203207
OpenCL, "OpenCL 2.0",
204-
LineComment | C99 | Digraphs | HexFloat | OpenCL)
208+
LineComment | C99 | Digraphs | HexFloat | OpenCL, std::nullopt)
205209
LANGSTANDARD(opencl30, "cl3.0",
206210
OpenCL, "OpenCL 3.0",
207-
LineComment | C99 | Digraphs | HexFloat | OpenCL)
211+
LineComment | C99 | Digraphs | HexFloat | OpenCL, std::nullopt)
208212

209213
LANGSTANDARD(openclcpp10, "clc++1.0",
210214
OpenCL, "C++ for OpenCL 1.0",
211215
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
212-
Digraphs | HexFloat | OpenCL)
216+
Digraphs | HexFloat | OpenCL, std::nullopt)
213217
LANGSTANDARD_ALIAS(openclcpp10, "clc++")
214218

215219
LANGSTANDARD(openclcpp2021, "clc++2021",
216220
OpenCL, "C++ for OpenCL 2021",
217221
LineComment | CPlusPlus | CPlusPlus11 | CPlusPlus14 | CPlusPlus17 |
218-
Digraphs | HexFloat | OpenCL)
222+
Digraphs | HexFloat | OpenCL, std::nullopt)
219223

220224
LANGSTANDARD_ALIAS_DEPR(opencl10, "CL")
221225
LANGSTANDARD_ALIAS_DEPR(opencl11, "CL1.1")
@@ -229,35 +233,35 @@ LANGSTANDARD_ALIAS_DEPR(openclcpp2021, "CLC++2021")
229233
// HLSL
230234
LANGSTANDARD(hlsl, "hlsl",
231235
HLSL, "High Level Shader Language",
232-
LineComment | HLSL | CPlusPlus | CPlusPlus11)
236+
LineComment | HLSL | CPlusPlus | CPlusPlus11, std::nullopt)
233237

234238
LANGSTANDARD(hlsl2015, "hlsl2015",
235239
HLSL, "High Level Shader Language 2015",
236-
LineComment | HLSL | CPlusPlus | CPlusPlus11)
240+
LineComment | HLSL | CPlusPlus | CPlusPlus11, std::nullopt)
237241

238242
LANGSTANDARD(hlsl2016, "hlsl2016",
239243
HLSL, "High Level Shader Language 2016",
240-
LineComment | HLSL | CPlusPlus | CPlusPlus11)
244+
LineComment | HLSL | CPlusPlus | CPlusPlus11, std::nullopt)
241245

242246
LANGSTANDARD(hlsl2017, "hlsl2017",
243247
HLSL, "High Level Shader Language 2017",
244-
LineComment | HLSL | CPlusPlus | CPlusPlus11)
248+
LineComment | HLSL | CPlusPlus | CPlusPlus11, std::nullopt)
245249

246250
LANGSTANDARD(hlsl2018, "hlsl2018",
247251
HLSL, "High Level Shader Language 2018",
248-
LineComment | HLSL | CPlusPlus | CPlusPlus11)
252+
LineComment | HLSL | CPlusPlus | CPlusPlus11, std::nullopt)
249253

250254
LANGSTANDARD(hlsl2021, "hlsl2021",
251255
HLSL, "High Level Shader Language 2021",
252-
LineComment | HLSL | CPlusPlus | CPlusPlus11)
256+
LineComment | HLSL | CPlusPlus | CPlusPlus11, std::nullopt)
253257

254258
LANGSTANDARD(hlsl202x, "hlsl202x",
255259
HLSL, "High Level Shader Language 202x",
256-
LineComment | HLSL | CPlusPlus | CPlusPlus11)
260+
LineComment | HLSL | CPlusPlus | CPlusPlus11, std::nullopt)
257261

258262
LANGSTANDARD(hlsl202y, "hlsl202y",
259263
HLSL, "High Level Shader Language 202y",
260-
LineComment | HLSL | CPlusPlus | CPlusPlus11)
264+
LineComment | HLSL | CPlusPlus | CPlusPlus11, std::nullopt)
261265

262266

263267
#undef LANGSTANDARD

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6211,11 +6211,12 @@ def static : Flag<["-", "--"], "static">, Group<Link_Group>,
62116211
Flags<[NoArgumentUnused]>;
62126212
def std_default_EQ : Joined<["-"], "std-default=">;
62136213
def std_EQ : Joined<["-", "--"], "std=">,
6214-
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
6215-
Group<CompileOnly_Group>, HelpText<"Language standard to compile for">,
6216-
ValuesCode<[{
6214+
Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
6215+
Group<CompileOnly_Group>,
6216+
HelpText<"Language standard to compile for">,
6217+
ValuesCode<[{
62176218
static constexpr const char VALUES_CODE [] =
6218-
#define LANGSTANDARD(id, name, lang, desc, features) name ","
6219+
#define LANGSTANDARD(id, name, lang, desc, features, version) name ","
62196220
#define LANGSTANDARD_ALIAS(id, alias) alias ","
62206221
#include "clang/Basic/LangStandards.def"
62216222
;

0 commit comments

Comments
 (0)