From e07d167a50165c85c4f440714bd2178e788f24b8 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Sat, 19 Jan 2013 00:49:00 +0000 Subject: [PATCH] Reword warning about using a *static* variable within its own initialization. The warning is still under -Wuninitialized (although technically this is defined behavior), but under a subgroup -Wstatic-self-init. This addresses PR 10265. llvm-svn: 172878 --- clang/include/clang/Basic/DiagnosticGroups.td | 4 +++- clang/include/clang/Basic/DiagnosticSemaKinds.td | 3 +++ clang/lib/Sema/SemaDecl.cpp | 14 ++++++++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td index 24c4ef3b40e3..8a2d59658690 100644 --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -259,7 +259,9 @@ def UndefinedReinterpretCast : DiagGroup<"undefined-reinterpret-cast">; def Unicode : DiagGroup<"unicode">; def UninitializedMaybe : DiagGroup<"conditional-uninitialized">; def UninitializedSometimes : DiagGroup<"sometimes-uninitialized">; -def Uninitialized : DiagGroup<"uninitialized", [UninitializedSometimes]>; +def UninitializedStaticSelfInit : DiagGroup<"static-self-init">; +def Uninitialized : DiagGroup<"uninitialized", [UninitializedSometimes, + UninitializedStaticSelfInit]>; def UnknownPragmas : DiagGroup<"unknown-pragmas">; def UnknownWarningOption : DiagGroup<"unknown-warning-option">; def NSobjectAttribute : DiagGroup<"NSObject-attribute">; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index c23d9eee4820..c3f034a53a4f 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -1292,6 +1292,9 @@ def warn_field_is_uninit : Warning<"field %0 is uninitialized when used here">, def warn_reference_field_is_uninit : Warning< "reference %0 is not yet bound to a value when used here">, InGroup; +def warn_static_self_reference_in_init : Warning< + "static variable %0 is suspiciously used within its own initialization">, + InGroup; def warn_uninit_self_reference_in_init : Warning< "variable %0 is uninitialized when used within its own initialization">, InGroup; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 9cf5e73665a4..dbdbc3d6a2c3 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -6683,11 +6683,17 @@ namespace { void VisitObjCMessageExpr(ObjCMessageExpr *E) { return; } void HandleDeclRefExpr(DeclRefExpr *DRE) { - Decl* ReferenceDecl = DRE->getDecl(); + Decl* ReferenceDecl = DRE->getDecl(); if (OrigDecl != ReferenceDecl) return; - unsigned diag = isReferenceType - ? diag::warn_uninit_self_reference_in_reference_init - : diag::warn_uninit_self_reference_in_init; + unsigned diag; + if (isReferenceType) { + diag = diag::warn_uninit_self_reference_in_reference_init; + } else if (cast(OrigDecl)->isStaticLocal()) { + diag = diag::warn_static_self_reference_in_init; + } else { + diag = diag::warn_uninit_self_reference_in_init; + } + S.DiagRuntimeBehavior(DRE->getLocStart(), DRE, S.PDiag(diag) << DRE->getNameInfo().getName()