LLVM 正在消除您的功能,因为它未使用。
尝试使用它并使其非内联(以防止它也被消除):
class Rectangle {
public:
Rectangle() {}
__attribute__((noinline)) void draw(int fooBar) {}
};
int main() {
Rectangle r;
r.draw(42);
}
你得到:
(module
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(export "main" (func $main))
(func $main (result i32)
(local $0 i32)
(i32.store offset=4
(i32.const 0)
(tee_local $0
(i32.sub
(i32.load offset=4
(i32.const 0)
)
(i32.const 16)
)
)
)
(call $_ZN9Rectangle4drawEi
(i32.add
(get_local $0)
(i32.const 8)
)
(i32.const 42)
)
(i32.store offset=4
(i32.const 0)
(i32.add
(get_local $0)
(i32.const 16)
)
)
(i32.const 0)
)
(func $_ZN9Rectangle4drawEi (param $0 i32) (param $1 i32)
)
)
使用 no-inline 是对优化的琐碎代码的一种破解。您也可以将其标记为已使用:
class Rectangle {
public:
Rectangle() {}
__attribute__((used)) void draw(int fooBar) {}
};
然后你会得到:
(module
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(func $_ZN9Rectangle4drawEi (param $0 i32) (param $1 i32)
)
)