llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.06k stars 11.98k forks source link

-Wunused-function false positive for the alias attribute with MicrosoftDemangle #88825

Open MaskRay opened 7 months ago

MaskRay commented 7 months ago

Clang supports the alias attribute from GCC. While primarily used for C projects on ELF platforms and possibly also windows-gnu, Clang accepts the alias attribute for windows-msvc targets as well.

This scenario is more like "the feature is generic, let's make it available for everyone" rather than "there are active users of this for windows-msvc". After removing some false positive results from rg '\(_?_?alias|::alias' -l | xargs grep -l windows, there is nearly no test. I recently added one in clang/test/Sema/alias-unused-win.cpp.

The -Wunused-function diagnostic does not know how to connect an alias target to an internal linkage declaration, so we will get a false positive.

static int f1() { return 42; } // cxx-warning{{unused function 'f1'}}
int g1() __attribute__((alias("?f1@@YAHXZ")));

This can be fixed if #88823 is available.

Note: #88593 is about whether we should mangle an internal linkage function in extern "C" (static wins, no language linkage)

llvmbot commented 7 months ago

@llvm/issue-subscribers-clang-frontend

Author: Fangrui Song (MaskRay)

Clang supports the [`alias` attribute](https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute) from GCC. While primarily used for C projects on ELF platforms and possibly also windows-gnu, Clang accepts the `alias` attribute for windows-msvc targets as well. This scenario is more like "the feature is generic, let's make it available for everyone" rather than "there are active users of this for windows-msvc". After removing some false positive results from `rg '\(_?_?alias|::alias' -l | xargs grep -l windows`, there is nearly no test. I recently added one in `clang/test/Sema/alias-unused-win.cpp`. The `-Wunused-function` diagnostic does not know how to connect an alias target to an internal linkage declaration, so we will get a false positive. ``` static int f1() { return 42; } // cxx-warning{{unused function 'f1'}} int g1() __attribute__((alias("?f1@@YAHXZ"))); ``` This can be fixed if #88823 is available.