feature request 1245255, no uninitialized variable warning for functions with referen...
authorbananeweizen <bananeweizen>
Sun, 15 Jan 2006 10:30:29 +0000 (10:30 +0000)
committerbananeweizen <bananeweizen>
Sun, 15 Jan 2006 10:30:29 +0000 (10:30 +0000)
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/Parser.java
net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/UninitializedVariableHandler.java [new file with mode: 0644]

index 95c4fd6..2cb48e4 100644 (file)
@@ -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);
                                                }
                                        }
@@ -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) {
@@ -3429,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();
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/UninitializedVariableHandler.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/compiler/parser/UninitializedVariableHandler.java
new file mode 100644 (file)
index 0000000..f19dd61
--- /dev/null
@@ -0,0 +1,54 @@
+package net.sourceforge.phpdt.internal.compiler.parser;
+
+import java.util.ArrayList;
+
+public class UninitializedVariableHandler {
+
+       private class Function {
+               private int count;
+               private String name;
+
+               public Function(String name, int count) {
+                       this.name=name;
+                       this.count=count;
+               }
+       }
+
+       private String functionName=null;
+       private int argumentCount=0;
+       private ArrayList functions=new ArrayList();
+
+       public UninitializedVariableHandler() {
+               add("ereg",3);
+               add("eregi",3);
+               add("fsockopen",3);
+               add("preg_match",3);
+               add("preg_match_all",3);
+               add("preg_replace",5);
+               add("preg_replace_callback",5);
+       }
+
+       private void add(String name, int countFrom) {
+               functions.add(new Function(name,countFrom));
+       }
+
+       protected boolean reportError() {
+               if (functionName!=null) {
+                       for (int i=0; i<functions.size();i++) {
+                               Function function=(Function) functions.get(i);
+                               if (functionName.equalsIgnoreCase(function.name) && argumentCount>=function.count) {
+                                       return false;
+                               }
+                       }
+               }
+               return true;
+       }
+
+       public void setFunctionName(String functionName) {
+               this.functionName=functionName;
+       }
+
+       public void incrementArgumentCount() {
+               argumentCount++;
+       }
+}