[PATCH] D79895: Add a new warning to warn when passing uninitialized variables as const reference parameters to a function

Zequan Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 21 14:39:16 PDT 2020


zequanwu added inline comments.


================
Comment at: clang/lib/Sema/AnalysisBasedWarnings.cpp:1590-1600
+    // flush all const reference uses diags
+    for (const auto &P : constRefUses) {
+      const VarDecl *vd = P.first;
+      const MappedType &V = P.second;
+
+      UsesVec *vec = V.getPointer();
+      for (const auto &U : *vec) {
----------------
aeubanks wrote:
> zequanwu wrote:
> > rsmith wrote:
> > > Do we want any idiomatic-self-init special-case handling here? For example:
> > > 
> > > ```
> > > void f(const int&);
> > > void g() {
> > >   int a = a;
> > >   f(a);
> > > }
> > > ```
> > > 
> > > Following the logic above, should that warn on the `int a = a;` not on the `f(a)` call? Or should we warn only on the `f(a)` call itself in this case? It seems like it might be surprising to say that `a` is "uninitialized" here, since an initializer was provided, even though it was a garbage one.
> > For this case, I think we should warn at `int a = a`, like the comment in `DiagnoseUninitializedUse` said, https://github.com/llvm/llvm-project/blob/master/clang/lib/Sema/AnalysisBasedWarnings.cpp#L986-L996
> > 
> > `f(a)` is considered as accessing `a`.
> Doesn't `DiagnoseUninitializedUse` say that we shouldn't warn at `int a = a`?
The last sentence of the comment says that `int a = a` left `a` as uninitialized state, and we should warn at `int a = a` if there is an access of `a` in uninitialized state. Otherwise, we don't need to warn.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D79895/new/

https://reviews.llvm.org/D79895





More information about the cfe-commits mailing list