Exemplo n.º 1
0
 public FileAnalyzerTests()
 {
     this.Content = path + @"input_test_with_content.txt";
     this.ContentWithInvalidExpressions    = path + @"input_test_with_content_with_invalid_expressions.txt";
     this.ContentWithoutInvalidExpressions = @"input_test_with_content_without_invalid_expressions.txt";
     this._FileInfo = _FileAnalyzer.Analyze(Content);
 }
Exemplo n.º 2
0
        private void OnFileEvent(object sender, FileEventArgs e)
        {
            RemoveInvalidClients();

            SendNotificationToAllClients(e);

            // Analyze each file on a separate Thread
            var _fileAnalyzerThread = new Thread(new ThreadStart(() =>
            {
                try
                {
                    Logger.WriteToLog("Starting a new FileAnalyzerThread.");

                    FileAnalyzer fileAnalyzer         = new FileAnalyzer(_cloudAnalyzerURL, e);
                    FileAnalysisStatus analysisStatus = fileAnalyzer.Analyze();
                    e.AnalysisStatus = analysisStatus;

                    SendNotificationToAllClients(e);
                }
                catch (Exception ex)
                {
                    Logger.WriteToLog("Exception caught in FileAnalyzerThread.");
                    Logger.WriteToLog(ex);
                }
            }))
            {
                Priority     = ThreadPriority.AboveNormal,
                IsBackground = true
            };

            _fileAnalyzersThreads.Add(_fileAnalyzerThread);
            _fileAnalyzerThread.Start();
        }
Exemplo n.º 3
0
        public void TestFileWithTabAfterEndif()
        {
            string         filename = "FileWithTabAfterEndIf.c"; // this is deployed through local.testsettings
            SortedListener listener = new SortedListener(10, new List <string>(), 0);

            using (StreamReader stream = new StreamReader(filename))
            {
                FileAnalyzer analyzer = new FileAnalyzer(stream, listener, null, true, filename);
                analyzer.Analyze();

                Assert.AreEqual(1, listener.Metrics.Count);

                Assert.IsNotNull(IntegrationTests.GetByName(listener.Metrics, "Foo"));
            }
        }
Exemplo n.º 4
0
        public void TestCStyleFuncDecl()
        {
            string         filename = "cstylefuncs.c"; // this is deployed through local.testsettings
            SortedListener listener = new SortedListener(10, new List <string>());

            using (StreamReader stream = new StreamReader(filename))
            {
                FileAnalyzer analyzer = new FileAnalyzer(stream, listener, null, true, filename);
                analyzer.Analyze();

                Assert.AreEqual(3, listener.Metrics.Count);

                Assert.IsNotNull(IntegrationTests.GetByName(listener.Metrics, "added"));
                Assert.IsNotNull(IntegrationTests.GetByName(listener.Metrics, "new_cfg_record"));
                Assert.IsNotNull(IntegrationTests.GetByName(listener.Metrics, "edit_cfg_record"));
            }
        }
Exemplo n.º 5
0
        public void TestTypescriptFileIsCorrectlyParsed()
        {
            string         filename = "TypeScript.ts"; // this is deployed through local.testsettings
            SortedListener listener = new SortedListener(10, new List <string>());

            using (StreamReader stream = new StreamReader(filename))
            {
                FileAnalyzer analyzer = new FileAnalyzer(stream, listener, null, false, filename);
                analyzer.Analyze();

                Assert.AreEqual(3, listener.Metrics.Count);

                Assert.IsNotNull(IntegrationTests.GetByName(listener.Metrics, "Sayings::Greeter::greet()"));
                Assert.IsNotNull(IntegrationTests.GetByName(listener.Metrics, "Sayings::Greeter::constructor(message:string)"));
                Assert.IsNotNull(IntegrationTests.GetByName(listener.Metrics, "button.onclick()"));
            }
        }
Exemplo n.º 6
0
        public static FileAnalyzer Create(Type recordType, Stream fileStream)
        {
            var options = new StorageOptions
            {
                LoadMask           = LoadMask.Records | LoadMask.StringTable,
                CopyToMemory       = false,
                InternStrings      = true,
                MemberType         = System.Reflection.MemberTypes.Property,
                IgnoreSignedChecks = true
            };

            fileStream.Position = 0;
            var instance = new FileAnalyzer(recordType, fileStream, options);

            instance.Analyze();
            return(instance);
        }
        private void AnalyzeScript(FileInfo fileInfo)
        {
            Logger.Debug("Analyzing script '{0}' ...", fileInfo.FullName);

            string scriptText = TryToReadFile(fileInfo);

            if (scriptText.Length == 0)
            {
                return;
            }

            SyntaxTree    syntaxTree    = _syntaxTrees[fileInfo.FullName];
            SemanticModel semanticModel = _compilation.GetSemanticModel(syntaxTree);

            FileAnalyzer fileAnalyzer = new FileAnalyzer(syntaxTree, semanticModel, _inputDirectory);

            _fileModels.Add(fileAnalyzer.Analyze(fileInfo));
        }
Exemplo n.º 8
0
        private static bool CCM(byte[] bytes, ref CodeMetrics metrics)
        {
            // create container for the metrics
            var listenter = new SortedListener(
                numMetrics: 30,
                ignores: new List <string>(),
                threshold: 0
                );

            // analyze
            using (var stream = new MemoryStream(bytes))
            {
                var reader = new StreamReader(stream);

                // analyze
                try
                {
                    var analyzer = new FileAnalyzer(
                        filestream: reader,
                        listenter,
                        context: null,
                        suppressMethodSignatures: false,
                        filename: "foo.cs"
                        );

                    analyzer.Analyze();

                    // consider the metrics in listener
                    foreach (var m in listenter.Metrics)
                    {
                        System.Diagnostics.Debug.WriteLine($"{m.Unit} {m.CCM} {m}");
                        metrics.CCM += m.CCM;
                        metrics.Methods++;
                    }
                }
                catch (Exception e)
                {
                    metrics.CCM = Single.MaxValue;
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 9
0
        private void AnalyzeFileInBackgroundThread(object filename)
        {
            try
            {
                string file = (string)filename;

                using (System.IO.StreamReader reader = new System.IO.StreamReader(file))
                {
                    FileAnalyzer analyzer = new FileAnalyzer(reader, this.listener, null, true, file);
                    analyzer.Analyze();
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                this.sempahore.Release();
            }
        }
Exemplo n.º 10
0
        private static AnalyzerCollector Analyze(string filename, bool suppressMethodSignature)
        {
            //TODO: start using SortedListener instead of AnalyzerCollector (remove this one)
            AnalyzerCollector collector = new AnalyzerCollector();

            StreamReader stream = new StreamReader(filename);

            try
            {
                FileAnalyzer analyzer =
                    new FileAnalyzer(stream, collector, null, suppressMethodSignature, filename);

                analyzer.Analyze();
            }
            finally
            {
                stream.Dispose();
            }

            return(collector);
        }
Exemplo n.º 11
0
        private void AnalyzeFilestream(object context)
        {
            AnalyzeThreadParameters parameters = (AnalyzeThreadParameters)context;

            try
            {
                FileAnalyzer analyzer =
                    new FileAnalyzer(parameters.stream, this.listener, null, this.configFile.SuppressMethodSignatures,
                                     parameters.filename, this.configFile.SwitchStatementBehavior);

                analyzer.Analyze();
                parameters.stream.Close(); // free up the stream.
            }
            catch (UnknownStructureException error)
            {
                lock (this.mutex)
                {
                    this.errors.Add(new ErrorInfo(parameters.filename, error.Message));
                }
            }
            catch (PreprocessorException)
            {
                lock (this.mutex)
                {
                    this.errors.Add(new ErrorInfo(parameters.filename, "Error running pre-processor. Only basic support for #ifdefs."));
                }
            }
            catch (Exception)
            {
                lock (this.mutex)
                {
                    this.errors.Add(new ErrorInfo(parameters.filename, "Unknown error parsing file."));
                }
            }

            this.threadSemaphore.Release();
        }
Exemplo n.º 12
0
        public void TestJavascriptFileContainsAllFunctions()
        {
            string         filename = "examples.js"; // this is deployed through local.testsettings
            SortedListener listener = new SortedListener(10, new List <string>());

            using (StreamReader stream = new StreamReader(filename))
            {
                FileAnalyzer analyzer = new FileAnalyzer(stream, listener, null, false, filename);
                analyzer.Analyze();

                Assert.AreEqual(10, listener.Metrics.Count);

                Assert.AreEqual(6, IntegrationTests.GetByName(listener.Metrics, "gcd(segmentA,segmentB)").CCM);
                Assert.AreEqual(2, IntegrationTests.GetByName(listener.Metrics, "function(monkey)").CCM);
                Assert.AreEqual(1, IntegrationTests.GetByName(listener.Metrics, "localFunction()").CCM);
                Assert.AreEqual(1, IntegrationTests.GetByName(listener.Metrics, "Some.Foo(args)").CCM);
                Assert.AreEqual(1, IntegrationTests.GetByName(listener.Metrics, "functionWithColon()").CCM);
                Assert.AreEqual(2, IntegrationTests.GetByName(listener.Metrics, "localFunction1()").CCM);
                Assert.AreEqual(4, IntegrationTests.GetByName(listener.Metrics, "outerFunction1()").CCM);
                Assert.AreEqual(1, IntegrationTests.GetByName(listener.Metrics, "localFunction2()").CCM);
                Assert.AreEqual(1, IntegrationTests.GetByName(listener.Metrics, "Monkey::feedMonkey()").CCM);
                Assert.AreEqual(1, IntegrationTests.GetByName(listener.Metrics, "afterMonkeyFeed()").CCM);
            }
        }
        public static CyclomaticComplexityMetric CalculateMetric(Stream stream, string filePath, bool disposeStream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var listener = new SortedListener(1000, new List <string>(), 0);

            try
            {
                var fileAnalyzer = new FileAnalyzer(new StreamReader(stream), listener, null, false, filePath);
                fileAnalyzer.Analyze();
            }
            finally
            {
                if (disposeStream)
                {
                    stream.Dispose();
                }
            }

            var ccmMetrics = listener.Metrics.Select(g => g.CCM).ToList();

            if (!ccmMetrics.Any())
            {
                return(null);
            }

            return(new CyclomaticComplexityMetric
            {
                CCMMax = ccmMetrics.Max(),
                CCMAvg = ccmMetrics.Average(),
                CCMMd = ccmMetrics.Median()
            });
        }
Exemplo n.º 14
0
 public QuestionAnalyzerTests()
 {
     _FileInfo = _FileAnalyzer.Analyze(Content);
 }
        /// <summary>
        /// Program entry point.
        /// </summary>
        /// <param name="args">The arguments.</param>
        public static void Main(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                string filePath = System.AppDomain.CurrentDomain.BaseDirectory
                                  .Replace(@"\MerchantsGuideToTheGalaxy\bin\Release\", @"\input.txt");
                Console.WriteLine(filePath);
                args = new string[] { filePath };
            }

            string path = args[0];

            FileAnalyzer analyzer = new FileAnalyzer();
            var          fileInfo = analyzer.Analyze(path);

            Console.Clear();
            Console.WriteLine("Test Input: ");
            if (fileInfo.ReferenceValues != null && fileInfo.ReferenceValues.Count() > 0)
            {
                foreach (var item in fileInfo.ReferenceValues)
                {
                    Console.WriteLine(item);
                }
            }
            else
            {
                Console.WriteLine("The file don't have any line with reference values or don't exists!");
            }

            if (fileInfo.Questions != null && fileInfo.Questions.Count() > 0)
            {
                foreach (var item in fileInfo.Questions)
                {
                    Console.WriteLine(item);
                }
            }
            else
            {
                Console.WriteLine("The file don't have any line with question or don't exists!");
            }

            Console.WriteLine();

            Console.WriteLine("Test Output: ");
            if (fileInfo.Output != null && fileInfo.Output.Count() > 0)
            {
                foreach (var item in fileInfo.Output)
                {
                    Console.WriteLine(item);
                }
            }
            else
            {
                Console.WriteLine("The file don't have any output or don't exists!");
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");

            Console.ReadKey();
        }
Exemplo n.º 16
0
        public MappingResolver(string fileName, FileAnalyzer source, FileAnalyzer target)
        {
            _fileName = fileName;

            #region setup default source if none - should never be true if you want relevant names

            if (source.RecordType == null)
            {
                var sourceTypeGenerator = CreateTypeFromAnalyzer(source, "SourceType");

                source.Stream.Position = 0;
                source = new FileAnalyzer(sourceTypeGenerator.Generate(), source.Stream, source.Options);
                source.Analyze();

                if (AdjustStringMembers(sourceTypeGenerator, source))
                {
                    source.Stream.Position = 0;
                    source = new FileAnalyzer(sourceTypeGenerator.Generate(), source.Stream, source.Options);
                    source.Analyze();
                }
            }
            #endregion

            #region Setup dummy target structure
            if (target.RecordType == null)
            {
                var targetTypeGenerator = CreateTypeFromAnalyzer(target, "TargetType");

                target.Stream.Position = 0;
                target = new FileAnalyzer(targetTypeGenerator.Generate(), target.Stream, target.Options);
                target.Analyze();

                if (AdjustStringMembers(targetTypeGenerator, target))
                {
                    target.Stream.Position = 0;
                    target = new FileAnalyzer(targetTypeGenerator.Generate(), target.Stream, target.Options);
                    target.Analyze();
                }
            }
            #endregion

            if (source.IndexColumn != target.IndexColumn)
            {
                Console.WriteLine("Index column moved!");
            }

            // Construct containers for each node
            var sourceList = CreateStore(source);
            var targetList = CreateStore(target);

            var mappingStore = new Dictionary <MemberInfo, List <MemberInfo> >();
            #region mapping
            foreach (var sourceKey in sourceList.Keys)
            {
                if (!targetList.Contains(sourceKey))
                {
                    continue;
                }

                var    sourceElement = sourceList[sourceKey];
                object targetElement;
                try
                {
                    targetElement = targetList[sourceKey];
                }
                catch
                {
                    // Silence the exception (which means item does not belong to collection)
                    continue;
                }

                foreach (var sourceExtendedMemberInfo in source.Members.Members)
                {
                    if (this.Any(m => m.Value.From == sourceExtendedMemberInfo.MemberInfo))
                    {
                        break;
                    }

                    var sourceMemberInfo  = sourceExtendedMemberInfo.MemberInfo;
                    var sourceMemberValue = BoxToIntOrSelf((sourceMemberInfo as PropertyInfo)?.GetValue(sourceElement));
                    if (sourceMemberValue == null)
                    {
                        continue;
                    }

                    if (!mappingStore.TryGetValue(sourceMemberInfo, out var mappingList))
                    {
                        // At first, pretend everything matches
                        mappingStore[sourceMemberInfo] = mappingList = new List <MemberInfo>();
                        mappingList.AddRange(target.Members.Members.Select(m => m.MemberInfo));
                    }
                    else if (mappingList.Count == 1)
                    {
                        continue;
                    }

                    var itr = 0;
                    while (mappingList.Count != 0 && itr < mappingList.Count)
                    {
                        if (mappingList.Count == 1)
                        {
                            break;
                        }

                        var targetMemberInfo = mappingList[itr] as PropertyInfo;
                        if (targetMemberInfo == null)
                        {
                            throw new InvalidOperationException("Unreachable");
                        }

                        var targetMemberValue = BoxToIntOrSelf(targetMemberInfo.GetValue(targetElement));

                        bool valueMatch = sourceMemberValue.Equals(targetMemberValue);
                        if (sourceMemberValue is Array arrSource && targetMemberValue is Array arrTarget)
                        {
                            valueMatch = true;
                            for (var i = 0; i < arrSource.Length && i < arrTarget.Length && valueMatch; ++i)
                            {
                                valueMatch = BoxToIntOrSelf(arrSource.GetValue(i)).Equals(BoxToIntOrSelf(arrTarget.GetValue(i)));
                            }
                        }

                        if (!valueMatch)
                        {
                            mappingList.Remove(targetMemberInfo);
                        }
                        else
                        {
                            ++itr;
                        }

                        if (mappingList.Count == 1)
                        {
                            break;
                        }
                    }
                }
            }
            #endregion

            // Create mappings
            foreach (var t in target.Members.Members.OrderBy(m => m.MemberInfo.GetCustomAttribute <OrderAttribute>().Order))
            {
                this[t.MemberInfo] = new ResolvedMapping();
            }

            // Fill mappings
            foreach (var mapInfo in mappingStore)
            {
                foreach (var targetNode in mapInfo.Value)
                {
                    if (!TryGetValue(targetNode, out var map))
                    {
                        map = this[targetNode] = new ResolvedMapping();
                    }

                    map.Candidates.Add(mapInfo.Key);
                }
            }

            // if only one candidate is found, map the match
            foreach (var t in this)
            {
                if (t.Value.Candidates.Count == 1)
                {
                    t.Value.From = t.Value.Candidates[0];
                }
            }

            // Sort the set by key, and assing it back to us
            var sortedSet = this.OrderBy(kv => kv.Key.GetCustomAttribute <OrderAttribute>().Order).ToArray();
            Clear();
            foreach (var kv in sortedSet)
            {
                Add(kv.Key, kv.Value);
            }

            // Generate resolved type and expose it
            var typeGen = new TypeGenerator();
            foreach (var kv in this)
            {
                if (kv.Value.From != null)
                {
                    typeGen.CreateProperty(kv.Value.From.Name,
                                           kv.Key.GetMemberType(),
                                           kv.Value.From.GetCustomAttribute <CardinalityAttribute>()?.SizeConst ?? 1,
                                           kv.Key.IsDefined(typeof(IndexAttribute), false));
                }
                else if (kv.Value.Candidates.Count != 1)
                {
                    typeGen.CreateProperty("unverified_" + kv.Key.GetCustomAttribute <OrderAttribute>().Order,
                                           kv.Key.GetMemberType(),
                                           kv.Key.GetCustomAttribute <CardinalityAttribute>()?.SizeConst ?? 1,
                                           kv.Key.IsDefined(typeof(IndexAttribute), false));
                }
            }

            foreach (var attr in target.RecordType.GetCustomAttributes <LayoutAttribute>())
            {
                typeGen.AddAttribute(attr.GetType().GetConstructor(new[] { typeof(uint) }), new object[] { attr.LayoutHash });
            }

            foreach (var attr in target.RecordType.GetCustomAttributes <BuildAttribute>())
            {
                typeGen.AddAttribute(attr.GetType().GetConstructor(new[] { typeof(string) }), new object[] { attr.ToString() });
            }

            foreach (var attr in target.RecordType.GetCustomAttributes <BuildRangeAttribute>())
            {
                typeGen.AddAttribute(attr.GetType().GetConstructor(new[] { typeof(BuildInfo), typeof(BuildInfo) }), new object[] { attr.From, attr.To });
            }

            Type = typeGen.Generate();
        }