/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
namespace Opc.Ua.SourceGeneration
{
internal static partial class SourceGenerator
{
///
/// The namespace prefix for generated code
///
public const string Name = nameof(StackSourceGenerator);
public static readonly DiagnosticDescriptor GenericError = new(
id: "STACKGEN001",
title: "Error",
messageFormat: (LocalizableString)"Error during stack generation '{0}'",
category: Name,
DiagnosticSeverity.Error,
isEnabledByDefault: true,
helpLinkUri: "www.opcfoundation.org",
customTags: ["opcua"]);
public static readonly DiagnosticDescriptor GenericWarning = new(
id: "STACKGEN002",
title: "Warning",
messageFormat: (LocalizableString)"Warning during stack generation '{0}'",
category: Name,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
helpLinkUri: "www.opcfoundation.org",
customTags: ["opcua"]);
public static readonly DiagnosticDescriptor Exception = new(
id: "STACKGEN003",
title: "Exception",
messageFormat: (LocalizableString)"Exception during stack generation '{0}': {1}",
category: Name,
DiagnosticSeverity.Error,
isEnabledByDefault: true,
helpLinkUri: "www.opcfoundation.org",
customTags: ["opcua"]);
///
/// A BrowseName from the design model contained characters that
/// required escaping when interpolated into a generated C# string
/// literal. See the matching descriptor in the model-side
/// SourceGenerator for the full rationale.
///
public static readonly DiagnosticDescriptor BrowseNameUnsafe = new(
id: "STACKGEN020",
title: "BrowseName requires C# string-literal escaping (UASG_BROWSENAME_UNSAFE)",
messageFormat: (LocalizableString)("BrowseName '{0}' contained characters that " +
"required escaping when emitted into a C# string literal. The generator " +
"escaped the value and continued; clean up the design XML to remove the " +
"warning."),
category: Name,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
helpLinkUri: "www.opcfoundation.org",
customTags: ["opcua"]);
///
/// Get diagnostic descriptor for event id
///
public static bool TryGetDiagnostic(
LogLevel logLevel,
EventId eventId,
out DiagnosticDescriptor descriptor)
{
descriptor = eventId.Id switch
{
1 => GenericError,
2 => GenericWarning,
3 => Exception,
20 => BrowseNameUnsafe,
_ => logLevel switch
{
// Critical is reported as enabled by the generator's logger,
// so it has to map to a diagnostic or the message is dropped.
LogLevel.Critical => GenericError,
LogLevel.Error => GenericError,
LogLevel.Warning => GenericWarning,
_ => null
}
};
return descriptor != null;
}
}
}