/
CommandLineParser.cs
1207 lines (1050 loc) · 44 KB
/
CommandLineParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
public abstract class CommandLineParser
{
private readonly CommonMessageProvider _messageProvider;
internal readonly bool IsScriptCommandLineParser;
private static readonly char[] s_searchPatternTrimChars = new char[] { '\t', '\n', '\v', '\f', '\r', ' ', '\x0085', '\x00a0' };
internal const string ErrorLogOptionFormat = "<file>[,version={1|1.0|2|2.1}]";
internal CommandLineParser(CommonMessageProvider messageProvider, bool isScriptCommandLineParser)
{
RoslynDebug.Assert(messageProvider != null);
_messageProvider = messageProvider;
IsScriptCommandLineParser = isScriptCommandLineParser;
}
internal CommonMessageProvider MessageProvider
{
get { return _messageProvider; }
}
protected abstract string RegularFileExtension { get; }
protected abstract string ScriptFileExtension { get; }
// internal for testing
internal virtual TextReader CreateTextFileReader(string fullPath)
{
return new StreamReader(
new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read),
detectEncodingFromByteOrderMarks: true);
}
/// <summary>
/// Enumerates files in the specified directory and subdirectories whose name matches the given pattern.
/// </summary>
/// <param name="directory">Full path of the directory to enumerate.</param>
/// <param name="fileNamePattern">File name pattern. May contain wildcards '*' (matches zero or more characters) and '?' (matches any character).</param>
/// <param name="searchOption">Specifies whether to search the specified <paramref name="directory"/> only, or all its subdirectories as well.</param>
/// <returns>Sequence of file paths.</returns>
internal virtual IEnumerable<string> EnumerateFiles(string? directory, string fileNamePattern, SearchOption searchOption)
{
if (directory is null)
{
return SpecializedCollections.EmptyEnumerable<string>();
}
Debug.Assert(PathUtilities.IsAbsolute(directory));
return Directory.EnumerateFiles(directory, fileNamePattern, searchOption);
}
internal abstract CommandLineArguments CommonParse(IEnumerable<string> args, string baseDirectory, string? sdkDirectory, string? additionalReferenceDirectories);
/// <summary>
/// Parses a command line.
/// </summary>
/// <param name="args">A collection of strings representing the command line arguments.</param>
/// <param name="baseDirectory">The base directory used for qualifying file locations.</param>
/// <param name="sdkDirectory">The directory to search for mscorlib, or null if not available.</param>
/// <param name="additionalReferenceDirectories">A string representing additional reference paths.</param>
/// <returns>a <see cref="CommandLineArguments"/> object representing the parsed command line.</returns>
public CommandLineArguments Parse(IEnumerable<string> args, string baseDirectory, string? sdkDirectory, string? additionalReferenceDirectories)
{
return CommonParse(args, baseDirectory, sdkDirectory, additionalReferenceDirectories);
}
private static bool IsOption(string arg)
{
return !string.IsNullOrEmpty(arg) && (arg[0] == '/' || arg[0] == '-');
}
internal static bool TryParseOption(string arg, [NotNullWhen(true)] out string? name, out string? value)
{
if (!IsOption(arg))
{
name = null;
value = null;
return false;
}
// handle stdin operator
if (arg == "-")
{
name = arg;
value = null;
return true;
}
int colon = arg.IndexOf(':');
// temporary heuristic to detect Unix-style rooted paths
// pattern /goo/* or //* will not be treated as a compiler option
//
// TODO: consider introducing "/s:path" to disambiguate paths starting with /
if (arg.Length > 1 && arg[0] != '-')
{
int separator = arg.IndexOf('/', 1);
if (separator > 0 && (colon < 0 || separator < colon))
{
// "/goo/
// "//
name = null;
value = null;
return false;
}
}
if (colon >= 0)
{
name = arg.Substring(1, colon - 1);
value = arg.Substring(colon + 1);
}
else
{
name = arg.Substring(1);
value = null;
}
name = name.ToLowerInvariant();
return true;
}
internal ErrorLogOptions? ParseErrorLogOptions(
string arg,
IList<Diagnostic> diagnostics,
string? baseDirectory,
out bool diagnosticAlreadyReported)
{
diagnosticAlreadyReported = false;
IEnumerator<string> partsEnumerator = ParseSeparatedStrings(arg, s_pathSeparators, StringSplitOptions.RemoveEmptyEntries).GetEnumerator();
if (!partsEnumerator.MoveNext() || string.IsNullOrEmpty(partsEnumerator.Current))
{
return null;
}
string? path = ParseGenericPathToFile(partsEnumerator.Current, diagnostics, baseDirectory);
if (path is null)
{
// ParseGenericPathToFile already reported the failure, so the caller should not
// report its own failure.
diagnosticAlreadyReported = true;
return null;
}
const char ParameterNameValueSeparator = '=';
SarifVersion sarifVersion = SarifVersion.Default;
if (partsEnumerator.MoveNext() && !string.IsNullOrEmpty(partsEnumerator.Current))
{
string part = partsEnumerator.Current;
string versionParameterDesignator = "version" + ParameterNameValueSeparator;
int versionParameterDesignatorLength = versionParameterDesignator.Length;
if (!(
part.Length > versionParameterDesignatorLength &&
part.Substring(0, versionParameterDesignatorLength).Equals(versionParameterDesignator, StringComparison.OrdinalIgnoreCase) &&
SarifVersionFacts.TryParse(part.Substring(versionParameterDesignatorLength), out sarifVersion)
))
{
return null;
}
}
if (partsEnumerator.MoveNext())
{
return null;
}
return new ErrorLogOptions(path, sarifVersion);
}
internal static void ParseAndNormalizeFile(
string unquoted,
string? baseDirectory,
out string? outputFileName,
out string? outputDirectory,
out string invalidPath)
{
outputFileName = null;
outputDirectory = null;
invalidPath = unquoted;
string? resolvedPath = FileUtilities.ResolveRelativePath(unquoted, baseDirectory);
if (resolvedPath != null)
{
try
{
// Check some ancient reserved device names, such as COM1,..9, LPT1..9, PRN, CON, or AUX etc., and bail out earlier
// Win32 API - GetFullFileName - will resolve them, say 'COM1', as "\\.\COM1"
resolvedPath = Path.GetFullPath(resolvedPath);
// preserve possible invalid path info for diagnostic purpose
invalidPath = resolvedPath;
outputFileName = Path.GetFileName(resolvedPath);
outputDirectory = Path.GetDirectoryName(resolvedPath);
}
catch (Exception)
{
resolvedPath = null;
}
if (outputFileName != null)
{
// normalize file
outputFileName = RemoveTrailingSpacesAndDots(outputFileName);
}
}
if (resolvedPath == null ||
// NUL-terminated, non-empty, valid Unicode strings
!MetadataHelpers.IsValidMetadataIdentifier(outputDirectory) ||
!MetadataHelpers.IsValidMetadataIdentifier(outputFileName))
{
outputFileName = null;
}
}
/// <summary>
/// Trims all '.' and whitespace from the end of the path
/// </summary>
[return: NotNullIfNotNull("path")]
internal static string? RemoveTrailingSpacesAndDots(string? path)
{
if (path == null)
{
return path;
}
int length = path.Length;
for (int i = length - 1; i >= 0; i--)
{
char c = path[i];
if (!char.IsWhiteSpace(c) && c != '.')
{
return i == (length - 1) ? path : path.Substring(0, i + 1);
}
}
return string.Empty;
}
protected ImmutableArray<KeyValuePair<string, string>> ParsePathMap(string pathMap, IList<Diagnostic> errors)
{
if (pathMap.IsEmpty())
{
return ImmutableArray<KeyValuePair<string, string>>.Empty;
}
var pathMapBuilder = ArrayBuilder<KeyValuePair<string, string>>.GetInstance();
foreach (var kEqualsV in SplitWithDoubledSeparatorEscaping(pathMap, ','))
{
if (kEqualsV.IsEmpty())
{
continue;
}
var kv = SplitWithDoubledSeparatorEscaping(kEqualsV, '=');
if (kv.Length != 2)
{
errors.Add(Diagnostic.Create(_messageProvider, _messageProvider.ERR_InvalidPathMap, kEqualsV));
continue;
}
var from = kv[0];
var to = kv[1];
if (from.Length == 0 || to.Length == 0)
{
errors.Add(Diagnostic.Create(_messageProvider, _messageProvider.ERR_InvalidPathMap, kEqualsV));
}
else
{
from = PathUtilities.EnsureTrailingSeparator(from);
to = PathUtilities.EnsureTrailingSeparator(to);
pathMapBuilder.Add(new KeyValuePair<string, string>(from, to));
}
}
return pathMapBuilder.ToImmutableAndFree();
}
/// <summary>
/// Splits specified <paramref name="str"/> on <paramref name="separator"/>
/// treating two consecutive separators as if they were a single non-separating character.
/// E.g. "a,,b,c" split on ',' yields ["a,b", "c"].
/// </summary>
internal static string[] SplitWithDoubledSeparatorEscaping(string str, char separator)
{
if (str.Length == 0)
{
return Array.Empty<string>();
}
var result = ArrayBuilder<string>.GetInstance();
var pooledPart = PooledStringBuilder.GetInstance();
var part = pooledPart.Builder;
int i = 0;
while (i < str.Length)
{
char c = str[i++];
if (c == separator)
{
if (i < str.Length && str[i] == separator)
{
i++;
}
else
{
result.Add(part.ToString());
part.Clear();
continue;
}
}
part.Append(c);
}
result.Add(part.ToString());
pooledPart.Free();
return result.ToArrayAndFree();
}
internal void ParseOutputFile(
string value,
IList<Diagnostic> errors,
string? baseDirectory,
out string? outputFileName,
out string? outputDirectory)
{
string unquoted = RemoveQuotesAndSlashes(value);
ParseAndNormalizeFile(unquoted, baseDirectory, out outputFileName, out outputDirectory, out string? invalidPath);
if (outputFileName == null ||
!MetadataHelpers.IsValidAssemblyOrModuleName(outputFileName))
{
errors.Add(Diagnostic.Create(_messageProvider, _messageProvider.FTL_InvalidInputFileName, invalidPath));
outputFileName = null;
outputDirectory = baseDirectory;
}
}
internal string? ParsePdbPath(
string value,
IList<Diagnostic> errors,
string? baseDirectory)
{
string? pdbPath = null;
string unquoted = RemoveQuotesAndSlashes(value);
ParseAndNormalizeFile(unquoted, baseDirectory, out string? outputFileName, out string? outputDirectory, out string? invalidPath);
if (outputFileName == null ||
PathUtilities.ChangeExtension(outputFileName, extension: null).Length == 0)
{
errors.Add(Diagnostic.Create(_messageProvider, _messageProvider.FTL_InvalidInputFileName, invalidPath));
}
else
{
// If outputDirectory were null, then outputFileName would be null (see ParseAndNormalizeFile)
Debug.Assert(outputDirectory is object);
pdbPath = Path.ChangeExtension(Path.Combine(outputDirectory, outputFileName), ".pdb");
}
return pdbPath;
}
internal string? ParseGenericPathToFile(
string unquoted,
IList<Diagnostic> errors,
string? baseDirectory,
bool generateDiagnostic = true)
{
string? genericPath = null;
ParseAndNormalizeFile(unquoted, baseDirectory, out string? outputFileName, out string? outputDirectory, out string? invalidPath);
if (string.IsNullOrWhiteSpace(outputFileName))
{
if (generateDiagnostic)
{
errors.Add(Diagnostic.Create(_messageProvider, _messageProvider.FTL_InvalidInputFileName, invalidPath));
}
}
else
{
// If outputDirectory were null, then outputFileName would be null (see ParseAndNormalizeFile)
genericPath = Path.Combine(outputDirectory!, outputFileName);
}
return genericPath;
}
internal void FlattenArgs(
IEnumerable<string> rawArguments,
IList<Diagnostic> diagnostics,
List<string> processedArgs,
List<string>? scriptArgsOpt,
string? baseDirectory,
List<string>? responsePaths = null)
{
bool parsingScriptArgs = false;
bool sourceFileSeen = false;
bool optionsEnded = false;
var args = new Stack<string>(rawArguments.Reverse());
while (args.Count > 0)
{
// EDMAURER trim off whitespace. Otherwise behavioral differences arise
// when the strings which represent args are constructed by cmd or users.
// cmd won't produce args with whitespace at the end.
string arg = args.Pop().TrimEnd();
if (parsingScriptArgs)
{
scriptArgsOpt!.Add(arg);
continue;
}
if (scriptArgsOpt != null)
{
// The order of the following two checks matters.
//
// Command line: Script: Script args:
// csi -- script.csx a b c script.csx ["a", "b", "c"]
// csi script.csx -- a b c script.csx ["--", "a", "b", "c"]
// csi -- @script.csx a b c @script.csx ["a", "b", "c"]
//
if (sourceFileSeen)
{
// csi/vbi: at most one script can be specified on command line, anything else is a script arg:
parsingScriptArgs = true;
scriptArgsOpt.Add(arg);
continue;
}
if (!optionsEnded && arg == "--")
{
// csi/vbi: no argument past "--" should be treated as an option/response file
optionsEnded = true;
processedArgs.Add(arg);
continue;
}
}
if (!optionsEnded && arg.StartsWith("@", StringComparison.Ordinal))
{
// response file:
string path = RemoveQuotesAndSlashes(arg.Substring(1)).TrimEnd(null);
string? resolvedPath = FileUtilities.ResolveRelativePath(path, baseDirectory);
if (resolvedPath != null)
{
foreach (string newArg in ParseResponseFile(resolvedPath, diagnostics).Reverse())
{
// Ignores /noconfig option specified in a response file
if (!string.Equals(newArg, "/noconfig", StringComparison.OrdinalIgnoreCase) && !string.Equals(newArg, "-noconfig", StringComparison.OrdinalIgnoreCase))
{
args.Push(newArg);
}
else
{
diagnostics.Add(Diagnostic.Create(_messageProvider, _messageProvider.WRN_NoConfigNotOnCommandLine));
}
}
if (responsePaths != null)
{
string? directory = PathUtilities.GetDirectoryName(resolvedPath);
if (directory is null)
{
diagnostics.Add(Diagnostic.Create(_messageProvider, _messageProvider.FTL_InvalidInputFileName, path));
}
else
{
responsePaths.Add(FileUtilities.NormalizeAbsolutePath(directory));
}
}
}
else
{
diagnostics.Add(Diagnostic.Create(_messageProvider, _messageProvider.FTL_InvalidInputFileName, path));
}
}
else
{
processedArgs.Add(arg);
sourceFileSeen |= optionsEnded || !IsOption(arg);
}
}
}
/// <summary>
/// Returns false if any of the client arguments are invalid and true otherwise.
/// </summary>
/// <param name="args">
/// The original args to the client.
/// </param>
/// <param name="parsedArgs">
/// The original args minus the client args, if no errors were encountered.
/// </param>
/// <param name="containsShared">
/// Only defined if no errors were encountered.
/// True if '/shared' was an argument, false otherwise.
/// </param>
/// <param name="keepAliveValue">
/// Only defined if no errors were encountered.
/// The value to the '/keepalive' argument if one was specified, null otherwise.
/// </param>
/// <param name="errorMessage">
/// Only defined if errors were encountered.
/// The error message for the encountered error.
/// </param>
/// <param name="pipeName">
/// Only specified if <paramref name="containsShared"/> is true and the session key
/// was provided. Can be null
/// </param>
internal static bool TryParseClientArgs(
IEnumerable<string> args,
out List<string>? parsedArgs,
out bool containsShared,
out string? keepAliveValue,
out string? pipeName,
out string? errorMessage)
{
containsShared = false;
keepAliveValue = null;
errorMessage = null;
parsedArgs = null;
pipeName = null;
var newArgs = new List<string>();
foreach (var arg in args)
{
if (isClientArgsOption(arg, "keepalive", out bool hasValue, out string? value))
{
if (string.IsNullOrEmpty(value))
{
errorMessage = CodeAnalysisResources.MissingKeepAlive;
return false;
}
if (int.TryParse(value, out int intValue))
{
if (intValue < -1)
{
errorMessage = CodeAnalysisResources.KeepAliveIsTooSmall;
return false;
}
keepAliveValue = value;
}
else
{
errorMessage = CodeAnalysisResources.KeepAliveIsNotAnInteger;
return false;
}
continue;
}
if (isClientArgsOption(arg, "shared", out hasValue, out value))
{
if (hasValue)
{
if (string.IsNullOrEmpty(value))
{
errorMessage = CodeAnalysisResources.SharedArgumentMissing;
return false;
}
pipeName = value;
}
containsShared = true;
continue;
}
newArgs.Add(arg);
}
if (keepAliveValue != null && !containsShared)
{
errorMessage = CodeAnalysisResources.KeepAliveWithoutShared;
return false;
}
else
{
parsedArgs = newArgs;
return true;
}
static bool isClientArgsOption(string arg, string optionName, out bool hasValue, out string? optionValue)
{
hasValue = false;
optionValue = null;
if (arg.Length == 0 || !(arg[0] == '/' || arg[0] == '-'))
{
return false;
}
arg = arg.Substring(1);
if (!arg.StartsWith(optionName, StringComparison.OrdinalIgnoreCase))
{
return false;
}
if (arg.Length > optionName.Length)
{
if (!(arg[optionName.Length] == ':' || arg[optionName.Length] == '='))
{
return false;
}
hasValue = true;
optionValue = arg.Substring(optionName.Length + 1).Trim('"');
}
return true;
}
}
internal static string MismatchedVersionErrorText => CodeAnalysisResources.MismatchedVersion;
/// <summary>
/// Parse a response file into a set of arguments. Errors opening the response file are output into "errors".
/// </summary>
internal IEnumerable<string> ParseResponseFile(string fullPath, IList<Diagnostic> errors)
{
List<string> lines = new List<string>();
try
{
Debug.Assert(PathUtilities.IsAbsolute(fullPath));
using TextReader reader = CreateTextFileReader(fullPath);
string? str;
while ((str = reader.ReadLine()) != null)
{
lines.Add(str);
}
}
catch (Exception)
{
errors.Add(Diagnostic.Create(_messageProvider, _messageProvider.ERR_OpenResponseFile, fullPath));
return SpecializedCollections.EmptyEnumerable<string>();
}
return ParseResponseLines(lines);
}
/// <summary>
/// Take a string of lines from a response file, remove comments,
/// and split into a set of command line arguments.
/// </summary>
internal static IEnumerable<string> ParseResponseLines(IEnumerable<string> lines)
{
List<string> arguments = new List<string>();
foreach (string line in lines)
{
arguments.AddRange(SplitCommandLineIntoArguments(line, removeHashComments: true));
}
return arguments;
}
private static readonly char[] s_resourceSeparators = { ',' };
internal static void ParseResourceDescription(
string resourceDescriptor,
string? baseDirectory,
bool skipLeadingSeparators, //VB does this
out string? filePath,
out string? fullPath,
out string? fileName,
out string resourceName,
out string? accessibility)
{
filePath = null;
fullPath = null;
fileName = null;
resourceName = "";
accessibility = null;
// resource descriptor is: "<filePath>[,<string name>[,public|private]]"
string[] parts = ParseSeparatedStrings(resourceDescriptor, s_resourceSeparators).ToArray();
int offset = 0;
int length = parts.Length;
if (skipLeadingSeparators)
{
for (; offset < length && string.IsNullOrEmpty(parts[offset]); offset++)
{
}
length -= offset;
}
if (length >= 1)
{
filePath = RemoveQuotesAndSlashes(parts[offset + 0]);
}
if (length >= 2)
{
resourceName = RemoveQuotesAndSlashes(parts[offset + 1]);
}
if (length >= 3)
{
accessibility = RemoveQuotesAndSlashes(parts[offset + 2]);
}
if (RoslynString.IsNullOrWhiteSpace(filePath))
{
return;
}
fileName = PathUtilities.GetFileName(filePath);
fullPath = FileUtilities.ResolveRelativePath(filePath, baseDirectory);
// The default resource name is the file name.
// Also use the file name for the name when user specifies string like "filePath,,private"
if (RoslynString.IsNullOrWhiteSpace(resourceName))
{
resourceName = fileName;
}
}
/// <summary>
/// See <see cref="CommandLineUtilities.SplitCommandLineIntoArguments(string, bool)"/>
/// </summary>
public static IEnumerable<string> SplitCommandLineIntoArguments(string commandLine, bool removeHashComments)
{
return CommandLineUtilities.SplitCommandLineIntoArguments(commandLine, removeHashComments);
}
/// <summary>
/// Remove the extraneous quotes and slashes from the argument. This function is designed to have
/// compat behavior with the native compiler.
/// </summary>
/// <remarks>
/// Mimics the function RemoveQuotes from the native C# compiler. The native VB equivalent of this
/// function is called RemoveQuotesAndSlashes. It has virtually the same behavior except for a few
/// quirks in error cases.
/// </remarks>
[return: NotNullIfNotNull(parameterName: "arg")]
internal static string? RemoveQuotesAndSlashes(string? arg)
{
if (arg == null)
{
return arg;
}
var pool = PooledStringBuilder.GetInstance();
var builder = pool.Builder;
var i = 0;
while (i < arg.Length)
{
var cur = arg[i];
switch (cur)
{
case '\\':
ProcessSlashes(builder, arg, ref i);
break;
case '"':
// Intentionally dropping quotes that don't have explicit escaping.
i++;
break;
default:
builder.Append(cur);
i++;
break;
}
}
return pool.ToStringAndFree();
}
/// <summary>
/// Mimic behavior of the native function by the same name.
/// </summary>
internal static void ProcessSlashes(StringBuilder builder, string arg, ref int i)
{
RoslynDebug.Assert(arg != null);
Debug.Assert(i < arg.Length);
var slashCount = 0;
while (i < arg.Length && arg[i] == '\\')
{
slashCount++;
i++;
}
if (i < arg.Length && arg[i] == '"')
{
// Before a quote slashes are interpretted as escape sequences for other slashes so
// output one for every two.
while (slashCount >= 2)
{
builder.Append('\\');
slashCount -= 2;
}
Debug.Assert(slashCount >= 0);
// If there is an odd number of slashes then the quote is escaped and hence a part
// of the output. Otherwise it is a normal quote and can be ignored.
if (slashCount == 1)
{
// The quote is escaped so eat it.
builder.Append('"');
}
i++;
}
else
{
// Slashes that aren't followed by quotes are simply slashes.
while (slashCount > 0)
{
builder.Append('\\');
slashCount--;
}
}
}
/// <summary>
/// Split a string, based on whether "splitHere" returned true on each character.
/// </summary>
private static IEnumerable<string> Split(string? str, Func<char, bool> splitHere)
{
if (str == null)
{
yield break;
}
int nextPiece = 0;
for (int c = 0; c < str.Length; c++)
{
if (splitHere(str[c]))
{
yield return str.Substring(nextPiece, c - nextPiece);
nextPiece = c + 1;
}
}
yield return str.Substring(nextPiece);
}
private static readonly char[] s_pathSeparators = { ';', ',' };
private static readonly char[] s_wildcards = new[] { '*', '?' };
internal static IEnumerable<string> ParseSeparatedPaths(string? str)
{
return ParseSeparatedStrings(str, s_pathSeparators, StringSplitOptions.RemoveEmptyEntries).Select(RemoveQuotesAndSlashes)!;
}
/// <summary>
/// Split a string by a set of separators, taking quotes into account.
/// </summary>
internal static IEnumerable<string> ParseSeparatedStrings(string? str, char[] separators, StringSplitOptions options = StringSplitOptions.None)
{
bool inQuotes = false;
var result = Split(str,
(c =>
{
if (c == '\"')
{
inQuotes = !inQuotes;
}
return !inQuotes && separators.Contains(c);
}));
return (options == StringSplitOptions.RemoveEmptyEntries) ? result.Where(s => s.Length > 0) : result;
}
internal IEnumerable<string> ResolveRelativePaths(IEnumerable<string> paths, string baseDirectory, IList<Diagnostic> errors)
{
foreach (var path in paths)
{
string? resolvedPath = FileUtilities.ResolveRelativePath(path, baseDirectory);
if (resolvedPath == null)
{
errors.Add(Diagnostic.Create(_messageProvider, _messageProvider.FTL_InvalidInputFileName, path));
}
else
{
yield return resolvedPath;
}
}
}
private protected CommandLineSourceFile ToCommandLineSourceFile(string resolvedPath, bool isInputRedirected = false)
{
string extension = PathUtilities.GetExtension(resolvedPath);
bool isScriptFile;
if (IsScriptCommandLineParser)
{
isScriptFile = !string.Equals(extension, RegularFileExtension, StringComparison.OrdinalIgnoreCase);
}
else
{
// TODO: uncomment when fixing https://github.com/dotnet/roslyn/issues/5325
//isScriptFile = string.Equals(extension, ScriptFileExtension, StringComparison.OrdinalIgnoreCase);
isScriptFile = false;
}
return new CommandLineSourceFile(resolvedPath, isScriptFile, isInputRedirected);
}
internal IEnumerable<string> ParseFileArgument(string arg, string? baseDirectory, IList<Diagnostic> errors)
{
Debug.Assert(IsScriptCommandLineParser || !arg.StartsWith("-", StringComparison.Ordinal) && !arg.StartsWith("@", StringComparison.Ordinal));
// We remove all doubles quotes from a file name. So that, for example:
// "Path With Spaces"\goo.cs
// becomes
// Path With Spaces\goo.cs
string path = RemoveQuotesAndSlashes(arg);
int wildcard = path.IndexOfAny(s_wildcards);
if (wildcard != -1)
{
foreach (var file in ExpandFileNamePattern(path, baseDirectory, SearchOption.TopDirectoryOnly, errors))
{
yield return file;
}
}
else
{
string? resolvedPath = FileUtilities.ResolveRelativePath(path, baseDirectory);
if (resolvedPath == null)
{
errors.Add(Diagnostic.Create(MessageProvider, (int)MessageProvider.FTL_InvalidInputFileName, path));
}
else
{
yield return resolvedPath;
}
}
}
private protected IEnumerable<string> ParseSeparatedFileArgument(string value, string? baseDirectory, IList<Diagnostic> errors)
{
foreach (string path in ParseSeparatedPaths(value).Where((path) => !string.IsNullOrWhiteSpace(path)))
{
foreach (var file in ParseFileArgument(path, baseDirectory, errors))
{
yield return file;
}
}
}
internal IEnumerable<CommandLineSourceFile> ParseRecurseArgument(string arg, string? baseDirectory, IList<Diagnostic> errors)
{
foreach (var path in ExpandFileNamePattern(arg, baseDirectory, SearchOption.AllDirectories, errors))
{
yield return ToCommandLineSourceFile(path);
}
}
internal static Encoding? TryParseEncodingName(string arg)
{
if (!string.IsNullOrWhiteSpace(arg)
&& long.TryParse(arg, NumberStyles.None, CultureInfo.InvariantCulture, out long codepage)
&& (codepage > 0))
{
try
{
return Encoding.GetEncoding((int)codepage);
}
catch (Exception)
{
return null;
}
}
return null;