本文整理汇总了C#中DiagnosticBag.Add方法的典型用法代码示例。如果您正苦于以下问题:C# DiagnosticBag.Add方法的具体用法?C# DiagnosticBag.Add怎么用?C# DiagnosticBag.Add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DiagnosticBag
的用法示例。
在下文中一共展示了DiagnosticBag.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckModifiers
internal static DeclarationModifiers CheckModifiers(
DeclarationModifiers modifiers,
DeclarationModifiers allowedModifiers,
Location errorLocation,
DiagnosticBag diagnostics,
out bool modifierErrors)
{
modifierErrors = false;
DeclarationModifiers errorModifiers = modifiers & ~allowedModifiers;
DeclarationModifiers result = modifiers & allowedModifiers;
while (errorModifiers != DeclarationModifiers.None)
{
DeclarationModifiers oneError = errorModifiers & ~(errorModifiers - 1);
Debug.Assert(oneError != DeclarationModifiers.None);
errorModifiers = errorModifiers & ~oneError;
switch (oneError)
{
case DeclarationModifiers.Partial:
diagnostics.Add(ErrorCode.ERR_PartialMethodOnlyMethods, errorLocation);
break;
default:
diagnostics.Add(ErrorCode.ERR_BadMemberFlag, errorLocation, ConvertSingleModifierToSyntaxText(oneError));
break;
}
modifierErrors = true;
}
bool isMethod = (allowedModifiers & (DeclarationModifiers.Partial | DeclarationModifiers.Virtual)) == (DeclarationModifiers.Partial | DeclarationModifiers.Virtual);
if (isMethod && ((result & (DeclarationModifiers.Partial | DeclarationModifiers.Private)) == (DeclarationModifiers.Partial | DeclarationModifiers.Private)))
{
diagnostics.Add(ErrorCode.ERR_PartialMethodInvalidModifier, errorLocation);
}
return result;
}
示例2: SourceMemberFieldSymbol
internal SourceMemberFieldSymbol(
SourceMemberContainerTypeSymbol containingType,
VariableDeclaratorSyntax declarator,
DeclarationModifiers modifiers,
bool modifierErrors,
DiagnosticBag diagnostics)
: base(containingType, declarator.Identifier.ValueText, declarator.GetReference(), declarator.Identifier.GetLocation())
{
this.modifiers = modifiers;
this.CheckAccessibility(diagnostics);
var location = Location;
if (modifierErrors)
{
// skip the following checks
}
else if (containingType.IsSealed && (DeclaredAccessibility == Accessibility.Protected || DeclaredAccessibility == Accessibility.ProtectedOrInternal))
{
diagnostics.Add(AccessCheck.GetProtectedMemberInSealedTypeError(containingType), location, this);
}
else if (IsVolatile && IsReadOnly)
{
diagnostics.Add(ErrorCode.ERR_VolatileAndReadonly, location, this);
}
else if (containingType.IsStatic && !IsStatic)
{
diagnostics.Add(ErrorCode.ERR_InstanceMemberInStaticClass, location, this);
}
// TODO: Consider checking presence of core type System.Runtime.CompilerServices.IsVolatile
// if there is a volatile modifier. Perhaps an appropriate error should be reported if the
// type isn’t available.
}
示例3: SourceConstructorSymbol
private SourceConstructorSymbol(
SourceMemberContainerTypeSymbol containingType,
Location location,
ConstructorDeclarationSyntax syntax,
MethodKind methodKind,
DiagnosticBag diagnostics) :
base(containingType, syntax.GetReference(), syntax.Body.GetReferenceOrNull(), ImmutableArray.Create(location))
{
bool modifierErrors;
var declarationModifiers = this.MakeModifiers(syntax.Modifiers, methodKind, location, diagnostics, out modifierErrors);
this.flags = MakeFlags(methodKind, declarationModifiers, returnsVoid: true, isExtensionMethod: false);
var bodyOpt = syntax.Body;
if (bodyOpt != null)
{
if (IsExtern)
{
diagnostics.Add(ErrorCode.ERR_ExternHasBody, location, this);
}
}
var info = ModifierUtils.CheckAccessibility(this.DeclarationModifiers);
if (info != null)
{
diagnostics.Add(info, location);
}
if (!modifierErrors)
{
this.CheckModifiers(methodKind, location, diagnostics);
}
}
示例4: MethodChecks
protected override void MethodChecks(DiagnosticBag diagnostics)
{
var syntax = (ConstructorDeclarationSyntax)syntaxReference.GetSyntax();
var binderFactory = this.DeclaringCompilation.GetBinderFactory(syntaxReference.SyntaxTree);
// NOTE: if we asked for the binder for the body of the constructor, we'd risk a stack overflow because
// we might still be constructing the member list of the containing type. However, getting the binder
// for the parameters should be safe.
var bodyBinder = binderFactory.GetBinder(syntax.ParameterList).WithContainingMemberOrLambda(this);
SyntaxToken arglistToken;
this.lazyParameters = ParameterHelpers.MakeParameters(bodyBinder, this, syntax.ParameterList, true, out arglistToken, diagnostics);
this.lazyIsVararg = (arglistToken.CSharpKind() == SyntaxKind.ArgListKeyword);
this.lazyReturnType = bodyBinder.GetSpecialType(SpecialType.System_Void, diagnostics, syntax);
if (MethodKind == MethodKind.StaticConstructor && (lazyParameters.Length != 0))
{
diagnostics.Add(ErrorCode.ERR_StaticConstParam, Locations[0], this);
}
this.CheckEffectiveAccessibility(lazyReturnType, lazyParameters, diagnostics);
if (this.lazyIsVararg && (IsGenericMethod || ContainingType.IsGenericType || this.lazyParameters.Length > 0 && this.lazyParameters[this.lazyParameters.Length - 1].IsParams))
{
diagnostics.Add(ErrorCode.ERR_BadVarargs, Locations[0]);
}
}
示例5: TypeChecks
private void TypeChecks(TypeSymbol type, BaseFieldDeclarationSyntax fieldSyntax, VariableDeclaratorSyntax declarator, DiagnosticBag diagnostics)
{
if (type.IsStatic)
{
// Cannot declare a variable of static type '{0}'
diagnostics.Add(ErrorCode.ERR_VarDeclIsStaticClass, this.Location, type);
}
else if (type.SpecialType == SpecialType.System_Void)
{
diagnostics.Add(ErrorCode.ERR_FieldCantHaveVoidType, fieldSyntax.Declaration.Type.Location);
}
else if (type.IsRestrictedType())
{
diagnostics.Add(ErrorCode.ERR_FieldCantBeRefAny, fieldSyntax.Declaration.Type.Location, type);
}
else if (IsConst && !type.CanBeConst())
{
SyntaxToken constToken = default(SyntaxToken);
foreach (var modifier in fieldSyntax.Modifiers)
{
if (modifier.CSharpKind() == SyntaxKind.ConstKeyword)
{
constToken = modifier;
break;
}
}
Debug.Assert(constToken.CSharpKind() == SyntaxKind.ConstKeyword);
diagnostics.Add(ErrorCode.ERR_BadConstType, constToken.GetLocation(), type);
}
else
{
if (ContainingType.TypeKind == TypeKind.Struct && !IsStatic && !IsConst)
{
var initializerOpt = declarator.Initializer;
if (initializerOpt != null)
{
// '{0}': cannot have instance field initializers in structs
diagnostics.Add(ErrorCode.ERR_FieldInitializerInStruct, this.Location, this);
}
}
if (IsVolatile && !type.IsValidVolatileFieldType())
{
// '{0}': a volatile field cannot be of the type '{1}'
diagnostics.Add(ErrorCode.ERR_VolatileStruct, this.Location, this, type);
}
}
HashSet<DiagnosticInfo> useSiteDiagnostics = null;
if (!this.IsNoMoreVisibleThan(type, ref useSiteDiagnostics))
{
// Inconsistent accessibility: field type '{1}' is less accessible than field '{0}'
diagnostics.Add(ErrorCode.ERR_BadVisFieldType, this.Location, this, type);
}
diagnostics.Add(this.Location, useSiteDiagnostics);
}
示例6: DiagnosticBag
public void DiagnosticBag()
{
DiagnosticBag bag1 = new DiagnosticBag();
bag1.Add(CreateDiagnostic(4));
bag1.Add(CreateDiagnostic(7));
Assert.False(bag1.IsEmpty);
Assert.Equal(2, bag1.Count());
Assert.NotNull(bag1.ToString());
bool found4 = false, found7 = false;
foreach (Diagnostic d in bag1)
{
if (d.Code == 4)
{
Assert.False(found4);
found4 = true;
}
else if (d.Code == 7)
{
Assert.False(found7);
found7 = true;
}
else
Assert.True(false);
}
DiagnosticBag bag2 = new DiagnosticBag();
bag1.Add(bag2);
Assert.False(bag1.IsEmpty);
Assert.Equal(2, bag1.Count());
found4 = false; found7 = false;
foreach (Diagnostic d in bag1)
{
if (d.Code == 4)
{
Assert.False(found4);
found4 = true;
}
else if (d.Code == 7)
{
Assert.False(found7);
found7 = true;
}
else
Assert.True(false);
}
DiagnosticBag bag3 = new DiagnosticBag();
bag3.Add(CreateDiagnostic(3));
bag3.Add(CreateDiagnostic(2));
bag3.Add(CreateDiagnostic(1));
bag1.Add(bag3);
Assert.False(bag1.IsEmpty);
Assert.Equal(5, bag1.Count());
}
示例7: ReportQueryLookupFailed
/// <summary>
/// This is a clone of the Dev10 logic for reporting query errors.
/// </summary>
internal void ReportQueryLookupFailed(
CSharpSyntaxNode queryClause,
BoundExpression instanceArgument,
string name,
ImmutableArray<Symbol> symbols,
DiagnosticBag diagnostics)
{
FromClauseSyntax fromClause = null;
for (CSharpSyntaxNode node = queryClause; ; node = node.Parent)
{
var e = node as QueryExpressionSyntax;
if (e != null)
{
fromClause = e.FromClause;
break;
}
}
HashSet<DiagnosticInfo> useSiteDiagnostics = null;
if (instanceArgument.Type.IsDynamic())
{
// CS1979: Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
diagnostics.Add(
new DiagnosticInfoWithSymbols(ErrorCode.ERR_BadDynamicQuery, SpecializedCollections.EmptyObjects, symbols),
new SourceLocation(queryClause));
}
else if (ImplementsStandardQueryInterface(instanceArgument.Type, name, ref useSiteDiagnostics))
{
// Could not find an implementation of the query pattern for source type '{0}'. '{1}' not found. Are you missing a reference to 'System.Core.dll' or a using directive for 'System.Linq'?
diagnostics.Add(new DiagnosticInfoWithSymbols(
ErrorCode.ERR_QueryNoProviderStandard,
new object[] { instanceArgument.Type, name },
symbols), new SourceLocation(fromClause != null ? fromClause.Expression : queryClause));
}
else if (fromClause != null && fromClause.Type == null && HasCastToQueryProvider(instanceArgument.Type, ref useSiteDiagnostics))
{
// Could not find an implementation of the query pattern for source type '{0}'. '{1}' not found. Consider explicitly specifying the type of the range variable '{2}'.
diagnostics.Add(new DiagnosticInfoWithSymbols(
ErrorCode.ERR_QueryNoProviderCastable,
new object[] { instanceArgument.Type, name, fromClause.Identifier.ValueText },
symbols), new SourceLocation(fromClause.Expression));
}
else
{
// Could not find an implementation of the query pattern for source type '{0}'. '{1}' not found.
diagnostics.Add(new DiagnosticInfoWithSymbols(
ErrorCode.ERR_QueryNoProvider,
new object[] { instanceArgument.Type, name },
symbols), new SourceLocation(fromClause != null ? fromClause.Expression : queryClause));
}
diagnostics.Add(queryClause, useSiteDiagnostics);
}
示例8: Rewrite
/// <summary>
/// The flow analysis pass. This pass reports required diagnostics for unreachable
/// statements and uninitialized variables (through the call to FlowAnalysisWalker.Analyze),
/// and inserts a final return statement if the end of a void-returning method is reachable.
/// </summary>
/// <param name="method">the method to be analyzed</param>
/// <param name="block">the method's body</param>
/// <param name="diagnostics">the receiver of the reported diagnostics</param>
/// <returns>the rewritten block for the method (with a return statement possibly inserted)</returns>
public static BoundBlock Rewrite(
MethodSymbol method,
BoundBlock block,
DiagnosticBag diagnostics)
{
var compilation = method.DeclaringCompilation;
if (method.ReturnsVoid || (object)method.IteratorElementType != null
|| (method.IsAsync && compilation.GetWellKnownType(WellKnownType.System_Threading_Tasks_Task) == method.ReturnType))
{
if (method.IsImplicitlyDeclared || Analyze(compilation, method, block, diagnostics))
{
// we don't analyze synthesized void methods.
var sourceMethod = method as SourceMethodSymbol;
block = AppendImplicitReturn(block, method, (object)sourceMethod != null ? sourceMethod.BlockSyntax : null);
}
}
else if (Analyze(compilation, method, block, diagnostics))
{
// If the method is a lambda expression being converted to a non-void delegate type
// and the end point is reachable then suppress the error here; a special error
// will be reported by the lambda binder.
Debug.Assert(method.MethodKind != MethodKind.AnonymousFunction);
// If there's more than one location, then the method is partial and we
// have already reported a non-void partial method error.
if (method.Locations.Length == 1)
{
diagnostics.Add(ErrorCode.ERR_ReturnExpected, method.Locations[0], method);
}
}
return block;
}
示例9: ResolveBounds
/// <summary>
/// Determine the effective base type, effective interface set, and set of type
/// parameters (excluding cycles) from the type parameter constraints. Conflicts
/// within the constraints and constraint types are returned as diagnostics.
/// 'inherited' should be true if the type parameters are from an overridden
/// generic method. In those cases, additional constraint checks are applied.
/// </summary>
public static TypeParameterBounds ResolveBounds(
this TypeParameterSymbol typeParameter,
AssemblySymbol corLibrary,
ConsList<TypeParameterSymbol> inProgress,
ImmutableArray<TypeSymbol> constraintTypes,
bool inherited,
CSharpCompilation currentCompilation,
DiagnosticBag diagnostics)
{
var diagnosticsBuilder = ArrayBuilder<TypeParameterDiagnosticInfo>.GetInstance();
ArrayBuilder<TypeParameterDiagnosticInfo> useSiteDiagnosticsBuilder = null;
var bounds = typeParameter.ResolveBounds(corLibrary, inProgress, constraintTypes, inherited, currentCompilation, diagnosticsBuilder, ref useSiteDiagnosticsBuilder);
if (useSiteDiagnosticsBuilder != null)
{
diagnosticsBuilder.AddRange(useSiteDiagnosticsBuilder);
}
foreach (var pair in diagnosticsBuilder)
{
diagnostics.Add(new CSDiagnostic(pair.DiagnosticInfo, pair.TypeParameter.Locations[0]));
}
diagnosticsBuilder.Free();
return bounds;
}
示例10: SourceEnumConstantSymbol
protected SourceEnumConstantSymbol(SourceMemberContainerTypeSymbol containingEnum, EnumMemberDeclarationSyntax syntax, DiagnosticBag diagnostics)
: base(containingEnum, syntax.Identifier.ValueText, syntax.GetReference(), syntax.Identifier.GetLocation())
{
if (this.Name == WellKnownMemberNames.EnumBackingFieldName)
{
diagnostics.Add(ErrorCode.ERR_ReservedEnumerator, this.Location, WellKnownMemberNames.EnumBackingFieldName);
}
}
示例11: AddDelegateMembers
private void AddDelegateMembers(
ArrayBuilder<Symbol> symbols,
DelegateDeclarationSyntax syntax,
BinderFactory binderFactory,
DiagnosticBag diagnostics)
{
var bodyBinder = binderFactory.GetBinder(syntax.ParameterList);
// A delegate has the following members: (see CLI spec 13.6)
// (1) a method named Invoke with the specified signature
var invoke = new DelegateInvokeMethodImplementation(this, syntax, bodyBinder, diagnostics);
invoke.CheckMethodVarianceSafety(diagnostics);
symbols.Add(invoke);
// (2) a constructor with argument types (object, System.IntPtr)
symbols.Add(new DelegateConstructor(this, syntax, bodyBinder));
var delegateBinder = new DelegateBinder(bodyBinder, this, invoke);
// (3) BeginInvoke
symbols.Add(new DelegateBeginInvokeMethod(this, syntax, delegateBinder, diagnostics));
// and (4) EndInvoke methods
symbols.Add(new DelegateEndInvokeMethod(this, syntax, delegateBinder, diagnostics));
if (this.DeclaredAccessibility <= Accessibility.Private)
{
return;
}
if (!this.IsNoMoreVisibleThan(invoke.ReturnType))
{
// Inconsistent accessibility: return type '{1}' is less accessible than delegate '{0}'
diagnostics.Add(ErrorCode.ERR_BadVisDelegateReturn, Locations[0], this, invoke.ReturnType);
}
foreach (var parameter in invoke.Parameters)
{
if (!parameter.Type.IsAtLeastAsVisibleAs(this))
{
// Inconsistent accessibility: parameter type '{1}' is less accessible than delegate '{0}'
diagnostics.Add(ErrorCode.ERR_BadVisDelegateParam, Locations[0], this, parameter.Type);
}
}
}
示例12: GetIteratorElementType
// NOTE: Specifically not overriding IsIndirectlyInIterator.
internal override TypeSymbol GetIteratorElementType(YieldStatementSyntax node, DiagnosticBag diagnostics)
{
if (node != null)
{
diagnostics.Add(ErrorCode.ERR_YieldInAnonMeth, node.YieldKeyword.GetLocation());
}
return CreateErrorType();
}
示例13: NotFound
internal override CSDiagnosticInfo NotFound(Location location, string name, DiagnosticBag diagnostics)
{
return diagnostics.Add(
ReferenceEquals(qualifier, Compilation.GlobalNamespace) ? ErrorCode.ERR_GlobalSingleTypeNameNotFound :
qualifier.IsNamespace ? ErrorCode.ERR_DottedTypeNameNotFoundInNS :
ErrorCode.ERR_DottedTypeNameNotFoundInAgg,
location, name, qualifier.GetFullName());
}
示例14: Rewrite
/// <summary>
/// The flow analysis pass. This pass reports required diagnostics for unreachable
/// statements and uninitialized variables (through the call to FlowAnalysisWalker.Analyze),
/// and inserts a final return statement if the end of a void-returning method is reachable.
/// </summary>
/// <param name="method">the method to be analyzed</param>
/// <param name="block">the method's body</param>
/// <param name="diagnostics">the receiver of the reported diagnostics</param>
/// <param name="hasTrailingExpression">indicates whether this Script had a trailing expression</param>
/// <param name="originalBodyNested">the original method body is the last statement in the block</param>
/// <returns>the rewritten block for the method (with a return statement possibly inserted)</returns>
public static BoundBlock Rewrite(
MethodSymbol method,
BoundBlock block,
DiagnosticBag diagnostics,
bool hasTrailingExpression,
bool originalBodyNested)
{
#if DEBUG
// We should only see a trailingExpression if we're in a Script initializer.
Debug.Assert(!hasTrailingExpression || method.IsScriptInitializer);
var initialDiagnosticCount = diagnostics.ToReadOnly().Length;
#endif
var compilation = method.DeclaringCompilation;
if (method.ReturnsVoid || method.IsIterator ||
(method.IsAsync && compilation.GetWellKnownType(WellKnownType.System_Threading_Tasks_Task) == method.ReturnType))
{
// we don't analyze synthesized void methods.
if ((method.IsImplicitlyDeclared && !method.IsScriptInitializer) || Analyze(compilation, method, block, diagnostics))
{
block = AppendImplicitReturn(block, method, (CSharpSyntaxNode)(method as SourceMethodSymbol)?.BodySyntax, originalBodyNested);
}
}
else if (Analyze(compilation, method, block, diagnostics))
{
// If the method is a lambda expression being converted to a non-void delegate type
// and the end point is reachable then suppress the error here; a special error
// will be reported by the lambda binder.
Debug.Assert(method.MethodKind != MethodKind.AnonymousFunction);
// Add implicit "return default(T)" if this is a submission that does not have a trailing expression.
var submissionResultType = (method as SynthesizedInteractiveInitializerMethod)?.ResultType;
if (!hasTrailingExpression && ((object)submissionResultType != null))
{
Debug.Assert(submissionResultType.SpecialType != SpecialType.System_Void);
var trailingExpression = new BoundDefaultOperator(method.GetNonNullSyntaxNode(), submissionResultType);
var newStatements = block.Statements.Add(new BoundReturnStatement(trailingExpression.Syntax, trailingExpression));
block = new BoundBlock(block.Syntax, ImmutableArray<LocalSymbol>.Empty, newStatements) { WasCompilerGenerated = true };
#if DEBUG
// It should not be necessary to repeat analysis after adding this node, because adding a trailing
// return in cases where one was missing should never produce different Diagnostics.
var flowAnalysisDiagnostics = DiagnosticBag.GetInstance();
Debug.Assert(!Analyze(compilation, method, block, flowAnalysisDiagnostics));
Debug.Assert(flowAnalysisDiagnostics.ToReadOnly().SequenceEqual(diagnostics.ToReadOnly().Skip(initialDiagnosticCount)));
flowAnalysisDiagnostics.Free();
#endif
}
// If there's more than one location, then the method is partial and we
// have already reported a non-void partial method error.
else if (method.Locations.Length == 1)
{
diagnostics.Add(ErrorCode.ERR_ReturnExpected, method.Locations[0], method);
}
}
return block;
}
示例15: GetConstantValue
internal override ConstantValue GetConstantValue(SyntaxNode node, LocalSymbol inProgress, DiagnosticBag diagnostics)
{
if (diagnostics != null && _value.IsBad)
{
diagnostics.Add(ErrorCode.ERR_BadPdbData, Location.None, Name);
}
return _value;
}