I'm writing a Roslyn analyzer (actually source generator, but they largely share the same API), and I would like to have it analyze only post-processor code. For instance, if I have code that is excluded with #if/#else, I would like my analyzer to not work on that code. Similarly, I would like #beginregion/#endregion and the like to not show up for my analyzer.
Is there a way to get a post-preprocessed compilation or syntax tree from Roslyn? I can do something like the following, but it will throw away all of my original location info and I am guessing that my compilations semantic model will know nothing about the newly generated syntax tree:
var preprocessorSymbols = candidate.SyntaxTree.Options.PreprocessorSymbolNames;
var parseOptions = CSharpParseOptions.Default.WithPreprocessorSymbols(preprocessorSymbols);
var newSyntaxTree = CSharpSyntaxTree.ParseText(candidate.SyntaxTree.ToString(), parseOptions);