-
Notifications
You must be signed in to change notification settings - Fork 15k
[clang][Basic] Add helper APIs to get language version codes from LangOptions #163348
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
Changes from 4 commits
375a56e
1c64d14
79150a3
ab03eae
bde7c01
28ff3a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,6 +184,28 @@ class LangOptionsBase { | |
| HLSL_202y = 2029, | ||
| }; | ||
|
|
||
| /// C language version codes as defined by the standard. | ||
| enum CLangStd : uint32_t { | ||
| // FIXME: Use correct value for C2y. | ||
| C_2y = 202400, | ||
| C_23 = 202311, | ||
| C_17 = 201710, | ||
| C_11 = 201112, | ||
| C_99 = 199901, | ||
| }; | ||
|
|
||
| /// C++ language version codes as defined by the standard. | ||
| enum CPlusPlusLangStd : uint32_t { | ||
| // FIXME: Use correct value for C++26. | ||
| CPP_26 = 202400, | ||
|
||
| CPP_23 = 202302, | ||
| CPP_20 = 202002, | ||
| CPP_17 = 201703, | ||
| CPP_14 = 201402, | ||
| CPP_11 = 201103, | ||
| CPP_03 = 199711, | ||
| }; | ||
|
|
||
| /// Clang versions with different platform ABI conformance. | ||
| enum class ClangABI { | ||
| #define ABI_VER_MAJOR_MINOR(Major, Minor) Ver##Major##_##Minor, | ||
|
|
@@ -756,6 +778,15 @@ class LangOptions : public LangOptionsBase { | |
| bool isTargetDevice() const { | ||
| return OpenMPIsTargetDevice || CUDAIsDevice || SYCLIsDevice; | ||
| } | ||
|
|
||
| /// Returns the most applicable C standard-compliant language version code. | ||
| /// If none could be determined, returns \ref std::nullopt. | ||
| std::optional<CLangStd> getCLangStd() const; | ||
|
|
||
| /// Returns the most applicable C++ standard-compliant language | ||
| /// version code. | ||
| /// If none could be determined, returns \ref std::nullopt. | ||
| std::optional<CPlusPlusLangStd> getCPlusPlusLangStd() const; | ||
| }; | ||
|
|
||
| /// Floating point control options | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -243,3 +243,49 @@ LLVM_DUMP_METHOD void FPOptionsOverride::dump() { | |
| #include "clang/Basic/FPOptions.def" | ||
| llvm::errs() << "\n"; | ||
| } | ||
|
|
||
| std::optional<clang::LangOptionsBase::CPlusPlusLangStd> | ||
| LangOptions::getCPlusPlusLangStd() const { | ||
|
||
| if (!CPlusPlus) | ||
| return std::nullopt; | ||
|
|
||
| if (CPlusPlus26) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking we could also generate this mapping if we hooked up the LangOptions name to the LangStandard macros. My concern is: every time there's a new standard version, we need to update a lot of places; reducing that isn't a bad idea. However, that's also probably a more involved refactoring, so I don't insist (or could be done in a follow-up). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah gotcha. Yea that would be neat That'll probably take me a bit because I'm not sure off the top what's involved. To unblock the debug-info changes I'll go ahead and merge this. Then will give the refactoring a shot. Thanks for the reviews! |
||
| return clang::LangOptionsBase::CPP_26; | ||
|
|
||
| if (CPlusPlus23) | ||
| return clang::LangOptionsBase::CPP_23; | ||
|
|
||
| if (CPlusPlus20) | ||
| return clang::LangOptionsBase::CPP_20; | ||
|
|
||
| if (CPlusPlus17) | ||
| return clang::LangOptionsBase::CPP_17; | ||
|
|
||
| if (CPlusPlus14) | ||
| return clang::LangOptionsBase::CPP_14; | ||
|
|
||
| if (CPlusPlus11) | ||
| return clang::LangOptionsBase::CPP_11; | ||
|
|
||
| return clang::LangOptionsBase::CPP_03; | ||
| } | ||
|
|
||
| std::optional<clang::LangOptionsBase::CLangStd> | ||
| LangOptions::getCLangStd() const { | ||
| if (C2y) | ||
| return clang::LangOptionsBase::C_2y; | ||
|
|
||
| if (C23) | ||
| return clang::LangOptionsBase::C_23; | ||
|
|
||
| if (C17) | ||
| return clang::LangOptionsBase::C_17; | ||
|
|
||
| if (C11) | ||
| return clang::LangOptionsBase::C_11; | ||
|
|
||
| if (C99) | ||
| return clang::LangOptionsBase::C_99; | ||
|
|
||
| return std::nullopt; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ add_distinct_clang_unittest(BasicTests | |
| DiagnosticTest.cpp | ||
| FileEntryTest.cpp | ||
| FileManagerTest.cpp | ||
| LangOptionsTest.cpp | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whoops, yes! |
||
| LineOffsetMappingTest.cpp | ||
| OffloadArchTest.cpp | ||
| SanitizersTest.cpp | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| //===- unittests/Basic/LangOptionsTest.cpp --------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "clang/Basic/LangOptions.h" | ||
| #include "clang/Testing/CommandLineArgs.h" | ||
| #include "gmock/gmock.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| using namespace llvm; | ||
| using namespace clang; | ||
|
|
||
| namespace { | ||
| TEST(LangOptsTest, CStdLang) { | ||
| LangOptions opts; | ||
| EXPECT_FALSE(opts.getCLangStd()); | ||
| opts.C99 = 1; | ||
| EXPECT_EQ(opts.getCLangStd(), clang::LangOptionsBase::CLangStd::C_99); | ||
| opts.C11 = 1; | ||
| EXPECT_EQ(opts.getCLangStd(), clang::LangOptionsBase::CLangStd::C_11); | ||
| opts.C17 = 1; | ||
| EXPECT_EQ(opts.getCLangStd(), clang::LangOptionsBase::CLangStd::C_17); | ||
| opts.C23 = 1; | ||
| EXPECT_EQ(opts.getCLangStd(), clang::LangOptionsBase::CLangStd::C_23); | ||
| opts.C2y = 1; | ||
| EXPECT_EQ(opts.getCLangStd(), clang::LangOptionsBase::CLangStd::C_2y); | ||
|
|
||
| EXPECT_FALSE(opts.getCPlusPlusLangStd()); | ||
| } | ||
|
|
||
| TEST(LangOptsTest, CppStdLang) { | ||
| LangOptions opts; | ||
| EXPECT_FALSE(opts.getCPlusPlusLangStd()); | ||
| opts.CPlusPlus = 1; | ||
| EXPECT_EQ(opts.getCPlusPlusLangStd(), | ||
| clang::LangOptionsBase::CPlusPlusLangStd::CPP_03); | ||
| opts.CPlusPlus11 = 1; | ||
| EXPECT_EQ(opts.getCPlusPlusLangStd(), | ||
| clang::LangOptionsBase::CPlusPlusLangStd::CPP_11); | ||
| opts.CPlusPlus14 = 1; | ||
| EXPECT_EQ(opts.getCPlusPlusLangStd(), | ||
| clang::LangOptionsBase::CPlusPlusLangStd::CPP_14); | ||
| opts.CPlusPlus17 = 1; | ||
| EXPECT_EQ(opts.getCPlusPlusLangStd(), | ||
| clang::LangOptionsBase::CPlusPlusLangStd::CPP_17); | ||
| opts.CPlusPlus20 = 1; | ||
| EXPECT_EQ(opts.getCPlusPlusLangStd(), | ||
| clang::LangOptionsBase::CPlusPlusLangStd::CPP_20); | ||
| opts.CPlusPlus23 = 1; | ||
| EXPECT_EQ(opts.getCPlusPlusLangStd(), | ||
| clang::LangOptionsBase::CPlusPlusLangStd::CPP_23); | ||
| opts.CPlusPlus26 = 1; | ||
| EXPECT_EQ(opts.getCPlusPlusLangStd(), | ||
| clang::LangOptionsBase::CPlusPlusLangStd::CPP_26); | ||
|
|
||
| EXPECT_FALSE(opts.getCLangStd()); | ||
| } | ||
| } // namespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C2yinstead ofC_2y?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree,
C2yseems more make sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not applicable now that we use
LangStandards.def