python libclang: Как отличить типы guish const от constexpr? - PullRequest
1 голос
/ 11 марта 2020

Как мы можем определить, является ли тип объявления переменной constexpr в libclang?
(Если это имеет значение, я использую привязки python)


Вот пример для игры.
Я не смог понять, как отличить guish объявление «const» от «constexpr».

file: a. cpp

constexpr int a = 1;
const int b = 1;

file: test.py

import clang.cindex

def treePrint(cursor,pad=""):
    print("{}{} : {}".format(pad,cursor.kind,cursor))
    for child in cursor.get_children():
        treePrint(child,pad+"  ")

clang.cindex.Config.set_library_path("/usr/lib/llvm-6.0//lib/")
index = clang.cindex.Index.create()
translation_unit = index.parse("a.cpp", args=["-std=c++17"])

c = translation_unit.cursor
print('-- tree', '-'*60)
treePrint(c)
print('-'*70)

CursorKind = clang.cindex.CursorKind
varDecls = [x for x in c.get_children() if x.kind == CursorKind.VAR_DECL]

for vDecl in varDecls:
    print("{} : {}".format(vDecl.spelling, vDecl.type.spelling))

run: python3 test.py
консольный вывод:

-- tree ------------------------------------------------------------
CursorKind.TRANSLATION_UNIT : <clang.cindex.Cursor object at 0x7fc60e776ae8>
  CursorKind.VAR_DECL : <clang.cindex.Cursor object at 0x7fc60e776c80>
    CursorKind.INTEGER_LITERAL : <clang.cindex.Cursor object at 0x7fc60e776d90>
  CursorKind.VAR_DECL : <clang.cindex.Cursor object at 0x7fc60e776d08>
    CursorKind.INTEGER_LITERAL : <clang.cindex.Cursor object at 0x7fc60e776b70>
----------------------------------------------------------------------
a : const int
b : const int
...