1 //===- BugReporterVisitors.h - Generate PathDiagnostics ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares BugReporterVisitors, which are used to generate enhanced
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGREPORTERVISITORS_H
16 #define LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGREPORTERVISITORS_H
18 #include "clang/Basic/LLVM.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
21 #include "llvm/ADT/FoldingSet.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/StringRef.h"
37 class BugReporterContext;
40 class PathDiagnosticPiece;
42 /// BugReporterVisitors are used to add custom diagnostics along a path.
43 class BugReporterVisitor : public llvm::FoldingSetNode {
45 BugReporterVisitor() = default;
46 BugReporterVisitor(const BugReporterVisitor &) = default;
47 BugReporterVisitor(BugReporterVisitor &&) {}
48 virtual ~BugReporterVisitor();
50 /// Return a diagnostic piece which should be associated with the
52 /// Note that this function does *not* get run on the very last node
53 /// of the report, as the PathDiagnosticPiece associated with the
54 /// last node should be unique.
55 /// Use {@code getEndPath} to customize the note associated with the report
58 /// The last parameter can be used to register a new visitor with the given
59 /// BugReport while processing a node.
60 virtual std::shared_ptr<PathDiagnosticPiece>
61 VisitNode(const ExplodedNode *Succ,
62 BugReporterContext &BRC, BugReport &BR) = 0;
64 /// Last function called on the visitor, no further calls to VisitNode
66 virtual void finalizeVisitor(BugReporterContext &BRC,
67 const ExplodedNode *EndPathNode,
70 /// Provide custom definition for the final diagnostic piece on the
71 /// path - the piece, which is displayed before the path is expanded.
73 /// NOTE that this function can be implemented on at most one used visitor,
74 /// and otherwise it crahes at runtime.
75 virtual std::shared_ptr<PathDiagnosticPiece>
76 getEndPath(BugReporterContext &BRC, const ExplodedNode *N, BugReport &BR);
78 virtual void Profile(llvm::FoldingSetNodeID &ID) const = 0;
80 /// Generates the default final diagnostic piece.
81 static std::shared_ptr<PathDiagnosticPiece>
82 getDefaultEndPath(BugReporterContext &BRC, const ExplodedNode *N,
86 /// Finds last store into the given region,
87 /// which is different from a given symbolic value.
88 class FindLastStoreBRVisitor final : public BugReporterVisitor {
91 bool Satisfied = false;
93 /// If the visitor is tracking the value directly responsible for the
94 /// bug, we are going to employ false positive suppression.
95 bool EnableNullFPSuppression;
98 /// Creates a visitor for every VarDecl inside a Stmt and registers it with
100 static void registerStatementVarDecls(BugReport &BR, const Stmt *S,
101 bool EnableNullFPSuppression);
103 FindLastStoreBRVisitor(KnownSVal V, const MemRegion *R,
104 bool InEnableNullFPSuppression)
105 : R(R), V(V), EnableNullFPSuppression(InEnableNullFPSuppression) {}
107 void Profile(llvm::FoldingSetNodeID &ID) const override;
109 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
110 BugReporterContext &BRC,
111 BugReport &BR) override;
114 class TrackConstraintBRVisitor final : public BugReporterVisitor {
115 DefinedSVal Constraint;
117 bool IsSatisfied = false;
120 /// We should start tracking from the last node along the path in which the
121 /// value is constrained.
122 bool IsTrackingTurnedOn = false;
125 TrackConstraintBRVisitor(DefinedSVal constraint, bool assumption)
126 : Constraint(constraint), Assumption(assumption),
127 IsZeroCheck(!Assumption && Constraint.getAs<Loc>()) {}
129 void Profile(llvm::FoldingSetNodeID &ID) const override;
131 /// Return the tag associated with this visitor. This tag will be used
132 /// to make all PathDiagnosticPieces created by this visitor.
133 static const char *getTag();
135 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
136 BugReporterContext &BRC,
137 BugReport &BR) override;
140 /// Checks if the constraint is valid in the current state.
141 bool isUnderconstrained(const ExplodedNode *N) const;
144 /// \class NilReceiverBRVisitor
145 /// Prints path notes when a message is sent to a nil receiver.
146 class NilReceiverBRVisitor final : public BugReporterVisitor {
148 void Profile(llvm::FoldingSetNodeID &ID) const override {
153 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
154 BugReporterContext &BRC,
155 BugReport &BR) override;
157 /// If the statement is a message send expression with nil receiver, returns
158 /// the receiver expression. Returns NULL otherwise.
159 static const Expr *getNilReceiver(const Stmt *S, const ExplodedNode *N);
162 /// Visitor that tries to report interesting diagnostics from conditions.
163 class ConditionBRVisitor final : public BugReporterVisitor {
164 // FIXME: constexpr initialization isn't supported by MSVC2013.
165 static const char *const GenericTrueMessage;
166 static const char *const GenericFalseMessage;
169 void Profile(llvm::FoldingSetNodeID &ID) const override {
174 /// Return the tag associated with this visitor. This tag will be used
175 /// to make all PathDiagnosticPieces created by this visitor.
176 static const char *getTag();
178 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
179 BugReporterContext &BRC,
180 BugReport &BR) override;
182 std::shared_ptr<PathDiagnosticPiece> VisitNodeImpl(const ExplodedNode *N,
183 BugReporterContext &BRC,
186 std::shared_ptr<PathDiagnosticPiece>
187 VisitTerminator(const Stmt *Term, const ExplodedNode *N,
188 const CFGBlock *srcBlk, const CFGBlock *dstBlk, BugReport &R,
189 BugReporterContext &BRC);
191 std::shared_ptr<PathDiagnosticPiece>
192 VisitTrueTest(const Expr *Cond, bool tookTrue, BugReporterContext &BRC,
193 BugReport &R, const ExplodedNode *N);
195 std::shared_ptr<PathDiagnosticPiece>
196 VisitTrueTest(const Expr *Cond, const DeclRefExpr *DR, const bool tookTrue,
197 BugReporterContext &BRC, BugReport &R, const ExplodedNode *N);
199 std::shared_ptr<PathDiagnosticPiece>
200 VisitTrueTest(const Expr *Cond, const BinaryOperator *BExpr,
201 const bool tookTrue, BugReporterContext &BRC, BugReport &R,
202 const ExplodedNode *N);
204 std::shared_ptr<PathDiagnosticPiece>
205 VisitConditionVariable(StringRef LhsString, const Expr *CondVarExpr,
206 const bool tookTrue, BugReporterContext &BRC,
207 BugReport &R, const ExplodedNode *N);
209 bool patternMatch(const Expr *Ex,
210 const Expr *ParentEx,
212 BugReporterContext &BRC,
214 const ExplodedNode *N,
215 Optional<bool> &prunable);
217 static bool isPieceMessageGeneric(const PathDiagnosticPiece *Piece);
220 /// Suppress reports that might lead to known false positives.
222 /// Currently this suppresses reports based on locations of bugs.
223 class LikelyFalsePositiveSuppressionBRVisitor final
224 : public BugReporterVisitor {
226 static void *getTag() {
228 return static_cast<void *>(&Tag);
231 void Profile(llvm::FoldingSetNodeID &ID) const override {
232 ID.AddPointer(getTag());
235 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *,
236 BugReporterContext &,
237 BugReport &) override {
241 void finalizeVisitor(BugReporterContext &BRC, const ExplodedNode *N,
242 BugReport &BR) override;
245 /// When a region containing undefined value or '0' value is passed
246 /// as an argument in a call, marks the call as interesting.
248 /// As a result, BugReporter will not prune the path through the function even
249 /// if the region's contents are not modified/accessed by the call.
250 class UndefOrNullArgVisitor final : public BugReporterVisitor {
251 /// The interesting memory region this visitor is tracking.
255 UndefOrNullArgVisitor(const MemRegion *InR) : R(InR) {}
257 void Profile(llvm::FoldingSetNodeID &ID) const override {
263 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
264 BugReporterContext &BRC,
265 BugReport &BR) override;
268 class SuppressInlineDefensiveChecksVisitor final : public BugReporterVisitor {
269 /// The symbolic value for which we are tracking constraints.
270 /// This value is constrained to null in the end of path.
273 /// Track if we found the node where the constraint was first added.
274 bool IsSatisfied = false;
276 /// Since the visitors can be registered on nodes previous to the last
277 /// node in the BugReport, but the path traversal always starts with the last
278 /// node, the visitor invariant (that we start with a node in which V is null)
279 /// might not hold when node visitation starts. We are going to start tracking
280 /// from the last node in which the value is null.
281 bool IsTrackingTurnedOn = false;
284 SuppressInlineDefensiveChecksVisitor(DefinedSVal Val, const ExplodedNode *N);
286 void Profile(llvm::FoldingSetNodeID &ID) const override;
288 /// Return the tag associated with this visitor. This tag will be used
289 /// to make all PathDiagnosticPieces created by this visitor.
290 static const char *getTag();
292 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *Succ,
293 BugReporterContext &BRC,
294 BugReport &BR) override;
297 class CXXSelfAssignmentBRVisitor final : public BugReporterVisitor {
298 bool Satisfied = false;
301 CXXSelfAssignmentBRVisitor() = default;
303 void Profile(llvm::FoldingSetNodeID &ID) const override {}
305 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *Succ,
306 BugReporterContext &BRC,
307 BugReport &BR) override;
310 /// The bug visitor prints a diagnostic message at the location where a given
311 /// variable was tainted.
312 class TaintBugVisitor final : public BugReporterVisitor {
317 TaintBugVisitor(const SVal V) : V(V) {}
318 void Profile(llvm::FoldingSetNodeID &ID) const override { ID.Add(V); }
320 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
321 BugReporterContext &BRC,
322 BugReport &BR) override;
325 /// The bug visitor will walk all the nodes in a path and collect all the
326 /// constraints. When it reaches the root node, will create a refutation
327 /// manager and check if the constraints are satisfiable
328 class FalsePositiveRefutationBRVisitor final : public BugReporterVisitor {
330 /// Holds the constraints in a given path
331 ConstraintRangeTy Constraints;
334 FalsePositiveRefutationBRVisitor();
336 void Profile(llvm::FoldingSetNodeID &ID) const override;
338 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
339 BugReporterContext &BRC,
340 BugReport &BR) override;
342 void finalizeVisitor(BugReporterContext &BRC, const ExplodedNode *EndPathNode,
343 BugReport &BR) override;
346 namespace bugreporter {
348 /// Attempts to add visitors to track expression value back to its point of
351 /// \param N A node "downstream" from the evaluation of the statement.
352 /// \param E The expression value which we are tracking
353 /// \param R The bug report to which visitors should be attached.
354 /// \param EnableNullFPSuppression Whether we should employ false positive
355 /// suppression (inlined defensive checks, returned null).
357 /// \return Whether or not the function was able to add visitors for this
358 /// statement. Note that returning \c true does not actually imply
359 /// that any visitors were added.
360 bool trackExpressionValue(const ExplodedNode *N, const Expr *E, BugReport &R,
361 bool EnableNullFPSuppression = true);
363 const Expr *getDerefExpr(const Stmt *S);
365 } // namespace bugreporter
371 #endif // LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGREPORTERVISITORS_H