Use compound assignment in /parser and /debugger (#17726)

This commit is contained in:
Dmitry Volodin
2022-07-25 21:54:36 +05:00
committed by GitHub
parent c6e3ba841b
commit 223c6b8905
6 changed files with 50 additions and 153 deletions
@@ -1057,12 +1057,9 @@ namespace System.Management.Automation
{
get
{
if (_isLocalSession == null)
{
// Remote debug sessions always have a ServerRemoteHost. Otherwise it is a local session.
_isLocalSession = !(((_context.InternalHost.ExternalHost != null) &&
(_context.InternalHost.ExternalHost is System.Management.Automation.Remoting.ServerRemoteHost)));
}
// Remote debug sessions always have a ServerRemoteHost. Otherwise it is a local session.
_isLocalSession ??= !((_context.InternalHost.ExternalHost != null) &&
(_context.InternalHost.ExternalHost is System.Management.Automation.Remoting.ServerRemoteHost));
return _isLocalSession.Value;
}
@@ -1949,10 +1946,7 @@ namespace System.Management.Automation
if (_preserveUnhandledDebugStopEvent)
{
// Lazily create the event object.
if (_preserveDebugStopEvent == null)
{
_preserveDebugStopEvent = new ManualResetEventSlim(true);
}
_preserveDebugStopEvent ??= new ManualResetEventSlim(true);
// Set the event handle to non-signaled.
if (!_preserveDebugStopEvent.IsSet)
@@ -2152,11 +2146,8 @@ namespace System.Management.Automation
{
lock (_syncObject)
{
if (_isSystemLockedDown == null)
{
_isSystemLockedDown = (System.Management.Automation.Security.SystemPolicy.GetSystemLockdownPolicy() ==
System.Management.Automation.Security.SystemEnforcementMode.Enforce);
}
_isSystemLockedDown ??= (System.Management.Automation.Security.SystemPolicy.GetSystemLockdownPolicy() ==
System.Management.Automation.Security.SystemEnforcementMode.Enforce);
}
}
@@ -1110,10 +1110,7 @@ namespace System.Management.Automation.Language
internal static Expression IsStrictMode(int version, Expression executionContext = null)
{
if (executionContext == null)
{
executionContext = ExpressionCache.NullExecutionContext;
}
executionContext ??= ExpressionCache.NullExecutionContext;
return Expression.Call(
CachedReflectionInfo.ExecutionContext_IsStrictVersion,
@@ -1758,18 +1755,15 @@ namespace System.Management.Automation.Language
// Unwrap the wrapped exception
var innerException = tie.InnerException;
var rte = innerException as RuntimeException;
if (rte == null)
{
rte = InterpreterError.NewInterpreterExceptionWithInnerException(
null,
typeof(RuntimeException),
attributeAst.Extent,
"ExceptionConstructingAttribute",
ExtendedTypeSystem.ExceptionConstructingAttribute,
innerException,
innerException.Message,
attributeAst.TypeName.FullName);
}
rte ??= InterpreterError.NewInterpreterExceptionWithInnerException(
null,
typeof(RuntimeException),
attributeAst.Extent,
"ExceptionConstructingAttribute",
ExtendedTypeSystem.ExceptionConstructingAttribute,
innerException,
innerException.Message,
attributeAst.TypeName.FullName);
InterpreterError.UpdateExceptionErrorRecordPosition(rte, attributeAst.Extent);
throw rte;
@@ -2054,10 +2048,7 @@ namespace System.Management.Automation.Language
// The sequence points are identical optimized or not. Regardless, we want to ensure
// that the list is unique no matter when the property is accessed, so make sure it is set just once.
if (scriptBlock.SequencePoints == null)
{
scriptBlock.SequencePoints = _sequencePoints.ToArray();
}
scriptBlock.SequencePoints ??= _sequencePoints.ToArray();
}
private static Action<FunctionContext> CompileTree(Expression<Action<FunctionContext>> lambda, CompileInterpretChoice compileInterpretChoice)
@@ -2126,10 +2117,7 @@ namespace System.Management.Automation.Language
// Can't be exposed to untrusted input - invoking arbitrary code could result in remote code
// execution.
if (lambda == null)
{
lambda = (new Compiler()).CompileSingleExpression(expressionAst, out sequencePoints, out localsTupleType);
}
lambda ??= (new Compiler()).CompileSingleExpression(expressionAst, out sequencePoints, out localsTupleType);
SessionStateInternal oldSessionState = context.EngineSessionState;
try
@@ -3946,10 +3934,7 @@ namespace System.Management.Automation.Language
// This will simply return a Linq.Expression representing the redirection.
var compiledRedirection = VisitFileRedirection(fileRedirectionAst);
if (extraFileRedirectExprs == null)
{
extraFileRedirectExprs = new List<Expression>(commandExpr.Redirections.Count);
}
extraFileRedirectExprs ??= new List<Expression>(commandExpr.Redirections.Count);
// Hold the current 'FileRedirection' instance for later use
var redirectionExpr = NewTemp(typeof(FileRedirection), "fileRedirection");
@@ -6146,10 +6131,7 @@ namespace System.Management.Automation.Language
}
}
if (childExpr == null)
{
childExpr = Compile(convertExpressionAst.Child);
}
childExpr ??= Compile(convertExpressionAst.Child);
if (typeName.FullName.Equals("PSCustomObject", StringComparison.OrdinalIgnoreCase))
{
@@ -443,8 +443,7 @@ namespace System.Management.Automation.Language
private Token PeekToken()
{
Token token = _ungotToken ?? _tokenizer.NextToken();
if (_ungotToken == null)
_ungotToken = token;
_ungotToken ??= token;
return token;
}
@@ -823,10 +822,7 @@ namespace System.Management.Automation.Language
SkipToken();
var statement = UsingStatementRule(token);
SkipNewlinesAndSemicolons();
if (result == null)
{
result = new List<UsingStatementAst>();
}
result ??= new List<UsingStatementAst>();
var usingStatement = statement as UsingStatementAst;
// otherwise returned statement is ErrorStatementAst.
@@ -1790,10 +1786,7 @@ namespace System.Management.Automation.Language
break;
}
if (startExtent == null)
{
startExtent = blockNameToken.Extent;
}
startExtent ??= blockNameToken.Extent;
endExtent = blockNameToken.Extent;
@@ -1934,10 +1927,7 @@ namespace System.Management.Automation.Language
// Track the last statement inside our loop so we don't use the EmptyPipeline
// as our last statement. The last statement is used to track the extent of
// this statement list.
if (firstStatement == null)
{
firstStatement = statement;
}
firstStatement ??= statement;
lastStatement = statement;
@@ -3231,10 +3221,7 @@ namespace System.Management.Automation.Language
if (topLevel)
{
if (_configurationKeywordsDefinedInThisFile == null)
{
_configurationKeywordsDefinedInThisFile = new Dictionary<string, DynamicKeyword>();
}
_configurationKeywordsDefinedInThisFile ??= new Dictionary<string, DynamicKeyword>();
_configurationKeywordsDefinedInThisFile[keywordToAddForThisConfigurationStatement.Keyword] = keywordToAddForThisConfigurationStatement;
}
@@ -3589,10 +3576,7 @@ namespace System.Management.Automation.Language
// ErrorRecovery: don't continue parsing the for statement.
UngetToken(rParen);
if (endErrorStatement == null)
{
endErrorStatement = lParen.Extent;
}
endErrorStatement ??= lParen.Extent;
ReportIncompleteInput(After(endErrorStatement),
nameof(ParserStrings.MissingEndParenthesisAfterStatement),
@@ -3901,10 +3885,7 @@ namespace System.Management.Automation.Language
// we aren't expecting a name, we still do this so that the signature of the implementing function remains
// the same.
ExpressionAst originalInstanceName = instanceName;
if (instanceName == null)
{
instanceName = new StringConstantExpressionAst(nameToken.Extent, elementName, StringConstantType.BareWord);
}
instanceName ??= new StringConstantExpressionAst(nameToken.Extent, elementName, StringConstantType.BareWord);
SkipNewlines();
@@ -4321,10 +4302,7 @@ namespace System.Management.Automation.Language
if (astsOnError != null && astsOnError.Count > 0)
{
if (nestedAsts == null)
{
nestedAsts = new List<Ast>();
}
nestedAsts ??= new List<Ast>();
nestedAsts.AddRange(astsOnError);
lastExtent = astsOnError.Last().Extent;
@@ -4353,10 +4331,7 @@ namespace System.Management.Automation.Language
var classDefn = new TypeDefinitionAst(extent, name.Value, customAttributes?.OfType<AttributeAst>(), members, TypeAttributes.Class, superClassesList);
if (customAttributes != null && customAttributes.OfType<TypeConstraintAst>().Any())
{
if (nestedAsts == null)
{
nestedAsts = new List<Ast>();
}
nestedAsts ??= new List<Ast>();
// no need to report error since the error is reported in method StatementRule
nestedAsts.AddRange(customAttributes.OfType<TypeConstraintAst>());
nestedAsts.Add(classDefn);
@@ -4420,10 +4395,7 @@ namespace System.Management.Automation.Language
if (attribute != null)
{
lastAttribute = attribute;
if (startExtent == null)
{
startExtent = attribute.Extent;
}
startExtent ??= attribute.Extent;
var attributeAst = attribute as AttributeAst;
if (attributeAst != null)
@@ -4443,10 +4415,7 @@ namespace System.Management.Automation.Language
}
token = PeekToken();
if (startExtent == null)
{
startExtent = token.Extent;
}
startExtent ??= token.Extent;
switch (token.Kind)
{
@@ -4661,10 +4630,7 @@ namespace System.Management.Automation.Language
return;
}
if (astsOnError == null)
{
astsOnError = new List<Ast>();
}
astsOnError ??= new List<Ast>();
astsOnError.Add(errAst);
}
@@ -4676,10 +4642,7 @@ namespace System.Management.Automation.Language
return;
}
if (astsOnError == null)
{
astsOnError = new List<Ast>();
}
astsOnError ??= new List<Ast>();
astsOnError.AddRange(errAsts);
}
@@ -5268,11 +5231,9 @@ namespace System.Management.Automation.Language
SetTokenizerMode(oldTokenizerMode);
}
if (baseCtorCallParams == null)
{
baseCtorCallParams ??=
// Assuming implicit default ctor
baseCtorCallParams = new List<ExpressionAst>();
}
new List<ExpressionAst>();
}
Token lCurly = NextToken();
@@ -5561,10 +5522,7 @@ namespace System.Management.Automation.Language
break;
}
if (exceptionTypes == null)
{
exceptionTypes = new List<TypeConstraintAst>();
}
exceptionTypes ??= new List<TypeConstraintAst>();
exceptionTypes.Add(typeConstraintAst);
@@ -6067,10 +6025,7 @@ namespace System.Management.Automation.Language
{
SkipToken();
if (redirections == null)
{
redirections = new RedirectionAst[CommandBaseAst.MaxRedirections];
}
redirections ??= new RedirectionAst[CommandBaseAst.MaxRedirections];
IScriptExtent unused = null;
lastRedirection = RedirectionRule(redirectionToken, redirections, ref unused);
@@ -6091,10 +6046,7 @@ namespace System.Management.Automation.Language
if (commandAst != null)
{
if (startExtent == null)
{
startExtent = commandAst.Extent;
}
startExtent ??= commandAst.Extent;
pipelineElements.Add(commandAst);
}
@@ -6447,10 +6399,7 @@ namespace System.Management.Automation.Language
}
commaToken = token;
if (commandArgs == null)
{
commandArgs = new List<ExpressionAst>();
}
commandArgs ??= new List<ExpressionAst>();
commandArgs.Add(exprAst);
@@ -6618,10 +6567,7 @@ namespace System.Management.Automation.Language
case TokenKind.RedirectInStd:
if ((context & CommandArgumentContext.CommandName) == 0)
{
if (redirections == null)
{
redirections = new RedirectionAst[CommandBaseAst.MaxRedirections];
}
redirections ??= new RedirectionAst[CommandBaseAst.MaxRedirections];
RedirectionRule((RedirectionToken)token, redirections, ref endExtent);
}
@@ -7223,10 +7169,7 @@ namespace System.Management.Automation.Language
}
}
if (expr == null)
{
expr = new TypeExpressionAst(lastAttribute.Extent, lastAttribute.TypeName);
}
expr ??= new TypeExpressionAst(lastAttribute.Extent, lastAttribute.TypeName);
}
for (int i = attributes.Count - 2; i >= 0; --i)
@@ -548,10 +548,7 @@ namespace System.Management.Automation.Language
ofs = t_context.SessionState.PSVariable.GetValue("OFS") as string;
}
if (ofs == null)
{
ofs = " ";
}
ofs ??= " ";
for (int offset = 0; offset < safeValues.Length; offset++)
{
@@ -370,10 +370,7 @@ namespace System.Management.Automation.Language
return typeName._typeDefinitionAst.Type;
}
if (context == null)
{
context = LocalPipeline.GetExecutionContextFromTLS();
}
context ??= LocalPipeline.GetExecutionContextFromTLS();
// Use the explicitly passed-in assembly list when it's specified by the caller.
// Otherwise, retrieve all currently loaded assemblies.
@@ -582,10 +579,7 @@ namespace System.Management.Automation.Language
internal static TypeResolutionState GetDefaultUsingState(ExecutionContext context)
{
if (context == null)
{
context = LocalPipeline.GetExecutionContextFromTLS();
}
context ??= LocalPipeline.GetExecutionContextFromTLS();
if (context != null)
{
@@ -1242,10 +1242,7 @@ namespace System.Management.Automation.Language
// Don't remember these tokens, they aren't useful in $$ and $^.
break;
default:
if (FirstToken == null)
{
FirstToken = token;
}
FirstToken ??= token;
LastToken = token;
break;
@@ -1815,8 +1812,7 @@ namespace System.Management.Automation.Language
}
else if (matchedRequires && _nestedTokensAdjustment == 0)
{
if (RequiresTokens == null)
RequiresTokens = new List<Token>();
RequiresTokens ??= new List<Token>();
RequiresTokens.Add(token);
}
}
@@ -1953,10 +1949,7 @@ namespace System.Management.Automation.Language
PSSnapinToken.StartsWith(parameter.ParameterName, StringComparison.OrdinalIgnoreCase))
{
snapinSpecified = true;
if (requiredSnapins == null)
{
requiredSnapins = new List<PSSnapInSpecification>();
}
requiredSnapins ??= new List<PSSnapInSpecification>();
break;
}
@@ -2222,8 +2215,7 @@ namespace System.Management.Automation.Language
return;
}
if (requiredModules == null)
requiredModules = new List<ModuleSpecification>();
requiredModules ??= new List<ModuleSpecification>();
requiredModules.Add(moduleSpecification);
}
}
@@ -2246,8 +2238,7 @@ namespace System.Management.Automation.Language
}
else
{
if (requiredAssemblies == null)
requiredAssemblies = new List<string>();
requiredAssemblies ??= new List<string>();
if (!requiredAssemblies.Contains((string)arg))
{
@@ -2269,8 +2260,7 @@ namespace System.Management.Automation.Language
}
else
{
if (requiredEditions == null)
requiredEditions = new List<string>();
requiredEditions ??= new List<string>();
var edition = (string)arg;
if (!Utils.IsValidPSEditionValue(edition))