clang无法独立解析我的.h文件 [英] clang can't parse my .h file standalone

查看:106
本文介绍了clang无法独立解析我的.h文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用libclang的python绑定,但我认为此问题是由libclang而不是python绑定引起的.

I'm using python binding of libclang but I think this problem is caused by libclang not by python binding.

我有一个标头 object.h

#ifndef OBJECT_H
#define OBJECT_H

class Object {
public:
  int run();
};

#endif

和实现 object.cpp

#include "object.h"

int Object::run() {
  int a = 0;
  return a*a;
}

如果我访问 object.h 的翻译单元的AST,则最后一个AST节点就是 VAR_DECL class Object .它不会访问 public:... 部分.如果我使用clang直接检查语法,则会抱怨我的头文件错误.

If I visit AST of the translation unit of object.h, the last AST node is VAR_DECL class Object and that's it. It won't visit public:... part. If I use clang to check syntax directly is would complain about my header file is wrong.

$ clang -Xclang -ast-dump -fsyntax-only object/object.h
object/object.h:4:1: error: unknown type name 'class'
class Object {
^
object/object.h:4:13: error: expected ';' after top level declarator
class Object {
            ^
            ;
TranslationUnitDecl 0x7f816102d2d0 <<invalid sloc>>
|-TypedefDecl 0x7f816102d7d0 <<invalid sloc>> __int128_t '__int128'
|-TypedefDecl 0x7f816102d830 <<invalid sloc>> __uint128_t 'unsigned __int128'
|-TypedefDecl 0x7f816102db80 <<invalid sloc>> __builtin_va_list '__va_list_tag [1]'
`-VarDecl 0x7f816102dbf0 <object/object.h:4:1, col:7> Object 'int' invalid
2 errors generated.

如果我使用clang来转储 object.cpp 的所有内容,则不会出现该错误.

If I use clang to dump ast of object.cpp, I won't have that error.

$ clang -Xclang -ast-dump -fsyntax-only object/object.cpp
TranslationUnitDecl 0x7fc6230302d0 <<invalid sloc>>
|-TypedefDecl 0x7fc623030810 <<invalid sloc>> __int128_t '__int128'
|-TypedefDecl 0x7fc623030870 <<invalid sloc>> __uint128_t 'unsigned __int128'
|-TypedefDecl 0x7fc623030c30 <<invalid sloc>> __builtin_va_list '__va_list_tag [1]'
|-CXXRecordDecl 0x7fc623030c80 <object/object.h:4:1, line:7:1> class Object definition
| |-CXXRecordDecl 0x7fc623030d90 <line:4:1, col:7> class Object
| |-AccessSpecDecl 0x7fc623030e20 <line:5:1, col:7> public
| `-CXXMethodDecl 0x7fc623030ea0 <line:6:3, col:11> run 'int (void)'
`-CXXMethodDecl 0x7fc62307be10 parent 0x7fc623030c80 prev 0x7fc623030ea0 <object/object.cpp:3:1, line:6:1> run 'int (void)'
  `-CompoundStmt 0x7fc62307c058 <line:3:19, line:6:1>
    |-DeclStmt 0x7fc62307bf78 <line:4:3, col:12>
    | `-VarDecl 0x7fc62307bf00 <col:3, col:11> a 'int'
    |   `-IntegerLiteral 0x7fc62307bf58 <col:11> 'int' 0
    `-ReturnStmt 0x7fc62307c038 <line:5:3, col:12>
      `-BinaryOperator 0x7fc62307c010 <col:10, col:12> 'int' '*'
        |-ImplicitCastExpr 0x7fc62307bfe0 <col:10> 'int' <LValueToRValue>
        | `-DeclRefExpr 0x7fc62307bf90 <col:10> 'int' lvalue Var 0x7fc62307bf00 'a' 'int'
        `-ImplicitCastExpr 0x7fc62307bff8 <col:12> 'int' <LValueToRValue>
          `-DeclRefExpr 0x7fc62307bfb8 <col:12> 'int' lvalue Var 0x7fc62307bf00 'a' 'int'

似乎clang将 object.h object.cpp 组合在一起然后进行解析.如果是这样,我如何在 object.cpp int Object :: run(){"的三行中获得 Object 的ast节点?为此有一个ast节点吗?

It seems like clang combine object.h object.cpp together then do the parsing. If that's so, how do I get the ast node of Object in thrid line of object.cpp int Object::run() {? Is there a ast node for that?

这也使我感到困惑,就像我在 object.cpp 中访问 run()方法时,会说当前位置在 object.cpp中,但范围在 object.h 中.范围到底是什么意思?除了libclang API文档之外,还有其他更简单的教程文档吗?

It also confuses me a lot like when I visit the run() method in object.cpp, it will say current location is in object.cpp but the extent is in object.h. What does the extent mean exactly? Any easier tutorial documents other than libclang API document?

推荐答案

Clang不知道您在 .h 文件中包含C ++代码.默认情况下,它将 .h 文件视为纯C.当在 .cpp 文件上运行clang时,它知道它正在解析C ++.

Clang doesn't know that you have C++ code in your .h file. By default, it treats a .h file as plain C. When you run clang on your .cpp file, it knows that it's parsing C++.

有两种方法可以解决此问题.

There are two ways to fix this.

  1. 使用 -x 标志告诉clang文件中的语言:

  1. Tell clang what language is in the file using the -x flag:

clang -x c++ -Xclang -ast-dump -fsyntax-only object/object.h

  • 使用后缀 .hh .hpp 重命名文件.这些后缀告诉clang假定文件包含C ++代码.

  • Rename your file to use a .hh or .hpp suffix. These suffixes tell clang to assume the file contains C++ code.

    mv object/object.h object/object.hpp
    clang -Xclang -ast-dump -fsyntax-only object/object.hpp
    

    如果重命名头文件,则需要更改您的 #include 语句以匹配.

    If you rename your header file, you'll need to change your #include statement to match.

    这篇关于clang无法独立解析我的.h文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆