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

Nick Desaulniers via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 10 09:58:39 PST 2019


On Thu, Jan 10, 2019 at 7:26 AM Nico Weber <thakis at chromium.org> wrote:
>
> Aren't C names sometimes slightly mangled too? For example, on macOS they're prefixed by a _, doesn't that have to be undone there?

Sure, but short of instantiating a demangler in Sema or rewriting how
Clang checks for unused variables, this quickly cuts down on tens of
false negatives when building the Linux kernel with various driver
configurations enabled.  The code review points out this is
suboptimal.

>
> On Wed, Jan 9, 2019 at 6:58 PM Nick Desaulniers via cfe-commits <cfe-commits at lists.llvm.org> wrote:
>>
>> Author: nickdesaulniers
>> Date: Wed Jan  9 15:54:55 2019
>> New Revision: 350776
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=350776&view=rev
>> Log:
>> [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
>>
>> Added:
>>     cfe/trunk/test/Sema/alias-unused.c
>> Modified:
>>     cfe/trunk/lib/Sema/SemaDeclAttr.cpp
>>
>> Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=350776&r1=350775&r2=350776&view=diff
>> ==============================================================================
>> --- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
>> +++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Wed Jan  9 15:54:55 2019
>> @@ -1898,7 +1898,16 @@ static void handleAliasAttr(Sema &S, Dec
>>      }
>>    }
>>
>> -  // 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()));
>>
>> Added: cfe/trunk/test/Sema/alias-unused.c
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/alias-unused.c?rev=350776&view=auto
>> ==============================================================================
>> --- cfe/trunk/test/Sema/alias-unused.c (added)
>> +++ cfe/trunk/test/Sema/alias-unused.c Wed Jan  9 15:54:55 2019
>> @@ -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")));
>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits



-- 
Thanks,
~Nick Desaulniers


More information about the cfe-commits mailing list