Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] macro definitions for static analysis

> I am implementing a simple C++ static analysis tool using CDT. Is it possible to configure the scanner / parser to visit all statements, independently of the preprocessing directives? I mean, I have a peace of code such as:
>
>
> #if ((defined __MINGW32__) || (defined _MSC_VER)) // *** changed in 1.4.0 and 1.4.1
> // "A" statements here...
> // ....
> #else
> // "B" statements here
> //....
> #endif
>
> I would like to collect some data related to both "A" statements and "B" statements. How can I force the scanner to ignore the macro definitions? Bellow, I present the code that I am using to parse a translation unit.
>
> public IASTTranslationUnit parse(char[] code) throws Exception {
> FileContent fc = FileContent.create("", code);
> Map<String, String> macroDefinitions = new HashMap<String, String>();
> String[] includeSearchPath = new String[0];
> IScannerInfo si = new ScannerInfo(macroDefinitions, includeSearchPath);
> IncludeFileContentProvider ifcp = IncludeFileContentProvider.getEmptyFilesProvider();
> IIndex idx = null;
> int options = ILanguage.OPTION_PARSE_INACTIVE_CODE;
> IParserLogService log = new DefaultLogService();
> return GPPLanguage.getDefault().getASTTranslationUnit(fc, si, ifcp, idx, options, log);
> }
>
> All the best,
>
> Rodrigo.

Perhaps you can parse the file twice, once with the relevant macros
defined and once with them not defined?

I don't think that trying to force the parser to construct an AST that
includes both branches of a preprocessor conditional is a good idea.
The resulting AST could very easily be ill-formed.

Regards,
Nate 		 	   		  

Back to the top