Esempio n. 1
0
        private string DeriveTypeFromCaseClauses(IParseTreeVisitorResults inspValues, VBAParser.SelectCaseStmtContext selectStmt)
        {
            var caseClauseTypeNames = new List <string>();

            foreach (var caseClause in selectStmt.caseClause())
            {
                foreach (var range in caseClause.rangeClause())
                {
                    if (TryDetectTypeHint(range.GetText(), out string hintTypeName))
                    {
                        caseClauseTypeNames.Add(hintTypeName);
                    }
                    else
                    {
                        var inspRange = _inspectionRangeFactory.Create(range, inspValues);
                        var types     = inspRange.ResultContexts.Select(rc => inspValues.GetTypeName(rc))
                                        .Where(tp => InspectableTypes.Contains(tp));
                        caseClauseTypeNames.AddRange(types);
                    }
                }
            }

            if (TryDetermineEvaluationTypeFromTypes(caseClauseTypeNames, out string evalTypeName))
            {
                return(evalTypeName);
            }
            return(string.Empty);
        }
Esempio n. 2
0
        private string DeriveTypeFromCaseClauses(IParseTreeVisitorResults inspValues, VBAParser.SelectCaseStmtContext selectStmt)
        {
            var caseClauseTypeNames = new List <string>();

            foreach (var caseClause in selectStmt.caseClause())
            {
                foreach (var range in caseClause.rangeClause())
                {
                    if (TryDetectTypeHint(range.GetText(), out string hintTypeName))
                    {
                        caseClauseTypeNames.Add(hintTypeName);
                    }
                    else
                    {
                        var typeNames = from context in range.children
                                        where context is ParserRuleContext &&
                                        IsResultContext(context)
                                        select inspValues.GetValueType(context as ParserRuleContext);

                        caseClauseTypeNames.AddRange(typeNames);
                        caseClauseTypeNames.RemoveAll(tp => !InspectableTypes.Contains(tp));
                    }
                }
            }

            if (TryGetSelectExpressionTypeNameFromTypes(caseClauseTypeNames, out string evalTypeName))
            {
                return(evalTypeName);
            }
            return(string.Empty);
        }
Esempio n. 3
0
 public UnreachableCaseInspector(VBAParser.SelectCaseStmtContext selectCaseContext,
                                 IParseTreeVisitorResults inspValues,
                                 IParseTreeValueFactory valueFactory,
                                 Func <string, ParserRuleContext, string> GetVariableTypeName = null)
 {
     _valueFactory    = valueFactory;
     _caseClauses     = selectCaseContext.caseClause();
     _caseElseContext = selectCaseContext.caseElseClause();
     GetVariableDeclarationTypeName = GetVariableTypeName;
     ParseTreeValueResults          = inspValues;
     SetSelectExpressionTypeName(selectCaseContext as ParserRuleContext, inspValues);
 }
Esempio n. 4
0
 public void Resolve(VBAParser.SelectCaseStmtContext context)
 {
     ResolveDefault(context.selectExpression().expression());
     if (context.caseClause() == null)
     {
         return;
     }
     foreach (var caseClause in context.caseClause())
     {
         foreach (var rangeClause in caseClause.rangeClause())
         {
             if (rangeClause.expression() != null)
             {
                 ResolveDefault(rangeClause.expression());
             }
             else
             {
                 ResolveDefault(rangeClause.selectStartValue().expression());
                 ResolveDefault(rangeClause.selectEndValue().expression());
             }
         }
     }
 }
Esempio n. 5
0
        public void InspectForUnreachableCases()
        {
            if (!InspectionCanEvaluateTypeName(EvaluationTypeName))
            {
                return;
            }

            var cummulativeRangeFilter = FilterFactory.Create(EvaluationTypeName, ValueFactory);

            foreach (var caseClause in _selectCaseContext.caseClause())
            {
                var inspectedRanges = new List <IRangeClauseContextWrapper>();
                foreach (var range in caseClause.rangeClause())
                {
                    var inspectionRange = _inspectionRangeFactory.Create(range, EvaluationTypeName, _inspValues);
                    if (inspectionRange.IsReachable(cummulativeRangeFilter))
                    {
                        cummulativeRangeFilter.Add(inspectionRange.AsFilter);
                    }
                    inspectedRanges.Add(inspectionRange);
                }

                if (inspectedRanges.All(ir => ir.HasIncompatibleType))
                {
                    _mismatchResults.Add(caseClause);
                }
                else if (inspectedRanges.All(ir => ir.IsUnreachable))
                {
                    _unreachableResults.Add(caseClause);
                }
            }
            if (cummulativeRangeFilter.FiltersAllValues && !(_selectCaseContext.caseElseClause() is null))
            {
                _caseElseResults.Add(_selectCaseContext.caseElseClause());
            }
        }