llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.83k stars 11.91k forks source link

Missing -Wuninitialized for copying structures in C++ mode #49411

Open davidbolvansky opened 3 years ago

davidbolvansky commented 3 years ago
Bugzilla Link 50067
Version trunk
OS Windows NT
CC @AaronBallman,@dwblaikie,@DougGregor,@MaskRay,@tkremenek,@Nathan-Huckleberry,@zygoloid

Extended Description

struct S {
  int i;
};

void test() {
  struct S s;
  struct S t;
  s = t;
}

void test2() {
  struct S s;
  struct S t;
  s = s;
}

class A {
  int i;
};

void test3() {
  A s;
  A t;
  s = t;
}

void test4() {
  class B {
    int i;
  };
  B s;
  B t;
  s = t;
}

https://godbolt.org/z/ob19oYzoj

Maybe due RecordDecl vs CXXRecordDecl somewhere? Any hints where to look?

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 3 years ago

In C++, those variables are initialized by the default constructor, and the warning isn't aware that the default constructor leaves the variable uninitialized. That happens here: https://github.com/llvm/llvm-project/blob/1cf3d68f9731199b3f753c5a87826c40a4d2168b/clang/lib/Analysis/UninitializedValues.cpp#L788

We'll also need to think about how to treat the assignment operator: if we don't explicitly handle it, we'll end up here: https://github.com/llvm/llvm-project/blob/1cf3d68f9731199b3f753c5a87826c40a4d2168b/clang/lib/Analysis/UninitializedValues.cpp#L435 and the right-hand side as a const-reference use, which means we'll warn immediately on an assignment under -Wuninitialized-const-reference, but we should probably warn unconditionally, since we "know" the assignment operator reads from its RHS operand.

llvmbot commented 2 years ago

@llvm/issue-subscribers-c-1