How -omit-trace-statements Works… Or Does NOT…
Flex 4 introduces a new compiler option, –compiler.omit-trace-statements. The purpose of this compiler option is to remove
trace() calls from your code when compiling in release (not debug) mode.
How does this work? Well, let’s take a look at the compiler code. First of all, where should you start? Usually, when trying to track down how a compiler option works, one would start from where the compiler option is set. If you have the compiler code, look for a method called
'cfgOmitTraceStatements' in CompilerConfiguration.java. Basically, the fully-qualified compiler option name kinda gives you the hint!
From there, you can search for all the references to
'omitTraceStatements' in the compiler code and eventually, you should end up in a file called NodeFactory.java in the actionscript compiler.
callExpression method is responsible for constructing a syntax tree node that represents a function call in your code. In fact, almost all methods in NodeFactory.java are helper methods, responsible for syntax tree constructions. You can see in the code that if –omit-trace-statements is
true and if the function call is
callExpression method simply skips the syntax tree node creation and return an empty statement.
Now, this implementation puzzles me!
The problem I have with this implementation is that it is done too early in the compilation. When constructing a syntax tree, the compiler has not yet done any analysis on the code and has not yet resolved the
trace() call to the player built-in
trace() function. To test my theory, I use a simple example:
When compiled with –omit-trace-statements=false, the test case changes the label text from
'foobar'. That means, the compiler resolves the
trace() call to the local function, not the player built-in
trace(). Now, turn on the trace statement omission:
The compiler skips over the syntax tree node generation for the click event handler.
I set a breakpoint on the green line above and verified what is being skipped over is in fact the
trace() call in the click event handler.
As a result, the compiler generates an incorrect SWF. The label text does not change from
'foobar' when the button is clicked…
Conceptually speaking, I consider the –omit-trace-statements implementation totally busted!
In my opinion, the implementation should be done during code generation. Everything should be resolved right before code generation, so when the compiler traverses the tree one last time, it can use the compiler option there to skip the bytecode generation.
Practically speaking though, the current implementation maybe okay because no one does anything like I did in the trick test case above.
Hope you enjoy the trip down the compiler lane… 🙂