本文整理汇总了Python中clang.cindex.TranslationUnit.from_source方法的典型用法代码示例。如果您正苦于以下问题:Python TranslationUnit.from_source方法的具体用法?Python TranslationUnit.from_source怎么用?Python TranslationUnit.from_source使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类clang.cindex.TranslationUnit
的用法示例。
在下文中一共展示了TranslationUnit.from_source方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_parse_arguments
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def test_parse_arguments():
path = os.path.join(kInputsDir, 'parse_arguments.c')
index = Index.create()
tu = TranslationUnit.from_source(path, ['-DDECL_ONE=hello', '-DDECL_TWO=hi'], index=index)
spellings = [c.spelling for c in tu.cursor.get_children()]
assert spellings[-2] == 'hello'
assert spellings[-1] == 'hi'
示例2: test_unsaved_files_2
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def test_unsaved_files_2():
import io
index = Index.create()
tu = TranslationUnit.from_source('fake.c', unsaved_files = [
('fake.c', io.StringIO('int x;'))], index=index)
spellings = [c.spelling for c in tu.cursor.get_children()]
assert spellings[-1] == 'x'
示例3: test_fail_from_source
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def test_fail_from_source():
path = os.path.join(kInputsDir, 'non-existent.cpp')
try:
tu = TranslationUnit.from_source(path)
except TranslationUnitLoadError:
tu = None
assert tu == None
示例4: get_tu
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def get_tu(source, lang='c', all_warnings=False, flags=[]):
"""Obtain a translation unit from source and language.
By default, the translation unit is created from source file "t.<ext>"
where <ext> is the default file extension for the specified language. By
default it is C, so "t.c" is the default file name.
Supported languages are {c, cpp, objc}.
all_warnings is a convenience argument to enable all compiler warnings.
"""
args = list(flags)
name = 't.c'
if lang == 'cpp':
name = 't.cpp'
args.append('-std=c++11')
elif lang == 'objc':
name = 't.m'
elif lang != 'c':
raise Exception('Unknown language: %s' % lang)
if all_warnings:
args += ['-Wall', '-Wextra']
return TranslationUnit.from_source(name, args, unsaved_files=[(name,
source)])
示例5: test_reparse_arguments
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def test_reparse_arguments(self):
path = os.path.join(kInputsDir, 'parse_arguments.c')
tu = TranslationUnit.from_source(path, ['-DDECL_ONE=hello', '-DDECL_TWO=hi'])
tu.reparse()
spellings = [c.spelling for c in tu.cursor.get_children()]
self.assertEqual(spellings[-2], 'hello')
self.assertEqual(spellings[-1], 'hi')
示例6: get_tu
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def get_tu(source, lang="c", all_warnings=False, flags=[]):
"""Obtain a translation unit from source and language.
By default, the translation unit is created from source file "t.<ext>"
where <ext> is the default file extension for the specified language. By
default it is C, so "t.c" is the default file name.
Supported languages are {c, cpp, objc}.
all_warnings is a convenience argument to enable all compiler warnings.
"""
args = list(flags)
name = "t.c"
if lang == "cpp":
name = "t.cpp"
args.append("-std=c++11")
elif lang == "objc":
name = "t.m"
elif lang != "c":
raise Exception("Unknown language: %s" % lang)
if all_warnings:
args += ["-Wall", "-Wextra"]
return TranslationUnit.from_source(name, args, unsaved_files=[(name, source)])
示例7: test_comment
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def test_comment(self):
files = [('fake.c', """
/// Aaa.
int test1;
/// Bbb.
/// x
void test2(void);
void f() {
}
""")]
# make a comment-aware TU
tu = TranslationUnit.from_source('fake.c', ['-std=c99'], unsaved_files=files,
options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION)
test1 = get_cursor(tu, 'test1')
self.assertIsNotNone(test1, "Could not find test1.")
self.assertTrue(test1.type.is_pod())
raw = test1.raw_comment
brief = test1.brief_comment
self.assertEqual(raw, """/// Aaa.""")
self.assertEqual(brief, """Aaa.""")
test2 = get_cursor(tu, 'test2')
raw = test2.raw_comment
brief = test2.brief_comment
self.assertEqual(raw, """/// Bbb.\n/// x""")
self.assertEqual(brief, """Bbb. x""")
f = get_cursor(tu, 'f')
raw = f.raw_comment
brief = f.brief_comment
self.assertIsNone(raw)
self.assertIsNone(brief)
示例8: test_comment
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def test_comment():
files = [('fake.c', """
/// Aaa.
int test1;
/// Bbb.
/// x
void test2(void);
void f() {
}
""")]
# make a comment-aware TU
tu = TranslationUnit.from_source('fake.c', ['-std=c99'], unsaved_files=files,
options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION)
test1 = get_cursor(tu, 'test1')
assert test1 is not None, "Could not find test1."
assert test1.type.is_pod()
raw = test1.raw_comment
brief = test1.brief_comment
assert raw == """/// Aaa."""
assert brief == """Aaa."""
test2 = get_cursor(tu, 'test2')
raw = test2.raw_comment
brief = test2.brief_comment
assert raw == """/// Bbb.\n/// x"""
assert brief == """Bbb. x"""
f = get_cursor(tu, 'f')
raw = f.raw_comment
brief = f.brief_comment
assert raw is None
assert brief is None
示例9: collect_nodes
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def collect_nodes(basename, constants):
functions = {}
types = OrderedDict()
# order is important, otherwise the builders will refer to non-existing types.
clang_args = [arg.encode('utf-8') for arg in constants.PKG_CONFIG_RES]
include_paths = [arg[2:] for arg in constants.PKG_CONFIG_RES if arg[:2] == '-I']
tu_name = join(C_FILES, basename + C_EXT)
tu = TranslationUnit.from_source(tu_name, args=clang_args)
for node in tu.cursor.get_children():
name = name_of(node)
if any(regex.match(name) for regex in constants.BLACKLISTED):
continue
source_file = node.location.file
if source_file:
source_filename = source_file.name.decode('utf-8')
if not any(source_filename.startswith(whitelist) for whitelist in constants.HEADER_WHITELIST):
continue
kind = node.kind
if kind == CursorKind.FUNCTION_DECL:
functions[name] = node
elif kind in {CursorKind.STRUCT_DECL, CursorKind.ENUM_DECL}:
types[name] = node
return (types.values(), functions.values())
示例10: test_code_complete
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def test_code_complete():
index = Index.create()
files = [
(
"fake.c",
"""
/// Aaa.
int test1;
/// Bbb.
void test2(void);
void f() {
}
""",
)
]
tu = TranslationUnit.from_source(
"fake.c",
["-std=c99"],
unsaved_files=files,
options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION,
index=index,
)
cr = tu.codeComplete("fake.c", 9, 1, unsaved_files=files, include_brief_comments=True)
expected = [
"{'int', ResultType} | {'test1', TypedText} || Priority: 50 || Availability: Available || Brief comment: Aaa.",
"{'void', ResultType} | {'test2', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 50 || Availability: Available || Brief comment: Bbb.",
"{'return', TypedText} || Priority: 40 || Availability: Available || Brief comment: None",
]
check_completion_results(cr, expected)
示例11: find_candidates
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def find_candidates(filename, ast):
""" Find patterns in 'filename' matching 'ast'
"""
tu = TranslationUnit.from_source(filename, ["-std=c++11"])
for cursor in resolve_ast(tu, ast):
yield cursor.location.line
示例12: test_code_complete
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def test_code_complete():
files = [('fake.c', """
/// Aaa.
int test1;
/// Bbb.
void test2(void);
void f() {
}
""")]
tu = TranslationUnit.from_source('fake.c', ['-std=c99'], unsaved_files=files,
options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION)
cr = tu.codeComplete('fake.c', 9, 1, unsaved_files=files, include_brief_comments=True)
assert cr is not None
assert len(cr.diagnostics) == 0
completions = []
for c in cr.results:
completions.append(str(c))
expected = [
"{'int', ResultType} | {'test1', TypedText} || Priority: 50 || Availability: Available || Brief comment: Aaa.",
"{'void', ResultType} | {'test2', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 50 || Availability: Available || Brief comment: Bbb.",
"{'return', TypedText} || Priority: 40 || Availability: Available || Brief comment: None"
]
for c in expected:
assert c in completions
示例13: test_unsaved_files_2
# 需要导入模块: from clang.cindex import TranslationUnit [as 别名]
# 或者: from clang.cindex.TranslationUnit import from_source [as 别名]
def test_unsaved_files_2():
try:
from StringIO import StringIO
except:
from io import StringIO
tu = TranslationUnit.from_source('fake.c', unsaved_files = [
('fake.c', StringIO('int x;'))])
spellings = [c.spelling for c in tu.cursor.get_children()]
assert spellings[-1] == 'x'