Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing InitListExpr #115

Closed
useche opened this issue Apr 9, 2016 · 3 comments
Closed

Missing InitListExpr #115

useche opened this issue Apr 9, 2016 · 3 comments
Assignees
Labels

Comments

@useche
Copy link

useche commented Apr 9, 2016

When parsing the following piece of code (taken from uemacs):

struct name_bind {
        char *n_name;
        int (*n_func)(int, int);
};

int ctrlg(int, int);

struct name_bind a[] = {{"a",ctrlg},{"",NULL}};

I don't get the InitListExpression for array a. However, when parsing the code:

int b[] = {1, 2};

I get the list for b just fine.

@marxriedl
Copy link
Contributor

Hi, i will look into it, as soon as I can!

@marxriedl
Copy link
Contributor

Ok, the thing is, atm we can't/don't handle function pointers.
I didn't look too much into how we handle the struct name_bind stuff, but you most certainly won't get the desired result anyway.

Further, I can't promise you when we will be able to add that feature (although it is certainly on my will be eventually added list).

I will close this here, will be fixed by #81.

Still, thanks for reporting and taking an interest in go-clang!

-Markus

@zimmski zimmski reopened this Sep 9, 2016
@zimmski
Copy link
Member

zimmski commented Sep 9, 2016

The problem is not go-clang. Clang presents the AST even if there are errors. You can check all problems using the TranslationUnit method Diagnostics. Given your example I get the following error "error: use of undeclared identifier 'NULL'". After including stddef.h I get "fatal error: 'stddef.h' file not found". And after I added the include path of Clang to the ParseTranslationUnit call it works using the following example program:

package main

import (
    "fmt"

    "github.com/go-clang/v3.7/clang"
)

func main() {
    idx := clang.NewIndex(0, 1)
    defer idx.Dispose()

    tu := idx.ParseTranslationUnit("tt.c", []string{"-I", "/usr/lib/clang/3.7/include"}, nil, 0)
    defer tu.Dispose()

    for _, d := range tu.Diagnostics() {
        fmt.Println("Problem:", d.Spelling())
    }

    cursor := tu.TranslationUnitCursor()

    cursor.Visit(func(cursor, parent clang.Cursor) clang.ChildVisitResult {
        if cursor.IsNull() {
            return clang.ChildVisit_Continue
        }

        fmt.Printf("%s: %s (%s)\n", cursor.Kind().Spelling(), cursor.Spelling(), cursor.USR())

        return clang.ChildVisit_Recurse
    })
}

which outputs for your adapted C code the following

TypedefDecl: ptrdiff_t (c:stddef.h@T@ptrdiff_t)
TypedefDecl: size_t (c:stddef.h@T@size_t)
TypedefDecl: wchar_t (c:stddef.h@T@wchar_t)
StructDecl:  (c:@SA@max_align_t)
FieldDecl: __clang_max_align_nonce1 (c:@SA@max_align_t@FI@__clang_max_align_nonce1)
UnexposedAttr:  ()
FieldDecl: __clang_max_align_nonce2 (c:@SA@max_align_t@FI@__clang_max_align_nonce2)
UnexposedAttr:  ()
TypedefDecl: max_align_t (c:__stddef_max_align_t.h@T@max_align_t)
StructDecl:  (c:@SA@max_align_t)
FieldDecl: __clang_max_align_nonce1 (c:@SA@max_align_t@FI@__clang_max_align_nonce1)
UnexposedAttr:  ()
FieldDecl: __clang_max_align_nonce2 (c:@SA@max_align_t@FI@__clang_max_align_nonce2)
UnexposedAttr:  ()
StructDecl: name_bind (c:@S@name_bind)
FieldDecl: n_name (c:@S@name_bind@FI@n_name)
FieldDecl: n_func (c:@S@name_bind@FI@n_func)
ParmDecl:  ()
ParmDecl:  ()
FunctionDecl: ctrlg (c:@F@ctrlg)
ParmDecl:  ()
ParmDecl:  ()
VarDecl: a (c:@a)
TypeRef: struct name_bind ()
InitListExpr:  ()
InitListExpr:  ()
UnexposedExpr:  ()
StringLiteral: "a" ()
UnexposedExpr: ctrlg ()
DeclRefExpr: ctrlg ()
InitListExpr:  ()
UnexposedExpr:  ()
StringLiteral: "" ()
UnexposedExpr:  ()
ParenExpr:  ()
CStyleCastExpr:  ()
IntegerLiteral:  ()

Of course you have to do the same with your program which is using go-clang and your correct include path.

@zimmski zimmski closed this as completed Sep 9, 2016
zimmski added a commit to go-clang/bootstrap that referenced this issue Sep 11, 2016
Maybe this helps for usage problems like in go-clang/gen#115
zimmski added a commit to go-clang/v3.4 that referenced this issue Dec 2, 2016
Maybe this helps for usage problems like in go-clang/gen#115
zimmski added a commit to go-clang/v3.4 that referenced this issue Dec 2, 2016
Maybe this helps for usage problems like in go-clang/gen#115
zimmski added a commit to go-clang/v3.4 that referenced this issue Dec 2, 2016
Maybe this helps for usage problems like in go-clang/gen#115
zimmski added a commit to go-clang/v3.6 that referenced this issue Dec 2, 2016
Maybe this helps for usage problems like in go-clang/gen#115
zimmski added a commit to go-clang/v3.7 that referenced this issue Dec 2, 2016
Maybe this helps for usage problems like in go-clang/gen#115
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants