Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/Common.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ dotnet_diagnostic.CA1805.severity = none
dotnet_diagnostic.CA1806.severity = none

# Initialize reference type static fields inline
dotnet_diagnostic.CA1810.severity = suggestion
dotnet_diagnostic.CA1810.severity = warning

# Avoid uninstantiated internal classes
dotnet_diagnostic.CA1812.severity = none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ internal static class ItemGroupLoggingHelper
/// to materialize the Message as that's a declaration assembly. We inject the logic
/// here.
/// </summary>
#pragma warning disable CA1810 // Initialize reference type static fields inline
static ItemGroupLoggingHelper()
#pragma warning restore CA1810 // Initialize reference type static fields inline
{
BuildEventArgs.ResourceStringFormatter = ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword;
TaskParameterEventArgs.MessageGetter = GetTaskParameterText;
Expand Down
27 changes: 4 additions & 23 deletions src/Build/Evaluation/ProjectRootElementCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,21 @@ internal class ProjectRootElementCache : ProjectRootElementCacheBase
/// If this number is increased much higher, the datastructure may
/// need to be changed from a linked list, since it's currently O(n).
/// </remarks>
#pragma warning disable CA1802 // Use literals where appropriate
private static readonly int s_maximumStrongCacheSize = 200;
#pragma warning restore CA1802 // Use literals where appropriate
private static readonly int s_maximumStrongCacheSize =
int.TryParse(Environment.GetEnvironmentVariable("MSBUILDPROJECTROOTELEMENTCACHESIZE"), out int cacheSize) ? cacheSize : 200;

/// <summary>
/// Whether the cache should log activity to the Debug.Out stream
/// </summary>
private static bool s_debugLogCacheActivity;
private static bool s_debugLogCacheActivity = Environment.GetEnvironmentVariable("MSBUILDDEBUGXMLCACHE") == "1";

/// <summary>
/// Whether the cache should check file content for cache entry invalidation.
/// </summary>
/// <remarks>
/// Value shall be true only in case of testing. Outside QA tests it shall be false.
/// </remarks>
private static bool s_сheckFileContent;
private static bool s_сheckFileContent = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MSBUILDCACHECHECKFILECONTENT"));

#if DEBUG
/// <summary>
Expand Down Expand Up @@ -123,24 +122,6 @@ internal class ProjectRootElementCache : ProjectRootElementCacheBase
/// </summary>
private Object _locker = new Object();

/// <summary>
/// Static constructor to choose cache size.
/// </summary>
static ProjectRootElementCache()
{
// Configurable in case a customer has related perf problems after shipping and so that
// we can measure different values for perf easily.
string userSpecifiedSize = Environment.GetEnvironmentVariable("MSBUILDPROJECTROOTELEMENTCACHESIZE");
if (!String.IsNullOrEmpty(userSpecifiedSize))
{
// Not catching as this is an undocumented setting
s_maximumStrongCacheSize = Convert.ToInt32(userSpecifiedSize, NumberFormatInfo.InvariantInfo);
}

s_debugLogCacheActivity = Environment.GetEnvironmentVariable("MSBUILDDEBUGXMLCACHE") == "1";
s_сheckFileContent = !String.IsNullOrEmpty(Environment.GetEnvironmentVariable("MSBUILDCACHECHECKFILECONTENT"));
}

/// <summary>
/// Creates an empty cache.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/MSBuild/XMake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ public enum ExitType
/// <summary>
/// Static constructor
/// </summary>
#pragma warning disable CA1810 // Initialize reference type static fields inline
static MSBuildApp()
#pragma warning restore CA1810 // Initialize reference type static fields inline
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think I'd slightly prefer putting the restore below the end of this function, but I don't care too much.

{
try
{
Expand Down
7 changes: 1 addition & 6 deletions src/Shared/ExceptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ namespace Microsoft.Build.Shared
/// </summary>
internal static class ExceptionHandling
{
private static readonly string s_debugDumpPath;

static ExceptionHandling()
{
s_debugDumpPath = GetDebugDumpPath();
}
private static readonly string s_debugDumpPath = GetDebugDumpPath();

/// <summary>
/// Gets the location of the directory used for diagnostic log files.
Expand Down
28 changes: 10 additions & 18 deletions src/Shared/FrameworkLocationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ internal static class FrameworkLocationHelper
/// <summary>
/// List the supported .net versions.
/// </summary>
private static readonly DotNetFrameworkSpec[] s_dotNetFrameworkSpecs =
private static readonly DotNetFrameworkSpec[] DotNetFrameworkSpecs =
{
// v1.1
new DotNetFrameworkSpecLegacy(
Expand Down Expand Up @@ -225,7 +225,7 @@ internal static class FrameworkLocationHelper
/// <remarks>
/// The items must be ordered by the version, because some methods depend on that fact to find the previous visual studio version.
/// </remarks>
private static readonly VisualStudioSpec[] s_visualStudioSpecs =
private static readonly VisualStudioSpec[] VisualStudioSpecs =
{
// VS10
new VisualStudioSpec(visualStudioVersion100, "Windows\\v7.0A", null, null, new []
Expand Down Expand Up @@ -376,17 +376,11 @@ private static readonly (Version, Version)[,] s_explicitFallbackRulesForPathToDo
};
#endif // FEATURE_WIN32_REGISTRY

private static readonly IReadOnlyDictionary<Version, DotNetFrameworkSpec> s_dotNetFrameworkSpecDict;
private static readonly IReadOnlyDictionary<Version, VisualStudioSpec> s_visualStudioSpecDict;
private static readonly Lazy<IReadOnlyDictionary<Version, DotNetFrameworkSpec>> DotNetFrameworkSpecDict = new(() => DotNetFrameworkSpecs.ToDictionary(spec => spec.Version));
private static readonly Lazy<IReadOnlyDictionary<Version, VisualStudioSpec>> VisualStudioSpecDict = new(() => VisualStudioSpecs.ToDictionary(spec => spec.Version));

#endregion // Static member variables

static FrameworkLocationHelper()
{
s_dotNetFrameworkSpecDict = s_dotNetFrameworkSpecs.ToDictionary(spec => spec.Version);
s_visualStudioSpecDict = s_visualStudioSpecs.ToDictionary(spec => spec.Version);
}

#region Static properties

internal static string PathToDotNetFrameworkV11
Expand Down Expand Up @@ -1118,13 +1112,13 @@ private static string FindRegistryValueUnderKey

private static VisualStudioSpec GetVisualStudioSpec(Version version)
{
ErrorUtilities.VerifyThrowArgument(s_visualStudioSpecDict.TryGetValue(version, out VisualStudioSpec spec), "FrameworkLocationHelper.UnsupportedVisualStudioVersion", version);
ErrorUtilities.VerifyThrowArgument(VisualStudioSpecDict.Value.TryGetValue(version, out VisualStudioSpec spec), "FrameworkLocationHelper.UnsupportedVisualStudioVersion", version);
return spec;
}

private static DotNetFrameworkSpec GetDotNetFrameworkSpec(Version version)
{
ErrorUtilities.VerifyThrowArgument(s_dotNetFrameworkSpecDict.TryGetValue(version, out DotNetFrameworkSpec spec), "FrameworkLocationHelper.UnsupportedFrameworkVersion", version);
ErrorUtilities.VerifyThrowArgument(DotNetFrameworkSpecDict.Value.TryGetValue(version, out DotNetFrameworkSpec spec), "FrameworkLocationHelper.UnsupportedFrameworkVersion", version);
return spec;
}

Expand Down Expand Up @@ -1473,11 +1467,11 @@ public virtual string GetPathToDotNetFrameworkSdkTools(VisualStudioSpec visualSt
// i.e. fallback to v110 if the current visual studio version is v120.
if (!foundExplicitRule)
{
int index = Array.IndexOf(s_visualStudioSpecs, visualStudioSpec);
int index = Array.IndexOf(VisualStudioSpecs, visualStudioSpec);
if (index > 0)
{
// The items in the array "visualStudioSpecs" must be ordered by version. That would allow us to fallback to the previous visual studio version easily.
VisualStudioSpec fallbackVisualStudioSpec = s_visualStudioSpecs[index - 1];
VisualStudioSpec fallbackVisualStudioSpec = VisualStudioSpecs[index - 1];
generatedPathToDotNetFrameworkSdkTools = FallbackToPathToDotNetFrameworkSdkToolsInPreviousVersion(
this.Version,
fallbackVisualStudioSpec.Version);
Expand Down Expand Up @@ -1570,10 +1564,8 @@ public virtual string GetPathToWindowsSdk()
#if FEATURE_WIN32_REGISTRY
private static string FallbackToPathToDotNetFrameworkSdkToolsInPreviousVersion(Version dotNetFrameworkVersion, Version visualStudioVersion)
{
VisualStudioSpec visualStudioSpec;
DotNetFrameworkSpec dotNetFrameworkSpec;
if (s_visualStudioSpecDict.TryGetValue(visualStudioVersion, out visualStudioSpec)
&& s_dotNetFrameworkSpecDict.TryGetValue(dotNetFrameworkVersion, out dotNetFrameworkSpec)
if (VisualStudioSpecDict.Value.TryGetValue(visualStudioVersion, out VisualStudioSpec visualStudioSpec)
&& DotNetFrameworkSpecDict.Value.TryGetValue(dotNetFrameworkVersion, out DotNetFrameworkSpec dotNetFrameworkSpec)
&& visualStudioSpec.SupportedDotNetFrameworkVersions.Contains(dotNetFrameworkVersion))
{
return dotNetFrameworkSpec.GetPathToDotNetFrameworkSdkTools(visualStudioSpec);
Expand Down
11 changes: 1 addition & 10 deletions src/Shared/MSBuildNameIgnoreCaseComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,7 @@ internal class MSBuildNameIgnoreCaseComparer : IConstrainedEqualityComparer<stri
/// <summary>
/// The processor architecture on which we are running, but default it will be x86
/// </summary>
private static readonly NativeMethodsShared.ProcessorArchitectures s_runningProcessorArchitecture;

/// <summary>
/// We need a static constructor to retrieve the running ProcessorArchitecture that way we can
/// avoid using optimized code that will not run correctly on IA64 due to alignment issues
/// </summary>
static MSBuildNameIgnoreCaseComparer()
{
s_runningProcessorArchitecture = NativeMethodsShared.ProcessorArchitecture;
}
private static readonly NativeMethodsShared.ProcessorArchitectures s_runningProcessorArchitecture = NativeMethodsShared.ProcessorArchitecture;

/// <summary>
/// The default immutable comparer instance.
Expand Down
9 changes: 1 addition & 8 deletions src/Shared/TypeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal class TypeLoader
/// <summary>
/// AssemblyContextLoader used to load DLLs outside of msbuild.exe directory
/// </summary>
private static readonly CoreClrAssemblyLoader s_coreClrAssemblyLoader;
private static readonly CoreClrAssemblyLoader s_coreClrAssemblyLoader = new CoreClrAssemblyLoader();
#endif

/// <summary>
Expand All @@ -41,13 +41,6 @@ internal class TypeLoader
/// </summary>
private Func<Type, object, bool> _isDesiredType;

#if FEATURE_ASSEMBLYLOADCONTEXT
static TypeLoader()
{
s_coreClrAssemblyLoader = new CoreClrAssemblyLoader();
}
#endif

/// <summary>
/// Constructor.
/// </summary>
Expand Down
19 changes: 9 additions & 10 deletions src/Tasks/CultureInfoCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,34 @@ namespace Microsoft.Build.Tasks
/// </summary>
internal static class CultureInfoCache
{
private static readonly HashSet<string> ValidCultureNames;
private static readonly HashSet<string> ValidCultureNames = InitializeValidCultureNames();

static CultureInfoCache()
static HashSet<string> InitializeValidCultureNames()
{
ValidCultureNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

#if !FEATURE_CULTUREINFO_GETCULTURES
if (!AssemblyUtilities.CultureInfoHasGetCultures())
{
ValidCultureNames = HardcodedCultureNames;
return;
return HardcodedCultureNames;
}
#endif

HashSet<string> validCultureNames = new(StringComparer.OrdinalIgnoreCase);
foreach (CultureInfo cultureName in AssemblyUtilities.GetAllCultures())
{
ValidCultureNames.Add(cultureName.Name);
validCultureNames.Add(cultureName.Name);
}

// https://docs.microsoft.com/en-gb/windows/desktop/Intl/using-pseudo-locales-for-localization-testing
// These pseudo-locales are available in versions of Windows from Vista and later.
// However, from Windows 10, version 1803, they are not returned when enumerating the
// installed cultures, even if the registry keys are set. Therefore, add them to the list manually.
var pseudoLocales = new[] { "qps-ploc", "qps-ploca", "qps-plocm", "qps-Latn-x-sh" };
string[] pseudoLocales = new[] { "qps-ploc", "qps-ploca", "qps-plocm", "qps-Latn-x-sh" };

foreach (string pseudoLocale in pseudoLocales)
{
ValidCultureNames.Add(pseudoLocale);
validCultureNames.Add(pseudoLocale);
}

return validCultureNames;
}

/// <summary>
Expand Down
27 changes: 2 additions & 25 deletions src/Tasks/GenerateResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,29 +546,6 @@ public GenerateResource()
// do nothing
}

#if FEATURE_COM_INTEROP
/// <summary>
/// Static constructor checks the registry opt-out for mark-of-the-web rejection.
/// </summary>
static GenerateResource()
{
if (!NativeMethodsShared.IsWindows)
{
allowMOTW = true;
return;
}
try
{
object allowUntrustedFiles = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\SDK", "AllowProcessOfUntrustedResourceFiles", null);
if (allowUntrustedFiles is string allowUntrustedFilesString)
{
allowMOTW = allowUntrustedFilesString.Equals("true", StringComparison.OrdinalIgnoreCase);
}
}
catch { }
}
#endif

/// <summary>
/// Logs a Resgen.exe command line that indicates what parameters were
/// passed to this task. Since this task is replacing Resgen, and we used
Expand Down Expand Up @@ -931,7 +908,7 @@ public override bool Execute()
}

#if FEATURE_COM_INTEROP
private static bool allowMOTW;
private static readonly bool AllowMOTW = !NativeMethodsShared.IsWindows || (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\SDK", "AllowProcessOfUntrustedResourceFiles", null) is string allowUntrustedFiles && allowUntrustedFiles.Equals("true", StringComparison.OrdinalIgnoreCase));

private const string CLSID_InternetSecurityManager = "7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4";
private const uint ZoneInternet = 3;
Expand All @@ -942,7 +919,7 @@ public override bool Execute()
private bool IsDangerous(String filename)
{
// If they are opted out, there's no work to do
if (allowMOTW)
if (AllowMOTW)
{
return false;
}
Expand Down
Loading