在 Roslyn 分析语法树时添加条件编译符号的支持

 

我们在代码中会写 #if DEBUG 或者 [Conditional("DEBUG")] 来使用已经定义好的条件编译符号。而定义条件编译符号可以在代码中使用 #define WALTERLV 来实现,也可以通过在项目属性中设置条件编译符号(Conditional Compilation Symbols)来实现。

然而如果我们没有做任何特殊处理,那么使用 Roslyn 分析使用了条件编译符号的源码时,就会无法识别这些源码。


如果你不知道条件编译符号是什么或者不知道怎么设置,请参见:

 

本文内容

 

我们在使用 Roslyn 分析语法树时,会创建语法树的一个实例。如果使用默认的构造函数,那么就不会识别设置了条件编译符号的语句,如下图:

不识别条件编译符号

而实际上构造函数的参数中带有 preprocessorSymbols 参数,即预处理符号。在传入此预处理符号的情况下,Roslyn 就可以识别此符号了:

识别的条件编译符号

方法是传入 preprocessorSymbols 参数:

var preprocessorSymbols = new[] {"DEBUG", "TRACE", "WALTERLV", "NETCOREAPP2_1"};
var syntaxTree = CSharpSyntaxTree.ParseText(originalText, new CSharpParseOptions(
    LanguageVersion.Latest, DocumentationMode.None, SourceCodeKind.Regular, preprocessorSymbols)

此后,你可以拿 syntaxTree 做其他事情了:

var compileTypeVisitor = new CompileTypeVisitor();
compileTypeVisitor.Visit(syntaxTree.GetRoot());
Types = compileTypeVisitor.Types.ToList();

当然这段代码你可能编译不通过,因为这是另一篇博客中的源码:

本文所用的查看语法树的插件,你可以查看另一篇博客:


我的博客会首发于 https://walterlv.com/,而 CSDN 和博客园仅从其中摘选发布,而且一旦发布了就不再更新。

知识共享许可协议

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名吕毅(包含链接:https://blog.csdn.net/wpwalter),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

转载于:https://www.cnblogs.com/walterlv/p/10236372.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learn how to build an interactive source code analytics system using Roslyn and JavaScript. This concise 150 page book will help you create and use practical code analysis tools utilizing the new features of Microsoft’s Roslyn compiler to understand the health of your code and identify parts of the code for refactoring. Source code is one of the biggest assets of a software company. However if not maintained well, it can become a big liability. As source code becomes larger. more complex and accessed via the cloud, maintaining code quality becomes even more challenging. The author provides straightforward tools and advice on how to manage code quality in this new environment. Roslyn exposes a set of APIs which allow developers to parse their C# and VB.NET code and drastically lower the barrier to entry for Meta programming in .NET. Roslyn has a dedicated set of APIs for creating custom refactoring for integrating with Visual Studio. This title will show readers how to use Roslyn along with industry standard JavaScript visualization APIs like HighCharts, D3.js etc to create a scalable and highly responsive source code analytics system. What You Will Learn Understand the Roslyn Syntax API Use Data Visualization techniques to assist code analysis process visually Code health monitoring matrices (from the standard of Code Query Language) Code mining techniques to identify design patterns used in source code Code forensics techniques to identify probable author of a given source code Techniques to identify duplicate/near duplicate code Who This Book is For .NET Software Developers and Architects Table of Contents Chapter 1: Meet Roslyn Syntax API Chapter 2: Code Quality Metrics Chapter 3: Design Quality Metrics Chapter 4: Code Performance Metrics Chapter 5: Code Mining Chapter 6: Code Forensics Chapter 7: Code Visualization

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值