Re: Using Semantic for Style Checking

"Eric M. Ludlam" <[email protected]> Fri, 12 Sep 2014 22:10:41 -0400
Newsgroups gmane.emacs.semantic
Message-ID <[email protected]>
On 09/11/2014 12:56 AM, Alex Murray wrote:
>> -----Original Message-----
> Hi Eric
>
> Thanks for the reply - this was what I was planning to do but was
> hoping I wouldn't have to - however I am not sure it will be enough.
>
> If I visit each function and call semantic-get-local-variables I think
> this will only give the variables in the top-level of the function
> scope - ie:
>
> int foo() {
>    int bar = 1;
>
>    if (bar) {
>      int baz = 0;
>    }
> }
>
> If I visit foo sobar will be in the list of tags but baz will not be.
> So it looks like I need to recursively walk all scopes within the file
> (and then do a semantic-get-local-variables at the deepest of each
> scope) to get them all - does this sound correct?
>
> Perhaps semantic isn't that well suited to this task of getting all
> tags within a buffer / file (I wonder if I could just use a bunch of
> regex's to match variable declarations / function definitions?)

If you think your code is very regular, then regexp would probably be 
fine.  Otherewise, the detailed scan down through all the code blocks in 
Semantic is the only way.  For complex code that will be pretty slow.

You could theoretically scan for "bad names" with a regexp, and then use 
semantic to parse those context to seek out the bad declarations.  If 
your bad-name regexp is straightforward, it is pretty easy to script up 
a scan to just highlight the bad names.  Even if it isn't a declaration, 
it will be declared somewhere in your code.

To be honest though, I suspect you should probably get a lint tool.  We 
use one where I work, and we've added some custom lint features.  It 
integrates with make / flymake and would give you some nice features.

A quick google search led me to 
http://sourceforge.net/p/cppcheck/wiki/Home/ which notes that you can 
make some custom rules.  I don't know which linter is best or how to 
modify them though.

Eric

------------------------------------------------------------------------------
Want excitement?
Manually upgrade your production database.
When you want reliability, choose Perforce
Perforce version control. Predictably reliable.
http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk