forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AutoBump] Merge with ea050ab (Oct 30)
- Loading branch information
Showing
1,613 changed files
with
145,974 additions
and
18,785 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
78 changes: 78 additions & 0 deletions
78
clang-tools-extra/clang-tidy/bugprone/NondeterministicPointerIterationOrderCheck.cpp
This file contains 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,78 @@ | ||
//===----- 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.