Example #1
0
        public static bool TryUpdateLineWithAttribute(
            SourceAnnotationFile sourceFile, int attributeLineNumber,
            CompetitionMetricValue metricValue)
        {
            var line = sourceFile[attributeLineNumber];

            if (!TryUpdateLineCore(line, metricValue, out var newLine))
            {
                return(false);
            }

            if (newLine != line)
            {
                sourceFile.ReplaceLine(attributeLineNumber, newLine);
            }
            return(true);
        }
Example #2
0
        public static int InsertLineWithAttribute(
            SourceAnnotationFile sourceCodeFile, int insertLineNumber,
            TargetSourceLines benchmarkMethod,
            CompetitionMetricValue metricValue)
        {
            var whitespacePrefix = GetWhitespacePrefix(sourceCodeFile[insertLineNumber]);

            var newLine = GetNewAnnotationLine(whitespacePrefix, metricValue);

            var newLineNumber = insertLineNumber + 1;

            sourceCodeFile.InsertLine(newLineNumber, newLine);
            var attributeTypeHandle = metricValue.Metric.AttributeType.TypeHandle;

            benchmarkMethod.AddAttribute(attributeTypeHandle, newLineNumber);

            return(newLineNumber);
        }
Example #3
0
        public static bool TryInsertAttributeInplace(
            SourceAnnotationFile sourceCodeFile, int inplaceLineNumber,
            TargetSourceLines benchmarkMethod,
            CompetitionMetricValue metricValue)
        {
            var line = sourceCodeFile[inplaceLineNumber];

            var inplacePosition = line.LastIndexOf(']');

            if (inplacePosition < 0)
            {
                return(false);
            }

            var appendText = GetInplaceAnnotationText(metricValue);

            line = line.Insert(inplacePosition, appendText);
            sourceCodeFile.ReplaceLine(inplaceLineNumber, line);
            var attributeTypeHandle = metricValue.Metric.AttributeType.TypeHandle;

            benchmarkMethod.AddAttribute(attributeTypeHandle, inplaceLineNumber);

            return(true);
        }
Example #4
0
        private static bool TryUpdate(
            SourceAnnotationFile sourceCodeFile,
            CompetitionTarget competitionTarget)
        {
            if (!sourceCodeFile.Parsed)
            {
                return(false);
            }

            var target    = competitionTarget.Target;
            var targetKey = target.Method.MethodHandle;

            if (!sourceCodeFile.BenchmarkMethods.TryGetValue(
                    targetKey,
                    out var benchmarkMethod))
            {
                return(false);
            }

            bool allFixed = true;

            var metricsByCategory = competitionTarget.MetricValues.GroupBy(m => m.Metric.Category);

            foreach (var metricGrouping in metricsByCategory.Select(x => x.ToArray()))
            {
                var metricAttributes          = metricGrouping.Select(m => m.Metric.AttributeType.TypeHandle).ToHashSet();
                var attributeAppendLineNumber = benchmarkMethod.AttributeLineNumbers
                                                .Where(p => metricAttributes.Contains(p.Key))
                                                .MinOrDefault(p => p.Value, -1);
                var attributeInsertLineNumber = benchmarkMethod.AttributeLineNumbers
                                                .Where(p => metricAttributes.Contains(p.Key))
                                                .MaxOrDefault(p => p.Value, benchmarkMethod.PrimaryAttributeLineNumber);

                foreach (var metricValue in metricGrouping.Where(m => m.HasUnsavedChanges || m.ValuesRange.IsEmpty))
                {
                    var attributeTypeHandle = metricValue.Metric.AttributeType.TypeHandle;
                    if (benchmarkMethod.AttributeLineNumbers.TryGetValue(
                            attributeTypeHandle,
                            out var attributeLineNumber))
                    {
                        bool updated = TryUpdateLineWithAttribute(sourceCodeFile, attributeLineNumber, metricValue);

                        if (updated && attributeAppendLineNumber <= 0)
                        {
                            attributeAppendLineNumber = attributeLineNumber;
                        }
                        allFixed &= updated;
                    }
                    else
                    {
                        bool inserted = false;
                        if (metricValue.Metric.AnnotateInPlace && attributeAppendLineNumber > 0)
                        {
                            inserted = TryInsertAttributeInPlace(
                                sourceCodeFile, attributeAppendLineNumber,
                                benchmarkMethod, metricValue);
                        }

                        if (!inserted)
                        {
                            attributeInsertLineNumber = InsertLineWithAttribute(
                                sourceCodeFile, attributeInsertLineNumber,
                                benchmarkMethod, metricValue);
                            if (attributeAppendLineNumber <= 0)
                            {
                                attributeAppendLineNumber = attributeInsertLineNumber;
                            }
                        }
                    }
                }
            }

            return(allFixed);
        }