本文整理汇总了C#中CompilationAnalysisContext类的典型用法代码示例。如果您正苦于以下问题:C# CompilationAnalysisContext类的具体用法?C# CompilationAnalysisContext怎么用?C# CompilationAnalysisContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CompilationAnalysisContext类属于命名空间,在下文中一共展示了CompilationAnalysisContext类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: AnalyzeCompilation
private static void AnalyzeCompilation(CompilationAnalysisContext context)
{
var compilation = context.Compilation;
var allPlugins = compilation.SyntaxTrees
.Select(x => x.GetCompilationUnitRoot())
.SelectMany(x => x.FindSyntax<ClassDeclarationSyntax>())
.Where(x => x.IsPluginClass())
.ToArray();
var plugins = allPlugins.Where(x => x.IsExportIPlugin()).ToArray();
foreach (var p in allPlugins)
{
var semanticModel = compilation.GetSemanticModel(p.SyntaxTree);
var syntax = p.GetGuidMetadataValueSyntax();
if (syntax == null) continue; //GUID な Metadata がない場合はスルー
// GUID として解釈できない値か、IPlugin で未定義の GUID が指定されてたらアウト
var guidMetadata = syntax.GetGuidMetadata(semanticModel);
if (!guidMetadata.HasValue
|| plugins.All(x => x.GetGuidMetadataValueSyntax()?.GetGuidMetadata(compilation.GetSemanticModel(x.SyntaxTree)) != guidMetadata))
{
context.ReportDiagnostic(Diagnostic.Create(InvalidGuidMetadataRule, p.GetLocation()));
}
}
}
开发者ID:Zcynical,项目名称:KanColleViewer,代码行数:25,代码来源:InvalidGuidMetadataAnalyzer.cs
示例2: HandleCompilation
public void HandleCompilation(CompilationAnalysisContext context)
{
if (Volatile.Read(ref this.documentationAnalysisDisabled))
{
context.ReportDiagnostic(Diagnostic.Create(Descriptor, Location.None));
}
}
开发者ID:EdwinEngelen,项目名称:StyleCopAnalyzers,代码行数:7,代码来源:SA0001XmlCommentAnalysisDisabled.cs
示例3: AnalyzeCompilation
private static void AnalyzeCompilation(CompilationAnalysisContext context)
{
if (AssemblyHasPublicTypes(context.Compilation.Assembly))
{
INamedTypeSymbol comVisibleAttributeSymbol = WellKnownTypes.ComVisibleAttribute(context.Compilation);
if (comVisibleAttributeSymbol == null)
{
return;
}
AttributeData attributeInstance = context.Compilation.Assembly.GetAttributes().FirstOrDefault(a => a.AttributeClass.Equals(comVisibleAttributeSymbol));
if (attributeInstance != null)
{
if (attributeInstance.ConstructorArguments.Length > 0 &&
attributeInstance.ConstructorArguments[0].Kind == TypedConstantKind.Primitive &&
attributeInstance.ConstructorArguments[0].Value != null &
attributeInstance.ConstructorArguments[0].Value.Equals(true))
{
// Has the attribute, with the value 'true'.
context.ReportDiagnostic(Diagnostic.Create(RuleA, Location.None, context.Compilation.Assembly.Name));
}
}
else
{
// No ComVisible attribute at all.
context.ReportDiagnostic(Diagnostic.Create(RuleB, Location.None, context.Compilation.Assembly.Name));
}
}
return;
}
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:32,代码来源:MarkAssembliesWithComVisible.cs
示例4: AnalyzeCompilation
private static void AnalyzeCompilation(CompilationAnalysisContext context)
{
var compilation = context.Compilation;
var plugins = compilation.SyntaxTrees
.Select(x => x.GetCompilationUnitRoot())
.SelectMany(x => x.FindSyntax<ClassDeclarationSyntax>())
.Where(x => x.IsExportIPlugin())
.ToArray();
var dupe = plugins
.GroupBy(x => x.GetGuidMetadataValue())
.Where(x => 1 < x.Count())
.ToArray();
if (!dupe.Any()) return;
foreach (var group in dupe)
{
foreach (var c in group)
{
var other = group.Except(new[] { c }).ToArray();
context.ReportDiagnostic(Diagnostic.Create(
DuplicateGuidRule,
c.GetLocation(),
string.Join(", ", other.Select(x => x.Identifier))));
}
}
}
开发者ID:NatLee,项目名称:KanColleViewer,代码行数:28,代码来源:DuplicateGuidAnalyzer.cs
示例5: AnalyzeCompilation
private void AnalyzeCompilation(CompilationAnalysisContext context)
{
if (AssemblyHasPublicTypes(context.Compilation.Assembly))
{
var comVisibleAttributeSymbol = WellKnownTypes.ComVisibleAttribute(context.Compilation);
if (comVisibleAttributeSymbol == null)
{
return;
}
var attributeInstance = context.Compilation.Assembly.GetAttributes().FirstOrDefault(a => a.AttributeClass.Equals(comVisibleAttributeSymbol));
if (attributeInstance != null)
{
if (attributeInstance.ConstructorArguments.Length > 0 &&
attributeInstance.ConstructorArguments[0].Kind == TypedConstantKind.Primitive &&
attributeInstance.ConstructorArguments[0].Value != null &
attributeInstance.ConstructorArguments[0].Value.Equals(true))
{
// Has the attribute, with the value 'true'.
context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, string.Format(SystemRuntimeAnalyzersResources.CA1017_AttributeTrue, context.Compilation.Assembly.Name)));
}
}
else
{
// No ComVisible attribute at all.
context.ReportDiagnostic(Diagnostic.Create(Rule, Location.None, string.Format(SystemRuntimeAnalyzersResources.CA1017_NoAttribute, context.Compilation.Assembly.Name)));
}
}
return;
}
开发者ID:ehsansajjad465,项目名称:roslyn,代码行数:32,代码来源:MarkAllAssembliesWithComVisible.cs
示例6: OnCompilation
private static void OnCompilation(CompilationAnalysisContext context)
{
var diags = OnCompilationAsync(context.Compilation).Result;
foreach (var d in diags)
{
context.ReportDiagnostic(d);
}
}
开发者ID:semdiffdotnet,项目名称:semdiff,代码行数:8,代码来源:SemDiffAnalyzer.cs
示例7: AnalyzeCompilation
public void AnalyzeCompilation(CompilationAnalysisContext context)
{
foreach (var item in _fieldDisposedMap)
{
if (!item.Value)
{
context.ReportDiagnostic(item.Key.CreateDiagnostic(Rule));
}
}
}
开发者ID:ehsansajjad465,项目名称:roslyn,代码行数:10,代码来源:DisposableFieldsShouldBeDisposed.cs
示例8: AnalyzeCompilation
private void AnalyzeCompilation(CompilationAnalysisContext context)
{
Trace.WriteLine("AnalyzeCompilation");
try
{
INamedTypeSymbol dbContextTypeSymbol;
IEnumerable<INamedTypeSymbol> entityTypeSymbols = SymbolHelper.GetAllEntityTypesFromDbContext(context.Compilation, out dbContextTypeSymbol);
if (!entityTypeSymbols.Any())
{
return;
}
var allTypeSymbols = context.Compilation.GetSymbolsWithName(s => !s.EndsWith("DbContext"), SymbolFilter.Type).Cast<INamedTypeSymbol>();
var allMemberSymbols = allTypeSymbols.SelectMany(t => t.GetMembers().Where(m => m.Kind == SymbolKind.Property));
Trace.WriteLine("Class count: " + allTypeSymbols.Count());
Trace.WriteLine("Property count: " + allMemberSymbols.Count());
var efRoslynTheorem = new EFRoslynTheorem();
var result = efRoslynTheorem.Solve(entityTypeSymbols);
if (result.Status == Status.Unsatisfiable)
{
var classAssumptions = efRoslynTheorem.ClassAssumptions.ToList();
var propertyAssumptions = efRoslynTheorem.PropertyAssumptions.OrderBy(pa => pa.Rank).ToList();
do
{
result = TryToRemoveWrongAssumption(efRoslynTheorem, result, classAssumptions, propertyAssumptions);
} while (result != null && result.Status != Status.Satisfiable);
if (result == null || result.Status != Status.Satisfiable)
{
var diagnostic2 = Diagnostic.Create(UnsatisfiableRule, dbContextTypeSymbol.Locations[0], dbContextTypeSymbol.Name);
Trace.WriteLine("ReportDiagnostic " + diagnostic2.Descriptor.Id);
context.ReportDiagnostic(diagnostic2);
return;
}
var cacheId = EFRoslynTheoremCache.Add(context.Compilation, efRoslynTheorem, result);
var props = ImmutableDictionary.Create<string, string>();
props = props.Add("CacheId", cacheId);
var diagnostic = Diagnostic.Create(SatisfiableRule, dbContextTypeSymbol.Locations[0], props, dbContextTypeSymbol.Name);
Trace.WriteLine("ReportDiagnostic " + diagnostic.Descriptor.Id);
context.ReportDiagnostic(diagnostic);
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.ToString());
throw;
}
}
开发者ID:RicardoNiepel,项目名称:Z3.ObjectTheorem,代码行数:54,代码来源:DiagnosticAnalyzer.cs
示例9: AnalyzeCompilation
private static void AnalyzeCompilation(CompilationAnalysisContext context)
{
var compilation = context.Compilation;
var isDefined = compilation.SyntaxTrees
.Select(x => x.GetCompilationUnitRoot())
.SelectMany(x => x.DescendantNodes().OfType<SimpleBaseTypeSyntax>())
.Any(x => x.ToString() == "IPlugin" || x.ToString() == "Grabacr07.KanColleViewer.Composition.IPlugin")
;
if (!isDefined)
context.ReportDiagnostic(Diagnostic.Create(RequireIPluginRule, Location.None));
}
开发者ID:NatLee,项目名称:KanColleViewer,代码行数:12,代码来源:RequireIPluginAnalyzer.cs
示例10: AnalyzeCompilation
private void AnalyzeCompilation(CompilationAnalysisContext context)
{
var globalNamespaces = context.Compilation.GlobalNamespace.GetNamespaceMembers()
.Where(item => item.ContainingAssembly == context.Compilation.Assembly);
var globalTypes = context.Compilation.GlobalNamespace.GetTypeMembers().Where(item =>
item.ContainingAssembly == context.Compilation.Assembly &&
IsExternallyVisible(item));
CheckTypeNames(globalTypes, context.ReportDiagnostic);
CheckNamespaceMembers(globalNamespaces, context.Compilation, context.ReportDiagnostic);
}
开发者ID:ehsansajjad465,项目名称:roslyn,代码行数:12,代码来源:CA1708DiagnosticAnalyzer.cs
示例11: AnalyzeCompilation
private static void AnalyzeCompilation(CompilationAnalysisContext context)
{
// Get all the suppressed analyzer diagnostic IDs.
var suppressedAnalyzerDiagnosticIds = GetSuppressedAnalyzerDiagnosticIds(context.Compilation.Options.SpecificDiagnosticOptions);
foreach (var suppressedDiagnosticId in suppressedAnalyzerDiagnosticIds)
{
// For all such suppressed diagnostic IDs, produce a diagnostic.
var diagnostic = Diagnostic.Create(Rule, Location.None, suppressedDiagnosticId);
context.ReportDiagnostic(diagnostic);
}
}
开发者ID:Rickinio,项目名称:roslyn,代码行数:12,代码来源:CompilationAnalyzer.cs
示例12: AnalyzeCompilation
private void AnalyzeCompilation(CompilationAnalysisContext context)
{
INamedTypeSymbol assemblyVersionAttributeSymbol = WellKnownTypes.AssemblyVersionAttribute(context.Compilation);
INamedTypeSymbol assemblyComplianceAttributeSymbol = WellKnownTypes.CLSCompliantAttribute(context.Compilation);
if (assemblyVersionAttributeSymbol == null && assemblyComplianceAttributeSymbol == null)
{
return;
}
bool assemblyVersionAttributeFound = false;
bool assemblyComplianceAttributeFound = false;
// Check all assembly level attributes for the target attribute
foreach (AttributeData attribute in context.Compilation.Assembly.GetAttributes())
{
if (attribute.AttributeClass.Equals(assemblyVersionAttributeSymbol))
{
// Mark the version attribute as found
assemblyVersionAttributeFound = true;
}
else if (attribute.AttributeClass.Equals(assemblyComplianceAttributeSymbol))
{
// Mark the compliance attribute as found
assemblyComplianceAttributeFound = true;
}
}
// Check for the case where we do not have the target attribute defined at all in our metadata references. If so, how can they reference it
if (assemblyVersionAttributeSymbol == null)
{
assemblyVersionAttributeFound = false;
}
if (assemblyComplianceAttributeSymbol == null)
{
assemblyComplianceAttributeFound = false;
}
// If there's at least one diagnostic to report, let's report them
if (!assemblyComplianceAttributeFound || !assemblyVersionAttributeFound)
{
if (!assemblyVersionAttributeFound)
{
context.ReportDiagnostic(Diagnostic.Create(CA1016Rule, Location.None));
}
if (!assemblyComplianceAttributeFound)
{
context.ReportDiagnostic(Diagnostic.Create(CA1014Rule, Location.None));
}
}
}
开发者ID:Anniepoh,项目名称:roslyn-analyzers,代码行数:53,代码来源:MarkAssembliesWithAttributesDiagnosticAnalyzer.cs
示例13: OnCompilationEnd
private void OnCompilationEnd(CompilationAnalysisContext context)
{
lock (_apisToEnsureExist)
{
if (_apisToEnsureExist.Count != 0)
{
// If we have not cleared the list of APIs that must exist then we need to give errors about them
foreach (var missingAPI in _apisToEnsureExist)
{
context.ReportDiagnostic(Diagnostic.Create(s_memberMustExistDiagnostic, Location.None, missingAPI));
}
}
}
}
开发者ID:schaabs,项目名称:buildtools,代码行数:14,代码来源:MembersMustExistAnalyzer.cs
示例14: HandleCompilation
private static void HandleCompilation(CompilationAnalysisContext context)
{
try
{
SettingsHelper.GetStyleCopSettings(context.Options, DeserializationFailureBehavior.ThrowException, context.CancellationToken);
}
catch (JsonException ex)
{
string details = ex.Message;
string completeDescription = string.Format(Description.ToString(CultureInfo.CurrentCulture), details);
var completeDescriptor = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.SpecialRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, completeDescription, HelpLink);
context.ReportDiagnostic(Diagnostic.Create(completeDescriptor, Location.None));
}
}
开发者ID:Romanx,项目名称:StyleCopAnalyzers,代码行数:15,代码来源:SA0002InvalidSettingsFile.cs
示例15: HandleCompilation
public void HandleCompilation(CompilationAnalysisContext context)
{
var compilation = context.Compilation;
var globalNamespace = compilation.GlobalNamespace;
var ruleDependencyAttributeSymbols = GetRuleDependencyAttributeTypeSymbols(globalNamespace);
// if ANTLR 4 isn't referenced, no need to run this analyzer
if (ruleDependencyAttributeSymbols.Length == 0)
return;
IEnumerable<INamedTypeSymbol> typesToCheck = GetAllTypes(compilation.SourceModule.GlobalNamespace);
var builder = ImmutableArray.CreateBuilder<Tuple<AttributeData, ISymbol>>();
foreach (var type in typesToCheck)
builder.AddRange(GetDependencies(type, ruleDependencyAttributeSymbols));
var dependencies = builder.ToImmutable();
if (dependencies.Length == 0)
return;
var recognizerDependencies = new Dictionary<INamedTypeSymbol, IList<Tuple<AttributeData, ISymbol>>>();
foreach (var dependency in dependencies)
{
if (dependency.Item1.AttributeConstructor == null)
continue;
INamedTypeSymbol recognizerType = GetRecognizerType(dependency.Item1);
if (recognizerType == null)
continue;
IList<Tuple<AttributeData, ISymbol>> list;
if (!recognizerDependencies.TryGetValue(recognizerType, out list))
{
list = new List<Tuple<AttributeData, ISymbol>>();
recognizerDependencies[recognizerType] = list;
}
list.Add(dependency);
}
foreach (var entry in recognizerDependencies)
{
var diagnostics = CheckDependencies((CSharpCompilation)compilation, entry.Value, entry.Key);
foreach (var diagnostic in diagnostics)
context.ReportDiagnostic(diagnostic);
}
}
开发者ID:modulexcite,项目名称:RuleDependencyAnalyzer,代码行数:46,代码来源:DiagnosticAnalyzer.cs
示例16: AnalizeCompilation
private void AnalizeCompilation(CompilationAnalysisContext context)
{
// Find CS0006: Metadata file '{0}' could not be found
Diagnostic invalidMetadataDiagnostic = context.Compilation
.GetDiagnostics().FirstOrDefault(d => string.Compare(d.Id, "CS0006") == 0);
if (invalidMetadataDiagnostic != null)
{
var argument = invalidMetadataDiagnostic.GetDiagnosticMessageArguments().First().ToString();
if (argument != null && string.Compare(Path.GetExtension(argument), ".dll") == 0)
{
var diagnostic = Diagnostic.Create(_supportedRule, invalidMetadataDiagnostic.Location,
argument, ".dll");
context.ReportDiagnostic(diagnostic);
}
}
}
开发者ID:Azure,项目名称:azure-webjobs-sdk-script,代码行数:18,代码来源:InvalidFileMetadataReferenceAnalyzer.cs
示例17: AnalyzeCompilation
private static void AnalyzeCompilation(CompilationAnalysisContext context)
{
var compilation = context.Compilation;
var allPlugins = compilation.SyntaxTrees
.Select(x => x.GetCompilationUnitRoot())
.SelectMany(x => x.FindSyntax<ClassDeclarationSyntax>())
.Where(x => x.IsPluginClass())
.ToArray();
var plugins = allPlugins.Where(x => x.IsExportIPlugin()).ToArray();
foreach (var p in allPlugins)
{
var guidVaue = p.GetGuidMetadataValue();
if (guidVaue == null) continue; //GUID な Metadata がない場合はスルー
// GUID として解釈できない値か、IPlugin で未定義の GUID が指定されてたらアウト
Guid guid;
if (!Guid.TryParse(guidVaue, out guid)
|| plugins.All(x => x.GetGuidMetadataValue().ToUpper() != guidVaue.ToUpper()))
context.ReportDiagnostic(Diagnostic.Create(InvalidGuidMetadataRule, p.GetLocation()));
}
}
开发者ID:NatLee,项目名称:KanColleViewer,代码行数:22,代码来源:InvalidGuidMetadataAnalyzer.cs
示例18: AnalyzeCompilation
private static void AnalyzeCompilation(CompilationAnalysisContext context)
{
INamedTypeSymbol assemblyVersionAttributeSymbol = WellKnownTypes.AssemblyVersionAttribute(context.Compilation);
INamedTypeSymbol assemblyComplianceAttributeSymbol = WellKnownTypes.CLSCompliantAttribute(context.Compilation);
if (assemblyVersionAttributeSymbol == null && assemblyComplianceAttributeSymbol == null)
{
return;
}
bool assemblyVersionAttributeFound = false;
bool assemblyComplianceAttributeFound = false;
// Check all assembly level attributes for the target attribute
foreach (AttributeData attribute in context.Compilation.Assembly.GetAttributes())
{
if (attribute.AttributeClass.Equals(assemblyVersionAttributeSymbol))
{
// Mark the version attribute as found
assemblyVersionAttributeFound = true;
}
else if (attribute.AttributeClass.Equals(assemblyComplianceAttributeSymbol))
{
// Mark the compliance attribute as found
assemblyComplianceAttributeFound = true;
}
}
if (!assemblyVersionAttributeFound && assemblyVersionAttributeSymbol != null)
{
context.ReportDiagnostic(Diagnostic.Create(CA1016Rule, Location.None));
}
if (!assemblyComplianceAttributeFound && assemblyComplianceAttributeSymbol != null)
{
context.ReportDiagnostic(Diagnostic.Create(CA1014Rule, Location.None));
}
}
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:38,代码来源:MarkAssembliesWithAttributesDiagnosticAnalyzer.cs
示例19: ReportDiagnostic
//reports a diagnostics
private static void ReportDiagnostic(CompilationAnalysisContext context, DiagnosticDescriptor rule, Location location, params string[] messageArgs)
{
Diagnostic diagnostic = Diagnostic.Create(rule, location, messageArgs);
context.ReportDiagnostic(diagnostic);
}
开发者ID:maggiemsft,项目名称:roslyn-analyzers,代码行数:6,代码来源:DiagnosticAnalyzer.cs
示例20: InitializeBody
// Checks the body of initializer, returns the invocation expression and member expression of the register statements, null if failed
private InitializeBodyInfo InitializeBody(CompilationAnalysisContext context, SyntaxList<StatementSyntax> statements)
{
var statement = statements[0] as ExpressionStatementSyntax;
if (statement == null)
{
ReportDiagnostic(context, InvalidStatementRule, statements[0].GetLocation());
return null;
}
var invocationExpr = statement.Expression as InvocationExpressionSyntax;
if (invocationExpr == null)
{
ReportDiagnostic(context, InvalidStatementRule, statements[0].GetLocation());
return null;
}
var memberExpr = invocationExpr.Expression as MemberAccessExpressionSyntax;
if (memberExpr == null)
{
ReportDiagnostic(context, InvalidStatementRule, statements[0].GetLocation());
return null;
}
var memberExprContext = memberExpr.Expression as IdentifierNameSyntax;
if (memberExprContext == null)
{
ReportDiagnostic(context, InvalidStatementRule, statements[0].GetLocation());
return null;
}
MethodDeclarationSyntax methodDeclaration = statement.Parent.Parent as MethodDeclarationSyntax;
ParameterSyntax parameter = methodDeclaration.ParameterList.Parameters[0] as ParameterSyntax;
if (memberExprContext.Identifier.Text != parameter.Identifier.ValueText)
{
ReportDiagnostic(context, InvalidStatementRule, statements[0].GetLocation());
return null;
}
var memberExprRegister = memberExpr.Name as IdentifierNameSyntax;
if (memberExprRegister == null)
{
ReportDiagnostic(context, InvalidStatementRule, statements[0].GetLocation());
return null;
}
if (!_branchesDict.ContainsKey(memberExprRegister.Identifier.Text))
{
ReportDiagnostic(context, IncorrectRegisterRule, memberExprRegister.GetLocation());
return null;
}
return new InitializeBodyInfo(invocationExpr, memberExpr);
}
开发者ID:maggiemsft,项目名称:roslyn-analyzers,代码行数:54,代码来源:DiagnosticAnalyzer.cs
注:本文中的CompilationAnalysisContext类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论