Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang parser - ignore directive #ifdef, parse everything

I need to find calls of some functions in C source codes. I'm using Clang Python bindings for this.

The problem is, that this parser ignores code inside of "inactive" #ifdef directives. I understand, that this behaviour is correct (if the condition result is False), but now I need it simply to find all the calls, because of some automatic editing.


Example:

#define CONST2

#ifdef CONST
f90()
#endif

f90()

#ifdef CONST2
f90()
#endif

The parser will find just the second and the third call of the function f90(), but not the first one, because CONST is undefined. But I need Clang to find even the first one.


Is there any way to change this Clang behaviour?

EDIT:

I don't have a problem with source codes. The problem is in the settings of Clang parser - I need it simply to parse the WHOLE code, regardless of unmet conditions.

EDIT 2:

Ok, I don't think there's a bug in Clang... My question is, if there's some setting (possibly some flag) in Clang, so it'll ignore #ifdef conditions during code parsing.

like image 480
Eenoku Avatar asked Sep 06 '25 06:09

Eenoku


1 Answers

No, there is no way to make most C parsers look att all the code regardless of preprocessor directives. There may be exceptions, but clang isn't one of them.

The reason is the way the compiler works in two separate steps. First the pre-processor parses the pre-processor directives. The pre-processor knows nothing of the C syntax, it only knows the pre-processor syntax.

Then the preprocessed code is processed by the compiler. The compiler knows everything about the C syntax and one of it's jobs is to find syntax errors. If you would send it all code, the code that should have been removed by he pre-processor is likely to cause syntax errors.

I would recommend that you either re-think your solution entirely, or that you supplement what you have with a step that looks for calls hidden inside pre-processor directives. Unless the code is poorly designed, there should not be so many of those.

Note that since the pre-processor step can do code manipulation, it is very difficult to build a tool that can handle all possible cases.

like image 170
Klas Lindbäck Avatar answered Sep 07 '25 20:09

Klas Lindbäck