Skip to content

[webkit.UncountedLambdaCapturesChecker] Treat every argument of std::ranges functions as noescape. #138995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -127,13 +127,22 @@ class RawPtrRefLambdaCapturesChecker
return true;
}

// WTF::switchOn(T, F... f) is a variadic template function and couldn't
// be annotated with NOESCAPE. We hard code it here to workaround that.
bool shouldTreatAllArgAsNoEscape(FunctionDecl *Decl) {
auto *NsDecl = Decl->getParent();
if (!NsDecl || !isa<NamespaceDecl>(NsDecl))
return false;
return safeGetName(NsDecl) == "WTF" && safeGetName(Decl) == "switchOn";
// WTF::switchOn(T, F... f) is a variadic template function and couldn't
// be annotated with NOESCAPE. We hard code it here to workaround that.
if (safeGetName(NsDecl) == "WTF" && safeGetName(Decl) == "switchOn")
return true;
// Treat every argument of functions in std::ranges as noescape.
if (safeGetName(NsDecl) == "ranges") {
if (auto *OuterDecl = NsDecl->getParent();
OuterDecl && isa<NamespaceDecl>(OuterDecl) &&
safeGetName(OuterDecl) == "std")
return true;
}
return false;
}

bool VisitCXXConstructExpr(CXXConstructExpr *CE) override {
33 changes: 33 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
Original file line number Diff line number Diff line change
@@ -9,6 +9,16 @@ T&& move(T& t) {
return static_cast<T&&>(t);
}

namespace ranges {

template<typename IteratorType, typename CallbackType>
void for_each(IteratorType first, IteratorType last, CallbackType callback) {
for (auto it = first; !(it == last); ++it)
callback(*it);
}

}

}

namespace WTF {
@@ -416,3 +426,26 @@ void capture_copy_in_lambda(CheckedObj& checked) {
ptr->method();
});
}

class Iterator {
public:
Iterator(void* array, unsigned long sizeOfElement, unsigned int index);
Iterator(const Iterator&);
Iterator& operator=(const Iterator&);
bool operator==(const Iterator&);

Iterator& operator++();
void* operator*();

private:
void* current { nullptr };
unsigned long sizeOfElement { 0 };
};

void ranges_for_each(RefCountable* obj) {
int array[] = { 1, 2, 3, 4, 5 };
std::ranges::for_each(Iterator(array, sizeof(*array), 0), Iterator(array, sizeof(*array), 5), [&](void* item) {
obj->method();
++(*static_cast<unsigned*>(item));
});
}