Files
pytorch/c10/test/core/CompileTimeFunctionPointer_test.cpp
cyy 8fa81a6066 Enable misc-use-internal-linkage check and apply fixes (#148948)
Enables clang-tidy rule [`misc-use-internal-linkage`](https://clang.llvm.org/extra/clang-tidy/checks/misc/use-internal-linkage.html). This new check was introduced in Clang-Tidy 18 and is available due to recent update of Clang-Tidy 19.

The check marks functions and variables used only in the translation unit as static. Therefore undesired symbols are not leaked into other units, more link time optimisations are possible and the resulting binaries may be smaller.

The detected violations were mostly fixed by using static. In other cases, the symbols were indeed consumed by others files, then their declaring headers were included. Still some declarations were wrong and have been fixed.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/148948
Approved by: https://github.com/Skylion007
2025-03-12 14:22:56 +00:00

73 lines
2.3 KiB
C++

#include <c10/core/CompileTimeFunctionPointer.h>
#include <gtest/gtest.h>
namespace test_is_compile_time_function_pointer {
static_assert(!c10::is_compile_time_function_pointer<void()>::value);
static void dummy() {}
static_assert(
c10::is_compile_time_function_pointer<TORCH_FN_TYPE(dummy)>::value);
} // namespace test_is_compile_time_function_pointer
namespace test_access_through_type {
static void dummy() {}
using dummy_ptr = TORCH_FN_TYPE(dummy);
static_assert(c10::is_compile_time_function_pointer<dummy_ptr>::value);
static_assert(dummy_ptr::func_ptr() == &dummy);
static_assert(std::is_same_v<void(), dummy_ptr::FuncType>);
} // namespace test_access_through_type
namespace test_access_through_value {
static void dummy() {}
constexpr auto dummy_ptr = TORCH_FN(dummy);
static_assert(dummy_ptr.func_ptr() == &dummy);
static_assert(std::is_same_v<void(), decltype(dummy_ptr)::FuncType>);
} // namespace test_access_through_value
namespace test_access_through_type_also_works_if_specified_as_pointer {
static void dummy() {}
using dummy_ptr = TORCH_FN_TYPE(&dummy);
static_assert(c10::is_compile_time_function_pointer<dummy_ptr>::value);
static_assert(dummy_ptr::func_ptr() == &dummy);
static_assert(std::is_same_v<void(), dummy_ptr::FuncType>);
} // namespace test_access_through_type_also_works_if_specified_as_pointer
namespace test_access_through_value_also_works_if_specified_as_pointer {
static void dummy() {}
constexpr auto dummy_ptr = TORCH_FN(&dummy);
static_assert(dummy_ptr.func_ptr() == &dummy);
static_assert(std::is_same_v<void(), decltype(dummy_ptr)::FuncType>);
} // namespace test_access_through_value_also_works_if_specified_as_pointer
namespace test_run_through_type {
static int add(int a, int b) {
return a + b;
}
using Add = TORCH_FN_TYPE(add);
template <class Func>
struct Executor {
int execute(int a, int b) {
return Func::func_ptr()(a, b);
}
};
TEST(CompileTimeFunctionPointerTest, runFunctionThroughType) {
Executor<Add> executor;
EXPECT_EQ(3, executor.execute(1, 2));
}
} // namespace test_run_through_type
namespace test_run_through_value {
static int add(int a, int b) {
return a + b;
}
template <class Func>
static int execute(Func, int a, int b) {
return Func::func_ptr()(a, b);
}
TEST(CompileTimeFunctionPointerTest, runFunctionThroughValue) {
EXPECT_EQ(3, execute(TORCH_FN(add), 1, 2));
}
} // namespace test_run_through_value