This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Mark target of __attribute__((alias("target"))) used for C
ClosedPublic

Authored by nickdesaulniers on Nov 6 2018, 4:16 PM.

Details

Summary

Prevents -Wunneeded-internal-delcaration warnings when the target has no
other references. This occurs frequently in device drivers in the Linux
kernel.

Sema would need to invoke the demangler on the target, since in C++ the
target name is mangled:

int f() { return 42; }
int g() attribute((alias("_Z1fv")));

Sema does not have the ability to demangle names at this time.

https://bugs.llvm.org/show_bug.cgi?id=39088
https://github.com/ClangBuiltLinux/linux/issues/232

Diff Detail

Repository
rL LLVM

Event Timeline

nickdesaulniers created this revision.Nov 6 2018, 4:16 PM
nickdesaulniers edited the summary of this revision. (Show Details)Nov 6 2018, 4:18 PM
rsmith accepted this revision.Jan 9 2019, 11:29 AM
rsmith added a reviewer: rjmccall.

I don't like having a solution that doesn't work for C++ and can't reasonably be extended to a solution for C++ (we really don't want to be demangling the alias target here and trying to match it against the program, and ideally we don't want to delay the warning in question to codegen time), but this does reduce warning false-positives so it seems hard to object to. (We'll still need to tell people to mark declarations as __attribute__((used)) when they're used in this way if their names are mangled.)

On balance I'm OK with this, but adding @rjmccall in case he has a different view or sees a better approach.

This revision is now accepted and ready to land.Jan 9 2019, 11:30 AM

I really dislike this particular idiom.

This is probably the most reasonable short-term solution to this problem. I do wonder if we should just shift to making Sema track a symbol-to-GD map, though, and maybe make Sema entirely responsible for pushing entities to IRGen to emit.

test/Sema/alias-unused.c
3 ↗(On Diff #172877)

Is this meant to be static?

nickdesaulniers marked an inline comment as done.Jan 9 2019, 1:38 PM
nickdesaulniers added inline comments.
test/Sema/alias-unused.c
3 ↗(On Diff #172877)

It doesn't make a difference for this test. Would you like me to add it before submitting?

rjmccall added inline comments.Jan 9 2019, 1:44 PM
test/Sema/alias-unused.c
3 ↗(On Diff #172877)

It's what you're trying to test, right? f() is not otherwise an internal declaration, so of course the warning won't fire for it.

  • add static keyword to test case
nickdesaulniers marked 2 inline comments as done.Jan 9 2019, 2:13 PM
nickdesaulniers added inline comments.
test/Sema/alias-unused.c
3 ↗(On Diff #172877)

sorry, you're right. (I was testing with my locally patched version of clang, godbolt to the rescue to test "pre-my-change-clang." Done.

This revision was automatically updated to reflect the committed changes.
nickdesaulniers marked an inline comment as done.

Don't worry, that's a familiar mistake. :)

LGTM.