Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        void Log_ProblemHandler(Microsoft.ExtendedReflection.Logging.ProblemEventArgs e)
        {
            //TODO: Xusheng's code for additional OCI issues
            if (e.Result == TryGetModelResult.Success)
            {
                return;
            }

            CodeLocation location = e.FlippedLocation;
            var          term     = e.Suffix;

            SafeDictionary <Field, FieldValueHolder> fieldValues;
            SafeList <TypeEx> allFieldTypes;
            SafeList <Field>  fields = TargetBranchAnalyzer.GetInvolvedFields(this.host, e.TermManager, term, out fieldValues, out allFieldTypes);

            //Not an object creation issue
            if (fields == null || fields.Count == 0)
            {
                return;
            }
            this.host.Log.LogMessage("ProblemHandler", "Recorded an issue at code location " + location.ToString());
            if (!PexMeConstants.USE_TERM_SOLVER)
            {
                //A heuristic to choose the explorable type
                this.tba.HandleTargetBranch(location, term, e.TermManager, allFieldTypes[0]);
            }
        }
Exemple #2
0
            private void Log_ProblemHandler(Microsoft.ExtendedReflection.Logging.ProblemEventArgs e)
            {
                RecordFlipCount(e);

                if (e.Result == TryGetModelResult.Success)
                {
                    Host.GetService <ProblemTrackDatabase>().SimpleLog.AppendLine("flipped result: " + e.Result);
                    Host.GetService <ProblemTrackDatabase>().CurrentSuccessfulFlippedPathCondition = e;
                    return;
                }
                if (e.Result != TryGetModelResult.Success)
                {
                    Host.GetService <ProblemTrackDatabase>().SimpleLog.AppendLine("flipped result: " + e.Result);
//                    return;
                }
                try
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    CodeLocation location        = e.FlippedLocation;
                    var          database        = Host.GetService <ProblemTrackDatabase>();
                    database.PexProblemEventArgsList.Add(e);

//                e.Result == TryGetModelResult.
                    this.Host.Log.Dump("My Category", "flipped location: " + location, null);
                    SequencePoint sp;
                    Host.Services.SymbolManager.TryGetSequencePoint(location.Method, location.Offset, out sp);
                    sb.AppendLine("flipped location: " + sp.Document + " line: " + sp.Line + " offset: " +
                                  location.Offset);
                    sb.AppendLine("e.ParentOfFlipped.InCodeBranch: " + e.ParentOfFlipped.InCodeBranch);
                    e.ParentOfFlipped.OutCodeBranches.ToList().ForEach(x => sb.AppendLine("out: " + x));
                    var flippedCondition = e.Suffix;
                    var parentOfFlipped  = e.ParentOfFlipped.CodeLocation;
                    Host.Services.SymbolManager.TryGetSequencePoint(parentOfFlipped.Method, parentOfFlipped.Offset,
                                                                    out sp);
                    sb.AppendLine("parent flipped location: " + sp.Document + " line: " + sp.Line + " offset: " +
                                  parentOfFlipped.Offset);

                    var stringWriter = new StringWriter();
                    var bodyWriter   = this.Host.Services.LanguageManager.DefaultLanguage.CreateBodyWriter(stringWriter,
                                                                                                           VisibilityContext
                                                                                                           .
                                                                                                           Private);
                    var emitter = new TermEmitter(e.TermManager, new NameCreator());
                    if (emitter.TryEvaluate(Indexable.One(flippedCondition), 1000, bodyWriter))
                    {
                        bodyWriter.Return(SystemTypes.Bool);
                    }
                    string flippedTerm = stringWriter.ToString();
                    stringWriter.WriteLine();
                    stringWriter.WriteLine("Feasible prefixes:");
                    if (e.FeasiblePrefix != null && e.FeasiblePrefix.Length > 0)
                    {
                        var bodyWriter2 =
                            this.Host.Services.LanguageManager.DefaultLanguage.CreateBodyWriter(stringWriter,
                                                                                                VisibilityContext
                                                                                                .
                                                                                                Private);
                        foreach (Term prefix in e.FeasiblePrefix)
                        {
                            if (emitter.TryEvaluate(Indexable.One(prefix), 1000, bodyWriter2))
                            {
                                bodyWriter2.Return(SystemTypes.Bool);
                            }
                        }
                    }
                    else
                    {
                        stringWriter.WriteLine("No feasible prefixes.");
                    }

                    string feasiblePrefix =
                        stringWriter.ToString().Substring(stringWriter.ToString().IndexOf("Feasible prefixes:"));
                    this.Host.Log.Dump("My Category", "condition", stringWriter.ToString());
                    sb.AppendLine(stringWriter.ToString());

                    int returnIndex = flippedTerm.IndexOf("return");
                    sb.AppendLine("flipped term: " + flippedTerm.Substring(returnIndex));
                    string targetObjectType = null;
                    bool   infeasible       = false;
                    if (flippedTerm.Contains("!=") && flippedTerm.Contains("null"))
                    {
                        targetObjectType = flippedTerm.Substring(flippedTerm.IndexOf("(") + 1,
                                                                 flippedTerm.IndexOf(")") - flippedTerm.IndexOf("(") -
                                                                 1);
                        sb.AppendLine("targetObjectType: " + targetObjectType);

                        int index  = flippedTerm.IndexOf("!=") - 2;
                        int length = 0;
                        while (flippedTerm[index] != ' ')
                        {
                            index--;
                            length++;
                        }
                        string variable = flippedTerm.Substring(index + 1, length);
                        sb.AppendLine("variable for targetObjectType: " + variable);

                        string infeasibleCheck = variable + ".GetType() != typeof(" + targetObjectType + ")";
                        string conflictCheck   = variable + " == (" + targetObjectType + ")null";
                        sb.AppendLine("test for infeasible: " + infeasibleCheck);
                        if (feasiblePrefix.Contains(infeasibleCheck))
                        {
                            sb.AppendLine("found infeasible constraint: " + infeasibleCheck);
                            infeasible = true;
                        }
                        else if (feasiblePrefix.Contains(conflictCheck))
                        {
                            sb.AppendLine("found conflict constraint: " + conflictCheck);
                            infeasible = true;
                        }
                        else if (targetObjectType.Contains("(") || targetObjectType.Contains(")") || targetObjectType.Contains("="))
                        {
                            sb.AppendLine("found wrong object type: " + targetObjectType);
                            infeasible = true;
                        }
                        else
                        {
                            var branchInfo = new BranchInfo(sp.Document, sp.Line, sp.Column, sp.EndColumn,
                                                            location.Method.FullName, location.Offset);
                            if (database.TargetObjectTypes.ContainsKey(targetObjectType))
                            {
                                database.TargetObjectTypes[targetObjectType].Add(branchInfo);
                            }
                            else
                            {
                                database.TargetObjectTypes.Add(targetObjectType,
                                                               new HashSet <BranchInfo> {
                                    branchInfo
                                });
                            }
                        }
                    }

                    IEnumerable <Field>  fields = GetInvolvedFields(e.TermManager, flippedCondition);
                    IEnumerable <TypeEx> types  = GetInvolvedObjectTypes(e.TermManager, flippedCondition);
                    var    simpleLog            = database.SimpleLog;
                    var    errorLog             = database.ErrorLog;
                    Field  target;
                    TypeEx declaringType;
                    simpleLog.AppendLine("============Log Problem================");
                    simpleLog.AppendLine("result: " + e.Result);
                    simpleLog.AppendLine(sb.ToString());

                    TypeEx targetType;
                    if (
                        !ObjectCreationProblemAnalyzer.GetTargetExplorableField(fields.Reverse(), out target,
                                                                                out declaringType,
                                                                                Host, out targetType))
                    {
                        simpleLog.AppendLine("can not analyze");
                    }


                    simpleLog.AppendLine("failed term: \n" + stringWriter.ToString());
                    fields.ToList().ForEach(x => simpleLog.AppendLine("involved field: " + x));
                    foreach (var f in fields)
                    {
                        simpleLog.AppendLine("involved field: ");
                        simpleLog.AppendLine("f.FullName:" + f.FullName);
                        simpleLog.AppendLine("f.Definition.FullName" + f.Definition.FullName);
                        simpleLog.AppendLine("f.InstanceFieldMapType:" + f.InstanceFieldMapType.FullName);
                        TypeEx type;
                        //                        type.
                        f.TryGetDeclaringType(out type);
                        simpleLog.AppendLine("f.TryGetDeclaringType: " + type.FullName);
                    }

                    types.ToList().ForEach(x => simpleLog.AppendLine("found object type: " + x));
                    types.ToList().ForEach(x => Host.GetService <ProblemTrackDatabase>().FoundTypes.Add(x.FullName));
                    fields.ToList().ForEach(x => Host.GetService <ProblemTrackDatabase>().FoundTypes.Add(x.Type.FullName));
                    fields.ToList().ForEach(x =>
                    {
                        TypeEx decType;
                        if (x.TryGetDeclaringType(out decType))
                        {
                            Host.GetService <ProblemTrackDatabase>().FoundTypes.Add(
                                decType.FullName);
                        }
                        ;
                    });
                    simpleLog.AppendLine("target field: " + target);

                    if (fields != null && fields.Count() > 0)
                    {
                        CreateCandidateObjectCreationProblem(database, location, sp, stringWriter, simpleLog, fields,
                                                             target,
                                                             errorLog, targetType, targetObjectType);
                    }

                    if (fields == null || fields.Count() == 0 && targetObjectType != null && !infeasible)
                    {
                        CreateCandidateObjectCreationProblemForSingleType(stringWriter, sp, location, targetObjectType,
                                                                          database, simpleLog, errorLog);
                    }

                    simpleLog.AppendLine("============end Log Problem================");
                    simpleLog.AppendLine();
                }
                catch (Exception ex)
                {
                    Host.GetService <ProblemTrackDatabase>().ErrorLog.AppendLine("Error in problem observer: " + ex);
                }
//                DumpInfoToDebugFile(sb.ToString(),outputFile);
            }