[cfe-dev] How to get C/C++ module information with libclang

cedlemo via cfe-dev cfe-dev at lists.llvm.org
Fri Jan 29 02:02:12 PST 2016


I am trying to use the module functionalities from libclang. Here is the 
context:

I have a clang module defined and a source file that call it:

module.modulemap

    module test {
       requires cplusplus
       header "test.h"
    }

test.h :

    #pragma once

    static inline int foo() { return 1; }

test.cpp :

    // Try the following command:
    // clang++ -fmodules -fcxx-modules -fmodules-cache-path=./cache_path
    -c test.cpp
    // If you see stuff in the ./cache_path directory, then it works!
    #include "test.h"

    int main(int, char **) {
       return foo();
    }

cache_path is at first empty then after the command in the comments I 
can see stuff in it so this is working.

My problem is when I try to use libclang to parse the test.cpp file in 
order to get informations about module:

    #include <stdio.h>
    #include "clang-c/Index.h"
    /*
    compile with:
    clang -lclang -o module_parser module_parser.c
    */
    static enum CXChildVisitResult
    visitor(CXCursor cursor, CXCursor parent, CXClientData data)
    {
       CXSourceLocation loc;
       CXFile file;
       CXString module_import;
       CXModule module;
       CXString module_name;
       CXString module_full_name;
       unsigned line;
       unsigned column;
       unsigned offset;
       if (clang_getCursorKind(cursor) == CXCursor_ModuleImportDecl)
       {
         loc = clang_getCursorLocation(cursor);
         clang_getSpellingLocation(loc,
                                   &file,
                                   &line,
                                   &column,
                                   &offset);

         module_import = clang_getCursorSpelling(cursor);
         printf("Module import dec at line: %d \"%s\"\n", line,
    clang_getCString(module_import));
         clang_disposeString(module_import);
       }
       module = clang_Cursor_getModule(cursor);
       module_name = clang_Module_getName(module);
       module_full_name = clang_Module_getFullName(module);
       printf("Module name %s , full name %s\n",
    clang_getCString(module_name),
    clang_getCString(module_full_name));
       clang_disposeString(module_name);
       clang_disposeString(module_full_name);

       return CXChildVisit_Recurse; // visit complete AST recursivly
    }

    int main(int argc, char *argv[]) {
       CXIndex Index = clang_createIndex(0, 1);
       const char *args[] = { "-x",
                              "c++",
                              "-fmodules",
                              "-fcxxmodules"//,
                              "-fmodules-cache-path",
                              "cache_path"
                              };
       CXTranslationUnit TU =
    clang_createTranslationUnitFromSourceFile(Index,
                                                           "test.cpp",
                                                           6,
                                                           args,
                                                           0,
                                                           0);

       clang_visitChildren(clang_getTranslationUnitCursor(TU), visitor, 0);

       clang_disposeTranslationUnit(TU);
       clang_disposeIndex(Index);
       return 0;
    }


The output of this code is :

    ...
    Module name  , full name
    Module name  , full name
    Module name  , full name
    Module name  , full name
    Module name  , full name
    ...

First it seems that clang doesn't detect any cursor of the kind 
CXCursor_ModuleImportDecl and then at any momment it find a valid module.

What am I doing wrong?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160129/477e473e/attachment.html>


More information about the cfe-dev mailing list