Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何获得C/C++;带有libclang的模块信息_C++_Clang_Clang++_Llvm Clang - Fatal编程技术网

C++ 如何获得C/C++;带有libclang的模块信息

C++ 如何获得C/C++;带有libclang的模块信息,c++,clang,clang++,llvm-clang,C++,Clang,Clang++,Llvm Clang,我正在尝试使用libclang中的模块功能。以下是上下文: 我定义了一个clang模块和一个调用它的源文件: module.modulemap 测试h: test.cpp: cache_path一开始是空的,然后在执行命令后,我可以看到其中的内容,所以这是可行的 我的问题是,当我试图使用libclang解析test.cpp文件以获取有关模块的信息时: #include <stdio.h> #include "clang-c/Index.h" /* compile with: clan

我正在尝试使用libclang中的模块功能。以下是上下文:

我定义了一个clang模块和一个调用它的源文件:

module.modulemap

测试h:

test.cpp:

cache_path一开始是空的,然后在执行命令后,我可以看到其中的内容,所以这是可行的

我的问题是,当我试图使用libclang解析test.cpp文件以获取有关模块的信息时:

#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;
}
首先,clang似乎没有检测到任何类型的游标
CXCursor\u ModuleImportDecl
,然后在任何时刻它都会找到一个有效的模块

我做错了什么

#pragma once

static inline int foo() { return 1; }
// 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();
}
#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;
}
...
Module name  , full name 
Module name  , full name 
Module name  , full name 
Module name  , full name 
Module name  , full name 
...