Skip to content

Commit 243fa66

Browse files
authored
Fix 12031: False positive: uninitialized variable (#5637)
1 parent 233e27b commit 243fa66

9 files changed

Lines changed: 370 additions & 107 deletions

File tree

lib/astutils.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,31 +107,17 @@ static int getArgumentPos(const Token* ftok, const Token* tokToFind){
107107
return findArgumentPos(startTok, tokToFind);
108108
}
109109

110-
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
111-
static void astFlattenRecursive(T* tok, std::vector<T*>& result, const char* op, nonneg int depth = 0)
112-
{
113-
++depth;
114-
if (!tok || depth >= 100)
115-
return;
116-
if (tok->str() == op) {
117-
astFlattenRecursive(tok->astOperand1(), result, op, depth);
118-
astFlattenRecursive(tok->astOperand2(), result, op, depth);
119-
} else {
120-
result.push_back(tok);
121-
}
122-
}
123-
124110
std::vector<const Token*> astFlatten(const Token* tok, const char* op)
125111
{
126112
std::vector<const Token*> result;
127-
astFlattenRecursive(tok, result, op);
113+
astFlattenCopy(tok, op, std::back_inserter(result));
128114
return result;
129115
}
130116

131117
std::vector<Token*> astFlatten(Token* tok, const char* op)
132118
{
133119
std::vector<Token*> result;
134-
astFlattenRecursive(tok, result, op);
120+
astFlattenCopy(tok, op, std::back_inserter(result));
135121
return result;
136122
}
137123

@@ -163,6 +149,15 @@ bool astHasVar(const Token * tok, nonneg int varid)
163149
return astHasVar(tok->astOperand1(), varid) || astHasVar(tok->astOperand2(), varid);
164150
}
165151

152+
bool astHasExpr(const Token* tok, nonneg int exprid)
153+
{
154+
if (!tok)
155+
return false;
156+
if (tok->exprId() == exprid)
157+
return true;
158+
return astHasExpr(tok->astOperand1(), exprid) || astHasExpr(tok->astOperand2(), exprid);
159+
}
160+
166161
static bool astIsCharWithSign(const Token *tok, ValueType::Sign sign)
167162
{
168163
if (!tok)

lib/astutils.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,29 @@ const Token* findExpression(const nonneg int exprid,
116116
const std::function<bool(const Token*)>& pred);
117117
const Token* findExpression(const Token* start, const nonneg int exprid);
118118

119+
template<class T, class OuputIterator, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
120+
void astFlattenCopy(T* tok, const char* op, OuputIterator out, nonneg int depth = 100)
121+
{
122+
--depth;
123+
if (!tok || depth < 0)
124+
return;
125+
if (tok->str() == op) {
126+
astFlattenCopy(tok->astOperand1(), op, out, depth);
127+
astFlattenCopy(tok->astOperand2(), op, out, depth);
128+
} else {
129+
*out = tok;
130+
++out;
131+
}
132+
}
133+
119134
std::vector<const Token*> astFlatten(const Token* tok, const char* op);
120135
std::vector<Token*> astFlatten(Token* tok, const char* op);
121136

122137
nonneg int astCount(const Token* tok, const char* op, int depth = 100);
123138

124139
bool astHasToken(const Token* root, const Token * tok);
125140

141+
bool astHasExpr(const Token* tok, nonneg int exprid);
126142
bool astHasVar(const Token * tok, nonneg int varid);
127143

128144
bool astIsPrimitive(const Token* tok);

lib/forwardanalyzer.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@
4040
#include <vector>
4141

4242
namespace {
43-
struct OnExit {
44-
std::function<void()> f;
45-
46-
~OnExit() {
47-
f();
48-
}
49-
};
50-
5143
struct ForwardTraversal {
5244
enum class Progress { Continue, Break, Skip };
5345
enum class Terminate { None, Bail, Inconclusive };

0 commit comments

Comments
 (0)