using Microsoft.CodeAnalysis;
namespace Nitride.Generators
{
///
/// Various wrappers around the diagnostics to simplify generation.
///
public static class CodeAnalysisExtensions
{
///
/// Creates an error message to break the build while generating code.
///
/// The context that contains the diagnostic.
/// The normalized message code.
/// The string format for the message.
/// The optional parameters.
public static void Error(
this GeneratorExecutionContext context,
MessageCode messageCode,
string format,
params object[] parameters)
{
Error(context, messageCode, null, format, parameters);
}
///
/// Creates an error message to break the build while generating code.
///
/// The context that contains the diagnostic.
/// The normalized message code.
/// The optional location for the message.
/// The string format for the message.
/// The optional parameters.
public static void Error(
this GeneratorExecutionContext context,
MessageCode messageCode,
Location? location,
string format,
params object[] parameters)
{
context.Message(
messageCode,
location,
DiagnosticSeverity.Error,
format,
parameters);
}
///
/// Creates an informational message to break the build while generating code.
///
/// The context that contains the diagnostic.
/// The normalized message code.
/// The string format for the message.
/// The optional parameters.
public static void Information(
this GeneratorExecutionContext context,
MessageCode messageCode,
string format,
params object[] parameters)
{
Information(context, messageCode, null, format, parameters);
}
///
/// Creates an informational message to break the build while generating code.
///
/// The context that contains the diagnostic.
/// The normalized message code.
/// The optional location for the message.
/// The string format for the message.
/// The optional parameters.
public static void Information(
this GeneratorExecutionContext context,
MessageCode messageCode,
Location? location,
string format,
params object[] parameters)
{
context.Message(
messageCode,
location,
DiagnosticSeverity.Info,
format,
parameters);
}
///
/// Creates a warning message to break the build while generating code.
///
/// The context that contains the diagnostic.
/// The normalized message code.
/// The string format for the message.
/// The optional parameters.
public static void Warning(
this GeneratorExecutionContext context,
MessageCode messageCode,
string format,
params object[] parameters)
{
Warning(context, messageCode, null, format, parameters);
}
///
/// Creates a warning message to break the build while generating code.
///
/// The context that contains the diagnostic.
/// The normalized message code.
/// The optional location for the message.
/// The string format for the message.
/// The optional parameters.
public static void Warning(
this GeneratorExecutionContext context,
MessageCode messageCode,
Location? location,
string format,
params object[] parameters)
{
context.Message(
messageCode,
location,
DiagnosticSeverity.Warning,
format,
parameters);
}
///
/// Creates a message to break the build while generating code.
///
/// The context that contains the diagnostic.
/// The normalized message code.
/// The optional location for the message.
/// The string format for the message.
/// The optional parameters.
/// The severity of the message.
private static void Message(
this GeneratorExecutionContext context,
MessageCode messageCode,
Location? location,
DiagnosticSeverity severity,
string format,
params object[] parameters)
{
context.ReportDiagnostic(
Diagnostic.Create(
"GN" + ((int)messageCode).ToString("D4"),
"Nitride",
string.Format(format, parameters),
severity,
severity,
true,
severity is DiagnosticSeverity.Warning
or DiagnosticSeverity.Info
? 4
: 0,
location: location));
}
}
}