[llvm] [TableGen] Allow emitter callbacks to use `const RecordKeeper &` (PR #104716)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 21 15:55:09 PDT 2024


================
@@ -24,29 +22,38 @@ class RecordKeeper;
 class raw_ostream;
 
 namespace TableGen::Emitter {
-using FnT = void (*)(RecordKeeper &Records, raw_ostream &OS);
-
-struct OptCreatorT {
-  static void *call();
-};
-
-extern ManagedStatic<cl::opt<FnT>, OptCreatorT> Action;
+// Support const and non-const forms of callback functions.
+using FnNonConstT = void (*)(RecordKeeper &Records, raw_ostream &OS);
+using FnConstT = void (*)(const RecordKeeper &Records, raw_ostream &OS);
----------------
jurahul wrote:

That does not seem to work, in the sense function_ref<> seems to erase the const vs non-const distinction. To verify, I added the following test to FunctionRefTest.cpp

```C++

std::string returns(function_ref<void(int &)> F) { return "int&"; }
std::string returns(function_ref<void(const int &)> F) { return "const int&"; }

TEST(FunctionRefTest, SFINAE) {
  EXPECT_EQ("int&", returns([](int&) { }));
  EXPECT_EQ("string", returns([](const int&) { }));
}
```

and I get the following error:

```
/home/rjoshi/upstream_llvm/llvm-project/llvm/unittests/ADT/FunctionRefTest.cpp:63:23: error: call to 'returns' is ambiguous
  EXPECT_EQ("string", returns([](const int&) { }));
/home/rjoshi/upstream_llvm/llvm-project/llvm/unittests/ADT/FunctionRefTest.cpp:55:13: note: candidate function
std::string returns(function_ref<void(int &)> F) { return "int&"; }
            ^
/home/rjoshi/upstream_llvm/llvm-project/llvm/unittests/ADT/FunctionRefTest.cpp:56:13: note: candidate function
std::string returns(function_ref<void(const int &)> F) { return "const int&"; }
```


https://github.com/llvm/llvm-project/pull/104716


More information about the llvm-commits mailing list