aboutsummaryrefslogtreecommitdiff
path: root/lib/Sema
diff options
context:
space:
mode:
authorHans Wennborg <hans@hanshq.net>2012-08-17 10:12:33 +0000
committerHans Wennborg <hans@hanshq.net>2012-08-17 10:12:33 +0000
commit8be9e77f7e532866fa0ef2bc8c3cd549f0cc8da2 (patch)
tree659b0a4c7f789355f04e19029a6e6db1813756b8 /lib/Sema
parent66a3d4754977cf0e98e510af9d68d00cbc14b7a8 (diff)
Warn about self-initialization of references.
Initializing a reference with itself, e.g. "int &a = a;" seems like a very bad idea. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@162093 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema')
-rw-r--r--lib/Sema/SemaDecl.cpp15
1 files changed, 9 insertions, 6 deletions
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index cb3ac68852..ea181de00a 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -6174,6 +6174,7 @@ namespace {
Decl *OrigDecl;
bool isRecordType;
bool isPODType;
+ bool isReferenceType;
public:
typedef EvaluatedExprVisitor<SelfReferenceChecker> Inherited;
@@ -6182,9 +6183,11 @@ namespace {
S(S), OrigDecl(OrigDecl) {
isPODType = false;
isRecordType = false;
+ isReferenceType = false;
if (ValueDecl *VD = dyn_cast<ValueDecl>(OrigDecl)) {
isPODType = VD->getType().isPODType(S.Context);
isRecordType = VD->getType()->isRecordType();
+ isReferenceType = VD->getType()->isReferenceType();
}
}
@@ -6192,9 +6195,9 @@ namespace {
// to determine which DeclRefExpr's to check. Assume that the casts
// are present and continue visiting the expression.
void HandleExpr(Expr *E) {
- // Skip checking T a = a where T is not a record type. Doing so is a
- // way to silence uninitialized warnings.
- if (isRecordType)
+ // Skip checking T a = a where T is not a record or reference type.
+ // Doing so is a way to silence uninitialized warnings.
+ if (isRecordType || isReferenceType)
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
HandleDeclRefExpr(DRE);
@@ -6309,11 +6312,11 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init,
}
// Check for self-references within variable initializers.
- // Variables declared within a function/method body are handled
- // by a dataflow analysis.
+ // Variables declared within a function/method body (except for references)
+ // are handled by a dataflow analysis.
// Record types initialized by initializer list are handled here.
// Initialization by constructors are handled in TryConstructorInitialization.
- if (!VDecl->hasLocalStorage() &&
+ if ((!VDecl->hasLocalStorage() || VDecl->getType()->isReferenceType()) &&
(isa<InitListExpr>(Init) || !VDecl->getType()->isRecordType()))
CheckSelfReference(RealDecl, Init);