-
Notifications
You must be signed in to change notification settings - Fork 14.3k
RFC: [clang-tidy] [analyzer] Move nondeterministic pointer usage check to tidy #110471
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
vabridgers
merged 1 commit into
llvm:main
from
vabridgers:nondeterministic-pointer-usage
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
clang-tools-extra/clang-tidy/bugprone/NondeterministicPointerIterationOrderCheck.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//===----- NondeterministicPointerIterationOrderCheck.cpp - clang-tidy ----===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "NondeterministicPointerIterationOrderCheck.h" | ||
#include "clang/AST/ASTContext.h" | ||
#include "clang/Lex/Lexer.h" | ||
|
||
using namespace clang::ast_matchers; | ||
|
||
namespace clang::tidy::bugprone { | ||
|
||
void NondeterministicPointerIterationOrderCheck::registerMatchers( | ||
MatchFinder *Finder) { | ||
|
||
auto LoopVariable = varDecl(hasType( | ||
qualType(hasCanonicalType(anyOf(referenceType(), pointerType()))))); | ||
|
||
auto RangeInit = declRefExpr(to(varDecl( | ||
hasType(recordDecl(hasAnyName("std::unordered_set", "std::unordered_map", | ||
"std::unordered_multiset", | ||
"std::unordered_multimap")) | ||
.bind("recorddecl"))))); | ||
|
||
Finder->addMatcher(cxxForRangeStmt(hasLoopVariable(LoopVariable), | ||
hasRangeInit(RangeInit.bind("rangeinit"))) | ||
.bind("cxxForRangeStmt"), | ||
this); | ||
|
||
auto SortFuncM = callee(functionDecl(hasAnyName( | ||
"std::is_sorted", "std::nth_element", "std::sort", "std::partial_sort", | ||
"std::partition", "std::stable_partition", "std::stable_sort"))); | ||
|
||
auto IteratesPointerEltsM = hasArgument( | ||
0, | ||
cxxMemberCallExpr(on(hasType(cxxRecordDecl(has(fieldDecl(hasType(qualType( | ||
hasCanonicalType(pointsTo(hasCanonicalType(pointerType())))))))))))); | ||
|
||
Finder->addMatcher( | ||
callExpr(allOf(SortFuncM, IteratesPointerEltsM)).bind("sortsemantic"), | ||
this); | ||
} | ||
|
||
void NondeterministicPointerIterationOrderCheck::check( | ||
const MatchFinder::MatchResult &Result) { | ||
const auto *ForRangePointers = | ||
Result.Nodes.getNodeAs<CXXForRangeStmt>("cxxForRangeStmt"); | ||
|
||
if ((ForRangePointers) && !(ForRangePointers->getBeginLoc().isMacroID())) { | ||
const auto *RangeInit = Result.Nodes.getNodeAs<Stmt>("rangeinit"); | ||
if (const auto *ClassTemplate = | ||
Result.Nodes.getNodeAs<ClassTemplateSpecializationDecl>( | ||
"recorddecl")) { | ||
const TemplateArgumentList &TemplateArgs = | ||
ClassTemplate->getTemplateArgs(); | ||
const llvm::StringRef AlgoName = ClassTemplate->getName(); | ||
const bool IsAlgoArgPointer = | ||
TemplateArgs[0].getAsType()->isPointerType(); | ||
|
||
if (IsAlgoArgPointer) { | ||
SourceRange R = RangeInit->getSourceRange(); | ||
diag(R.getBegin(), "iteration of pointers is nondeterministic") << R; | ||
} | ||
} | ||
return; | ||
} | ||
const auto *SortPointers = Result.Nodes.getNodeAs<Stmt>("sortsemantic"); | ||
|
||
if ((SortPointers) && !(SortPointers->getBeginLoc().isMacroID())) { | ||
SourceRange R = SortPointers->getSourceRange(); | ||
diag(R.getBegin(), "sorting pointers is nondeterministic") << R; | ||
} | ||
} | ||
|
||
} // namespace clang::tidy::bugprone |
39 changes: 39 additions & 0 deletions
39
clang-tools-extra/clang-tidy/bugprone/NondeterministicPointerIterationOrderCheck.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//=== NondeterministicPointerIterationOrderCheck.h - clang-tidy -*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NONDETERMINISTIC_POINTER_ITERATION_ORDER_CHECK_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NONDETERMINISTIC_POINTER_ITERATION_ORDER_CHECK_H | ||
|
||
#include "../ClangTidyCheck.h" | ||
|
||
namespace clang::tidy::bugprone { | ||
|
||
/// Finds nondeterministic usages of pointers in unordered containers. The | ||
/// check also finds calls to sorting-like algorithms on a container of | ||
/// pointers. | ||
/// | ||
/// For the user-facing documentation see: | ||
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/nondeterministic-pointer-iteration-order.html | ||
class NondeterministicPointerIterationOrderCheck : public ClangTidyCheck { | ||
public: | ||
NondeterministicPointerIterationOrderCheck(StringRef Name, | ||
ClangTidyContext *Context) | ||
: ClangTidyCheck(Name, Context) {} | ||
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { | ||
return LangOpts.CPlusPlus; | ||
} | ||
void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
std::optional<TraversalKind> getCheckTraversalKind() const override { | ||
return TK_IgnoreUnlessSpelledInSource; | ||
} | ||
}; | ||
|
||
} // namespace clang::tidy::bugprone | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_NONDETERMINISTIC_POINTER_ITERATION_ORDER_CHECK_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...ra/docs/clang-tidy/checks/bugprone/nondeterministic-pointer-iteration-order.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
.. title:: clang-tidy - bugprone-nondeterministic-pointer-iteration-order | ||
|
||
bugprone-nondeterministic-pointer-iteration-order | ||
================================================= | ||
|
||
Finds nondeterministic usages of pointers in unordered containers. | ||
|
||
One canonical example is iteration across a container of pointers. | ||
|
||
.. code-block:: c++ | ||
|
||
{ | ||
int a = 1, b = 2; | ||
std::unordered_set<int *> UnorderedPtrSet = {&a, &b}; | ||
for (auto i : UnorderedPtrSet) | ||
f(i); | ||
} | ||
|
||
Another such example is sorting a container of pointers. | ||
|
||
.. code-block:: c++ | ||
|
||
{ | ||
int a = 1, b = 2; | ||
std::vector<int *> VectorOfPtr = {&a, &b}; | ||
std::sort(VectorOfPtr.begin(), VectorOfPtr.end()); | ||
} | ||
|
||
Iteration of a containers of pointers may present the order of different | ||
pointers differently across different runs of a program. In some cases this | ||
may be acceptable behavior, in others this may be unexpected behavior. This | ||
check is advisory for this reason. | ||
|
||
This check only detects range-based for loops over unordered sets and maps. It | ||
also detects calls sorting-like algorithms on containers holding pointers. | ||
Other similar usages will not be found and are false negatives. | ||
|
||
Limitations: | ||
|
||
* This check currently does not check if a nondeterministic iteration order is | ||
likely to be a mistake, and instead marks all such iterations as bugprone. | ||
* std::reference_wrapper is not considered yet. | ||
* Only for loops are considered, other iterators can be included in | ||
improvements. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ools-extra/test/clang-tidy/checkers/bugprone/Inputs/system-header-simulator/sim_algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef _SIM_ALGORITHM | ||
#define _SIM_ALGORITHM | ||
|
||
#pragma clang system_header | ||
|
||
namespace std { | ||
|
||
template<class ForwardIt> | ||
bool is_sorted(ForwardIt first, ForwardIt last); | ||
|
||
template <class RandomIt> | ||
void nth_element(RandomIt first, RandomIt nth, RandomIt last); | ||
|
||
template<class RandomIt> | ||
void partial_sort(RandomIt first, RandomIt middle, RandomIt last); | ||
|
||
template<class RandomIt> | ||
void sort (RandomIt first, RandomIt last); | ||
|
||
template<class RandomIt> | ||
void stable_sort(RandomIt first, RandomIt last); | ||
|
||
template<class BidirIt, class UnaryPredicate> | ||
BidirIt partition(BidirIt first, BidirIt last, UnaryPredicate p); | ||
|
||
template<class BidirIt, class UnaryPredicate> | ||
BidirIt stable_partition(BidirIt first, BidirIt last, UnaryPredicate p); | ||
|
||
} // namespace std | ||
|
||
#endif // _SIM_ALGORITHM |
11 changes: 11 additions & 0 deletions
11
...ls-extra/test/clang-tidy/checkers/bugprone/Inputs/system-header-simulator/sim_c++config.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef _SIM_CPP_CONFIG_H | ||
#define _SIM_CPP_CONFIG_H | ||
|
||
#pragma clang system_header | ||
|
||
typedef unsigned char uint8_t; | ||
|
||
typedef __typeof__(sizeof(int)) size_t; | ||
typedef __typeof__((char*)0-(char*)0) ptrdiff_t; | ||
|
||
#endif // _SIM_CPP_CONFIG_H |
39 changes: 39 additions & 0 deletions
39
...tra/test/clang-tidy/checkers/bugprone/Inputs/system-header-simulator/sim_initializer_list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef _INITIALIZER_LIST | ||
#define _INITIALIZER_LIST | ||
|
||
#pragma clang system_header | ||
# | ||
#include "sim_c++config.h" // size_t | ||
|
||
namespace std { | ||
|
||
template <class _E> | ||
class initializer_list { | ||
const _E* __begin_; | ||
size_t __size_; | ||
|
||
initializer_list(const _E* __b, size_t __s) | ||
: __begin_(__b), | ||
__size_(__s) | ||
{} | ||
|
||
public: | ||
typedef _E value_type; | ||
typedef const _E& reference; | ||
typedef const _E& const_reference; | ||
typedef size_t size_type; | ||
|
||
typedef const _E* iterator; | ||
typedef const _E* const_iterator; | ||
|
||
initializer_list() : __begin_(0), __size_(0) {} | ||
|
||
size_t size() const {return __size_;} | ||
const _E* begin() const {return __begin_;} | ||
const _E* end() const {return __begin_ + __size_;} | ||
|
||
}; // class initializer_list | ||
|
||
} // namespace std | ||
|
||
#endif // _INITIALIZER_LIST |
22 changes: 22 additions & 0 deletions
22
...-extra/test/clang-tidy/checkers/bugprone/Inputs/system-header-simulator/sim_iterator_base
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef _SIM_ITERATOR_BASE | ||
#define _SIM_ITERATOR_BASE | ||
|
||
namespace std { | ||
|
||
struct input_iterator_tag { }; | ||
struct output_iterator_tag { }; | ||
struct forward_iterator_tag : public input_iterator_tag { }; | ||
struct bidirectional_iterator_tag : public forward_iterator_tag { }; | ||
struct random_access_iterator_tag : public bidirectional_iterator_tag { }; | ||
|
||
template <typename Iterator> struct iterator_traits { | ||
typedef typename Iterator::difference_type difference_type; | ||
typedef typename Iterator::value_type value_type; | ||
typedef typename Iterator::pointer pointer; | ||
typedef typename Iterator::reference reference; | ||
typedef typename Iterator::iterator_category iterator_category; | ||
}; | ||
|
||
} // namespace std | ||
|
||
#endif // _SIM_ITERATOR_BASE |
34 changes: 34 additions & 0 deletions
34
clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/system-header-simulator/sim_map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
#ifndef _SIM_MAP | ||
#define _SIM_MAP | ||
|
||
#pragma clang system_header | ||
#include "sim_stl_pair" | ||
|
||
namespace std { | ||
|
||
template <typename Key, typename Value> | ||
class map { | ||
public: | ||
using value_type = pair<Key, Value>; | ||
map(); | ||
map(initializer_list<pair<Key, Value>> initList); | ||
value_type& operator[](const Key& key); | ||
value_type& operator[](Key&& key); | ||
class iterator { | ||
public: | ||
iterator(Key *key): ptr(key) {} | ||
iterator& operator++() { ++ptr; return *this; } | ||
bool operator!=(const iterator &other) const { return ptr != other.ptr; } | ||
const Key &operator*() const { return *ptr; } | ||
private: | ||
Key *ptr; | ||
}; | ||
Key *val; | ||
iterator begin() const { return iterator(val); } | ||
iterator end() const { return iterator(val + 1); } | ||
}; | ||
|
||
} // namespace std | ||
|
||
#endif // _SIM_MAP |
44 changes: 44 additions & 0 deletions
44
clang-tools-extra/test/clang-tidy/checkers/bugprone/Inputs/system-header-simulator/sim_set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
#ifndef _SIM_SET | ||
#define _SIM_SET | ||
|
||
#pragma clang system_header | ||
#include "sim_initializer_list" | ||
|
||
namespace std { | ||
|
||
template< class T = void > | ||
struct less; | ||
|
||
template< class T > | ||
struct allocator; | ||
|
||
template< class Key > | ||
struct hash; | ||
|
||
template< | ||
class Key, | ||
class Compare = std::less<Key>, | ||
class Alloc = std::allocator<Key> | ||
> class set { | ||
public: | ||
set(initializer_list<Key> __list) {} | ||
|
||
class iterator { | ||
public: | ||
iterator(Key *key): ptr(key) {} | ||
iterator& operator++() { ++ptr; return *this; } | ||
bool operator!=(const iterator &other) const { return ptr != other.ptr; } | ||
const Key &operator*() const { return *ptr; } | ||
private: | ||
Key *ptr; | ||
}; | ||
|
||
Key *val; | ||
iterator begin() const { return iterator(val); } | ||
iterator end() const { return iterator(val + 1); } | ||
}; | ||
|
||
} // namespace std | ||
|
||
#endif // _SIM_SET |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.