Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/PointerSortingChecker.cpp
Line
Count
Source
1
//== PointerSortingChecker.cpp --------------------------------- -*- C++ -*--=//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file defines PointerSortingChecker which checks for non-determinism
10
// caused due to sorting containers with pointer-like elements.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/ASTMatchers/ASTMatchFinder.h"
15
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16
#include "clang/StaticAnalyzer/Core/Checker.h"
17
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
18
19
using namespace clang;
20
using namespace ento;
21
using namespace ast_matchers;
22
23
namespace {
24
25
// ID of a node at which the diagnostic would be emitted.
26
constexpr llvm::StringLiteral WarnAtNode = "sort";
27
28
class PointerSortingChecker : public Checker<check::ASTCodeBody> {
29
public:
30
  void checkASTCodeBody(const Decl *D,
31
                        AnalysisManager &AM,
32
                        BugReporter &BR) const;
33
};
34
35
static void emitDiagnostics(const BoundNodes &Match, const Decl *D,
36
                            BugReporter &BR, AnalysisManager &AM,
37
7
                            const PointerSortingChecker *Checker) {
38
7
  auto *ADC = AM.getAnalysisDeclContext(D);
39
7
40
7
  const auto *MarkedStmt = Match.getNodeAs<CallExpr>(WarnAtNode);
41
7
  assert(MarkedStmt);
42
7
43
7
  auto Range = MarkedStmt->getSourceRange();
44
7
  auto Location = PathDiagnosticLocation::createBegin(MarkedStmt,
45
7
                                                      BR.getSourceManager(),
46
7
                                                      ADC);
47
7
  std::string Diagnostics;
48
7
  llvm::raw_string_ostream OS(Diagnostics);
49
7
  OS << "Sorting pointer-like elements "
50
7
     << "can result in non-deterministic ordering";
51
7
52
7
  BR.EmitBasicReport(ADC->getDecl(), Checker,
53
7
                     "Sorting of pointer-like elements", "Non-determinism",
54
7
                     OS.str(), Location, Range);
55
7
}
56
57
49
auto callsName(const char *FunctionName) -> decltype(callee(functionDecl())) {
58
49
  return callee(functionDecl(hasName(FunctionName)));
59
49
}
60
61
// FIXME: Currently we simply check if std::sort is used with pointer-like
62
// elements. This approach can have a big false positive rate. Using std::sort,
63
// std::unique and then erase is common technique for deduplicating a container
64
// (which in some cases might even be quicker than using, let's say std::set).
65
// In case a container contains arbitrary memory addresses (e.g. multiple
66
// things give different stuff but might give the same thing multiple times)
67
// which we don't want to do things with more than once, we might use
68
// sort-unique-erase and the sort call will emit a report.
69
7
auto matchSortWithPointers() -> decltype(decl()) {
70
7
  // Match any of these function calls.
71
7
  auto SortFuncM = anyOf(
72
7
                     callsName("std::is_sorted"),
73
7
                     callsName("std::nth_element"),
74
7
                     callsName("std::partial_sort"),
75
7
                     callsName("std::partition"),
76
7
                     callsName("std::sort"),
77
7
                     callsName("std::stable_partition"),
78
7
                     callsName("std::stable_sort")
79
7
                    );
80
7
81
7
  // Match only if the container has pointer-type elements.
82
7
  auto IteratesPointerEltsM = hasArgument(0,
83
7
                                hasType(cxxRecordDecl(has(
84
7
                                  fieldDecl(hasType(hasCanonicalType(
85
7
                                    pointsTo(hasCanonicalType(pointerType()))
86
7
                                  )))
87
7
                              ))));
88
7
89
7
  auto PointerSortM = stmt(callExpr(allOf(SortFuncM, IteratesPointerEltsM))
90
7
                      ).bind(WarnAtNode);
91
7
92
7
  return decl(forEachDescendant(PointerSortM));
93
7
}
94
95
void PointerSortingChecker::checkASTCodeBody(const Decl *D,
96
                                             AnalysisManager &AM,
97
7
                                             BugReporter &BR) const {
98
7
  auto MatcherM = matchSortWithPointers();
99
7
100
7
  auto Matches = match(MatcherM, *D, AM.getASTContext());
101
7
  for (const auto &Match : Matches)
102
7
    emitDiagnostics(Match, D, BR, AM, this);
103
7
}
104
105
} // end of anonymous namespace
106
107
2
void ento::registerPointerSortingChecker(CheckerManager &Mgr) {
108
2
  Mgr.registerChecker<PointerSortingChecker>();
109
2
}
110
111
2
bool ento::shouldRegisterPointerSortingChecker(const LangOptions &LO) {
112
2
  return LO.CPlusPlus;
113
2
}