<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, Jan 20, 2015 at 4:15 PM, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Mon, Jan 19, 2015 at 1:00 AM, David Majnemer<br>
<<a href="mailto:david.majnemer@gmail.com">david.majnemer@gmail.com</a>> wrote:<br>
> Author: majnemer<br>
> Date: Mon Jan 19 03:00:28 2015<br>
> New Revision: 226436<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=226436&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=226436&view=rev</a><br>
> Log:<br>
> Sema: Variable definitions cannot be __attribute__((alias))<br>
><br>
> Things that are OK:<br>
> extern int var1 __attribute((alias("v1")));<br>
> static int var2 __attribute((alias("v2")));<br>
<br>
</span>This seems surprising; it looks like there are two definitions of var2<br>
here (one from the alias and the other from the variable definition).<br>
I don't see that this is different from var3.<br></blockquote><div><br></div><div>I agree, GCC singles these out as being fine though.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span class=""><br>
> Things that are not OK:<br>
> int var3 __attribute((alias("v3")));<br>
> extern int var4 __attribute((alias("v4"))) = 4;<br>
<br>
</span>Is this OK:<br>
<br>
const int var6 __attribute__((alias("v6")));<br></blockquote><div><br></div><div>I assume this is -x c? GCC rejects this.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
It implicitly has internal linkage, and adding 'extern' to it would<br>
change semantics, but it looks more like var3 than var2.<br>
<div><div class="h5"><br>
> We choose to accpet:<br>
> struct S { static int var5 __attribute((alias("v5"))); };<br>
><br>
> This code causes assertion failues in GCC 4.8 and ICC 13.0.1, we have<br>
> no reason to reject it.<br>
><br>
> This partially fixes PR22217.<br>
><br>
> Modified:<br>
> cfe/trunk/include/clang/Basic/Attr.td<br>
> cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
> cfe/trunk/lib/Sema/SemaDecl.cpp<br>
> cfe/trunk/lib/Sema/SemaDeclAttr.cpp<br>
> cfe/trunk/test/Misc/ast-dump-attr.cpp<br>
> cfe/trunk/test/Sema/alias-redefinition.c<br>
><br>
> Modified: cfe/trunk/include/clang/Basic/Attr.td<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Attr.td?rev=226436&r1=226435&r2=226436&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Attr.td?rev=226436&r1=226435&r2=226436&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/include/clang/Basic/Attr.td (original)<br>
> +++ cfe/trunk/include/clang/Basic/Attr.td Mon Jan 19 03:00:28 2015<br>
> @@ -345,6 +345,8 @@ def AddressSpace : TypeAttr {<br>
> def Alias : Attr {<br>
> let Spellings = [GCC<"alias">];<br>
> let Args = [StringArgument<"Aliasee">];<br>
> + let Subjects = SubjectList<[Function, GlobalVar], ErrorDiag,<br>
> + "ExpectedFunctionGlobalVarMethodOrProperty">;<br>
> let Documentation = [Undocumented];<br>
> }<br>
><br>
><br>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=226436&r1=226435&r2=226436&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=226436&r1=226435&r2=226436&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)<br>
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Jan 19 03:00:28 2015<br>
> @@ -3902,10 +3902,8 @@ def warn_missing_variable_declarations :<br>
> def err_static_data_member_reinitialization :<br>
> Error<"static data member %0 already has an initializer">;<br>
> def err_redefinition : Error<"redefinition of %0">;<br>
> -def err_alias_after_tentative :<br>
> - Error<"alias definition of %0 after tentative definition">;<br>
> -def err_tentative_after_alias :<br>
> - Error<"tentative definition of %0 after alias definition">;<br>
> +def err_alias_is_definition :<br>
> + Error<"definition %0 cannot also be an alias">;<br>
> def err_definition_of_implicitly_declared_member : Error<<br>
> "definition of implicitly declared %select{default constructor|copy "<br>
> "constructor|move constructor|copy assignment operator|move assignment "<br>
><br>
> Modified: cfe/trunk/lib/Sema/SemaDecl.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=226436&r1=226435&r2=226436&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=226436&r1=226435&r2=226436&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/lib/Sema/SemaDecl.cpp (original)<br>
> +++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Jan 19 03:00:28 2015<br>
> @@ -2219,18 +2219,8 @@ static void checkNewAttributesAfterDef(S<br>
> const Attr *NewAttribute = NewAttributes[I];<br>
><br>
> if (isa<AliasAttr>(NewAttribute)) {<br>
> - if (FunctionDecl *FD = dyn_cast<FunctionDecl>(New))<br>
> - S.CheckForFunctionRedefinition(FD, cast<FunctionDecl>(Def));<br>
> - else {<br>
> - VarDecl *VD = cast<VarDecl>(New);<br>
> - unsigned Diag = cast<VarDecl>(Def)->isThisDeclarationADefinition() ==<br>
> - VarDecl::TentativeDefinition<br>
> - ? diag::err_alias_after_tentative<br>
> - : diag::err_redefinition;<br>
> - S.Diag(VD->getLocation(), Diag) << VD->getDeclName();<br>
> - S.Diag(Def->getLocation(), diag::note_previous_definition);<br>
> - VD->setInvalidDecl();<br>
> - }<br>
> + FunctionDecl *FD = cast<FunctionDecl>(New);<br>
> + S.CheckForFunctionRedefinition(FD, cast<FunctionDecl>(Def));<br>
> ++I;<br>
> continue;<br>
> }<br>
> @@ -5103,6 +5093,18 @@ static void checkAttributesAfterMerging(<br>
> if (ND.isExternallyVisible()) {<br>
> S.Diag(Attr->getLocation(), diag::err_attribute_weakref_not_static);<br>
> ND.dropAttr<WeakRefAttr>();<br>
> + ND.dropAttr<AliasAttr>();<br>
> + }<br>
> + }<br>
> +<br>
> + if (auto *VD = dyn_cast<VarDecl>(&ND)) {<br>
> + if (VD->hasInit()) {<br>
> + if (const auto *Attr = VD->getAttr<AliasAttr>()) {<br>
> + assert(VD->isThisDeclarationADefinition() &&<br>
> + !VD->isExternallyVisible() && "Broken AliasAttr handled late!");<br>
> + S.Diag(Attr->getLocation(), diag::err_alias_is_definition) << VD;<br>
> + VD->dropAttr<AliasAttr>();<br>
> + }<br>
> }<br>
> }<br>
><br>
> @@ -9617,18 +9619,6 @@ Sema::FinalizeDeclaration(Decl *ThisDecl<br>
> }<br>
> }<br>
><br>
> - if (!VD->isInvalidDecl() &&<br>
> - VD->isThisDeclarationADefinition() == VarDecl::TentativeDefinition) {<br>
> - if (const VarDecl *Def = VD->getDefinition()) {<br>
> - if (Def->hasAttr<AliasAttr>()) {<br>
> - Diag(VD->getLocation(), diag::err_tentative_after_alias)<br>
> - << VD->getDeclName();<br>
> - Diag(Def->getLocation(), diag::note_previous_definition);<br>
> - VD->setInvalidDecl();<br>
> - }<br>
> - }<br>
> - }<br>
> -<br>
> const DeclContext *DC = VD->getDeclContext();<br>
> // If there's a #pragma GCC visibility in scope, and this isn't a class<br>
> // member, set the visibility of this variable.<br>
><br>
> Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=226436&r1=226435&r2=226436&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=226436&r1=226435&r2=226436&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)<br>
> +++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Mon Jan 19 03:00:28 2015<br>
> @@ -1492,6 +1492,20 @@ static void handleAliasAttr(Sema &S, Dec<br>
> return;<br>
> }<br>
><br>
> + // Aliases should be on declarations, not definitions.<br>
> + if (const auto *FD = dyn_cast<FunctionDecl>(D)) {<br>
> + if (FD->isThisDeclarationADefinition()) {<br>
> + S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD;<br>
> + return;<br>
> + }<br>
> + } else {<br>
> + const auto *VD = cast<VarDecl>(D);<br>
> + if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) {<br>
> + S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD;<br>
> + return;<br>
> + }<br>
> + }<br>
> +<br>
> // FIXME: check if target symbol exists in current file<br>
><br>
> D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str,<br>
><br>
> Modified: cfe/trunk/test/Misc/ast-dump-attr.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/ast-dump-attr.cpp?rev=226436&r1=226435&r2=226436&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/ast-dump-attr.cpp?rev=226436&r1=226435&r2=226436&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/test/Misc/ast-dump-attr.cpp (original)<br>
> +++ cfe/trunk/test/Misc/ast-dump-attr.cpp Mon Jan 19 03:00:28 2015<br>
> @@ -79,7 +79,7 @@ void TestInt(void) __attribute__((constr<br>
> // CHECK: FunctionDecl{{.*}}TestInt<br>
> // CHECK-NEXT: ConstructorAttr{{.*}} 123<br>
><br>
> -int TestString __attribute__((alias("alias1")));<br>
> +static int TestString __attribute__((alias("alias1")));<br>
> // CHECK: VarDecl{{.*}}TestString<br>
> // CHECK-NEXT: AliasAttr{{.*}} "alias1"<br>
><br>
><br>
> Modified: cfe/trunk/test/Sema/alias-redefinition.c<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/alias-redefinition.c?rev=226436&r1=226435&r2=226436&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/alias-redefinition.c?rev=226436&r1=226435&r2=226436&view=diff</a><br>
> ==============================================================================<br>
> --- cfe/trunk/test/Sema/alias-redefinition.c (original)<br>
> +++ cfe/trunk/test/Sema/alias-redefinition.c Mon Jan 19 03:00:28 2015<br>
> @@ -23,22 +23,5 @@ void fun4(void);<br>
> void f5() {}<br>
> void __attribute((alias("f5"))) fun5(void) {} // expected-error {{redefinition of 'fun5'}} // expected-note {{previous definition}}<br>
><br>
> -int v1;<br>
> -int var1 __attribute((alias("v1"))); // expected-note {{previous definition}}<br>
> -int var1 __attribute((alias("v1"))); // expected-error {{redefinition of 'var1'}}<br>
> -<br>
> -int v2;<br>
> -int var2 = 2; // expected-note {{previous definition}}<br>
> -int var2 __attribute((alias("v2"))); // expected-error {{redefinition of 'var2'}}<br>
> -<br>
> -int v3;<br>
> -int var3 __attribute((alias("v3"))); // expected-note {{previous definition}}<br>
> -int var3 = 2; // expected-error {{redefinition of 'var3'}}<br>
> -<br>
> -int v4;<br>
> -int var4; // expected-note {{previous definition}}<br>
> -int var4 __attribute((alias("v4"))); // expected-error {{alias definition of 'var4' after tentative definition}}<br>
> -<br>
> -int v5;<br>
> -int var5 __attribute((alias("v5"))); // expected-note {{previous definition}}<br>
> -int var5; // expected-error {{tentative definition of 'var5' after alias definition}}<br>
<br>
</div></div>Might it be useful to keep these testcases (and mark the definitions<br>
with the alias attribute as as 'extern' to restore the former behavior<br>
of the tests)?<br>
<div class="HOEnZb"><div class="h5"><br>
> +int var1 __attribute((alias("v1"))); // expected-error {{definition 'var1' cannot also be an alias}}<br>
> +static int var2 __attribute((alias("v2"))) = 2; // expected-error {{definition 'var2' cannot also be an alias}}<br>
><br>
><br>
> _______________________________________________<br>
> cfe-commits mailing list<br>
> <a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</div></div></blockquote></div><br></div></div>