Cpp Parser

The Cpp parser preprocesses C and C++ files. As any normal preprocessor, it will generate a file suitable as input for a C or C++ parser, i.e. it processes include and macro statements. However, it will store the encountered preprocessor directives in the AST for further analysis.

As the list of included files may grow rather large, there exist two mechanisms to restrict the number of files for which information is retained. The main_file_only parameter is used to indicate that only the top-level file being parsed should be included. The base_path parameter, on the other hand, will restrict the number files if main_file_only is set to False. In this case, the base_path is used as a prefix, and only those file whose name starts with that prefix are marked as main.

For each included file, a SourceFile object is created and added to the parent's Include list. Further, all macro declarations, as well as macro calls, are recorded. While most useful in conjunction with the C and Cxx processors, these data can be of use stand-alone, too. For example consider a tool that reports file dependencies based on #include statements. The Dot formatter (see the section called “Dot Formatter”) can generate a file dependency graph from the Cpp processor output alone:

Particular care has been taken in order to emulate system compilers, as these all provide their own sets of macros, include paths, etc. Thus, the Cpp parser can be trained to understand these compiler-specific settings.

For details about the parameters see the section called “Synopsis.Parsers.Cpp.Parser”.