]> granicus.if.org Git - clang/commitdiff
Warn about self-initialization of references.
authorHans Wennborg <hans@hanshq.net>
Fri, 17 Aug 2012 10:12:33 +0000 (10:12 +0000)
committerHans Wennborg <hans@hanshq.net>
Fri, 17 Aug 2012 10:12:33 +0000 (10:12 +0000)
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

lib/Sema/SemaDecl.cpp
test/Analysis/stack-addr-ps.cpp
test/SemaCXX/convert-to-bool.cpp
test/SemaCXX/references.cpp
test/SemaCXX/uninitialized.cpp

index cb3ac68852787cae42f9304058e53387b89cdf4c..ea181de00a988f6f4a7898b7d2e1416b22cae51b 100644 (file)
@@ -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);
 
index b21a03dc38a4e2e64dbc26b8e920d7ff26909852..cbdb143c1857e0d02d9360c9d0b867f4e270dddf 100644 (file)
@@ -87,6 +87,6 @@ struct TS {
 
 // rdar://11345441
 int* f5() {
-  int& i = i; // expected-warning {{Assigned value is garbage or undefined}} expected-note {{binding reference variable 'i' here}}
+  int& i = i; // expected-warning {{Assigned value is garbage or undefined}} expected-note {{binding reference variable 'i' here}} expected-warning{{variable 'i' is uninitialized when used within its own initialization}}
   return &i; // expected-warning {{address of stack memory associated with local variable 'i' returned}}
 }
index c9a355549fdf20055a732641bfda6d9fb8e30d24..b52f11c93d39a40d0575796ad8d52ca53b186cfc 100644 (file)
@@ -62,6 +62,5 @@ struct C {
 
 void test_copy_init_conversions(C c) {
   A &a = c; // expected-error{{no viable conversion from 'C' to 'A'}}
-  B &b = b; // okay
+  B &b = c; // okay
 }
-
index 70d3799a0ea5684e0d1bc47d67eee8e4e5c2afda..028c6909210ec1129fb9b97a98dd5625d5b80d86 100644 (file)
@@ -136,4 +136,4 @@ namespace PR8608 {
 }
 
 // The following crashed trying to recursively evaluate the LValue.
-const int &do_not_crash = do_not_crash;
+const int &do_not_crash = do_not_crash; // expected-warning{{variable 'do_not_crash' is uninitialized when used within its own initialization}}
index 890f21293030dd9ad151fcdc25725a09e11121a9..385548b51cca63f6554d42ad1c836432a9bf7c6b 100644 (file)
@@ -378,3 +378,22 @@ namespace statics {
     }
   }
 }
+
+namespace references {
+  int &a = a; // expected-warning{{variable 'a' is uninitialized when used within its own initialization}}
+
+  struct S {
+    S() : a(a) {} // expected-warning{{field is uninitialized when used here}}
+    int &a;
+  };
+
+  void f() {
+    int &a = a; // expected-warning{{variable 'a' is uninitialized when used within its own initialization}}
+  }
+
+  struct T {
+    T() : a(b), b(a) {} // FIXME: Warn here.
+    int &a, &b;
+    int &c = c; // FIXME: Warn here.
+  };
+}