in DataExtraction/SourceGraphExtractionUtils/GraphDataExtractor.cs [968:1068]
public void ExtractAllSamplesFromSemanticModel(object sender, SemanticModel semanticModel)
{
var tree = semanticModel.SyntaxTree;
if (_fileResumeList.AddIfNotContained(tree.FilePath))
{
int numExpressionsExtracted = 0;
try
{
if (tree.FilePath.Contains("TemporaryGeneratedFile") || tree.FilePath.EndsWith(".Generated.cs"))
{
Console.WriteLine($"Ignoring file {tree.FilePath}.");
return; // TODO: Remove any temp files in obj
}
//Console.WriteLine($"Writing out samples for {tree.FilePath}");
var allTokens = tree.GetRoot().DescendantTokens().Where(t => t.Text.Length > 0).ToArray();
var addedTypes = new HashSet<ITypeSymbol>();
foreach (var syntaxNode in tree.GetRoot().DescendantNodes())
{
var symbol = RoslynUtils.GetReferenceSymbol(syntaxNode, semanticModel);
if (RoslynUtils.GetTypeSymbol(symbol, out var typeSymbol) && addedTypes.Add(typeSymbol))
{
TypeHierarchy.Add(typeSymbol);
}
}
SourceGraph sourceGraph = null;
foreach (var simpleExpressionNode in SimpleExpressionIdentifier.GetSimpleExpressions(semanticModel))
{
try
{
if (sourceGraph == null)
{
sourceGraph = ExtractSourceGraph(semanticModel, allTokens); // Compute the graph if we haven't done so yet...
//sourceGraph.ToDotFile(Path.GetFileName(tree.FilePath) + ".graph.dot", sourceGraph.GetNodeNumberer(), new Dictionary<SyntaxNodeOrToken, string>(), false);
}
}
catch (Exception e)
{
Console.WriteLine($"Error while extracting graph. Aborting file. Cause: {e.Message}");
return;
}
var (contextGraph, holeDummyNode, tokenBeforeHole, variableNodesInScope) = ExtractContextInformationForTargetNode(semanticModel, allTokens, sourceGraph, simpleExpressionNode);
var allVariablesInExpression = RoslynUtils.GetUsedVariableSymbols(semanticModel, simpleExpressionNode, onlyLocalFileVariables: false);
if (!allVariablesInExpression.Any())
{
// We filter nodes that do not contain any variables.
continue;
}
bool isNotInCurrentFile(ISymbol symbol)
{
var symbolLocation = symbol.Locations.First();
return symbolLocation.IsInMetadata || symbolLocation.SourceTree.FilePath != tree.FilePath;
};
if (allVariablesInExpression.Count == 0 || allVariablesInExpression.Any(isNotInCurrentFile))
{
continue;
}
var productions = TurnIntoProductions(simpleExpressionNode).ToList();
// Debug:
/*
Console.WriteLine($"Simple Expression: {simpleExpressionNode}");
foreach (var (nonterm, productionRhs) in productions)
{
var productionStrings =
productionRhs.Select(t => (t.SymbolKind == ExpansionSymbolKind.Token) ? t.Label.ToString() : t.SymbolKind.ToString()
+ "[" + t.Id + "]"
+ ((t.Label != null) ? "(" + t.Label + ")" : ""));
Console.WriteLine($" {nonterm.SymbolKind.ToString()}[{nonterm.Id}] --> {String.Join(" ", productionStrings)}");
}
*/
var sample = new HoleContextInformation(_repositoryRootPath, simpleExpressionNode, contextGraph, holeDummyNode, tokenBeforeHole, variableNodesInScope, productions);
WriteSample(sample);
numExpressionsExtracted++;
// Optionally invoke additional extractor.
_additionalExtractors?.Invoke(simpleExpressionNode);
}
if (numExpressionsExtracted > 0)
{
_log.LogMessage($"Extracted {numExpressionsExtracted} expressions from {tree.FilePath}.");
}
}
catch (Exception e)
{
Console.WriteLine($"Exception when extracting data from file: {e.Message}: {e.StackTrace}");
_log.LogMessage($"Error{e.Message}: {e.StackTrace} while extracting. Managed to extract {numExpressionsExtracted} expressions from {tree.FilePath}.");
}
if (_rng.NextDouble() > .9)
{
// Once in a while save they type hierarchy
TypeHierarchy.SaveTypeHierarchy(_typeHierarchyOutputFilename);
}
}
}