Clang Checker for Import Statements

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Clang Checker for Import Statements

ajotwani
I want to write a checker that will analyze the #import statements within Objective-C files and issue a warning when a certain file is included in the import directive. For example if I have:

#import "x.h"

and x.h is the file name I am looking for then the checker would issue a warning that x.h is being used when it comes across this import statement.

What would be the best way to go about doing this? Using the InclusionDirective class?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Clang Checker for Import Statements

Jordy Rose
As I understand it, #import is completely taken care of in the preprocessing phase...you won't see it in the AST. That means that writing a static analysis checker won't work.

If you really wanted to do this in code, you'd probably be modifying code in the Lex library (which I'm not so familiar with). But an easier way to solve your problem might be to just use the #warning extension: if you put a #warning in x.h, you'll get a warning whenever x.h is included. If you don't have control over x.h, or you want the warning every time x.h is referenced, you could make a wrapper that looks like this:

#warning "Use of x.h"
#import "x.h"

...and then *#include* your wrapper. Admittedly, that requires a little more intrusion into your project, but it /doesn't/ require a custom compiler build. :-)

Jordy


On Jul 14, 2011, at 13:34, ajotwani wrote:

> I want to write a checker that will analyze the #import statements within
> Objective-C files and issue a warning when a certain file is included in the
> import directive. For example if I have:
>
> #import "x.h"
>
> and x.h is the file name I am looking for then the checker would issue a
> warning that x.h is being used when it comes across this import statement.
>
> What would be the best way to go about doing this? Using the
> InclusionDirective class?
>
> Thanks
>
> --
> View this message in context: http://clang-developers.42468.n3.nabble.com/Clang-Checker-for-Import-Statements-tp3170252p3170252.html
> Sent from the Clang Developers mailing list archive at Nabble.com.
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clang Checker for Import Statements

Ted Kremenek
This information can possibly be groveled through in the SourceManager (so it could be a static analysis check, at least partially), but I don't think we get SourceLocations when we include (say) an empty header or we #include the same header again that we know has include guards.

On Jul 14, 2011, at 3:39 PM, Jordy Rose wrote:

> As I understand it, #import is completely taken care of in the preprocessing phase...you won't see it in the AST. That means that writing a static analysis checker won't work.
>
> If you really wanted to do this in code, you'd probably be modifying code in the Lex library (which I'm not so familiar with). But an easier way to solve your problem might be to just use the #warning extension: if you put a #warning in x.h, you'll get a warning whenever x.h is included. If you don't have control over x.h, or you want the warning every time x.h is referenced, you could make a wrapper that looks like this:
>
> #warning "Use of x.h"
> #import "x.h"
>
> ...and then *#include* your wrapper. Admittedly, that requires a little more intrusion into your project, but it /doesn't/ require a custom compiler build. :-)
>
> Jordy
>
>
> On Jul 14, 2011, at 13:34, ajotwani wrote:
>
>> I want to write a checker that will analyze the #import statements within
>> Objective-C files and issue a warning when a certain file is included in the
>> import directive. For example if I have:
>>
>> #import "x.h"
>>
>> and x.h is the file name I am looking for then the checker would issue a
>> warning that x.h is being used when it comes across this import statement.
>>
>> What would be the best way to go about doing this? Using the
>> InclusionDirective class?
>>
>> Thanks
>>
>> --
>> View this message in context: http://clang-developers.42468.n3.nabble.com/Clang-Checker-for-Import-Statements-tp3170252p3170252.html
>> Sent from the Clang Developers mailing list archive at Nabble.com.
>> _______________________________________________
>> cfe-dev mailing list
>> [hidden email]
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Clang Checker for Import Statements

Scott Conger
If you don't want to touch the code itself, you can still go with
Jordy's solution below and just update the include paths. You can
perhaps even avoid touching makefiles if you use environment
variables.

-Scott

On Thu, Jul 14, 2011 at 4:15 PM, Ted Kremenek <[hidden email]> wrote:

> This information can possibly be groveled through in the SourceManager (so it could be a static analysis check, at least partially), but I don't think we get SourceLocations when we include (say) an empty header or we #include the same header again that we know has include guards.
>
> On Jul 14, 2011, at 3:39 PM, Jordy Rose wrote:
>
>> As I understand it, #import is completely taken care of in the preprocessing phase...you won't see it in the AST. That means that writing a static analysis checker won't work.
>>
>> If you really wanted to do this in code, you'd probably be modifying code in the Lex library (which I'm not so familiar with). But an easier way to solve your problem might be to just use the #warning extension: if you put a #warning in x.h, you'll get a warning whenever x.h is included. If you don't have control over x.h, or you want the warning every time x.h is referenced, you could make a wrapper that looks like this:
>>
>> #warning "Use of x.h"
>> #import "x.h"
>>
>> ...and then *#include* your wrapper. Admittedly, that requires a little more intrusion into your project, but it /doesn't/ require a custom compiler build. :-)
>>
>> Jordy
>>
>>
>> On Jul 14, 2011, at 13:34, ajotwani wrote:
>>
>>> I want to write a checker that will analyze the #import statements within
>>> Objective-C files and issue a warning when a certain file is included in the
>>> import directive. For example if I have:
>>>
>>> #import "x.h"
>>>
>>> and x.h is the file name I am looking for then the checker would issue a
>>> warning that x.h is being used when it comes across this import statement.
>>>
>>> What would be the best way to go about doing this? Using the
>>> InclusionDirective class?
>>>
>>> Thanks
>>>
>>> --
>>> View this message in context: http://clang-developers.42468.n3.nabble.com/Clang-Checker-for-Import-Statements-tp3170252p3170252.html
>>> Sent from the Clang Developers mailing list archive at Nabble.com.
>>> _______________________________________________
>>> cfe-dev mailing list
>>> [hidden email]
>>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>>
>> _______________________________________________
>> cfe-dev mailing list
>> [hidden email]
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev