]> granicus.if.org Git - clang/commitdiff
Disable the uninitialized field warning in uninstantiated classes.
authorRichard Trieu <rtrieu@google.com>
Wed, 22 Oct 2014 02:52:00 +0000 (02:52 +0000)
committerRichard Trieu <rtrieu@google.com>
Wed, 22 Oct 2014 02:52:00 +0000 (02:52 +0000)
If a templated class is not instantiated, then the AST for it could be missing
some things that would throw the field checker off.  Wait until specialization
before emitting these warnings.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220363 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDeclCXX.cpp
test/SemaCXX/uninitialized.cpp

index 1d87abb33f3a7679ecbfe1a4a90a976935d4cbcf..283f032061fde205cdf2730d0e28dd98e5619dcc 100644 (file)
@@ -2525,6 +2525,9 @@ namespace {
 
     const CXXRecordDecl *RD = Constructor->getParent();
 
+    if (RD->getDescribedClassTemplate() != nullptr)
+      return;
+
     // Holds fields that are uninitialized.
     llvm::SmallPtrSet<ValueDecl*, 4> UninitializedFields;
 
index 2596dd044deec66abc65846fc4ac5d87f3e80b88..61dabb2d0010d636b8dc5e4f6d6c7d5dd54a9ac1 100644 (file)
@@ -1220,3 +1220,46 @@ namespace init_list {
     {}
   };
 }
+
+namespace template_class {
+class Foo {
+ public:
+    int *Create() { return nullptr; }
+};
+
+template <typename T>
+class A {
+public:
+  // Don't warn on foo here.
+  A() : ptr(foo->Create()) {}
+
+private:
+  Foo *foo = new Foo;
+  int *ptr;
+};
+
+template <typename T>
+class B {
+public:
+  // foo is uninitialized here, but class B is never instantiated.
+  B() : ptr(foo->Create()) {}
+
+private:
+  Foo *foo;
+  int *ptr;
+};
+
+template <typename T>
+class C {
+public:
+  C() : ptr(foo->Create()) {}
+  // expected-warning@-1 {{field 'foo' is uninitialized when used here}}
+private:
+  Foo *foo;
+  int *ptr;
+};
+
+C<int> c;
+// expected-note@-1 {{in instantiation of member function 'template_class::C<int>::C' requested here}}
+
+}