From: Nick Desaulniers Date: Wed, 9 Jan 2019 23:54:55 +0000 (+0000) Subject: [Sema] Mark target of __attribute__((alias("target"))) used for C X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c5a37dd350a4e86e75ae49e3a4d6c55c0e803160;p=clang [Sema] Mark target of __attribute__((alias("target"))) used for C 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 Reviewers: rsmith, rjmccall Reviewed By: rsmith Subscribers: erik.pilkington, cfe-commits, pirama, srhines Differential Revision: https://reviews.llvm.org/D54188 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@350776 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 864c930136..b5859d1fb7 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -1898,7 +1898,16 @@ static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) { } } - // FIXME: check if target symbol exists in current file + // Mark target used to prevent unneeded-internal-declaration warnings. + if (!S.LangOpts.CPlusPlus) { + // FIXME: demangle Str for C++, as the attribute refers to the mangled + // linkage name, not the pre-mangled identifier. + const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc()); + LookupResult LR(S, target, Sema::LookupOrdinaryName); + if (S.LookupQualifiedName(LR, S.getCurLexicalContext())) + for (NamedDecl *ND : LR) + ND->markUsed(S.Context); + } D->addAttr(::new (S.Context) AliasAttr(AL.getRange(), S.Context, Str, AL.getAttributeSpellingListIndex())); diff --git a/test/Sema/alias-unused.c b/test/Sema/alias-unused.c new file mode 100644 index 0000000000..5cedc93c29 --- /dev/null +++ b/test/Sema/alias-unused.c @@ -0,0 +1,7 @@ +// RUN: %clang_cc1 -triple x86_64-linux-gnu -Wunneeded-internal-declaration -x c -verify %s +// expected-no-diagnostics +static int f() { return 42; } +int g() __attribute__((alias("f"))); + +static int foo [] = { 42, 0xDEAD }; +extern typeof(foo) bar __attribute__((unused, alias("foo")));