Fraunhofer-AISEC / cpg

A library to extract Code Property Graphs from C/C++, Java, Go, Python, Ruby and every other language through LLVM-IR.
https://fraunhofer-aisec.github.io/cpg/
Apache License 2.0
286 stars 62 forks source link

Question about typedef #412

Open peckto opened 3 years ago

peckto commented 3 years ago

I have a question about the representation of C typedef statements in the cpg.

Example:

#include <stdio.h>

typedef int MyInt;

struct  MyS {
    int a;
    int b;
    MyInt c;
};

typedef struct MyS MyS_t;

int main() {
    struct MyS s1;
    MyS_t s2;

    return 0;
}

When loading the sample C code via cpg-vis-neo4j (cpg v3.5.1), the graph looks like the following: cpg_graph

When analyzing structs (where typedefs are often used), it would be nice, to have an RECORD_DECLARATION edge between the ObjectType MyS_t and the Declaration of MyS. In this way the Declaration of s1 and s2 could be resolved the same way. In the current cpg, the only way I see to resolve this problem is to create a link between the VariableDeclaration MyS_t and the ObjectType MyS_t based the name.

Can you reproduce this behavior? Is there a different way, to resolve the Declaration of s2?

Masrepus commented 3 years ago

Typedefs are generally supported, as you can see with the TypedefDeclaration child of the TranslationUnitDeclaration that maps MyInt to int. The concrete case you have here is unfortunately parsed in a wrong way by the typedef handling mechanisms