19 using namespace clang;
26 constexpr llvm::StringLiteral WarnAtNode =
"sort";
28 class PointerSortingChecker :
public Checker<check::ASTCodeBody> {
30 void checkASTCodeBody(
const Decl *D,
32 BugReporter &BR)
const;
36 BugReporter &BR, AnalysisManager &AM,
37 const PointerSortingChecker *Checker) {
38 auto *ADC = AM.getAnalysisDeclContext(D);
43 auto Range = MarkedStmt->getSourceRange();
45 BR.getSourceManager(),
47 std::string Diagnostics;
48 llvm::raw_string_ostream
OS(Diagnostics);
49 OS <<
"Sorting pointer-like elements " 50 <<
"can result in non-deterministic ordering";
52 BR.EmitBasicReport(ADC->getDecl(), Checker,
53 "Sorting of pointer-like elements",
"Non-determinism",
54 OS.str(), Location, Range);
57 auto callsName(
const char *FunctionName) -> decltype(callee(
functionDecl())) {
69 auto matchSortWithPointers() -> decltype(
decl()) {
71 auto SortFuncM =
anyOf(
72 callsName(
"std::is_sorted"),
73 callsName(
"std::nth_element"),
74 callsName(
"std::partial_sort"),
75 callsName(
"std::partition"),
76 callsName(
"std::sort"),
77 callsName(
"std::stable_partition"),
78 callsName(
"std::stable_sort")
82 auto IteratesPointerEltsM = hasArgument(0,
95 void PointerSortingChecker::checkASTCodeBody(
const Decl *D,
97 BugReporter &BR)
const {
98 auto MatcherM = matchSortWithPointers();
101 for (
const auto &Match : Matches)
107 void ento::registerPointerSortingChecker(CheckerManager &Mgr) {
108 Mgr.registerChecker<PointerSortingChecker>();
111 bool ento::shouldRegisterPointerSortingChecker(
const LangOptions &LO) {
const internal::VariadicDynCastAllOfMatcher< Stmt, CallExpr > callExpr
Matches call expressions.
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
const internal::ArgumentAdaptingMatcherFunc< internal::HasMatcher > has
Matches AST nodes that have child AST nodes that match the provided matcher.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> anyOf
Matches if any of the given matchers matches.
Decl - This represents one declaration (or definition), e.g.
const AstTypeMatcher< PointerType > pointerType
Matches pointer types, but does not match Objective-C object pointer types.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> allOf
Matches if all given matchers match.
const internal::VariadicDynCastAllOfMatcher< Decl, FunctionDecl > functionDecl
Matches function declarations.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
const T * getNodeAs(StringRef ID) const
Returns the AST node bound to ID.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
const internal::ArgumentAdaptingMatcherFunc< internal::ForEachDescendantMatcher > forEachDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher. ...
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
const internal::VariadicDynCastAllOfMatcher< Decl, FieldDecl > fieldDecl
Matches field declarations.
Maps string IDs to AST nodes matched by parts of a matcher.
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
ASTContext & getASTContext() const LLVM_READONLY
static void emitDiagnostics(BoundNodes &Match, const Decl *D, BugReporter &BR, AnalysisManager &AM, const ObjCAutoreleaseWriteChecker *Checker)
Dataflow Directional Tag Classes.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXRecordDecl > cxxRecordDecl
Matches C++ class declarations.
Indicates that the tracking object is a descendant of a referenced-counted OSObject, used in the Darwin kernel.
internal::Matcher< NamedDecl > hasName(const std::string &Name)
Matches NamedDecl nodes that have the specified name.
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).