Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 0 additions & 14 deletions src/QsCompiler/CommandLineTool/Commands/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,9 @@ private static IEnumerable<string> SplitCommandLineArguments(string commandLine)
/// <summary>
/// Reads the content off all given response files and tries to parse their concatenated content as command line arguments.
/// Logs a suitable exceptions and returns null if the parsing fails.
/// Throws an ArgumentNullException if the given sequence of responseFiles is null.
/// </summary>
private static BuildOptions? FromResponseFiles(IEnumerable<string> responseFiles)
{
if (responseFiles == null)
{
throw new ArgumentNullException(nameof(responseFiles));
}
var commandLine = string.Join(" ", responseFiles.Select(File.ReadAllText));
var args = SplitCommandLineArguments(commandLine);
var parsed = Parser.Default.ParseArguments<BuildOptions>(args);
Expand All @@ -177,18 +172,9 @@ private static IEnumerable<string> SplitCommandLineArguments(string commandLine)
/// <summary>
/// Builds the compilation for the Q# code or Q# snippet and referenced assemblies defined by the given options.
/// Returns a suitable error code if one of the compilation or generation steps fails.
/// Throws an ArgumentNullException if any of the given arguments is null.
/// </summary>
public static int Run(BuildOptions options, ConsoleLogger logger)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}
if (!BuildOptions.IncorporateResponseFiles(options, out var incorporated))
{
logger.Log(ErrorCode.InvalidCommandLineArgsInResponseFiles, Array.Empty<string>());
Expand Down
35 changes: 0 additions & 35 deletions src/QsCompiler/CommandLineTool/Commands/Diagnose.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,9 @@ public static IEnumerable<Example> UsageExamples
/// Logs the content of each file in the given compilation as Information using the given logger.
/// If the id of a file is consistent with the one assigned to a code snippet,
/// strips the lines of code that correspond to the wrapping defined by WrapSnippet.
/// Throws an ArgumentNullException if the given compilation is null.
/// </summary>
private static void PrintFileContentInMemory(Compilation compilation, ILogger logger)
{
if (compilation == null)
{
throw new ArgumentNullException(nameof(compilation));
}
foreach (var file in compilation.SourceFiles)
{
IEnumerable<string> inMemory = compilation.FileContent[file];
Expand All @@ -110,14 +105,9 @@ private static void PrintFileContentInMemory(Compilation compilation, ILogger lo
/// Logs the tokenization of each file in the given compilation as Information using the given logger.
/// If the id of a file is consistent with the one assigned to a code snippet,
/// strips the tokens that correspond to the wrapping defined by WrapSnippet.
/// Throws an ArgumentNullException if the given compilation is null.
/// </summary>
private static void PrintContentTokenization(Compilation compilation, ILogger logger)
{
if (compilation == null)
{
throw new ArgumentNullException(nameof(compilation));
}
foreach (var file in compilation.SourceFiles)
{
var tokenization = compilation.Tokenization[file].Select(tokens => tokens.Select(token => token.Kind));
Expand Down Expand Up @@ -152,14 +142,9 @@ private static void PrintContentTokenization(Compilation compilation, ILogger lo
/// If the id of a file is consistent with the one assigned to a code snippet,
/// strips the lines of code that correspond to the wrapping defined by WrapSnippet.
/// Throws an ArgumentException if this is not possible because the given syntax tree is inconsistent with that wrapping.
/// Throws an ArgumentNullException if the given compilation is null.
/// </summary>
private static void PrintSyntaxTree(IEnumerable<QsNamespace>? evaluatedTree, Compilation compilation, ILogger logger)
{
if (compilation == null)
{
throw new ArgumentNullException(nameof(compilation));
}
evaluatedTree ??= compilation.SyntaxTree.Values;

foreach (var file in compilation.SourceFiles)
Expand Down Expand Up @@ -191,14 +176,9 @@ void PrintTree(string serialization) => logger.Log(
/// If the id of a file is consistent with the one assigned to a code snippet,
/// strips the lines of code that correspond to the wrapping defined by WrapSnippet.
/// Throws an ArgumentException if this is not possible because the given syntax tree is inconsistent with that wrapping.
/// Throws an ArgumentNullException if the given compilation is null.
/// </summary>
private static void PrintGeneratedQs(IEnumerable<QsNamespace>? evaluatedTree, Compilation compilation, ILogger logger)
{
if (compilation == null)
{
throw new ArgumentNullException(nameof(compilation));
}
evaluatedTree ??= compilation.SyntaxTree.Values;

foreach (var file in compilation.SourceFiles)
Expand All @@ -224,14 +204,9 @@ private static void PrintGeneratedQs(IEnumerable<QsNamespace>? evaluatedTree, Co
/// <summary>
/// Strips the namespace and callable declaration that is consistent with the wrapping defined by WrapSnippet.
/// Throws an ArgumentException if this is not possible because the given syntax tree is inconsistent with that wrapping.
/// Throws an ArgumentNullException if the given syntaxTree is null.
/// </summary>
public static IEnumerable<QsStatement> StripSnippetWrapping(IEnumerable<QsNamespace> syntaxTree)
{
if (syntaxTree == null)
{
throw new ArgumentNullException(nameof(syntaxTree));
}
var incorrectWrapperException = new ArgumentException("syntax tree does not reflect the expected wrapper");
if (syntaxTree.Count() != 1 || syntaxTree.Single().Elements.Count() != 1)
{
Expand All @@ -253,19 +228,9 @@ public static IEnumerable<QsStatement> StripSnippetWrapping(IEnumerable<QsNamesp
/// Builds the compilation for the Q# code or Q# snippet and referenced assemblies defined by the given options.
/// Prints the data structures requested by the given options using the given logger.
/// Returns a suitable error code if one of the compilation or generation steps fails.
/// Throws an ArgumentNullException if any of the given arguments is null.
/// </summary>
public static int Run(DiagnoseOptions options, ConsoleLogger logger)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}

if (!options.ParseAssemblyProperties(out var assemblyConstants))
{
logger.Log(WarningCode.InvalidAssemblyProperties, Array.Empty<string>());
Expand Down
21 changes: 0 additions & 21 deletions src/QsCompiler/CommandLineTool/Commands/Format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,9 @@ public static string UpdateArrayLiterals(string fileContent)
/// If the id of a file is consistent with the one assigned to a code snippet,
/// strips the lines of code that correspond to the wrapping defined by WrapSnippet.
/// Throws an ArgumentException if this is not possible because the given syntax tree is inconsistent with that wrapping.
/// Throws an ArgumentNullException if the given compilation is null.
/// </summary>
private static IEnumerable<string> GenerateQsCode(Compilation compilation, NonNullable<string> file, ILogger logger)
{
if (compilation == null)
{
throw new ArgumentNullException(nameof(compilation));
}
if (Options.IsCodeSnippet(file))
{
var subtree = compilation.SyntaxTree.Values.Select(ns => FilterBySourceFile.Apply(ns, file)).Where(ns => ns.Elements.Any());
Expand Down Expand Up @@ -119,15 +114,9 @@ string FormatComments(IEnumerable<string> comments) =>
/// logs the generated code using the given logger.
/// Creates a file containing the generated code in the given output folder otherwise.
/// Returns true if the generation succeeded, and false if an exception was thrown.
/// Throws an ArgumentNullException if the given compilation, the file uri or its absolute path are null.
/// </summary>
private static bool GenerateFormattedQsFile(Compilation compilation, NonNullable<string> fileName, string? outputFolder, ILogger logger)
{
if (compilation == null)
{
throw new ArgumentNullException(nameof(compilation));
}

var code = Enumerable.Empty<string>();
try
{
Expand Down Expand Up @@ -158,19 +147,9 @@ private static bool GenerateFormattedQsFile(Compilation compilation, NonNullable
/// Generates formatted Q# code for each source file in the compilation.
/// Returns a suitable error code if some of the source files or references could not be found or loaded, or if the Q# generation failed.
/// Compilation errors are not reflected in the return code, but are logged using the given logger.
/// Throws an ArgumentNullException if any of the given arguments is null.
/// </summary>
public static int Run(FormatOptions options, ConsoleLogger logger)
{
if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
if (logger == null)
{
throw new ArgumentNullException(nameof(logger));
}

ImmutableDictionary<Uri, string> LoadSources(SourceFileLoader loadFromDisk) =>
options.LoadSourcesOrSnippet(logger)(loadFromDisk)
.ToImmutableDictionary(entry => entry.Key, entry => UpdateArrayLiterals(entry.Value)); // manually replace array literals
Expand Down
2 changes: 1 addition & 1 deletion src/QsCompiler/CommandLineTool/LoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void RemoveFromPath(params string[] paths) =>

private LoadContext(string parentAssembly)
{
this.PathToParentAssembly = parentAssembly ?? throw new ArgumentNullException(nameof(parentAssembly));
this.PathToParentAssembly = parentAssembly;
this.resolver = new AssemblyDependencyResolver(this.PathToParentAssembly);
this.fallbackPaths = new HashSet<string>();
this.Resolving += this.OnResolving;
Expand Down
5 changes: 0 additions & 5 deletions src/QsCompiler/CommandLineTool/Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,9 @@ public ConsoleLogger(
/// <summary>
/// Prints the given message to the Console.
/// Errors and Warnings are printed to the error stream.
/// Throws an ArgumentNullException if the given message is null.
/// </summary>
private static void PrintToConsole(DiagnosticSeverity severity, string message)
{
if (message == null)
{
throw new ArgumentNullException(nameof(message));
}
var (stream, color) =
severity == DiagnosticSeverity.Error ? (Console.Error, ConsoleColor.Red) :
severity == DiagnosticSeverity.Warning ? (Console.Error, ConsoleColor.Yellow) :
Expand Down
31 changes: 4 additions & 27 deletions src/QsCompiler/CompilationManager/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,16 @@ public static class AssemblyLoader
/// Returns false if some of the content could not be loaded successfully,
/// possibly because the referenced assembly has been compiled with an older compiler version.
/// If onDeserializationException is specified, invokes the given action on any exception thrown during deserialization.
/// Throws an ArgumentNullException if the given uri is null.
/// Throws an ArgumentException if the URI is not an absolute file URI.
/// Throws a FileNotFoundException if no file with the given name exists.
/// Throws the corresponding exceptions if the information cannot be extracted.
/// </summary>
public static bool LoadReferencedAssembly(Uri asm, out References.Headers headers, bool ignoreDllResources = false, Action<Exception>? onDeserializationException = null)
{
if (asm == null)
var id = CompilationUnitManager.GetFileId(asm);
if (!File.Exists(asm.LocalPath))
{
throw new ArgumentNullException(nameof(asm));
}
if (!CompilationUnitManager.TryGetFileId(asm, out var id) || !File.Exists(asm.LocalPath))
{
throw new FileNotFoundException($"The uri '{asm}' given to the assembly loader is invalid or the file does not exist.");
throw new FileNotFoundException($"The file '{asm.LocalPath}' given to the assembly loader does not exist.");
}

using var stream = File.OpenRead(asm.LocalPath);
Expand All @@ -65,18 +62,13 @@ public static bool LoadReferencedAssembly(Uri asm, out References.Headers header
/// possibly because the referenced assembly has been compiled with an older compiler version.
/// Catches any exception throw upon loading the compilation, and invokes onException with it if such an action has been specified.
/// Sets the out parameter to null if an exception occurred during loading.
/// Throws an ArgumentNullException if the given uri is null.
/// Throws a FileNotFoundException if no file with the given name exists.
/// </summary>
public static bool LoadReferencedAssembly(
string asmPath,
[NotNullWhen(true)] out QsCompilation? compilation,
Action<Exception>? onException = null)
{
if (asmPath == null)
{
throw new ArgumentNullException(nameof(asmPath));
}
if (!File.Exists(asmPath))
{
throw new FileNotFoundException($"The file '{asmPath}' does not exist.");
Expand All @@ -102,17 +94,12 @@ public static bool LoadReferencedAssembly(
/// Given a stream containing the binary representation of compiled Q# code, returns the corresponding Q# compilation.
/// Returns true if the compilation could be deserialized without throwing an exception, and false otherwise.
/// If onDeserializationException is specified, invokes the given action on any exception thrown during deserialization.
/// Throws an ArgumentNullException if the given stream is null, but ignores exceptions thrown during deserialization.
/// </summary>
public static bool LoadSyntaxTree(
Stream stream,
[NotNullWhen(true)] out QsCompilation? compilation,
Action<Exception>? onDeserializationException = null)
{
if (stream == null)
{
throw new ArgumentNullException(nameof(stream));
}
using var reader = new BsonDataReader(stream);
(compilation, reader.ReadRootValueAsArray) = (null, false);
try
Expand Down Expand Up @@ -141,18 +128,13 @@ private static ImmutableDictionary<string, ManifestResource> Resources(this Meta
/// Given a reader for the byte stream of a dotnet dll, loads any Q# compilation included as a resource.
/// Returns true as well as the loaded compilation if the given dll includes a suitable resource, and returns false otherwise.
/// If onDeserializationException is specified, invokes the given action on any exception thrown during deserialization.
/// Throws an ArgumentNullException if any of the given readers is null.
/// May throw an exception if the given binary file has been compiled with a different compiler version.
/// </summary>
private static bool FromResource(
PEReader assemblyFile,
[NotNullWhen(true)] out QsCompilation? compilation,
Action<Exception>? onDeserializationException = null)
{
if (assemblyFile == null)
{
throw new ArgumentNullException(nameof(assemblyFile));
}
var metadataReader = assemblyFile.GetMetadataReader();
compilation = null;

Expand Down Expand Up @@ -239,15 +221,10 @@ private static (string, string)? GetAttribute(MetadataReader metadataReader, Cus
/// Given a reader for the byte stream of a dotnet dll, read its custom attributes and
/// returns a tuple containing the name of the attribute and the constructor argument
/// for all attributes defined in a Microsoft.Quantum* namespace.
/// Throws an ArgumentNullException if the given stream is null.
/// Throws the corresponding exceptions if the information cannot be extracted.
/// </summary>
private static IEnumerable<(string, string)> LoadHeaderAttributes(PEReader assemblyFile)
{
if (assemblyFile == null)
{
throw new ArgumentNullException(nameof(assemblyFile));
}
var metadataReader = assemblyFile.GetMetadataReader();
return metadataReader.GetAssemblyDefinition().GetCustomAttributes()
.Select(metadataReader.GetCustomAttribute)
Expand Down
Loading