[llvm] [SandboxIR] Add test that checks if classof is missing. (PR #106313)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 27 17:02:20 PDT 2024


https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/106313

Forgetting to implement an `<Instruction Subclass>::classof()` function does not cause any failures because it falls back to Instruction::classof(). This patch adds an explicit check for all instruction classes to confirm that they have a classof implementation.

>From 3a214ed6700fcd52c42a8af3d1cec3c15ee85d74 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vporpodas at google.com>
Date: Tue, 27 Aug 2024 16:44:01 -0700
Subject: [PATCH] [SandboxIR] Add test that checks if classof is missing.

Forgetting to implement an `<Instruction Subclass>::classof()` function
does not cause any failures because it falls back to Instruction::classof().
This patch adds an explicit check for all instruction classes to confirm
that they have a classof implementation.
---
 llvm/include/llvm/SandboxIR/SandboxIR.h    | 4 ++++
 llvm/unittests/SandboxIR/SandboxIRTest.cpp | 7 +++++++
 2 files changed, 11 insertions(+)

diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index b7bdf9acd2ef45..8449008c2b2c21 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -2678,6 +2678,10 @@ class AtomicCmpXchgInst
          AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
          BasicBlock *InsertAtEnd, Context &Ctx,
          SyncScope::ID SSID = SyncScope::System, const Twine &Name = "");
+
+  static bool classof(const Value *From) {
+    return From->getSubclassID() == ClassID::AtomicCmpXchg;
+  }
 };
 
 class AllocaInst final : public UnaryInstruction {
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index bc3fddf9e163dc..4e7c55dddb8bca 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -4213,3 +4213,10 @@ define void @foo() {
   EXPECT_EQ(NewUIEnd->getParent(), BB);
   EXPECT_EQ(NewUIEnd->getNextNode(), nullptr);
 }
+
+/// Makes sure that all Instruction sub-classes have a classof().
+TEST_F(SandboxIRTest, CheckClassof) {
+#define DEF_INSTR(ID, OPC, CLASS)                                              \
+  EXPECT_NE(&sandboxir::CLASS::classof, sandboxir::Instruction::classof);
+#include "llvm/SandboxIR/SandboxIRValues.def"
+}



More information about the llvm-commits mailing list