-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
108 lines (84 loc) · 2.96 KB
/
Program.cs
File metadata and controls
108 lines (84 loc) · 2.96 KB
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
using System;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
return Run(args);
static int Run(string[] args)
{
//args = new[] { "C:\\Users\\Max\\source\\repos\\CodeAsPreProcessed\\Example\\VisibleComponent.cs", "OPT3" };
if (args.Length < 1)
{
Console.Error.WriteLine("Usage: CodeAsPreProcessed <path-to-code-file> [DEFINE1 DEFINE2 ...]");
return 1;
}
var inputPath = args[0];
var defines = args.Skip(1).ToArray();
if (!File.Exists(inputPath))
{
Console.Error.WriteLine($"Input file '{inputPath}' does not exist.");
return 1;
}
var code = File.ReadAllText(inputPath);
var parseOptions = new CSharpParseOptions(
preprocessorSymbols: defines
);
var tree = CSharpSyntaxTree.ParseText(code, parseOptions);
var root = tree.GetRoot();
// This is the expanded source with preprocessor directives and
// disabled regions removed.
var cleanedRoot = new PreprocessorStripRewriter().Visit(root);
var processed = cleanedRoot.ToFullString();
var directory = Path.GetDirectoryName(inputPath) ?? "";
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(inputPath);
var extension = Path.GetExtension(inputPath);
var suffix = "_PREPROCESSED";
if (defines.Length > 0)
{
suffix += "_" + string.Join("_", defines);
}
var outputFileName = fileNameWithoutExtension + suffix + ".cs";
var outputPath = Path.Combine(directory, outputFileName);
File.WriteAllText(outputPath, processed);
return 0;
}
sealed class PreprocessorStripRewriter : CSharpSyntaxRewriter
{
public override SyntaxToken VisitToken(SyntaxToken token)
{
// First visit into child structure so we don't lose changes.
var visited = base.VisitToken(token);
var leading = FilterTrivia(visited.LeadingTrivia);
var trailing = FilterTrivia(visited.TrailingTrivia);
if (leading != visited.LeadingTrivia)
{
visited = visited.WithLeadingTrivia(leading);
}
if (trailing != visited.TrailingTrivia)
{
visited = visited.WithTrailingTrivia(trailing);
}
return visited;
}
private static SyntaxTriviaList FilterTrivia(SyntaxTriviaList triviaList)
{
var kept = new System.Collections.Generic.List<SyntaxTrivia>(triviaList.Count);
foreach (var trivia in triviaList)
{
// Drop all directives (#if, #else, #endif, #define, #region, etc.)
if (trivia.IsDirective)
{
continue;
}
// Drop disabled text (code from inactive branches).
if (trivia.Kind() == SyntaxKind.DisabledTextTrivia)
{
continue;
}
kept.Add(trivia);
}
return SyntaxFactory.TriviaList(kept);
}
}