Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Thursday, May 19, 2011

Double inclusion and circular dependencies


If you thought we were out of neat tricks that can be performed with pycdep, here's some more :)

Detecting header files that are included twice (or more).


Here's a simple prolog program to keep only those elements in a list that occur more than once:



You could use it in a query as follows:


Detecting circular dependencies


Circular dependencies are a code maintainer's nightmare. They can cause all kinds of subtle and less subtle problems (if you get it to compile at least).

To get rid of circular dependencies one can do several things (depending on the situation at hand). Sometimes it suffices to move data structures into a file of their own, where the circularly dependent files can now both include it. Another technique is to avoid #includes and use as much forward declarations as possible.

Before you can solve circular dependencies, you have to be aware of them. Especially if the cycles are big (a includes b which includes c which includes d which includes a), it may not be obvious at first sight that there is a circular dependency. Luckily pycdep is able to tell us about circular dependencies. More precisely, we will calculate the dependency graph's strongly connected components (SCC). A strongly connected component is a subgraph of which each node is reachable from each other node. This is only possible if it contains cycles. Note that an SCC can encompass multiple elementary cycles. In the code shown below, no attempt is made to further decompose the SCC into elementary cycles.

The algorithm shown here is not very efficient (for reasons of efficiency Tarjan's SCC detection algorithm would be more suitable, but it's not as easy to write and understand as the one shown here. Maybe one day... :-) ). I have used it successfully on a 2800+ file code base with around 19000 include dependencies, to find all cycles in a few minutes on a reasonably modern pc.



You can use it as follows:



The program works by noting that two nodes X and Y belong to the same SCC if there's a path from X to Y, and vice versa. The scc(X, Result) predicate calculates the list Result, which contains all nodes that belong to the same SCC as X. The all_scc(R) returns a list of all the SCC found in the include file graph. To perform its magic, it uses a predicate all_scc_helper that starts from a node taken from the list of all eligible nodes, then finds all the nodes belonging to the same SCC. All the nodes that belong to the SCC, are then removed from the list of eligible nodes, and the same steps are done on the remaining nodes.

Note that the program can be sped up by memoizing the path calculation in the implementation of "X depends on Y". Memoization is a technique in which results are calculated once, and then the outcome of the calculation is stored as a new fact in the prolog database. This avoids that the same calculation has to be redone multiple times. Using a better algorithm (like the ones by Tarjan, Kosoraju or Gabow) of course would be a better approach.

Memoization of the path calculation can be achieved by changing the definition of X depends on Y as follows:

Sunday, April 17, 2011

C/C++ include file resolution


Another why-oh-why experience today. I'm trying to find out how the preprocessor is resolving include files, like: what paths will be searched first.

I expected the following behaviour:
  • #include <somefile.h> : only search the paths that were passed to the compiler using the -I switch
  • #include "somefile.h" : first search the folder in which the file being examined lives, only then start searching the folders passed to the compiler using -I.
Instead I found that the behaviour is compiler dependendent (hence the why-oh-why?!) According to this article:
  • the #include <somefile.h> variant:

    StepVisual studio GCC
    1 paths passed by /I paths passed by -I
    2 paths specified in INCLUDE environment variable system directories
  • the #include "somefile.h" variant

    StepVisual studio GCC
    1 the same directory as the file that contains the #include statement the same directory as the file that contains the #include statement
    2 the directories of any previously opened include files in the reverse order in which they were opened directories passed in by the -iquote option
    3 paths passed by /I inside the quote directories
    4 paths in the INCLUDE environment variable
To clarify the "inside the quotes" method: for example if /usr/include/sys/stat.h contains #include "types.h", GCC looks for types.h first in /usr/include/sys, then in its usual search path. So what now? Do we really need separate project resolution algorithms that mimick these approaches? Since I'm not trying to keep the tool's command line options compatible with those of the compiler, I think I can get away with a simpler scheme (counter examples are welcome of course). For now include files will be resolved as follows:
  • Step 1: check the folder of the file being examined (only if the "" style of include was used)
  • Step 2: check the include folders that were passed to the tool using -I like options (in the order in which they were passed in)

Saturday, April 9, 2011

Analyzing C/C++ include file dependencies



Have you ever been looking for a tool that you can query for things like:
  • Which include files does file XXX.cpp include directly ?
  • Which include files does file XXX.cpp include recursively ?
  • Which files include XXX.h ?
  • How does file XXX.cpp include file YYY.h ?
  • How much lines of code have to be recompiled if I touch file YYY.h ?
  • How does project P1 depend on project P2 ?
  • What include statements can be removed from file F because they are implied by other include statements in that same file ?
  • ...and more
Would you also like to 
  • Visualize dependency graphs ?
  • Automatically track violations in case certain projects are not supposed to include from other projects ?
  • Have a tool that is free under GPLv3, open source and cross-platform ?
In a series of blog posts, we will systematically develop exactly such a tool using swi-prolog and python 2.