public void TestCreateSrcMLArchiveFtF() { using (Archive srcmlArchive = new Archive(), srcmlArchive2 = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlUnit.SetUnitFilename(Path.Combine(TestInputPath, "input.cpp")); srcmlArchive.SetArchiveLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.SetOutputFile("output"); srcmlArchive.ArchivePack(); srcmlUnit.SetUnitFilename(Path.Combine(TestInputPath, "input2.cpp")); srcmlArchive2.SetArchiveLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX); srcmlArchive2.AddUnit(srcmlUnit); srcmlArchive2.SetOutputFile("output"); srcmlArchive2.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); IntPtr structPtr2 = srcmlArchive2.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); structArrayPtr.Add(structPtr2); try { Assert.True(LibSrcMLRunner.SrcmlCreateArchiveFtF(structArrayPtr.ToArray(), structArrayPtr.Count()) == IntPtr.Zero); } catch (SrcMLException e) { Console.WriteLine(e.Message); } } { Assert.True(File.Exists("output0.cpp.xml")); SrcMLFile srcFile = new SrcMLFile("output0.cpp.xml"); Assert.IsNotNull(srcFile); var files = srcFile.FileUnits.ToList(); Assert.AreEqual(1, files.Count()); string file = Path.Combine(TestInputPath, "input.cpp"); var f1 = (from ele in files where ele.Attribute("filename").Value == file select ele); Assert.AreEqual(file, f1.FirstOrDefault().Attribute("filename").Value); } { Assert.True(File.Exists("output1.cpp.xml")); SrcMLFile srcFile = new SrcMLFile("output1.cpp.xml"); Assert.IsNotNull(srcFile); var files = srcFile.FileUnits.ToList(); Assert.AreEqual(1, files.Count()); string file1 = Path.Combine(TestInputPath, "input2.cpp"); var f2 = (from ele in files where ele.Attribute("filename").Value == file1 select ele); Assert.AreEqual(file1, f2.FirstOrDefault().Attribute("filename").Value); } } }
/// <summary> /// Generates srcML from the given string of source code /// </summary> /// <param name="sources">list of strings of code (each string is a whole file)</param> /// <param name="unitFilename">What name to give the unit</param> /// <param name="language">The language</param> /// <param name="namespaceArguments">additional arguments</param> /// <param name="omitXmlDeclaration">If true, the XML header is omitted</param> /// <returns>The srcML</returns> public ICollection<string> GenerateSrcMLFromStrings(ICollection<string> sources, ICollection<string> unitFilename, Language language, ICollection<UInt32> namespaceArguments, bool omitXmlDeclaration) { Contract.Requires(sources.Count == unitFilename.Count); try { using (Archive srcmlArchive = new Archive()) { srcmlArchive.SetArchiveLanguage(LanguageEnumDictionary[language]); srcmlArchive.EnableOption(GenerateArguments(namespaceArguments)); var sourceandfile = sources.Zip(unitFilename, (src, fle) => new { source = src, file = fle }); foreach (var pair in sourceandfile) { using (Unit srcmlUnit = new Unit()) { if (omitXmlDeclaration) { srcmlArchive.DisableOption(LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_XML_DECL); } srcmlUnit.SetUnitBuffer(pair.source); srcmlUnit.SetUnitFilename(pair.file); srcmlUnit.SetUnitLanguage(LanguageEnumDictionary[language]); srcmlArchive.AddUnit(srcmlUnit); } } return RunSrcML(srcmlArchive, LibSrcMLRunner.SrcmlCreateArchiveMtM); } } catch (Exception e) { throw new SrcMLException(e.Message, e); } }
/// <summary> /// Generates srcML from a file /// </summary> /// <param name="fileNames">An enumerable of filenames</param> /// <param name="xmlFileName">the output file name</param> /// <param name="language">The language to use</param> /// <param name="namespaceArguments">additional arguments</param> /// <param name="extensionMapping">an extension mapping</param> public void GenerateSrcMLFromFiles(ICollection<string> fileNames, string xmlFileName, Language language, ICollection<UInt32> namespaceArguments, IDictionary<string, Language> extensionMapping) { UInt32 arguments = GenerateArguments(namespaceArguments); try { using (Archive srcmlArchive = new Archive()) { if (Convert.ToBoolean(extensionMapping.Count())) { srcmlArchive.RegisterFileExtension(extensionMapping.ElementAt(0).Key, extensionMapping.ElementAt(0).Value.ToString()); } foreach (string file in fileNames) { using (Unit srcmlUnit = new Unit()) { srcmlUnit.SetUnitFilename(file); srcmlUnit.SetUnitLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX); srcmlArchive.AddUnit(srcmlUnit); } } srcmlArchive.SetOutputFile(xmlFileName); RunSrcML(srcmlArchive, LibSrcMLRunner.SrcmlCreateArchiveFtF); } } catch (Exception e) { throw new SrcMLException(e.Message, e); } }
/// <summary> /// Add a unit to the archive /// </summary> /// <param name="unit">a srcmlUnit allocated via ABB.SrcML.Unit class</param> public void AddUnit(Unit unit) { ++unit.referencecount; units.Add(unit); }
public void TestCreateSrcMLArchiveFtM() { IntPtr s = new IntPtr(0); List<String> documents = new List<String>(); using (Archive srcmlArchive = new Archive(), srcmlArchive2 = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlUnit.SetUnitFilename(Path.Combine(TestInputPath, "input.cpp")); srcmlUnit.SetUnitLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.ArchivePack(); srcmlUnit.SetUnitFilename(Path.Combine(TestInputPath, "input2.cpp")); srcmlUnit.SetUnitLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX); srcmlArchive2.AddUnit(srcmlUnit); srcmlArchive2.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); IntPtr structPtr2 = srcmlArchive2.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); structArrayPtr.Add(structPtr2); try { s = LibSrcMLRunner.SrcmlCreateArchiveFtM(structArrayPtr.ToArray(), structArrayPtr.Count()); } catch (Exception e) { throw new SrcMLException(e.Message, e); } for (int i = 0; i < 2; ++i) { IntPtr docptr = Marshal.ReadIntPtr(s); String docstr = Marshal.PtrToStringAnsi(docptr); Marshal.FreeHGlobal(docptr); documents.Add(docstr); s += Marshal.SizeOf(typeof(IntPtr)); } } Assert.False(String.IsNullOrEmpty(documents.ElementAt(0))); XDocument doc = XDocument.Parse(documents.ElementAt(0)); var units = from unit in doc.Descendants(XName.Get("unit", "http://www.srcML.org/srcML/src")) where unit.Attribute("filename") != null select unit; string file = Path.Combine(TestInputPath, "input.cpp"); ; var f1 = (from ele in units where ele.Attribute("filename").Value == file select ele); Assert.AreEqual(file, f1.FirstOrDefault().Attribute("filename").Value); Assert.False(String.IsNullOrEmpty(documents.ElementAt(1))); XDocument doc2 = XDocument.Parse(documents.ElementAt(1)); var units2 = from unit in doc2.Descendants(XName.Get("unit", "http://www.srcML.org/srcML/src")) where unit.Attribute("filename") != null select unit; string file2 = Path.Combine(TestInputPath, "input2.cpp"); ; var f2 = (from ele in units2 where ele.Attribute("filename").Value == file2 select ele); Assert.AreEqual(file2, f2.FirstOrDefault().Attribute("filename").Value); } }
public void TestUnitUnparseSetEol() { using (Archive srcmlArchive = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlUnit.UnparseSetEol(50); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); Assert.IsTrue(LibSrcMLRunner.TestUnitUnparseSetEol(structArrayPtr.ToArray())); } } }
public void TestUnitSetLanguage() { using (Archive srcmlArchive = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlUnit.SetUnitLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); Assert.IsTrue(LibSrcMLRunner.TestUnitSetLanguage(structArrayPtr.ToArray())); } } }
public void TestUnitSetFilename() { using (Archive srcmlArchive = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlUnit.SetUnitFilename("Bleep.cpp"); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); Assert.IsTrue(LibSrcMLRunner.TestUnitSetFilename(structArrayPtr.ToArray())); } } }
public void TestArchiveRegisterMacro() { using (Archive srcmlArchive = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlArchive.RegisterMacro("Token", "type"); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); Assert.IsTrue(LibSrcMLRunner.TestArchiveRegisterMacro(structArrayPtr.ToArray())); } } }
public void TestArchiveSetProcessingInstruction() { using (Archive srcmlArchive = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlArchive.SetProcessingInstruction("hpp", "data"); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); Assert.IsTrue(LibSrcMLRunner.TestArchiveSetProcessingInstruction(structArrayPtr.ToArray())); } } }
public void TestArchiveRegisterNamespace() { using (Archive srcmlArchive = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlArchive.RegisterNamespace("abb", "www.abb.com"); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); Assert.IsTrue(LibSrcMLRunner.TestArchiveRegisterNamespace(structArrayPtr.ToArray())); } } }
public void TestArchiveDisableOption() { using (Archive srcmlArchive = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlArchive.SetOptions(LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_LITERAL); srcmlArchive.DisableOption(LibSrcMLRunner.SrcMLOptions.SRCML_OPTION_LITERAL); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); Assert.IsTrue(LibSrcMLRunner.TestArchiveDisableOption(structArrayPtr.ToArray())); } } }
public void TestArchiveSetUrl() { using (Archive srcmlArchive = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlArchive.SetArchiveUrl("http://www.srcml.org/"); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); Assert.IsTrue(LibSrcMLRunner.TestArchiveSetUrl(structArrayPtr.ToArray())); } } }
public void TestArchiveXmlEncoding() { using (Archive srcmlArchive = new Archive()) { using (Unit srcmlUnit = new Unit()) { srcmlArchive.SetArchiveXmlEncoding("ISO-8859-1"); srcmlArchive.AddUnit(srcmlUnit); srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); Assert.IsTrue(LibSrcMLRunner.TestArchiveSetXmlEncoding(structArrayPtr.ToArray())); } } }
public void TestCreateSrcMLArchiveMtM() { using (Archive srcmlArchive = new Archive()) { List<String> bufferList = new List<String>(); List<String> fileList = new List<String>(); String str = "int main(){int c; c = 0; ++c;}"; String str2 = "int foo(){int c; c = 0; ++c;}"; bufferList.Add(str); bufferList.Add(str2); fileList.Add("input.cpp"); fileList.Add("input2.cpp"); var buffandfile = bufferList.Zip(fileList, (b, f) => new { buf = b, file = f }); foreach (var pair in buffandfile) { using (Unit srcmlUnit = new Unit()) { srcmlUnit.SetUnitBuffer(pair.buf); srcmlUnit.SetUnitFilename(pair.file); srcmlUnit.SetUnitLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX); srcmlArchive.AddUnit(srcmlUnit); } } srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); IntPtr s = new IntPtr(0); try { s = LibSrcMLRunner.SrcmlCreateArchiveMtM(structArrayPtr.ToArray(), structArrayPtr.Count()); } catch (Exception e) { Console.WriteLine("EXCEPTION: {0}", e.Message); Assert.True(false); } List<String> documents = new List<String>(); for (int i = 0; i < 1; ++i) { IntPtr docptr = Marshal.ReadIntPtr(s); String docstr = Marshal.PtrToStringAnsi(docptr); Marshal.FreeHGlobal(docptr); documents.Add(docstr); s += Marshal.SizeOf(typeof(IntPtr)); } Assert.False(String.IsNullOrEmpty(documents.ElementAt(0))); XDocument doc = XDocument.Parse(documents.ElementAt(0)); var units = from unit in doc.Descendants(XName.Get("unit", "http://www.srcML.org/srcML/src")) where unit.Attribute("filename") != null select unit; string file = "input.cpp"; var f1 = (from ele in units where ele.Attribute("filename").Value == file select ele); Assert.AreEqual("input.cpp", f1.FirstOrDefault().Attribute("filename").Value); string file2 = "input2.cpp"; var f2 = (from ele in units where ele.Attribute("filename").Value == file2 select ele); Assert.AreEqual("input2.cpp", f2.FirstOrDefault().Attribute("filename").Value); Console.WriteLine(s); } }
public void TestCreateSrcMLArchiveMtF() { using (Archive srcmlArchive = new Archive()) { List<String> bufferList = new List<String>(); List<String> fileList = new List<String>(); String str = "int main(){int c; c = 0; ++c;}"; String str2 = "int foo(){int c; c = 0; ++c;}"; fileList.Add("input.cpp"); fileList.Add("input2.cpp"); bufferList.Add(str); bufferList.Add(str2); var buffandfile = bufferList.Zip(fileList, (b, f) => new { buf = b, file = f }); foreach (var pair in buffandfile) { using (Unit srcmlUnit = new Unit()) { srcmlUnit.SetUnitBuffer(pair.buf); srcmlUnit.SetUnitFilename(pair.file); srcmlArchive.SetArchiveLanguage(LibSrcMLRunner.SrcMLLanguages.SRCML_LANGUAGE_CXX); srcmlArchive.AddUnit(srcmlUnit); } } srcmlArchive.SetOutputFile("output"); srcmlArchive.ArchivePack(); IntPtr structPtr = srcmlArchive.GetPtrToStruct(); List<IntPtr> structArrayPtr = new List<IntPtr>(); structArrayPtr.Add(structPtr); Assert.True(LibSrcMLRunner.SrcmlCreateArchiveMtF(structArrayPtr.ToArray(), structArrayPtr.Count()) == IntPtr.Zero); Assert.True(File.Exists("output0.cpp.xml")); SrcMLFile srcFile = new SrcMLFile("output0.cpp.xml"); Assert.IsNotNull(srcFile); var files = srcFile.FileUnits.ToList(); Assert.AreEqual(2, files.Count()); string file = "input.cpp"; var f1 = (from ele in files where ele.Attribute("filename").Value == file select ele); Assert.AreEqual("input.cpp", f1.FirstOrDefault().Attribute("filename").Value); string file2 = "input2.cpp"; var f2 = (from ele in files where ele.Attribute("filename").Value == file2 select ele); Assert.AreEqual(file2, f2.FirstOrDefault().Attribute("filename").Value); } }