forked from OSchip/llvm-project
[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
This commit is contained in:
parent
a39e9cf6be
commit
054704082b
|
@ -405,6 +405,15 @@ static bool isPointerToConst(const QualType &QT) {
|
||||||
return QT->isAnyPointerType() && QT->getPointeeType().isConstQualified();
|
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) {
|
void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
|
||||||
// Classify arguments to std::move as used.
|
// Classify arguments to std::move as used.
|
||||||
if (CE->isCallToStdMove()) {
|
if (CE->isCallToStdMove()) {
|
||||||
|
@ -413,7 +422,7 @@ void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
|
||||||
classify(CE->getArg(0), Use);
|
classify(CE->getArg(0), Use);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
bool isTrivialBody = hasTrivialBody(CE);
|
||||||
// If a value is passed by const pointer to a function,
|
// If a value is passed by const pointer to a function,
|
||||||
// we should not assume that it is initialized by the call, and we
|
// we should not assume that it is initialized by the call, and we
|
||||||
// conservatively do not assume that it is used.
|
// conservatively do not assume that it is used.
|
||||||
|
@ -423,7 +432,7 @@ void ClassifyRefs::VisitCallExpr(CallExpr *CE) {
|
||||||
I != E; ++I) {
|
I != E; ++I) {
|
||||||
if ((*I)->isGLValue()) {
|
if ((*I)->isGLValue()) {
|
||||||
if ((*I)->getType().isConstQualified())
|
if ((*I)->getType().isConstQualified())
|
||||||
classify((*I), ConstRefUse);
|
classify((*I), isTrivialBody ? Ignore : ConstRefUse);
|
||||||
} else if (isPointerToConst((*I)->getType())) {
|
} else if (isPointerToConst((*I)->getType())) {
|
||||||
const Expr *Ex = stripCasts(DC->getParentASTContext(), *I);
|
const Expr *Ex = stripCasts(DC->getParentASTContext(), *I);
|
||||||
const auto *UO = dyn_cast<UnaryOperator>(Ex);
|
const auto *UO = dyn_cast<UnaryOperator>(Ex);
|
||||||
|
|
|
@ -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 {
|
class A {
|
||||||
public:
|
public:
|
||||||
|
@ -9,6 +9,16 @@ public:
|
||||||
bool operator!=(const 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);
|
A const_ref_use_A(const A &a);
|
||||||
int const_ref_use(const int &i);
|
int const_ref_use(const int &i);
|
||||||
A const_use_A(const A a);
|
A const_use_A(const A a);
|
||||||
|
@ -33,4 +43,13 @@ void f(int a) {
|
||||||
if (a < 42)
|
if (a < 42)
|
||||||
m = 1;
|
m = 1;
|
||||||
const_ref_use(m);
|
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}}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue