[clang] 0547040 - [SemaCXX] Fix false positive of -Wuninitialized-const-reference in empty function body.
Zequan Wu via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 6 10:52:14 PDT 2020
Author: Zequan Wu
Date: 2020-07-06T10:52:05-07:00
New Revision: 054704082b461418d3dac3a379792cdaf52d40b3
URL: https://github.com/llvm/llvm-project/commit/054704082b461418d3dac3a379792cdaf52d40b3
DIFF: https://github.com/llvm/llvm-project/commit/054704082b461418d3dac3a379792cdaf52d40b3.diff
LOG: [SemaCXX] Fix false positive of -Wuninitialized-const-reference in empty function body.
Summary:
Some libraries use empty function to ignore unused variable warnings, which gets a new warning from `-Wuninitialized-const-reference`, discussed here https://reviews.llvm.org/D79895#2107604.
This patch should fix that.
Reviewers: hans, nick, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: aaron.ballman, riccibruno, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D82425
Added:
Modified:
clang/lib/Analysis/UninitializedValues.cpp
clang/test/SemaCXX/warn-uninitialized-const-reference.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/UninitializedValues.cpp b/clang/lib/Analysis/UninitializedValues.cpp
index 3d44d1c070fe..67cd39728c35 100644
--- a/clang/lib/Analysis/UninitializedValues.cpp
+++ b/clang/lib/Analysis/UninitializedValues.cpp
@@ -405,6 +405,15 @@ static bool isPointerToConst(const QualType &QT) {
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 @@ void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
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 @@ void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
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);
diff --git a/clang/test/SemaCXX/warn-uninitialized-const-reference.cpp b/clang/test/SemaCXX/warn-uninitialized-const-reference.cpp
index 292793ba483a..d24b561441d8 100644
--- a/clang/test/SemaCXX/warn-uninitialized-const-reference.cpp
+++ b/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 @@ class A {
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 @@ void f(int a) {
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}}
}
More information about the cfe-commits
mailing list