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