MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / analyzeCondition

Function analyzeCondition

lib/vf_analyzers.cpp:116–189  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

114 };
115
116 ConditionState analyzeCondition(const Token* tok, int depth = 20) const
117 {
118 if (!tok)
119 return {};
120 if (depth < 0)
121 return {};
122 depth--;
123 if (analyze(tok, Direction::Forward).isRead()) {
124 ConditionState result;
125 result.dependent = true;
126 result.unknown = false;
127 return result;
128 }
129 if (tok->hasKnownIntValue() || tok->isLiteral()) {
130 ConditionState result;
131 result.dependent = false;
132 result.unknown = false;
133 return result;
134 }
135 if (Token::Match(tok, "%cop%")) {
136 if (isLikelyStream(tok->astOperand1())) {
137 ConditionState result;
138 result.dependent = false;
139 return result;
140 }
141 ConditionState lhs = analyzeCondition(tok->astOperand1(), depth - 1);
142 if (lhs.isUnknownDependent() || !tok->astOperand2())
143 return lhs;
144 ConditionState rhs = analyzeCondition(tok->astOperand2(), depth - 1);
145 if (rhs.isUnknownDependent())
146 return rhs;
147 ConditionState result;
148 if (Token::Match(tok, "%comp%"))
149 result.dependent = lhs.dependent && rhs.dependent;
150 else
151 result.dependent = lhs.dependent || rhs.dependent;
152 result.unknown = lhs.unknown || rhs.unknown;
153 return result;
154 }
155 if (Token::Match(tok->previous(), "%name% (")) {
156 std::vector<const Token*> args = getArguments(tok->previous());
157 if (Token::Match(tok->tokAt(-2), ". %name% (")) {
158 args.push_back(tok->tokAt(-2)->astOperand1());
159 }
160 ConditionState result;
161 result.dependent = std::any_of(args.cbegin(), args.cend(), [&](const Token* arg) {
162 ConditionState cs = analyzeCondition(arg, depth - 1);
163 return cs.dependent;
164 });
165 if (result.dependent) {
166 // Check if we can evaluate the function
167 if (!evaluate(Evaluate::Integral, tok).empty())
168 result.unknown = false;
169 }
170 return result;
171 }
172
173 std::unordered_map<nonneg int, const Token*> symbols = getSymbols(tok);

Callers 1

stopOnConditionMethod · 0.85

Calls 12

analyzeFunction · 0.85
isLikelyStreamFunction · 0.85
getSymbolsFunction · 0.85
astOperand1Method · 0.80
isUnknownDependentMethod · 0.80
astOperand2Method · 0.80
getArgumentsFunction · 0.70
evaluateFunction · 0.70
hasKnownIntValueMethod · 0.45
tokAtMethod · 0.45
push_backMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected