[PATCH] D82425: [SemaCXX] Fix false positive of -Wuninitialized-const-reference in empty function body.

Zequan Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 7 00:28:06 PDT 2020


This revision was not accepted when it landed; it landed in state "Needs Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rG054704082b46: [SemaCXX] Fix false positive of -Wuninitialized-const-reference in empty… (authored by zequanwu).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D82425

Files:
  clang/lib/Analysis/UninitializedValues.cpp
  clang/test/SemaCXX/warn-uninitialized-const-reference.cpp


Index: clang/test/SemaCXX/warn-uninitialized-const-reference.cpp
===================================================================
--- clang/test/SemaCXX/warn-uninitialized-const-reference.cpp
+++ clang/test/SemaCXX/warn-uninitialized-const-reference.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -Wuninitialized-const-reference -verify %s
+// RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -fexceptions -Wuninitialized-const-reference -verify %s
 
 class A {
 public:
@@ -9,6 +9,16 @@
   bool operator!=(const A &);
 };
 
+template <class T>
+void ignore_template(const T &) {}
+void ignore(const int &i) {}
+void dont_ignore_non_empty(const int &i) { ; } // Calling this won't silence the warning for you
+void dont_ignore_block(const int &i) {
+  {}
+} // Calling this won't silence the warning for you
+void ignore_function_try_block_maybe_who_knows(const int &) try {
+} catch (...) {
+}
 A const_ref_use_A(const A &a);
 int const_ref_use(const int &i);
 A const_use_A(const A a);
@@ -33,4 +43,13 @@
   if (a < 42)
     m = 1;
   const_ref_use(m);
+
+  int l;
+  ignore_template(l); // This is a pattern to avoid "unused variable" warnings (e.g. boost::ignore_unused).
+  ignore(l);
+  dont_ignore_non_empty(l); // expected-warning {{variable 'l' is uninitialized when passed as a const reference argument here}}
+  int l1;
+  dont_ignore_block(l1); // expected-warning {{variable 'l1' is uninitialized when passed as a const reference argument here}}
+  int l2;
+  ignore_function_try_block_maybe_who_knows(l2); // expected-warning {{variable 'l2' is uninitialized when passed as a const reference argument here}}
 }
Index: clang/lib/Analysis/UninitializedValues.cpp
===================================================================
--- clang/lib/Analysis/UninitializedValues.cpp
+++ clang/lib/Analysis/UninitializedValues.cpp
@@ -405,6 +405,15 @@
   return QT->isAnyPointerType() && QT->getPointeeType().isConstQualified();
 }
 
+static bool hasTrivialBody(CallExpr *CE) {
+  if (FunctionDecl *FD = CE->getDirectCallee()) {
+    if (FunctionTemplateDecl *FTD = FD->getPrimaryTemplate())
+      return FTD->getTemplatedDecl()->hasTrivialBody();
+    return FD->hasTrivialBody();
+  }
+  return false;
+}
+
 void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
   // Classify arguments to std::move as used.
   if (CE->isCallToStdMove()) {
@@ -413,7 +422,7 @@
       classify(CE->getArg(0), Use);
     return;
   }
-
+  bool isTrivialBody = hasTrivialBody(CE);
   // If a value is passed by const pointer to a function,
   // we should not assume that it is initialized by the call, and we
   // conservatively do not assume that it is used.
@@ -423,7 +432,7 @@
        I != E; ++I) {
     if ((*I)->isGLValue()) {
       if ((*I)->getType().isConstQualified())
-        classify((*I), ConstRefUse);
+        classify((*I), isTrivialBody ? Ignore : ConstRefUse);
     } else if (isPointerToConst((*I)->getType())) {
       const Expr *Ex = stripCasts(DC->getParentASTContext(), *I);
       const auto *UO = dyn_cast<UnaryOperator>(Ex);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82425.275619.patch
Type: text/x-patch
Size: 3049 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200707/f7b51c19/attachment-0001.bin>


More information about the cfe-commits mailing list