digitalmars.D.learn - Generation of AST for semantic rule checking
Chris Katko <ckatko gmail.com> writes:
I have an idea for something. I know I can't be the only one to think this way but I can't find very much information on it. Basically, I want compile-time enforcement of semantic rules. For one example--I asked previously here--if it was possible to generate a compile-time warning or error if someone used a function without checking the return value. But that is one specific implementation of a more abstract idea: Setting up rulesets to prevent common developer errors in my codebase that trigger at compile-time. I'm wondering how to go about a more general-purpose setup. Now, if I was using C/C++, I could generate the AST (Abstract Syntax Tree) with Clang. (And if you've never done it, I strongly recommend it, it's quite beautiful.) Clang also has libclang for modifying and generating AST on-the-fly which opens up huge potential--but back to the point: if I can find how to get LDC2 to generate AST, I could write a parser and scan the AST for patterns. I've seen DIP50 and AST Macros which are very interesting... but as far as I know, it's not implemented in any compiler. So the question is: Is there a way to get LDC2 to generate AST or similar, and if not, any other way to go about this? And lastly, is there a proper term for this process, and/or a field of research? I have found GCC MELT. But documentation is sparse and it seems restricted to GCC and C/C++. I guess you could call this "static analysis" with custom rules?
May 21 2016
thedeemon <dlang thedeemon.com> writes:
On Sunday, 22 May 2016 at 04:33:44 UTC, Chris Katko wrote:Basically, I want compile-time enforcement of semantic rules.So the question is: Is there a way to get LDC2 to generate AST or similar, and if not, any other way to go about this?I think one popular approach to the task is to use linters / static analysis tools before compiling, as a build step or separately. There is this project: https://github.com/Hackerpilot/Dscanner that uses a D parser to get AST and applies different semantic checks, you could use that as a base and just add your own checks and conditions.
May 22 2016