<div dir="ltr"><div>Indeed!</div><div><br></div><div>There pointer is moved later on! Interestingly, I run these warnings on 300+ projects and none of them had this pattern. Will revert or fix the patch soon. <br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 9 Aug 2019 at 17:13, Nico Weber <<a href="mailto:thakis@chromium.org">thakis@chromium.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><a href="http://lab.llvm.org:8011/builders/clang-with-lto-ubuntu/builds/14045" target="_blank">http://lab.llvm.org:8011/builders/clang-with-lto-ubuntu/builds/14045</a><br><div><br></div><div>FAILED: tools/clang/lib/Frontend/CMakeFiles/obj.clangFrontend.dir/ASTConsumers.cpp.o <br>/home/buildslave/buildslave1a/clang-with-lto-ubuntu/install/stage1/bin/clang++ -DGTEST_HAS_RTTI=0 -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Itools/clang/lib/Frontend -I/home/buildslave/buildslave1a/clang-with-lto-ubuntu/llvm.src/tools/clang/lib/Frontend -I/home/buildslave/buildslave1a/clang-with-lto-ubuntu/llvm.src/tools/clang/include -Itools/clang/include -Iinclude -I/home/buildslave/buildslave1a/clang-with-lto-ubuntu/llvm.src/include -fPIC -fvisibility-inlines-hidden -Werror -Werror=date-time -Werror=unguarded-availability-new -std=c++11 -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wcovered-switch-default -Wno-noexcept-type -Wnon-virtual-dtor -Wdelete-non-virtual-dtor -Wstring-conversion -fdiagnostics-color -ffunction-sections -fdata-sections -fno-common -Woverloaded-virtual -Wno-nested-anon-types -O3 -UNDEBUG -fno-exceptions -fno-rtti -MD -MT tools/clang/lib/Frontend/CMakeFiles/obj.clangFrontend.dir/ASTConsumers.cpp.o -MF tools/clang/lib/Frontend/CMakeFiles/obj.clangFrontend.dir/ASTConsumers.cpp.o.d -o tools/clang/lib/Frontend/CMakeFiles/obj.clangFrontend.dir/ASTConsumers.cpp.o -c /home/buildslave/buildslave1a/clang-with-lto-ubuntu/llvm.src/tools/clang/lib/Frontend/ASTConsumers.cpp<br>/home/buildslave/buildslave1a/clang-with-lto-ubuntu/llvm.src/tools/clang/lib/Frontend/ASTConsumers.cpp:40:22: error: binding reference member 'Out' to stack allocated parameter 'Out' [-Werror,-Wdangling-field]<br> : Out(Out ? *Out : llvm::outs()), OwnedOut(std::move(Out)),<br> ^~~<br>/home/buildslave/buildslave1a/clang-with-lto-ubuntu/llvm.src/tools/clang/lib/Frontend/ASTConsumers.cpp:98:18: note: reference member declared here<br> raw_ostream &Out;<br> ^<br>1 error generated.<br></div><div><br></div><div><a href="http://llvm-cs.pcc.me.uk/tools/clang/lib/Frontend/ASTConsumers.cpp#40" target="_blank">http://llvm-cs.pcc.me.uk/tools/clang/lib/Frontend/ASTConsumers.cpp#40</a><br></div><div><br></div><div>That looks like a false positive.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Aug 9, 2019 at 7:02 PM Gabor Horvath via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Author: xazax<br>
Date: Fri Aug 9 16:03:50 2019<br>
New Revision: 368499<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=368499&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=368499&view=rev</a><br>
Log:<br>
Attempt to reapply "Even more warnings utilizing gsl::Owner/gsl::Pointer annotations"<br>
<br>
Modified:<br>
cfe/trunk/lib/Sema/SemaInit.cpp<br>
cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaInit.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=368499&r1=368498&r2=368499&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaInit.cpp?rev=368499&r1=368498&r2=368499&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaInit.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaInit.cpp Fri Aug 9 16:03:50 2019<br>
@@ -6568,19 +6568,33 @@ static bool shouldTrackImplicitObjectArg<br>
if (auto *Conv = dyn_cast_or_null<CXXConversionDecl>(Callee))<br>
if (isRecordWithAttr<PointerAttr>(Conv->getConversionType()))<br>
return true;<br>
- if (!Callee->getParent()->isInStdNamespace() || !Callee->getIdentifier())<br>
+ if (!Callee->getParent()->isInStdNamespace())<br>
return false;<br>
if (!isRecordWithAttr<PointerAttr>(Callee->getThisObjectType()) &&<br>
!isRecordWithAttr<OwnerAttr>(Callee->getThisObjectType()))<br>
return false;<br>
- if (!isRecordWithAttr<PointerAttr>(Callee->getReturnType()) &&<br>
- !Callee->getReturnType()->isPointerType())<br>
- return false;<br>
- return llvm::StringSwitch<bool>(Callee->getName())<br>
- .Cases("begin", "rbegin", "cbegin", "crbegin", true)<br>
- .Cases("end", "rend", "cend", "crend", true)<br>
- .Cases("c_str", "data", "get", true)<br>
- .Default(false);<br>
+ if (Callee->getReturnType()->isPointerType() ||<br>
+ isRecordWithAttr<PointerAttr>(Callee->getReturnType())) {<br>
+ if (!Callee->getIdentifier())<br>
+ return false;<br>
+ return llvm::StringSwitch<bool>(Callee->getName())<br>
+ .Cases("begin", "rbegin", "cbegin", "crbegin", true)<br>
+ .Cases("end", "rend", "cend", "crend", true)<br>
+ .Cases("c_str", "data", "get", true)<br>
+ // Map and set types.<br>
+ .Cases("find", "equal_range", "lower_bound", "upper_bound", true)<br>
+ .Default(false);<br>
+ } else if (Callee->getReturnType()->isReferenceType()) {<br>
+ if (!Callee->getIdentifier()) {<br>
+ auto OO = Callee->getOverloadedOperator();<br>
+ return OO == OverloadedOperatorKind::OO_Subscript ||<br>
+ OO == OverloadedOperatorKind::OO_Star;<br>
+ }<br>
+ return llvm::StringSwitch<bool>(Callee->getName())<br>
+ .Cases("front", "back", "at", true)<br>
+ .Default(false);<br>
+ }<br>
+ return false;<br>
}<br>
<br>
static void handleGslAnnotatedTypes(IndirectLocalPath &Path, Expr *Call,<br>
@@ -6600,6 +6614,12 @@ static void handleGslAnnotatedTypes(Indi<br>
if (MD && shouldTrackImplicitObjectArg(MD))<br>
VisitPointerArg(MD, MCE->getImplicitObjectArgument());<br>
return;<br>
+ } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(Call)) {<br>
+ FunctionDecl *Callee = OCE->getDirectCallee();<br>
+ if (Callee && Callee->isCXXInstanceMember() &&<br>
+ shouldTrackImplicitObjectArg(cast<CXXMethodDecl>(Callee)))<br>
+ VisitPointerArg(Callee, OCE->getArg(0));<br>
+ return;<br>
}<br>
<br>
if (auto *CCE = dyn_cast<CXXConstructExpr>(Call)) {<br>
<br>
Modified: cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp?rev=368499&r1=368498&r2=368499&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp?rev=368499&r1=368498&r2=368499&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp (original)<br>
+++ cfe/trunk/test/Sema/warn-lifetime-analysis-nocfg.cpp Fri Aug 9 16:03:50 2019<br>
@@ -121,24 +121,47 @@ void initLocalGslPtrWithTempOwner() {<br>
<br>
namespace std {<br>
template <typename T><br>
-struct basic_iterator {};<br>
+struct basic_iterator {<br>
+ basic_iterator operator++();<br>
+ T& operator*();<br>
+};<br>
+<br>
+template<typename T><br>
+bool operator!=(basic_iterator<T>, basic_iterator<T>);<br>
<br>
template <typename T><br>
struct vector {<br>
typedef basic_iterator<T> iterator;<br>
iterator begin();<br>
+ iterator end();<br>
T *data();<br>
+ T &at(int n);<br>
+};<br>
+<br>
+template<typename T><br>
+struct basic_string_view {<br>
+ basic_string_view(const T *);<br>
+ const T *begin() const;<br>
};<br>
<br>
template<typename T><br>
struct basic_string {<br>
const T *c_str() const;<br>
+ operator basic_string_view<T> () const;<br>
};<br>
<br>
+<br>
template<typename T><br>
struct unique_ptr {<br>
T *get() const;<br>
};<br>
+<br>
+template<typename T><br>
+struct optional {<br>
+ optional();<br>
+ optional(const T&);<br>
+ T &operator*();<br>
+};<br>
}<br>
<br>
void modelIterators() {<br>
@@ -168,3 +191,29 @@ int *danglingUniquePtrFromTemp() {<br>
int *danglingUniquePtrFromTemp2() {<br>
return std::unique_ptr<int>().get(); // expected-warning {{returning address of local temporary object}}<br>
}<br>
+<br>
+void danglingReferenceFromTempOwner() {<br>
+ int &r = *std::optional<int>(); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}<br>
+ int &r2 = *std::optional<int>(5); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}<br>
+ int &r3 = std::vector<int>().at(3); // expected-warning {{object backing the pointer will be destroyed at the end of the full-expression}}<br>
+}<br>
+<br>
+std::vector<int> getTempVec();<br>
+std::optional<std::vector<int>> getTempOptVec();<br>
+<br>
+int &usedToBeFalsePositive(std::vector<int> &v) {<br>
+ std::vector<int>::iterator it = v.begin();<br>
+ int& value = *it;<br>
+ return value; // ok<br>
+}<br>
+<br>
+int &doNotFollowReferencesForLocalOwner() {<br>
+ std::unique_ptr<int> localOwner;<br>
+ int &p = *localOwner.get();<br>
+ // In real world code localOwner is usually moved here.<br>
+ return p; // ok<br>
+}<br>
+<br>
+const char *trackThroughMultiplePointer() {<br>
+ return std::basic_string_view<char>(std::basic_string<char>()).begin(); // expected-warning {{returning address of local temporary object}}<br>
+}<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>
</blockquote></div>