public NotNullMethodInfo VisitMethod(SemanticModel model, MethodDeclarationSyntax method) { var symbol = model.GetDeclaredSymbol(method); if (symbol != null) { if (symbol.HasNotNull()) { if (symbol.ReturnsVoid || IsValueType(symbol.ReturnType)) { context.ReportDiagnostic(MainAnalyzer.CreateBadAttributeUsageError(method.GetLocation(), true)); return(null); } if (method.Body != null) { return(new NotNullMethodInfo(method.GetLocation(), symbol, method.Body)); } } else if (FindInheritedReference(model, symbol).HasNotNull()) { context.ReportDiagnostic(MainAnalyzer.CreateMissingAttribute(method.GetLocation(), symbol.ToString())); } } return(null); }
public NotNullMethodInfo VisitGetterMethod(SemanticModel model, IPropertySymbol propertySymbol, AccessorDeclarationSyntax getter) { var symbol = model.GetDeclaredSymbol(getter); if (symbol != null) { if (propertySymbol.HasNotNull() || symbol.HasNotNull()) { if (symbol.ReturnsVoid || IsValueType(symbol.ReturnType)) { context.ReportDiagnostic(MainAnalyzer.CreateBadAttributeUsageError(getter.GetLocation(), true)); return(null); } if (getter.Body != null) { return(new NotNullMethodInfo(getter.GetLocation(), symbol, getter.Body)); } } else if (FindInheritedReference(model, symbol).HasNotNull()) { context.ReportDiagnostic(MainAnalyzer.CreateMissingAttribute(getter.GetLocation(), symbol.ToString())); } } return(null); }
public NotNullExpressionBodyInfo VisitExpressionBody(SemanticModel model, IPropertySymbol propertySymbol, ExpressionSyntax expression, Location location) { if (propertySymbol.HasNotNull()) { if (!propertySymbol.Type.IsReferenceType) { context.ReportDiagnostic(MainAnalyzer.CreateBadAttributeUsageError(location, true)); return(null); } return(new NotNullExpressionBodyInfo(location, propertySymbol, expression)); } else if (FindInheritedReference(model, propertySymbol).HasNotNull()) { context.ReportDiagnostic(MainAnalyzer.CreateMissingAttribute(location, propertySymbol.ToString())); } return(null); }
GetTrackedMembers(SemanticModel model, ClassDeclarationSyntax type) { var trackedFields = new Dictionary <ISymbol, NotNullFieldInfo>(); var trackedMethods = new Dictionary <IMethodSymbol, NotNullMethodInfo>(); var trackedExpressionBodies = new Dictionary <ISymbol, NotNullExpressionBodyInfo>(); foreach (var member in type.ChildNodes() .Where(i => i is PropertyDeclarationSyntax || i is FieldDeclarationSyntax || i is MethodDeclarationSyntax)) { var property = member as PropertyDeclarationSyntax; if (property != null) { var symbol = model.GetDeclaredSymbol(property); if (symbol == null) { throw new ParseFailedException(member.GetLocation(), "Parse failed on: " + property); } if (!property.IsAutoProperty()) { if (property.ExpressionBody?.Expression != null) { var info = VisitExpressionBody(model, symbol, property.ExpressionBody.Expression, property.GetLocation()); if (info != null) { trackedExpressionBodies.Add(info.Symbol, info); } } else { var getter = property.AccessorList?.Accessors.FirstOrDefault(i => i.Kind() == SyntaxKind.GetAccessorDeclaration); if (getter != null) { var info = VisitGetterMethod(model, symbol, getter); if (info != null) { trackedMethods.Add(info.Symbol, info); } } } // Is computed property, so we don't need to worry about the ctor setting it. continue; } if (symbol.HasNotNull()) { if (symbol.SetMethod != null || IsValueType(symbol.Type)) { context.ReportDiagnostic(MainAnalyzer.CreateBadAttributeUsageError(member.GetLocation(), IsValueType(symbol.Type))); continue; } trackedFields.Add(symbol, new NotNullFieldInfo(member.GetLocation(), symbol, property.Initializer)); } else if (FindInheritedReference(model, symbol).HasNotNull()) { context.ReportDiagnostic(MainAnalyzer.CreateMissingAttribute(member.GetLocation(), symbol.ToString())); } } var field = member as FieldDeclarationSyntax; if (field != null) { var declaration = field.Declaration.Variables.First(); var symbol = model.GetDeclaredSymbol(declaration) as IFieldSymbol; if (symbol == null) { throw new ParseFailedException(member.GetLocation(), "Parse failed on: " + declaration); } if (symbol.HasNotNull()) { if (!symbol.IsReadOnlyOrConst() || IsValueType(symbol.Type)) { context.ReportDiagnostic(MainAnalyzer.CreateBadAttributeUsageError(member.GetLocation(), IsValueType(symbol.Type))); continue; } trackedFields.Add(symbol, new NotNullFieldInfo(member.GetLocation(), symbol, declaration.Initializer)); } } if (member is MethodDeclarationSyntax method) { var info = VisitMethod(model, method); if (info != null) { trackedMethods.Add(info.Symbol, info); } } } #if PORTABLE return(new Tuple <Dictionary <ISymbol, NotNullFieldInfo>, Dictionary <IMethodSymbol, NotNullMethodInfo>, Dictionary <ISymbol, NotNullExpressionBodyInfo> >(trackedFields, trackedMethods, trackedExpressionBodies)); #else return(trackedFields, trackedMethods, trackedExpressionBodies); #endif }