예제 #1
0
        /// <summary>
        /// Apply the annotations, optionally remove errorenous annotations
        /// </summary>
        /// <param name="annotations">annotations to be applied</param>
        /// <param name="originalCompilation">compilation to annotate</param>
        /// <param name="project">originalCompilation's project</param>
        /// <param name="check_for_errors">if true, will remove annotations cause errors (according to Roslyn)</param>
        /// <returns>A modified version of originalCompilation with the annotations added</returns>
        private static Compilation ApplyAnnotations(IEnumerable <BaseAnnotation> annotations, Compilation originalCompilation, Project project, bool check_for_errors = true)
        {
            #region CodeContracts
            Contract.Requires(originalCompilation != null);
            Contract.Requires(annotations != null);
            Contract.Ensures(Contract.Result <Compilation>() != null);
            #endregion CodeContracts

            IEnumerable <BaseAnnotation> annotations2, annotations3;
            var newCompilation = originalCompilation;
            newCompilation = InterfaceAnnotationHelpers.CreateOrFindContractsClasses(annotations, project, newCompilation, out annotations2);
            newCompilation = ReadonlyHelpers.ReadonlyPass(annotations2, newCompilation);
            newCompilation = ObjectInvariantHelpers.ObjectInvariantPass(newCompilation, annotations2, out annotations3);
            var syntosugdict = Searcher.GetSyntaxNodeToAnnotationDict(newCompilation, annotations3);
            var syntosyndict = Replacer.PrecomputeReplacementNodes(syntosugdict, newCompilation);
            var comp2        = Replacer.RewriteCompilation(newCompilation, syntosyndict);
            var diagnostics  = comp2.GetDiagnostics().Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Error);
            if (diagnostics.Any() && check_for_errors)
            {
                Output.WriteLine("Fix the errors introduced by bad contracts");

                Contract.Assert(annotations3.Count() == annotations.Count());
                var filteredAnnotations = HelperForDiagnostics.FilterBadSuggestions(comp2, annotations3);
                // unresolve interface annotations, we're reverting to the original compilation and forgetting everything
                filteredAnnotations = filteredAnnotations.Select(x => x is ResolvedInterfaceAnnotation ? (x as ResolvedInterfaceAnnotation).OriginalAnnotation : x);
                filteredAnnotations = filteredAnnotations.Select(x => x is ResolvedObjectInvariant ? (x as ResolvedObjectInvariant).OriginalAnnotation : x);
                comp2 = ApplyAnnotations(filteredAnnotations, originalCompilation, project, false);
            }
            return(comp2);
        }