-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[lldb] Add a static_assert that g_core_definitions matches the Core enum #159452
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
Conversation
@llvm/pr-subscribers-lldb Author: Jonas Devlieghere (JDevlieghere) ChangesThis PR uses the same trick as 7ced9ff to ensure the Full diff: https://github.com/llvm/llvm-project/pull/159452.diff 1 Files Affected:
diff --git a/lldb/source/Utility/ArchSpec.cpp b/lldb/source/Utility/ArchSpec.cpp
index 2a87cc6bf7de9..079a77690c745 100644
--- a/lldb/source/Utility/ArchSpec.cpp
+++ b/lldb/source/Utility/ArchSpec.cpp
@@ -41,7 +41,7 @@ struct CoreDefinition {
} // namespace lldb_private
// This core information can be looked using the ArchSpec::Core as the index
-static const CoreDefinition g_core_definitions[] = {
+static constexpr const CoreDefinition g_core_definitions[] = {
{eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_generic,
"arm"},
{eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv4,
@@ -90,12 +90,12 @@ static const CoreDefinition g_core_definitions[] = {
"thumbv6m"},
{eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7,
"thumbv7"},
- {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7f,
- "thumbv7f"},
{eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7s,
"thumbv7s"},
{eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7k,
"thumbv7k"},
+ {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7f,
+ "thumbv7f"},
{eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7m,
"thumbv7m"},
{eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7em,
@@ -257,6 +257,15 @@ static_assert(sizeof(g_core_definitions) / sizeof(CoreDefinition) ==
ArchSpec::kNumCores,
"make sure we have one core definition for each core");
+template <int I> struct ArchSpecValidator : ArchSpecValidator<I + 1> {
+ static_assert(g_core_definitions[I].core == I,
+ "g_core_definitions order doesn't match Core enumeration");
+};
+
+template <> struct ArchSpecValidator<ArchSpec::eCore_wasm32> {};
+
+ArchSpecValidator<ArchSpec::eCore_arm_generic> validator;
+
struct ArchDefinitionEntry {
ArchSpec::Core core;
uint32_t cpu;
|
This is what the compiler tells you when you make a mistake:
|
pretty cool, I don't understand templates enough to really get how it works, but definitely a nice safety net. |
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.
ArchSpecValidator<ArchSpec::eCore_arm_generic>
starts instantiating a chain of ArchSpecValidator<I + 1>
until it gets to ArchSpecValidator<ArchSpec::eCore_wasm32>
which does not inherit from anything so it stops there.
arm_generic
being the first core value and wasm32
being the last. At least right now.
Could you use kNum_Cores
so we don't have to remember to update this when new ones are added?
Also the eCore_wasm32
one has an empty body, so does that mean it does not get checked?
And you say 2 cores were in the wrong order but only one is moved in the diff. Maybe the other is a downstream thing?
Good catch, that's an unintentional off-by-one. Using
Haha, I guess it depends on how you (or the diff algorithm) looks at it. |
This PR uses the same trick as 7ced9ff to ensure the g_core_definitions table is correctly indexed by the Core enum. It's easy to make a mistake. Case in point: this caught two entries that appeared in the wrong order.
2509452
to
57198d2
Compare
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.
1 moves up or 2 move down, it's the same picture.
LGTM.
…num (llvm#159452) This PR uses the same trick as 7ced9ff to ensure the `g_core_definitions` table is correctly indexed by the Core enum. It's easy to make a mistake. Case in point: this caught two entries that appeared in the wrong order.
This PR uses the same trick as 7ced9ff to ensure the
g_core_definitions
table is correctly indexed by the Core enum. It's easy to make a mistake. Case in point: this caught two entries that appeared in the wrong order.