-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathNullCheckAfterDereferenceCheck.cpp
154 lines (127 loc) · 5.25 KB
/
NullCheckAfterDereferenceCheck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//===--- NullCheckAfterDereferenceCheck.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 "NullCheckAfterDereferenceCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/FlowSensitive/DataflowAnalysisContext.h"
#include "clang/Analysis/FlowSensitive/DataflowEnvironment.h"
#include "clang/Analysis/FlowSensitive/DataflowLattice.h"
#include "clang/Analysis/FlowSensitive/Models/NullPointerAnalysisModel.h"
#include "clang/Analysis/FlowSensitive/WatchedLiteralsSolver.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/Any.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Error.h"
#include <clang/Analysis/FlowSensitive/AdornedCFG.h>
#include <memory>
#include <vector>
namespace clang::tidy::bugprone {
using ast_matchers::MatchFinder;
using dataflow::NullCheckAfterDereferenceDiagnoser;
using dataflow::NullPointerAnalysisModel;
using Diagnoser = NullCheckAfterDereferenceDiagnoser;
static constexpr llvm::StringLiteral FuncID("fun");
struct ExpandedResult {
Diagnoser::DiagnosticEntry Entry;
std::optional<SourceLocation> DerefLoc;
};
using ExpandedResultType = llvm::SmallVector<ExpandedResult>;
static std::optional<ExpandedResultType>
analyzeFunction(const FunctionDecl &FuncDecl) {
using dataflow::AdornedCFG;
using dataflow::DataflowAnalysisState;
using llvm::Expected;
ASTContext &ASTCtx = FuncDecl.getASTContext();
if (FuncDecl.getBody() == nullptr) {
return std::nullopt;
}
Expected<AdornedCFG> Context =
AdornedCFG::build(FuncDecl, *FuncDecl.getBody(), ASTCtx);
if (!Context)
return std::nullopt;
dataflow::DataflowAnalysisContext AnalysisContext(
std::make_unique<dataflow::WatchedLiteralsSolver>());
dataflow::Environment Env(AnalysisContext, FuncDecl);
NullPointerAnalysisModel Analysis(ASTCtx);
Diagnoser Diagnoser;
Expected<Diagnoser::ResultType> Diagnostics =
dataflow::diagnoseFunction<NullPointerAnalysisModel, Diagnoser::DiagnosticEntry>(
FuncDecl, ASTCtx, Diagnoser);
if (llvm::Error E = Diagnostics.takeError()) {
llvm::dbgs() << "Dataflow analysis failed: " << llvm::toString(std::move(E))
<< ".\n";
return std::nullopt;
}
ExpandedResultType ExpandedDiagnostics;
llvm::transform(*Diagnostics,
std::back_inserter(ExpandedDiagnostics),
[&](Diagnoser::DiagnosticEntry Entry) -> ExpandedResult {
if (auto Val = Diagnoser.WarningLocToVal[Entry.Location];
auto DerefExpr = Diagnoser.ValToDerefLoc[Val]) {
return {Entry, DerefExpr->getBeginLoc()};
}
return {Entry, std::nullopt};
});
return ExpandedDiagnostics;
}
void NullCheckAfterDereferenceCheck::registerMatchers(MatchFinder *Finder) {
using namespace ast_matchers;
auto containsPointerValue =
hasDescendant(NullPointerAnalysisModel::ptrValueMatcher());
Finder->addMatcher(
decl(anyOf(functionDecl(unless(isExpansionInSystemHeader()),
// FIXME: Remove the filter below when lambdas are
// well supported by the check.
unless(hasDeclContext(cxxRecordDecl(isLambda()))),
hasBody(containsPointerValue)),
cxxConstructorDecl(
unless(hasDeclContext(cxxRecordDecl(isLambda()))),
hasAnyConstructorInitializer(
withInitializer(containsPointerValue)))))
.bind(FuncID),
this);
}
void NullCheckAfterDereferenceCheck::check(
const MatchFinder::MatchResult &Result) {
if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred())
return;
const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(FuncID);
assert(FuncDecl && "invalid FuncDecl matcher");
if (FuncDecl->isTemplated())
return;
if (const auto Diagnostics = analyzeFunction(*FuncDecl)) {
for (const auto [Entry, DerefLoc] : *Diagnostics) {
const auto [WarningLoc, Type] = Entry;
switch (Type) {
case Diagnoser::DiagnosticType::CheckAfterDeref:
diag(WarningLoc, "pointer value is checked even though "
"it cannot be null at this point");
if (DerefLoc) {
diag(*DerefLoc,
"one of the locations where the pointer's value cannot be null",
DiagnosticIDs::Note);
}
break;
case Diagnoser::DiagnosticType::CheckWhenNull:
diag(WarningLoc,
"pointer value is checked but it can only be null at this point");
if (DerefLoc) {
diag(*DerefLoc,
"one of the locations where the pointer's value can only be null",
DiagnosticIDs::Note);
}
break;
}
}
}
}
} // namespace clang::tidy::bugprone