diff --git a/clang/lib/Analysis/UninitializedValues.cpp b/clang/lib/Analysis/UninitializedValues.cpp --- a/clang/lib/Analysis/UninitializedValues.cpp +++ b/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(Ex); diff --git a/clang/test/SemaCXX/warn-uninitialized-const-reference.cpp b/clang/test/SemaCXX/warn-uninitialized-const-reference.cpp --- 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 @@ bool operator!=(const A &); }; +template +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}} }