<commit_before>﻿// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the MIT License.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MSBuild;
using System.IO;
using System.Configuration;
using System.Threading.Tasks;
using ReachingTypeAnalysis.Roslyn;
using System.Threading;

namespace ReachingTypeAnalysis
{
	public static class Utils
	{
		public static MethodDescriptor CreateMethodDescriptor(IMethodSymbol method)
		{
			Contract.Assert(method != null);
			var typeDescriptor = Utils.CreateTypeDescriptor(method.ContainingType);

			var result = new MethodDescriptor(typeDescriptor, method.Name, method.IsStatic,
				method.Parameters.Select(parmeter => Utils.CreateTypeDescriptor(parmeter.Type)),
				Utils.CreateTypeDescriptor(method.ReturnType));

			return result;
		}

		public static IList<Project> FilterProjects(Solution solution)
		{
			var filteredProjects = from project in solution.Projects
								   where project.Language == LanguageNames.CSharp
								   select project;

			return new List<Project>(filteredProjects);
		}

		public static TypeDescriptor CreateTypeDescriptor(ITypeSymbol type, bool isConcrete = true)
		{
			Contract.Assert(type != null);
			var assemblyName = "Unknown";
			var namespaceName = Utils.GetFullNamespaceName(type);
			var kind = Utils.Convert(type.TypeKind);

			if (type.ContainingAssembly != null)
			{
				assemblyName = type.ContainingAssembly.Name;
			}
			else if (type.TypeKind == TypeKind.Array && type.BaseType != null)
			{
				assemblyName = type.BaseType.ContainingAssembly.Name;
			}

			return new TypeDescriptor(namespaceName, type.Name, assemblyName, type.IsReferenceType, kind, isConcrete);
		}

		private static string GetFullNamespaceName(ISymbol symbol)
		{
			var namespaceName = string.Empty;

			if (symbol.ContainingNamespace != null && !symbol.ContainingNamespace.IsGlobalNamespace)
			{
				namespaceName = symbol.ContainingNamespace.ToDisplayString();
			}

			return namespaceName;
		}

		private static SerializableTypeKind Convert(TypeKind kind)
		{
			switch (kind)<commit_msg>Fixing bug in CreateTypeDescriptor to handle non named types like arrays.<commit_after>﻿// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the MIT License.  See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MSBuild;
using System.IO;
using System.Configuration;
using System.Threading.Tasks;
using ReachingTypeAnalysis.Roslyn;
using System.Threading;

namespace ReachingTypeAnalysis
{
	public static class Utils
	{
		public static MethodDescriptor CreateMethodDescriptor(IMethodSymbol method)
		{
			Contract.Assert(method != null);
			var typeDescriptor = Utils.CreateTypeDescriptor(method.ContainingType);

			var result = new MethodDescriptor(typeDescriptor, method.Name, method.IsStatic,
				method.Parameters.Select(parmeter => Utils.CreateTypeDescriptor(parmeter.Type)),
				Utils.CreateTypeDescriptor(method.ReturnType));

			return result;
		}

		public static IList<Project> FilterProjects(Solution solution)
		{
			var filteredProjects = from project in solution.Projects
								   where project.Language == LanguageNames.CSharp
								   select project;

			return new List<Project>(filteredProjects);
		}

		public static TypeDescriptor CreateTypeDescriptor(ITypeSymbol type, bool isConcrete = true)
		{
			Contract.Assert(type != null);
			var assemblyName = "Unknown";
			var namespaceName = "Unknown";
			var typeName = "Unknown";
			var kind = Utils.Convert(type.TypeKind);

			if (type is INamedTypeSymbol)
			{
				var namedType = type as INamedTypeSymbol;
				assemblyName = namedType.ContainingAssembly.Name;
				namespaceName = Utils.GetFullNamespaceName(namedType);
				typeName = namedType.Name;
			}
			else if (type is ITypeParameterSymbol)
			{
				var typeParameter = type as ITypeParameterSymbol;
				assemblyName = typeParameter.ContainingAssembly.Name;
				namespaceName = Utils.GetFullNamespaceName(typeParameter);
				typeName = typeParameter.Name;
			}
			else if (type is IArrayTypeSymbol)
			{
				var arrayType = type as IArrayTypeSymbol;
				assemblyName = arrayType.ElementType.ContainingAssembly.Name;
				namespaceName = Utils.GetFullNamespaceName(arrayType.ElementType);
				typeName = arrayType.ElementType.Name;
			}
			else
			{
				var message = string.Format("Unsupported type: {0}", type);
				throw new Exception(message);
			}

			var result = new TypeDescriptor(namespaceName, typeName, assemblyName, type.IsReferenceType, kind, isConcrete);
			return result;
		}

		private static string GetFullNamespaceName(ISymbol symbol)
		{
			var namespaceName = string.Empty;

			if (symbol.ContainingNamespace != null && !symbol.ContainingNamespace.IsGlobalNamespace)
			{
				namespaceName = symbol.ContainingNamespace.ToDisplayString();
			}

			return namespaceName;
		}

		private static SerializableTypeKind Convert(TypeKind kind)
		{
			switch (kind)
{'fraction_non_alphanumeric': 0.08424365858174412, 'fraction_numerical': 0.00018515089798185522, 'mean_word_length': 4.546201232032854, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '490370', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1462, 'n_words': 371}