From: Fariborz Jahanian Date: Thu, 29 Sep 2011 18:40:01 +0000 (+0000) Subject: c - Enumerators may inherit the deprecated/unavailable X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=97db7265ac1993e14e5877292e23d5ed2e9cf719;p=clang c - Enumerators may inherit the deprecated/unavailable attributes from the enumeration type. // rdar://10201690 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140800 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index bb7521b243..e7731fed42 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -2222,7 +2222,8 @@ public: bool CanUseDecl(NamedDecl *D); bool DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, - const ObjCInterfaceDecl *UnknownObjCClass = 0); + const ObjCInterfaceDecl *UnknownObjCClass = 0, + const EnumDecl *EnumeratorEnumDecl = 0); std::string getDeletedOrUnavailableSuffix(const FunctionDecl *FD); bool DiagnosePropertyAccessorMismatch(ObjCPropertyDecl *PD, ObjCMethodDecl *Getter, diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 2b67b4dc48..f9d34a7fa3 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -69,7 +69,8 @@ bool Sema::CanUseDecl(NamedDecl *D) { /// referenced), false otherwise. /// bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, - const ObjCInterfaceDecl *UnknownObjCClass) { + const ObjCInterfaceDecl *UnknownObjCClass, + const EnumDecl *EnumeratorEnumDecl) { if (getLangOptions().CPlusPlus && isa(D)) { // If there were any diagnostics suppressed by template argument deduction, // emit them now. @@ -106,11 +107,12 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, // See if this declaration is unavailable or deprecated. std::string Message; - switch (D->getAvailability(&Message)) { + AvailabilityResult Result = D->getAvailability(&Message); + switch (Result) { case AR_Available: case AR_NotYetIntroduced: break; - + case AR_Deprecated: EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass); break; @@ -134,9 +136,17 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, } // Warn if this is used but marked unused. - if (D->hasAttr()) + if (D->hasAttr() && !EnumeratorEnumDecl) Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); - + // For available enumerator, it will become unavailable/deprecated + // if its enum declaration is as such. + if (Result == AR_Available) + if (const EnumConstantDecl *ECD = dyn_cast(D)) { + const DeclContext *DC = ECD->getDeclContext(); + if (const EnumDecl *TheEnumDecl = dyn_cast(DC)) + DiagnoseUseOfDecl(const_cast< EnumDecl *>(TheEnumDecl), + Loc, UnknownObjCClass, TheEnumDecl); + } return false; } diff --git a/test/Sema/attr-deprecated.c b/test/Sema/attr-deprecated.c index eeef0d757a..2889f8fa11 100644 --- a/test/Sema/attr-deprecated.c +++ b/test/Sema/attr-deprecated.c @@ -109,7 +109,7 @@ enum __attribute__((deprecated)) Test20 { void test20() { enum Test20 f; // expected-warning {{'Test20' is deprecated}} f = test20_a; // expected-warning {{'test20_a' is deprecated}} - f = test20_b; + f = test20_b; // expected-warning {{'Test20' is deprecated}} } char test21[__has_feature(attribute_deprecated_with_message) ? 1 : -1]; diff --git a/test/Sema/attr-unavailable-message.c b/test/Sema/attr-unavailable-message.c index 9f663fc4ef..9b0c3debd8 100644 --- a/test/Sema/attr-unavailable-message.c +++ b/test/Sema/attr-unavailable-message.c @@ -26,3 +26,24 @@ void unavail(void) { void (*fp)() = &bar; double (*fp4)(double) = dfoo; } + +// rdar://10201690 +enum foo { + a = 1, + b __attribute__((deprecated())) = 2, + c = 3 +}__attribute__((deprecated())); + +enum fee { // expected-note 2 {{declaration has been explicitly marked unavailable here}} + r = 1, + s = 2, + t = 3 +}__attribute__((unavailable())); + +enum fee f() { // expected-error {{error: 'fee' is unavailable}} + int i = a; // expected-warning {{'foo' is deprecated }} + + i = b; // expected-warning {{'b' is deprecated}} + + return r; // expected-error {{'fee' is unavailable}} +} diff --git a/test/SemaCXX/attr-deprecated.cpp b/test/SemaCXX/attr-deprecated.cpp index fe7c833d32..945aff363e 100644 --- a/test/SemaCXX/attr-deprecated.cpp +++ b/test/SemaCXX/attr-deprecated.cpp @@ -198,7 +198,7 @@ namespace test6 { }; void testA() { A x; // expected-warning {{'A' is deprecated}} - x = a0; + x = a0; // expected-warning {{'A' is deprecated}} } enum B { @@ -218,7 +218,7 @@ namespace test6 { }; void testC() { C::Enum x; // expected-warning {{'Enum' is deprecated}} - x = C::c0; + x = C::c0; // expected-warning {{'Enum' is deprecated}} } template struct D {