Ensure that all functions have a body node in the AST
Some traversers that insert code to main() assume that the main()
function has a non-null body node in place. This assumption was
previously wrong, since functions could be missing the body node
in case the function body was empty.
Fix possible invalid dereferencing of missing function body nodes by
always adding an empty sequence node to represent the body of
functions that have an empty body in the ESSL source. This also
enables simplifying some tree traversers that used to take the
possibility of missing function body nodes into account.
Also fix AddDefaultReturnStatements to check the last statement inside
the function body for a return statement, instead of checking the
first statement.
BUG=angleproject:1539
TEST=angle_unittests, angle_end2end_tests
Change-Id: I2fbd18c78653fa2f1a96dbd9a619accc4874030d
Reviewed-on: https://chromium-review.googlesource.com/392046
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
diff --git a/src/compiler/translator/OutputGLSLBase.cpp b/src/compiler/translator/OutputGLSLBase.cpp
index 731e0fd..32db9de 100644
--- a/src/compiler/translator/OutputGLSLBase.cpp
+++ b/src/compiler/translator/OutputGLSLBase.cpp
@@ -818,22 +818,19 @@
out << " " << hashFunctionNameIfNeeded(node->getNameObj());
incrementDepth(node);
- // Function definition node contains one or two children nodes
- // representing function parameters and function body. The latter
- // is not present in case of empty function bodies.
+ // Function definition node contains two child nodes representing the function parameters
+ // and the function body.
const TIntermSequence &sequence = *(node->getSequence());
- ASSERT((sequence.size() == 1) || (sequence.size() == 2));
- TIntermSequence::const_iterator seqIter = sequence.begin();
+ ASSERT(sequence.size() == 2);
// Traverse function parameters.
- TIntermAggregate *params = (*seqIter)->getAsAggregate();
+ TIntermAggregate *params = sequence[0]->getAsAggregate();
ASSERT(params != NULL);
ASSERT(params->getOp() == EOpParameters);
params->traverse(this);
// Traverse function body.
- TIntermAggregate *body = ++seqIter != sequence.end() ?
- (*seqIter)->getAsAggregate() : NULL;
+ TIntermAggregate *body = sequence[1]->getAsAggregate();
visitCodeBlock(body);
decrementDepth();