Пример #1
0
        private void AnalyzeSyntax(SemanticModelAnalysisContext context)
        {
            var compilation = (CSharpCompilation)context.SemanticModel.Compilation;
            var options     = new ProjectAnalyzer.Options()
            {
                breakOnError      = false,
                writeSyntaxSuffix = false,
                scanChilderen     = false
            };
            var analyzer = new ProjectAnalyzer(compilation, options);

            analyzer.SyntaxErrorCallback += Analyzer_SyntaxErrorCallback;

            void Analyzer_SyntaxErrorCallback(SyntaxError error)
            {
                var location = error.node.GetLocation();

                if (!location.IsInSource || Location.None.Equals(location))
                {
                    return;
                }
                var diagnostic = Diagnostic.Create(ErrorRule, location, error.message);

                context.ReportDiagnostic(diagnostic);
            }

            try
            {
                analyzer.Analyze(context.SemanticModel.SyntaxTree);
            }
            catch (Exception e)
            {
                var diagnostic = Diagnostic.Create(WarningRule, Location.None, "CS2X: Analyzer error: " + e.Message);
                context.ReportDiagnostic(diagnostic);
            }
        }
Пример #2
0
        internal async Task Parse()
        {
            if (isParsed)
            {
                return;
            }
            isParsed = true;

            // get compilation
            compilation = (CSharpCompilation)await roslynProject.GetCompilationAsync();

            // validate syntax rules
            var options = new ProjectAnalyzer.Options()
            {
                breakOnError      = false,
                writeSyntaxSuffix = true,
                scanChilderen     = true
            };
            var analyzer = new ProjectAnalyzer(compilation, options);

            if (!await analyzer.Analyze(roslynProject))
            {
                throw new Exception("Failed to Analyze project: " + roslynProject.FilePath);
            }

            // gather references
            var references = new List <Project>();
            var sln        = roslynProject.Solution;

            foreach (var reference in roslynProject.AllProjectReferences)
            {
                var project = solution.projects.FirstOrDefault(x => x.roslynProject.Id == reference.ProjectId);
                if (project == null)
                {
                    throw new Exception("Project reference not found in solution: " + reference.ProjectId);
                }
                references.Add(project);
                await project.Parse();
            }

            this.references = references;

            // init main objects
            classTypes     = new List <INamedTypeSymbol>();
            structTypes    = new List <INamedTypeSymbol>();
            interfaceTypes = new List <INamedTypeSymbol>();
            enumTypes      = new List <INamedTypeSymbol>();
            delegateTypes  = new List <INamedTypeSymbol>();

            // parse normal members
            ParseNamespace(compilation.Assembly.GlobalNamespace);

            // merge all types in one list
            var allTypesList = new List <INamedTypeSymbol>();

            allTypesList.AddRange(classTypes);
            allTypesList.AddRange(structTypes);
            allTypesList.AddRange(interfaceTypes);
            allTypesList.AddRange(enumTypes);
            allTypesList.AddRange(delegateTypes);

            // dependency sort all types
            allTypes = DependencySortTypes(allTypesList);
        }