An error reported by CppCheck.
Example #1
0
 private static void AssertOuterPropertiesAreExampleError(CppCheckError uut)
 {
     Assert.AreEqual("id", uut.Id);
     Assert.AreEqual("message", uut.Message);
     Assert.AreEqual("verbose", uut.VerboseMessage);
     Assert.AreEqual("style", uut.Severity);
 }
Example #2
0
        public void CppCheckError_ErrorWithSingleLocationIsConvertedToSarifIssue()
        {
            Result result = new CppCheckError("id", "message", "verbose", "my fancy severity", ImmutableArray.Create(
                                                  new CppCheckLocation("foo.cpp", 1234)
                                                  )).ToSarifIssue();

            Assert.AreEqual("id", result.RuleId);
            Assert.AreEqual("message", result.ShortMessage);
            Assert.AreEqual("verbose", result.FullMessage);
            result.Properties.Should().Equal(new Dictionary <string, string> {
                { "Severity", "my fancy severity" }
            });
            result.Locations.Should().Equal(new[] { new Location {
                                                        ResultFile = new[]
                                                        {
                                                            new PhysicalLocationComponent
                                                            {
                                                                Uri      = new Uri("foo.cpp", UriKind.RelativeOrAbsolute),
                                                                MimeType = MimeType.Cpp,
                                                                Region   = new Region {
                                                                    StartLine = 1234
                                                                }
                                                            }
                                                        }
                                                    } });
            Assert.IsNull(result.ExecutionFlows);
        }
Example #3
0
        public void CppCheckError_DoesNotEmitShortMessageWhenVerboseMessageIsTheSame()
        {
            Result result = new CppCheckError("id", "message", "message", "style", _dummyLocations)
                            .ToSarifIssue();

            Assert.Equal("message", result.Message.Text);
        }
        public void CppCheckError_ErrorWithMultipleLocationsFillsOutCodeFlow()
        {
            Result result = new CppCheckError("id", "message", "verbose", "my fancy severity", ImmutableArray.Create(
                new CppCheckLocation("foo.cpp", 1234),
                new CppCheckLocation("bar.cpp", 5678)
                )).ToSarifIssue();

            result.Locations.SequenceEqual(new[] { new Location {
                        ResultFile = new PhysicalLocation
                        {
                            Uri = new Uri("bar.cpp", UriKind.RelativeOrAbsolute),
                            Region = new Region { StartLine = 5678 }
                        }
                    }
                }, Location.ValueComparer).Should().BeTrue();

            Assert.AreEqual(1, result.CodeFlows.Count);
            result.CodeFlows.First().Locations.SequenceEqual(new[]
                {
                    new AnnotatedCodeLocation {
                        PhysicalLocation = new PhysicalLocation {
                            Uri = new Uri("foo.cpp", UriKind.RelativeOrAbsolute),
                            Region = new Region { StartLine = 1234 },
                        },
                        Importance = AnnotatedCodeLocationImportance.Essential
                    },
                    new AnnotatedCodeLocation {
                        PhysicalLocation = new PhysicalLocation {
                            Uri = new Uri("bar.cpp", UriKind.RelativeOrAbsolute),
                            Region = new Region { StartLine = 5678 }
                        },
                        Importance = AnnotatedCodeLocationImportance.Essential
                    }
                }, AnnotatedCodeLocation.ValueComparer).Should().BeTrue();
        }
Example #5
0
        public void CppCheckError_PassesThroughConstructorParameters()
        {
            var uut = new CppCheckError("id", "message", "verbose", "style", _dummyLocations);

            AssertOuterPropertiesAreExampleError(uut);
            Assert.AreEqual(_dummyLocations, uut.Locations);
        }
Example #6
0
 public void CppCheckError_RejectsSelfClosingError()
 {
     using (XmlReader xml = Utilities.CreateXmlReaderFromString(ExampleErrorXmlSelfClosed))
     {
         CppCheckError check = Parse(xml);
         check.Locations.Should().BeEmpty();
     }
 }
Example #7
0
 public void CppCheckError_ErrorWithNoLocationsShouldNotThrow()
 {
     using (XmlReader xml = Utilities.CreateXmlReaderFromString(ExampleErrorXmlOpen + ExampleErrorClose))
     {
         CppCheckError check = Parse(xml);
         check.Locations.Should().BeEmpty();
     }
 }
Example #8
0
        public void CppCheckError_ErrorWithMultipleLocationsFillsOutCodeFlow()
        {
            Result result = new CppCheckError("id", "message", "verbose", "my fancy severity", ImmutableArray.Create(
                                                  new CppCheckLocation(ExampleFileName, 1234),
                                                  new CppCheckLocation(ExampleFileName2, 5678)
                                                  )).ToSarifIssue();

            result.Locations.SequenceEqual(new[] { new Location {
                                                       PhysicalLocation = new PhysicalLocation
                                                       {
                                                           ArtifactLocation = new ArtifactLocation
                                                           {
                                                               Uri = new Uri(ExampleFileName2, UriKind.RelativeOrAbsolute)
                                                           },
                                                           Region = new Region {
                                                               StartLine = 5678
                                                           }
                                                       }
                                                   } }, Location.ValueComparer).Should().BeTrue();

            Assert.Equal(1, result.CodeFlows.Count);
            result.CodeFlows.First().ThreadFlows.First().Locations.SequenceEqual(new[]
            {
                new ThreadFlowLocation {
                    Location = new Location
                    {
                        PhysicalLocation = new PhysicalLocation
                        {
                            ArtifactLocation = new ArtifactLocation
                            {
                                Uri = new Uri(ExampleFileName, UriKind.RelativeOrAbsolute)
                            },
                            Region = new Region {
                                StartLine = 1234
                            },
                        }
                    },
                    Importance = ThreadFlowLocationImportance.Essential
                },
                new ThreadFlowLocation {
                    Location = new Location
                    {
                        PhysicalLocation = new PhysicalLocation
                        {
                            ArtifactLocation = new ArtifactLocation
                            {
                                Uri = new Uri(ExampleFileName2, UriKind.RelativeOrAbsolute)
                            },
                            Region = new Region {
                                StartLine = 5678
                            }
                        }
                    },
                    Importance = ThreadFlowLocationImportance.Essential
                }
            }, ThreadFlowLocation.ValueComparer).Should().BeTrue();
        }
Example #9
0
        public void CppCheckError_CanParseErrorWithNoLocation()
        {
            string errorXml = ExampleErrorXmlOpen + ExampleErrorClose;

            using (XmlReader xml = Utilities.CreateXmlReaderFromString(errorXml))
            {
                CppCheckError uut = Parse(xml);
                AssertOuterPropertiesAreExampleError(uut);
                uut.Locations.Should().BeEmpty();
            }
        }
Example #10
0
        public void CppCheckError_MinimalErrorCanBeConvertedToSarifIssue()
        {
            Result result = new CppCheckError("id", "message", "verbose", "style", _dummyLocations)
                            .ToSarifIssue();

            Assert.Equal("id", result.RuleId);
            Assert.Equal("verbose", result.Message.Text);
            result.PropertyNames.Count.Should().Be(1);
            result.GetProperty("Severity").Should().Be("style");
            Assert.Equal("file.cpp", result.Locations.First().PhysicalLocation.ArtifactLocation.Uri.ToString());
        }
Example #11
0
        public void CppCheckError_CanParseErrorWithSingleLocationAndSymbol()
        {
            string errorXml = ExampleErrorXmlOpen + " <location file=\"" + ExampleFileName + "\" line=\"42\" /><symbol>s</symbol> " + ExampleErrorClose;

            using (XmlReader xml = Utilities.CreateXmlReaderFromString(errorXml))
            {
                CppCheckError uut = Parse(xml);
                AssertOuterPropertiesAreExampleError(uut);
                uut.Locations.Should().Equal(new[] { new CppCheckLocation(ExampleFileName, 42) });
            }
        }
Example #12
0
        public void CppCheckError_MinimalErrorCanBeConvertedToSarifIssue()
        {
            Result result = new CppCheckError("id", "message", "verbose", "style", _dummyLocations)
                            .ToSarifIssue();

            Assert.AreEqual("id", result.RuleId);
            Assert.AreEqual("verbose", result.Message);
            result.Properties.Should().Equal(new Dictionary <string, string> {
                { "Severity", "style" }
            });
            Assert.AreEqual("file.cpp", result.Locations.First().ResultFile.Uri.ToString());
        }
Example #13
0
        public void CppCheckError_CanParseErrorWithMultipleLocations()
        {
            string errorXml = exampleErrorXmlOpen + " <location file=\"" + ExampleFileName + "\" line=\"42\" />  <location file=\"" + ExampleFileName2 + "\" line=\"1729\" /> " + exampleErrorClose;

            using (XmlReader xml = Utilities.CreateXmlReaderFromString(errorXml))
            {
                CppCheckError uut = Parse(xml);
                AssertOuterPropertiesAreExampleError(uut);
                uut.Locations.Should().Equal(new[] {
                    new CppCheckLocation(ExampleFileName, 42),
                    new CppCheckLocation(ExampleFileName2, 1729)
                });
            }
        }
        private void ProcessCppCheckLog(XmlReader reader, IResultLogWriter issueWriter)
        {
            reader.ReadStartElement(_strings.Results);

            if (!Ref.Equal(reader.LocalName, _strings.CppCheck))
            {
                throw reader.CreateException(SarifResources.CppCheckCppCheckElementMissing);
            }

            string version = reader.GetAttribute(_strings.Version);

            if (String.IsNullOrWhiteSpace(version))
            {
                throw reader.CreateException(SarifResources.CppCheckCppCheckElementMissing);
            }

            issueWriter.WriteToolAndRunInfo(new ToolInfo
            {
                Name    = "CppCheck",
                Version = version,
            }, null);

            reader.Skip(); // <cppcheck />

            if (!Ref.Equal(reader.LocalName, _strings.Errors))
            {
                throw reader.CreateException(SarifResources.CppCheckErrorsElementMissing);
            }

            if (reader.IsEmptyElement)
            {
                reader.Skip(); // <errors />
            }
            else
            {
                int errorsDepth = reader.Depth;
                reader.Read(); // <errors>
                while (reader.Depth > errorsDepth)
                {
                    var parsedError = CppCheckError.Parse(reader, _strings);
                    issueWriter.WriteResult(parsedError.ToSarifIssue());
                }

                reader.ReadEndElement(); // </errors>
            }

            reader.ReadEndElement(); // </results>
        }
Example #15
0
        public void CppCheckError_ErrorWithMultipleLocationsFillsOutExecutionFlow()
        {
            Result result = new CppCheckError("id", "message", "verbose", "my fancy severity", ImmutableArray.Create(
                                                  new CppCheckLocation("foo.cpp", 1234),
                                                  new CppCheckLocation("bar.cpp", 5678)
                                                  )).ToSarifIssue();

            result.Locations.Should().Equal(new[] { new Location {
                                                        ResultFile = new[]
                                                        {
                                                            new PhysicalLocationComponent
                                                            {
                                                                Uri      = new Uri("bar.cpp", UriKind.RelativeOrAbsolute),
                                                                MimeType = MimeType.Cpp,
                                                                Region   = new Region {
                                                                    StartLine = 5678
                                                                }
                                                            }
                                                        }
                                                    } });

            Assert.AreEqual(1, result.ExecutionFlows.Count);
            result.ExecutionFlows[0].Should().Equal(new[]
            {
                new AnnotatedCodeLocation {
                    PhysicalLocation = new[] { new PhysicalLocationComponent {
                                                   Uri      = new Uri("foo.cpp", UriKind.RelativeOrAbsolute),
                                                   MimeType = MimeType.Cpp,
                                                   Region   = new Region {
                                                       StartLine = 1234
                                                   }
                                               } }
                },
                new AnnotatedCodeLocation {
                    PhysicalLocation = new[] { new PhysicalLocationComponent {
                                                   Uri      = new Uri("bar.cpp", UriKind.RelativeOrAbsolute),
                                                   MimeType = MimeType.Cpp,
                                                   Region   = new Region {
                                                       StartLine = 5678
                                                   }
                                               } }
                }
            });
        }
Example #16
0
        public void CppCheckError_ErrorWithSingleLocationIsConvertedToSarifIssue()
        {
            Result result = new CppCheckError("id", "message", "verbose", "my fancy severity", ImmutableArray.Create(
                                                  new CppCheckLocation("foo.cpp", 1234)
                                                  )).ToSarifIssue();

            Assert.AreEqual("id", result.RuleId);
            Assert.AreEqual("verbose", result.Message);
            result.PropertyNames.Count.Should().Be(1);
            result.GetProperty("Severity").Should().Be("my fancy severity");
            result.Locations.SequenceEqual(new[] { new Location {
                                                       ResultFile = new PhysicalLocation
                                                       {
                                                           Uri    = new Uri("foo.cpp", UriKind.RelativeOrAbsolute),
                                                           Region = new Region {
                                                               StartLine = 1234
                                                           }
                                                       }
                                                   } }, Location.ValueComparer).Should().BeTrue();
            Assert.IsNull(result.CodeFlows);
        }
Example #17
0
        public void CppCheckError_ErrorWithMultipleLocationsFillsOutCodeFlow()
        {
            Result result = new CppCheckError("id", "message", "verbose", "my fancy severity", ImmutableArray.Create(
                                                  new CppCheckLocation("foo.cpp", 1234),
                                                  new CppCheckLocation("bar.cpp", 5678)
                                                  )).ToSarifIssue();

            result.Locations.SequenceEqual(new[] { new Location {
                                                       ResultFile = new PhysicalLocation
                                                       {
                                                           Uri    = new Uri("bar.cpp", UriKind.RelativeOrAbsolute),
                                                           Region = new Region {
                                                               StartLine = 5678
                                                           }
                                                       }
                                                   } }, Location.ValueComparer).Should().BeTrue();

            Assert.AreEqual(1, result.CodeFlows.Count);
            result.CodeFlows.First().Locations.SequenceEqual(new[]
            {
                new AnnotatedCodeLocation {
                    PhysicalLocation = new PhysicalLocation {
                        Uri    = new Uri("foo.cpp", UriKind.RelativeOrAbsolute),
                        Region = new Region {
                            StartLine = 1234
                        },
                    },
                    Importance = AnnotatedCodeLocationImportance.Essential
                },
                new AnnotatedCodeLocation {
                    PhysicalLocation = new PhysicalLocation {
                        Uri    = new Uri("bar.cpp", UriKind.RelativeOrAbsolute),
                        Region = new Region {
                            StartLine = 5678
                        }
                    },
                    Importance = AnnotatedCodeLocationImportance.Essential
                }
            }, AnnotatedCodeLocation.ValueComparer).Should().BeTrue();
        }
Example #18
0
 private static CppCheckError Parse(XmlReader xml)
 {
     return(CppCheckError.Parse(xml, new CppCheckStrings(xml.NameTable)));
 }
Example #19
0
 public void CppCheckError_MinimalErrorCanBeConvertedToSarifIssue()
 {
     Result result = new CppCheckError("id", "message", "verbose", "style", _dummyLocations)
         .ToSarifIssue();
     Assert.AreEqual("id", result.RuleId);
     Assert.AreEqual("verbose", result.Message);
     result.PropertyNames.Count.Should().Be(1);
     result.GetProperty("Severity").Should().Be("style");
     Assert.AreEqual("file.cpp", result.Locations.First().ResultFile.Uri.ToString());
 }
Example #20
0
 public void CppCheckError_PassesThroughConstructorParameters()
 {
     var uut = new CppCheckError("id", "message", "verbose", "style", _dummyLocations);
     AssertOuterPropertiesAreExampleError(uut);
     Assert.AreEqual(_dummyLocations, uut.Locations);
 }
Example #21
0
 private static void AssertOuterPropertiesAreExampleError(CppCheckError uut)
 {
     Assert.AreEqual("id", uut.Id);
     Assert.AreEqual("message", uut.Message);
     Assert.AreEqual("verbose", uut.VerboseMessage);
     Assert.AreEqual("style", uut.Severity);
 }
Example #22
0
 public void CppCheckError_ErrorWithSingleLocationIsConvertedToSarifIssue()
 {
     Result result = new CppCheckError("id", "message", "verbose", "my fancy severity", ImmutableArray.Create(
         new CppCheckLocation("foo.cpp", 1234)
         )).ToSarifIssue();
     Assert.AreEqual("id", result.RuleId);
     Assert.AreEqual("verbose", result.Message);
     result.PropertyNames.Count.Should().Be(1);
     result.GetProperty("Severity").Should().Be("my fancy severity");
     result.Locations.SequenceEqual(new[] { new Location {
             ResultFile = new PhysicalLocation
             {
                 Uri = new Uri("foo.cpp", UriKind.RelativeOrAbsolute),
                 Region = new Region { StartLine = 1234 }
             }
         }
     }, Location.ValueComparer).Should().BeTrue();
     Assert.IsNull(result.CodeFlows);
 }
Example #23
0
        private void ProcessCppCheckLog(XmlReader reader, IResultLogWriter output, OptionallyEmittedData dataToInsert)
        {
            reader.ReadStartElement(_strings.Results);

            if (!StringReference.AreEqual(reader.LocalName, _strings.CppCheck))
            {
                throw reader.CreateException(ConverterResources.CppCheckCppCheckElementMissing);
            }

            string version = reader.GetAttribute(_strings.Version);

            if (version != null && !version.IsSemanticVersioningCompatible())
            {
                // This logic only fixes up simple cases, such as being passed
                // 1.66, where Semantic Versioning 2.0 requires 1.66.0. Also
                // strips Revision member if passed a complete .NET version.
                Version dotNetVersion;
                if (Version.TryParse(version, out dotNetVersion))
                {
                    version =
                        Math.Max(0, dotNetVersion.Major) + "." +
                        Math.Max(0, dotNetVersion.Minor) + "." +
                        Math.Max(0, dotNetVersion.Build);
                }
            }

            if (string.IsNullOrWhiteSpace(version))
            {
                throw reader.CreateException(ConverterResources.CppCheckCppCheckElementMissing);
            }

            reader.Skip(); // <cppcheck />

            if (!StringReference.AreEqual(reader.LocalName, _strings.Errors))
            {
                throw reader.CreateException(ConverterResources.CppCheckErrorsElementMissing);
            }

            var results = new List <Result>();

            if (reader.IsEmptyElement)
            {
                reader.Skip(); // <errors />
            }
            else
            {
                int errorsDepth = reader.Depth;
                reader.Read(); // <errors>

                while (reader.Depth > errorsDepth)
                {
                    var parsedError = CppCheckError.Parse(reader, _strings);
                    results.Add(parsedError.ToSarifIssue());
                }

                reader.ReadEndElement(); // </errors>
            }

            reader.ReadEndElement(); // </results>

            var run = new Run()
            {
                Tool = new Tool {
                    Driver = new ToolComponent {
                        Name = ToolName, Version = version
                    }
                },
            };

            PersistResults(output, results, run);
        }
        private void ProcessCppCheckLog(XmlReader reader, IResultLogWriter output, OptionallyEmittedData dataToInsert)
        {
            reader.ReadStartElement(_strings.Results);

            if (!StringReference.AreEqual(reader.LocalName, _strings.CppCheck))
            {
                throw reader.CreateException(ConverterResources.CppCheckCppCheckElementMissing);
            }

            string version = reader.GetAttribute(_strings.Version);

            if (version != null && !version.IsSemanticVersioningCompatible())
            {
                // This logic only fixes up simple cases, such as being passed
                // 1.66, where Semantic Versioning 2.0 requires 1.66.0. Also
                // strips Revision member if passed a complete .NET version.
                Version dotNetVersion;
                if (Version.TryParse(version, out dotNetVersion))
                {
                    version =
                        Math.Max(0, dotNetVersion.Major) + "." +
                        Math.Max(0, dotNetVersion.Minor) + "." +
                        Math.Max(0, dotNetVersion.Build);
                }
            }

            if (String.IsNullOrWhiteSpace(version))
            {
                throw reader.CreateException(ConverterResources.CppCheckCppCheckElementMissing);
            }

            reader.Skip(); // <cppcheck />

            if (!StringReference.AreEqual(reader.LocalName, _strings.Errors))
            {
                throw reader.CreateException(ConverterResources.CppCheckErrorsElementMissing);
            }

            var results = new List <Result>();

            if (reader.IsEmptyElement)
            {
                reader.Skip(); // <errors />
            }
            else
            {
                int errorsDepth = reader.Depth;
                reader.Read(); // <errors>

                while (reader.Depth > errorsDepth)
                {
                    var parsedError = CppCheckError.Parse(reader, _strings);
                    results.Add(parsedError.ToSarifIssue());
                }

                reader.ReadEndElement(); // </errors>
            }

            reader.ReadEndElement(); // </results>

            var tool = new Tool
            {
                Name    = "CppCheck",
                Version = version,
            };

            var fileInfoFactory = new FileInfoFactory(uri => MimeType.Cpp, dataToInsert);
            Dictionary <string, FileData> fileDictionary = fileInfoFactory.Create(results);

            var run = new Run()
            {
                Tool = tool
            };

            output.Initialize(run);

            if (fileDictionary != null && fileDictionary.Count > 0)
            {
                output.WriteFiles(fileDictionary);
            }

            output.OpenResults();
            output.WriteResults(results);
            output.CloseResults();
        }
Example #25
0
 public void CppCheckError_DoesNotEmitShortMessageWhenVerboseMessageIsTheSame()
 {
     Result result = new CppCheckError("id", "message", "message", "style", _dummyLocations)
         .ToSarifIssue();
     Assert.AreEqual("message", result.Message);
 }