feature request 1245255, no uninitialized variable warning for functions with referen...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / parser / Parser.java
index a79cbef..2cb48e4 100644 (file)
@@ -456,10 +456,10 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                                                }
                                                if (token == TokenNameif || token == TokenNameswitch || token == TokenNamefor || token == TokenNamewhile
                                                                || token == TokenNamedo || token == TokenNameforeach || token == TokenNamecontinue || token == TokenNamebreak
-                                                               || token == TokenNamereturn || token == TokenNameexit || token == TokenNameecho || token == TokenNameglobal
-                                                               || token == TokenNamestatic || token == TokenNameunset || token == TokenNamefunction || token == TokenNamedeclare
-                                                               || token == TokenNametry || token == TokenNamecatch || token == TokenNamethrow || token == TokenNamefinal
-                                                               || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
+                                                               || token == TokenNamereturn || token == TokenNameexit || token == TokenNameecho || token == TokenNameECHO_INVISIBLE
+                                                               || token == TokenNameglobal || token == TokenNamestatic || token == TokenNameunset || token == TokenNamefunction
+                                                               || token == TokenNamedeclare || token == TokenNametry || token == TokenNamecatch || token == TokenNamethrow
+                                                               || token == TokenNamefinal || token == TokenNameabstract || token == TokenNameclass || token == TokenNameinterface) {
                                                        break;
                                                }
                                                // System.out.println(scanner.toStringAction(token));
@@ -751,33 +751,27 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                                getNextToken();
                        }
                        return statement;
-               } else if (token == TokenNameINLINE_HTML) {
-                       if (scanner.phpExpressionTag) {
-                               // start of <?= ... ?> block
+               } else if (token == TokenNameECHO_INVISIBLE) {
+                       // 0-length token directly after PHP short tag &lt;?=
+                       getNextToken();
+                       expressionList();
+                       if (token == TokenNameSEMICOLON) {
                                getNextToken();
-                               expr();
-                               if (token == TokenNameSEMICOLON) {
-                                       getNextToken();
-                               }
+                               // if (token != TokenNameINLINE_HTML) {
+                               // // TODO should this become a configurable warning?
+                               // reportSyntaxError("Probably '?>' expected after PHP short tag
+                               // expression (only the first expression will be echoed).");
+                               // }
+                       } else {
                                if (token != TokenNameINLINE_HTML) {
-                                       throwSyntaxError("Missing '?>' for open PHP expression block ('<?=').");
+                                       throwSyntaxError("';' expected after PHP short tag '<?=' expression.");
                                }
-                       } else {
                                getNextToken();
                        }
                        return statement;
-                       // } else if (token == TokenNameprint) {
-                       // getNextToken();
-                       // expression();
-                       // if (token == TokenNameSEMICOLON) {
-                       // getNextToken();
-                       // } else {
-                       // if (token != TokenNameStopPHP) {
-                       // throwSyntaxError("';' expected after 'print' statement.");
-                       // }
-                       // getNextToken();
-                       // }
-                       // return;
+               } else if (token == TokenNameINLINE_HTML) {
+                       getNextToken();
+                       return statement;
                } else if (token == TokenNameglobal) {
                        getNextToken();
                        global_var_list();
@@ -1666,10 +1660,10 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                // | non_empty_parameter_list ',' optional_class_type T_VARIABLE '='
                // static_scalar
                char[] typeIdentifier = null;
-               if (token == TokenNameIdentifier || token == TokenNameVariable || token == TokenNameAND) {
+               if (token == TokenNameIdentifier || token == TokenNamearray || token == TokenNameVariable || token == TokenNameAND) {
                        HashSet set = peekVariableSet();
                        while (true) {
-                               if (token == TokenNameIdentifier) {
+                               if (token == TokenNameIdentifier || token == TokenNamearray) {// feature req. #1254275
                                        typeIdentifier = scanner.getCurrentIdentifierSource();
                                        getNextToken();
                                }
@@ -1749,6 +1743,11 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                                expr(); // constant();
                                if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
                                        getNextToken();
+                                       if (token == TokenNameRBRACE) {
+                                               // empty case; assumes that the '}' token belongs to the wrapping
+                                               // switch statement - #1371992
+                                               break;
+                                       }
                                        if (token == TokenNamecase || token == TokenNamedefault) {
                                                // empty case statement ?
                                                continue;
@@ -1776,7 +1775,8 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                                if (token == TokenNameCOLON || token == TokenNameSEMICOLON) {
                                        getNextToken();
                                        if (token == TokenNameRBRACE) {
-                                               // empty default case
+                                               // empty default case; ; assumes that the '}' token belongs to the
+                                               // wrapping switch statement - #1371992
                                                break;
                                        }
                                        if (token != TokenNamecase) {
@@ -2172,11 +2172,11 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                if (Scanner.TRACE) {
                        System.out.println("TRACE: expr()");
                }
-               return expr_without_variable(true);
+               return expr_without_variable(true,null);
                // }
        }
 
-       private Expression expr_without_variable(boolean only_variable) {
+       private Expression expr_without_variable(boolean only_variable, UninitializedVariableHandler initHandler) {
                int exprSourceStart = scanner.getCurrentTokenStartPosition();
                int exprSourceEnd = scanner.getCurrentTokenEndPosition();
                Expression expression = new Expression();
@@ -2526,8 +2526,10 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                                                        && token != TokenNameLEFT_SHIFT_EQUAL) {
                                                FieldReference ref = (FieldReference) lhs;
                                                if (!containsVariableSet(ref.token)) {
-                                                       problemReporter.uninitializedLocalVariable(new String(ref.token), ref.sourceStart(), ref.sourceEnd(),
+                                                       if (null==initHandler || initHandler.reportError()) {
+                                                               problemReporter.uninitializedLocalVariable(new String(ref.token), ref.sourceStart(), ref.sourceEnd(),
                                                                        referenceContext, compilationUnit.compilationResult);
+                                                       }
                                                        addVariableSet(ref.token);
                                                }
                                        }
@@ -2787,7 +2789,7 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                if (Scanner.TRACE) {
                        System.out.println("TRACE: dynamic_class_name_reference()");
                }
-               base_variable();
+               base_variable(true);
                if (token == TokenNameMINUS_GREATER) {
                        getNextToken();
                        object_property();
@@ -3034,12 +3036,16 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                return ref;
        }
 
+       private void non_empty_function_call_parameter_list() {
+               this.non_empty_function_call_parameter_list(null);
+       }
+
        // private void function_call_parameter_list() {
        // function_call_parameter_list:
        // non_empty_function_call_parameter_list { $$ = $1; }
        // | /* empty */
        // }
-       private void non_empty_function_call_parameter_list() {
+       private void non_empty_function_call_parameter_list(String functionName) {
                // non_empty_function_call_parameter_list:
                // expr_without_variable
                // | variable
@@ -3050,7 +3056,10 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                if (Scanner.TRACE) {
                        System.out.println("TRACE: non_empty_function_call_parameter_list()");
                }
+               UninitializedVariableHandler initHandler = new UninitializedVariableHandler();
+               initHandler.setFunctionName(functionName);
                while (true) {
+                       initHandler.incrementArgumentCount();
                        if (token == TokenNameAND) {
                                getNextToken();
                                w_variable(true);
@@ -3060,7 +3069,7 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                                // || token == TokenNameDOLLAR) {
                                // variable();
                                // } else {
-                               expr_without_variable(true);
+                               expr_without_variable(true, initHandler);
                                // }
                        }
                        if (token != TokenNameCOMMA) {
@@ -3103,7 +3112,7 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                return function_call(lefthandside, ignoreVar);
        }
 
-       private Expression base_variable() {
+       private Expression base_variable(boolean lefthandside) {
                // base_variable:
                // reference_variable
                // | simple_indirect_reference reference_variable
@@ -3118,7 +3127,7 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                        while (token == TokenNameDOLLAR) {
                                getNextToken();
                        }
-                       reference_variable(false, false);
+                       reference_variable(lefthandside, false);
                }
                return ref;
        }
@@ -3148,11 +3157,8 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                                }
                                getNextToken();
                        } else if (token == TokenNameLBRACKET) {
-                               if (ref != null && ref instanceof FieldReference) {
-                                       FieldReference fref = (FieldReference) ref;
-                                       addVariableSet(fref.token);
-                               }
-                               ref = null;
+                               // To remove "ref = null;" here, is probably better than the patch
+                               // commented in #1368081 - axelcl
                                getNextToken();
                                if (token != TokenNameRBRACKET) {
                                        expr();
@@ -3432,14 +3438,14 @@ public class Parser implements ITerminalSymbols, CompilerModifiers, ParserBasicI
                                getNextToken();
                                ref = null;
                        } else {
-                               non_empty_function_call_parameter_list();
+                               String functionName;
+                               if (ident == null) {
+                                       functionName = new String(" ");
+                               } else {
+                                       functionName = new String(ident);
+                               }
+                               non_empty_function_call_parameter_list(functionName);
                                if (token != TokenNameRPAREN) {
-                                       String functionName;
-                                       if (ident == null) {
-                                               functionName = new String(" ");
-                                       } else {
-                                               functionName = new String(ident);
-                                       }
                                        throwSyntaxError("')' expected in function call (" + functionName + ").");
                                }
                                getNextToken();