/// <summary> /// Instruments production code for measuring code coverage and returns the modifieid code. /// </summary> /// <param name="support">A support to measure coverage.</param> /// <param name="fileInfo">A <c>FileInfo</c> instance to be instrumented.</param> /// <returns>The modified production code.</returns> public string InstrumentStatementAndPredicate( LanguageSupport support, FileInfo fileInfo) { var root = support.CodeToXml.GenerateFromFile(fileInfo.FullName); var inserter = support.AstTransformer; var fileId = RegisterFile(fileInfo); // ステートメントを挿入できるようにブロックを補う inserter.SupplementBlock(root); // switch文を正しく測定できるようにdefault節を追加する inserter.SupplementDefaultCase(root); var analyzer = support.AstAnalyzer; var funcs = analyzer.FindFunctions(root); foreach (var func in funcs) { var funcName = analyzer.GetFunctionName(func); var funcId = RegisterFunction(fileId, funcName, CodeRange.Locate(func)); InstrumentStatement(support, fileId, funcId, inserter, func); InstrumentBranch(support, fileId, funcId, inserter, func); } // Add import for logging executed items inserter.InsertImport(root); return(support.XmlToCode.Generate(root)); }
private static void ConvertIndiciesToRange( CstNode node, string code, int inclusiveStart, int exclusiveEnd, CodeRange range) { var newRange = CodeRange.ConvertFromIndicies(code, inclusiveStart, exclusiveEnd); var newInclusiveStart = char.IsWhiteSpace(code[inclusiveStart - 1]) ? inclusiveStart - 1 : inclusiveStart; var newExclusiveEnd = char.IsWhiteSpace(code[exclusiveEnd]) ? exclusiveEnd + 1 : exclusiveEnd; var elem = CodeRange.ConvertFromIndiciesSkippingWhitespaces( code, ref newInclusiveStart, ref newExclusiveEnd) .FindInnermostNode(node); Assert.That(CodeRange.Locate(elem), Is.EqualTo(newRange)); newRange.ConvertToIndicies(code, out newInclusiveStart, out newExclusiveEnd); Assert.That(newRange, Is.EqualTo(range)); Assert.That(newInclusiveStart, Is.EqualTo(inclusiveStart)); Assert.That(newExclusiveEnd, Is.EqualTo(exclusiveEnd)); var scode = new StructuredCode(code); Assert.That( CodeRange.ConvertFromIndicies(scode, inclusiveStart, exclusiveEnd), Is.EqualTo(range)); newRange.ConvertToIndicies(scode, out newInclusiveStart, out newExclusiveEnd); Assert.That(newInclusiveStart, Is.EqualTo(inclusiveStart)); Assert.That(newExclusiveEnd, Is.EqualTo(exclusiveEnd)); }
private void InstrumentBranch( LanguageSupport support, long fileId, long funcId, AstTransformer inserter, XElement func) { var branches = support.AstAnalyzer.FindBranches(func); foreach (var branch in branches) { var position = CodeRange.Locate(branch); var branchId = RegisterBranch(fileId, funcId, position); inserter.InsertPredicate(branch, branchId, ElementType.Decision); } }
private void InstrumentStatement( LanguageSupport support, long fileId, long funcId, AstTransformer inserter, XElement func) { var stmts = support.AstAnalyzer.FindStatements(func); foreach (var stmt in stmts) { var position = CodeRange.Locate(stmt); var stmtId = RegisterStatement(fileId, funcId, position); inserter.InsertStatementBefore(stmt, stmtId, CodeTransformer.Done, ElementType.Statement); } }
public static IEnumerable <Tuple <int, LocationInfo> > GetSimilarElements( CstGenerator cstGenerator, IEnumerable <LocationInfo> locations, IEnumerable <FileInfo> targets, int range = 5, bool inner = true, bool outer = true) { var path2Ast = new Dictionary <string, CstNode>(); var paths = locations.Select(l => l.FileInfo).Concat(targets) .Select(f => f.FullName) .ToHashSet(); foreach (var path in paths) { path2Ast.Add(path, cstGenerator.GenerateTreeFromCodePath(path)); } var elements = new List <CstNode>(); foreach (var location in locations) { var root = path2Ast[location.FileInfo.FullName]; elements.Add(location.CodeRange.FindOutermostNode(root)); } var names = AdoptNodeNames(elements); var candidates = new Dictionary <string, IEnumerable <CstNode> >(); foreach (var keyAndValue in path2Ast) { candidates.Add( keyAndValue.Key, keyAndValue.Value.Descendants() .Where(e => names.Contains(e.Name))); } var commonKeys = elements.GetCommonKeys(range, true, true); return(candidates.SelectMany( kv => { var fileInfo = new FileInfo(kv.Key); return kv.Value.Select( e => Tuple.Create( e.GetSurroundingKeys(range, inner, outer) .Count(commonKeys.Contains), e)) .Select( t => Tuple.Create( t.Item1, new LocationInfo { FileInfo = fileInfo, CodeRange = CodeRange.Locate(t.Item2), })); }) .OrderByDescending(t => t.Item1)); }
public Dictionary <CodeRange, CstNode> ExtractRange2Elements(CstNode ast) { var elements = ExtractElements(ast); // Elements having the same range can appear so can't use ToDictiornary var ret = new Dictionary <CodeRange, CstNode>(); foreach (var element in elements) { var range = CodeRange.Locate(element); ret[range] = element; } return(ret); }
public void FindElement() { var cst = new JavaCstGeneratorUsingAntlr3().GenerateTreeFromCodeText(@" public class Hello { public static void main(String[] args) { System.out.println(1); } }"); var stmt = cst.Descendants("statement").First(); var pos = CodeRange.Locate(stmt); Assert.That(pos.FindInnermostNode(cst), Is.EqualTo(stmt)); Assert.That(pos.FindOutermostNode(cst), Is.EqualTo(stmt.Parent)); }
private static CodeRange ConvertRangeToIndicies( string code, CstNode node, out int inclusiveStart, out int exclusiveEnd) { var range = CodeRange.Locate(node); range.ConvertToIndicies(code, out inclusiveStart, out exclusiveEnd); Assert.That( code.Substring(inclusiveStart, exclusiveEnd - inclusiveStart), Is.EqualTo(node.Code.Trim())); range.ConvertToIndicies(new StructuredCode(code), out inclusiveStart, out exclusiveEnd); Assert.That( code.Substring(inclusiveStart, exclusiveEnd - inclusiveStart), Is.EqualTo(node.Code.Trim())); return(range); }
public void Locate() { var code = @" public class Hello { public static void main(String[] args) { } }"; var xml = new JavaCstGeneratorUsingAntlr3().GenerateTreeFromCodeText(code); var id = xml.Descendants("IDENTIFIER").First(); var range = CodeRange.Locate(id); Assert.That(range.GetCodeFragment(code), Is.EqualTo(id.TokenText)); Assert.That( range.GetCodeFragment(new StructuredCode(code)), Is.EqualTo(id.TokenText)); }
private void markToolStripMenuItem_Click(object sender, EventArgs e) { // Calculate the selected range var selectedRange = GetSelectedRange(); // Get the selected element // TODO: Should be use FindInnerElement or FindOuterElement ? var element = selectedRange.FindOuterElement(_activeFile.Ast); var range = CodeRange.Locate(element); if (!_activeFile.Range2Elements.ContainsKey(range)) { _activeFile.Range2Elements.Add(range, element); NormalizeAllRange2Element(); // To show good element lists DrawMarkers(_activeFile.Range2Elements, 0, 1); ListElements(_activeFile.Range2Elements, lvMarkedElements); } }
public TestCase(string relativePath, string name, XElement node) : this(relativePath, name, CodeRange.Locate(node)) { }
public CoverageElement(string relativePath, XElement node, Tagger tagger) { RelativePath = relativePath; Position = CodeRange.Locate(node); Qualifiers = tagger.Tag(node); }