//== PointerSortingChecker.cpp --------------------------------- -*- 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 // //===----------------------------------------------------------------------===// // // This file defines PointerSortingChecker which checks for non-determinism // caused due to sorting containers with pointer-like elements. // //===----------------------------------------------------------------------===// #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" #include "clang/StaticAnalyzer/Core/Checker.h" #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" usingnamespaceclang; usingnamespaceento; usingnamespaceast_matchers; namespace { // ID of a node at which the diagnostic would be emitted. constexpr llvm::StringLiteral WarnAtNode = …; class PointerSortingChecker : public Checker<check::ASTCodeBody> { … }; static void emitDiagnostics(const BoundNodes &Match, const Decl *D, BugReporter &BR, AnalysisManager &AM, const PointerSortingChecker *Checker) { … } decltype(auto) callsName(const char *FunctionName) { … } // FIXME: Currently we simply check if std::sort is used with pointer-like // elements. This approach can have a big false positive rate. Using std::sort, // std::unique and then erase is common technique for deduplicating a container // (which in some cases might even be quicker than using, let's say std::set). // In case a container contains arbitrary memory addresses (e.g. multiple // things give different stuff but might give the same thing multiple times) // which we don't want to do things with more than once, we might use // sort-unique-erase and the sort call will emit a report. auto matchSortWithPointers() -> decltype(decl()) { … } void PointerSortingChecker::checkASTCodeBody(const Decl *D, AnalysisManager &AM, BugReporter &BR) const { … } } // end of anonymous namespace void ento::registerPointerSortingChecker(CheckerManager &Mgr) { … } bool ento::shouldRegisterPointerSortingChecker(const CheckerManager &mgr) { … }