xazax.hun created this revision. xazax.hun added reviewers: mgehre, gribozavr. xazax.hun added a project: clang. Herald added subscribers: Szelethus, Charusso, gamesh411, dkrupp, rnkovacs.
This patch adds support to the idiom when people using the free version of `begin`, `end` and the like instead of the member functions. Moreover, some containers, like `any` or `variant` only support this idiom. Once this patch is accepted we do not anticipate adding more hard coded rules. Hopefully, they will all be subsumed by function annotations in the future. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D66303 Files: clang/lib/Sema/SemaInit.cpp clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
Index: clang/test/Sema/warn-lifetime-analysis-nocfg.cpp =================================================================== --- clang/test/Sema/warn-lifetime-analysis-nocfg.cpp +++ clang/test/Sema/warn-lifetime-analysis-nocfg.cpp @@ -131,13 +131,16 @@ } namespace std { -template<class T> struct remove_reference { typedef T type; }; -template<class T> struct remove_reference<T &> { typedef T type; }; -template<class T> struct remove_reference<T &&> { typedef T type; }; +template<typename T> struct remove_reference { typedef T type; }; +template<typename T> struct remove_reference<T &> { typedef T type; }; +template<typename T> struct remove_reference<T &&> { typedef T type; }; -template<class T> +template<typename T> typename remove_reference<T>::type &&move(T &&t) noexcept; +template <typename C> +auto data(const C &c) -> decltype(c.data()); + template <typename T> struct vector { typedef __gnu_cxx::basic_iterator<T> iterator; @@ -182,6 +185,11 @@ struct stack { T &top(); }; + +struct any {}; + +template<typename T> +T any_cast(const any& operand); } void modelIterators() { @@ -193,6 +201,22 @@ return std::vector<int>().begin(); // expected-warning {{returning address of local temporary object}} } +const int *modelFreeFunctions() { + return std::data(std::vector<int>()); // expected-warning {{returning address of local temporary object}} +} + +int &modelAnyCast() { + return std::any_cast<int&>(std::any{}); // expected-warning {{returning reference to local temporary object}} +} + +int modelAnyCast2() { + return std::any_cast<int>(std::any{}); // ok +} + +int modelAnyCast3() { + return std::any_cast<int&>(std::any{}); // ok +} + const char *danglingRawPtrFromLocal() { std::basic_string<char> s; return s.c_str(); // expected-warning {{address of stack memory associated with local variable 's' returned}} Index: clang/lib/Sema/SemaInit.cpp =================================================================== --- clang/lib/Sema/SemaInit.cpp +++ clang/lib/Sema/SemaInit.cpp @@ -6616,6 +6616,30 @@ return false; } +static bool shouldTrackFirstArgument(const FunctionDecl *FD) { + if (!FD->getIdentifier()) + return false; + const auto *RD = FD->getParamDecl(0)->getType()->getPointeeCXXRecordDecl(); + if (!FD->isInStdNamespace() || !RD || !RD->isInStdNamespace()) + return false; + if (!isRecordWithAttr<PointerAttr>(QualType(RD->getTypeForDecl(), 0)) && + !isRecordWithAttr<OwnerAttr>(QualType(RD->getTypeForDecl(), 0))) + return false; + if (FD->getReturnType()->isPointerType() || + isRecordWithAttr<PointerAttr>(FD->getReturnType())) { + return llvm::StringSwitch<bool>(FD->getName()) + .Cases("begin", "rbegin", "cbegin", "crbegin", true) + .Cases("end", "rend", "cend", "crend", true) + .Case("data", true) + .Default(false); + } else if (FD->getReturnType()->isReferenceType()) { + return llvm::StringSwitch<bool>(FD->getName()) + .Cases("get", "any_cast", true) + .Default(false); + } + return false; +} + static void handleGslAnnotatedTypes(IndirectLocalPath &Path, Expr *Call, LocalVisitor Visit) { auto VisitPointerArg = [&](const Decl *D, Expr *Arg) { @@ -6639,6 +6663,12 @@ shouldTrackImplicitObjectArg(cast<CXXMethodDecl>(Callee))) VisitPointerArg(Callee, OCE->getArg(0)); return; + } else if (auto *CE = dyn_cast<CallExpr>(Call)) { + FunctionDecl *Callee = CE->getDirectCallee(); + if (Callee && Callee->getNumParams() == 1 && + shouldTrackFirstArgument(Callee)) + VisitPointerArg(Callee, CE->getArg(0)); + return; } if (auto *CCE = dyn_cast<CXXConstructExpr>(Call)) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits