当前位置: 首页>>代码示例>>Python>>正文


Python cindex.TranslationUnit类代码示例

本文整理汇总了Python中clang.cindex.TranslationUnit的典型用法代码示例。如果您正苦于以下问题:Python TranslationUnit类的具体用法?Python TranslationUnit怎么用?Python TranslationUnit使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了TranslationUnit类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_code_complete

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)
开发者ID:plotnikovanton,项目名称:dotfiles,代码行数:35,代码来源:test_code_completion.py

示例2: test_comment

    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)
开发者ID:Bekenn,项目名称:clang,代码行数:35,代码来源:test_comment.py

示例3: test_fail_from_ast_file

def test_fail_from_ast_file():
    path = os.path.join(kInputsDir, 'non-existent.ast')
    try:
        tu = TranslationUnit.from_ast_file(path)
    except TranslationUnitLoadError:
        tu = None
    assert tu == None
开发者ID:AntonBikineev,项目名称:clang,代码行数:7,代码来源:test_translation_unit.py

示例4: test_unsaved_files_2

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'
开发者ID:Elv13,项目名称:Config_Files,代码行数:7,代码来源:test_translation_unit.py

示例5: test_comment

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
开发者ID:4ntoine,项目名称:clang,代码行数:35,代码来源:test_comment.py

示例6: get_tu

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)])
开发者ID:rgov,项目名称:ctypeslib,代码行数:25,代码来源:util.py

示例7: test_code_complete

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
开发者ID:FunkMonkey,项目名称:libClang,代码行数:32,代码来源:test_code_completion.py

示例8: collect_nodes

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())
开发者ID:kennytm,项目名称:mozart2-gtk3,代码行数:28,代码来源:translator.py

示例9: test_fail_from_source

 def test_fail_from_source(self):
     path = os.path.join(kInputsDir, 'non-existent.cpp')
     try:
         tu = TranslationUnit.from_source(path)
     except TranslationUnitLoadError:
         tu = None
     self.assertEqual(tu, None)
开发者ID:Bekenn,项目名称:clang,代码行数:7,代码来源:test_translation_unit.py

示例10: test_reparse_arguments

 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')
开发者ID:Bekenn,项目名称:clang,代码行数:7,代码来源:test_translation_unit.py

示例11: test_parse_arguments

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'
开发者ID:Elv13,项目名称:Config_Files,代码行数:7,代码来源:test_translation_unit.py

示例12: find_candidates

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
开发者ID:chubbymaggie,项目名称:SyntaxAwareSearch,代码行数:7,代码来源:cpp.py

示例13: get_tu

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)])
开发者ID:AndrewWalker,项目名称:sealang,代码行数:26,代码来源:util.py

示例14: test_fail_from_source

def test_fail_from_source():
    path = os.path.join(kInputsDir, 'non-existent.cpp')
    try:
        index = Index.create()
        tu = TranslationUnit.from_source(path, index=index)
    except TranslationUnitLoadError:
        tu = None
    assert tu == None
开发者ID:Elv13,项目名称:Config_Files,代码行数:8,代码来源:test_translation_unit.py

示例15: test_unsaved_files_2

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'
开发者ID:CTSRD-SOAAP,项目名称:clang,代码行数:9,代码来源:test_translation_unit.py


注:本文中的clang.cindex.TranslationUnit类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。