[Mesa-dev] [RFC 0/3] glsl: add "use of uninitialized variable" warning

Alejandro Piñeiro apinheiro at igalia.com
Wed Feb 24 19:04:44 UTC 2016


This series implement enhancement suggested on this bug:
https://bugs.freedesktop.org/show_bug.cgi?id=94129

Although the bug mentions that detecting uninitialized (undefined)
usage of variables on NIR is easy, this series implements the feature
on ast-to-hir. NIR is (imho) too high level to deal with this kind of
warnings, and would lack the access to the source location, needed to
print the source line:column number. For that last reason I also
discarded implement it on GLSL IR later, as suggested on one comment
of the bug.

The series make use of the already available ir_variable.data.assigned
field, that tracks if any variable was already assigned. So when
processing an ast_identifier node on ast_expression::do_hir, it raises
the warning if used but not yet assigned. FWIW, this is the same place
where the "undeclared variable" error is raised.

But, just knowing if the variable was already assigned is not
enough. So for example on a code like this:

  float myVar;
  float myUndefined;

  myVar = myUndefined;

We only want to raise the warning for "myUndefined" but not for
"myVar".

But while checking ast_identifier, we don't know if the variable is
being used as the lhs or the rhs of an assignment expression, and ast
doesnt provide a way to navigate to the parent expression (or at least
I didn't find it).

The first two patches add a new hir(..) method on ast_expression,
that allows to pass if the current expression being converted is an
assignment recipient or not. The already existing method becames a
wrapper that pass a default value. This seems somewhat too intrusive,
so the reason I send the patches as a RFC first.

Suggestions would be welcome. Meanwhile I will check the available
compiler unit tests, to check if it makes sense to add a unit tests
for this warning, as it doesn't fit as a piglit test.

Final note: probably the first two patches make more sense as just
one. Splitted in two for an easier review.

Alejandro Piñeiro (3):
  glsl: add an assignment_recipient parameter on ast_expression
  glsl: set non-default cases for assignment_recipient
  glsl: raise warning when using uninitialized variables

 src/compiler/glsl/ast.h            | 11 ++++++++++-
 src/compiler/glsl/ast_function.cpp |  8 ++++++++
 src/compiler/glsl/ast_to_hir.cpp   | 38 ++++++++++++++++++++++++++++----------
 3 files changed, 46 insertions(+), 11 deletions(-)

-- 
2.5.0



More information about the mesa-dev mailing list