]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ConstraintManager.h
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / include / clang / StaticAnalyzer / Core / PathSensitive / ConstraintManager.h
1 //== ConstraintManager.h - Constraints on symbolic values.-------*- C++ -*--==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defined the interface to manage constraints on symbolic values.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_GR_CONSTRAINT_MANAGER_H
15 #define LLVM_CLANG_GR_CONSTRAINT_MANAGER_H
16
17 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
19 #include "llvm/Support/SaveAndRestore.h"
20
21 namespace llvm {
22 class APSInt;
23 }
24
25 namespace clang {
26 namespace ento {
27
28 class SubEngine;
29
30 class ConditionTruthVal {
31   llvm::Optional<bool> Val;
32 public:
33   /// Construct a ConditionTruthVal indicating the constraint is constrained
34   /// to either true or false, depending on the boolean value provided.
35   ConditionTruthVal(bool constraint) : Val(constraint) {}
36
37   /// Construct a ConstraintVal indicating the constraint is underconstrained.
38   ConditionTruthVal() {}
39   
40   /// Return true if the constraint is perfectly constrained to 'true'.
41   bool isConstrainedTrue() const {
42     return Val.hasValue() && Val.getValue();
43   }
44
45   /// Return true if the constraint is perfectly constrained to 'false'.
46   bool isConstrainedFalse() const {
47     return Val.hasValue() && !Val.getValue();
48   }
49
50   /// Return true if the constrained is perfectly constrained.
51   bool isConstrained() const {
52     return Val.hasValue();
53   }
54
55   /// Return true if the constrained is underconstrained and we do not know
56   /// if the constraint is true of value.
57   bool isUnderconstrained() const {
58     return !Val.hasValue();
59   }
60 };
61   
62 class ConstraintManager {
63 public:
64   ConstraintManager() : NotifyAssumeClients(true) {}
65   
66   virtual ~ConstraintManager();
67   virtual ProgramStateRef assume(ProgramStateRef state,
68                                  DefinedSVal Cond,
69                                  bool Assumption) = 0;
70
71   typedef std::pair<ProgramStateRef, ProgramStateRef> ProgramStatePair;
72
73   /// Returns a pair of states (StTrue, StFalse) where the given condition is
74   /// assumed to be true or false, respectively.
75   ProgramStatePair assumeDual(ProgramStateRef State, DefinedSVal Cond) {
76     ProgramStateRef StTrue = assume(State, Cond, true);
77
78     // If StTrue is infeasible, asserting the falseness of Cond is unnecessary
79     // because the existing constraints already establish this.
80     if (!StTrue) {
81       // FIXME: This is fairly expensive and should be disabled even in
82       // Release+Asserts builds.
83       assert(assume(State, Cond, false) && "System is over constrained.");
84       return ProgramStatePair((ProgramStateRef)NULL, State);
85     }
86
87     ProgramStateRef StFalse = assume(State, Cond, false);
88     if (!StFalse) {
89       // We are careful to return the original state, /not/ StTrue,
90       // because we want to avoid having callers generate a new node
91       // in the ExplodedGraph.
92       return ProgramStatePair(State, (ProgramStateRef)NULL);
93     }
94
95     return ProgramStatePair(StTrue, StFalse);
96   }
97
98   /// \brief If a symbol is perfectly constrained to a constant, attempt
99   /// to return the concrete value.
100   ///
101   /// Note that a ConstraintManager is not obligated to return a concretized
102   /// value for a symbol, even if it is perfectly constrained.
103   virtual const llvm::APSInt* getSymVal(ProgramStateRef state,
104                                         SymbolRef sym) const {
105     return 0;
106   }
107
108   virtual ProgramStateRef removeDeadBindings(ProgramStateRef state,
109                                                  SymbolReaper& SymReaper) = 0;
110
111   virtual void print(ProgramStateRef state,
112                      raw_ostream &Out,
113                      const char* nl,
114                      const char *sep) = 0;
115
116   virtual void EndPath(ProgramStateRef state) {}
117   
118   /// Convenience method to query the state to see if a symbol is null or
119   /// not null, or if neither assumption can be made.
120   ConditionTruthVal isNull(ProgramStateRef State, SymbolRef Sym) {
121     llvm::SaveAndRestore<bool> DisableNotify(NotifyAssumeClients, false);
122
123     return checkNull(State, Sym);
124   }
125
126 protected:
127   /// A flag to indicate that clients should be notified of assumptions.
128   /// By default this is the case, but sometimes this needs to be restricted
129   /// to avoid infinite recursions within the ConstraintManager.
130   ///
131   /// Note that this flag allows the ConstraintManager to be re-entrant,
132   /// but not thread-safe.
133   bool NotifyAssumeClients;
134
135   /// canReasonAbout - Not all ConstraintManagers can accurately reason about
136   ///  all SVal values.  This method returns true if the ConstraintManager can
137   ///  reasonably handle a given SVal value.  This is typically queried by
138   ///  ExprEngine to determine if the value should be replaced with a
139   ///  conjured symbolic value in order to recover some precision.
140   virtual bool canReasonAbout(SVal X) const = 0;
141
142   /// Returns whether or not a symbol is known to be null ("true"), known to be
143   /// non-null ("false"), or may be either ("underconstrained"). 
144   virtual ConditionTruthVal checkNull(ProgramStateRef State, SymbolRef Sym);
145 };
146
147 ConstraintManager* CreateRangeConstraintManager(ProgramStateManager& statemgr,
148                                                 SubEngine *subengine);
149
150 } // end GR namespace
151
152 } // end clang namespace
153
154 #endif