llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.85k stars 11.47k forks source link

Segmentation fault when assessing diagnostics via CodeCompletionResults in Python bindings #36566

Open llvmbot opened 6 years ago

llvmbot commented 6 years ago
Bugzilla Link 37218
Version 6.0
OS Linux
Reporter LLVM Bugzilla Contributor

Extended Description

Recorded terminal session with demonstration of the bug: https://asciinema.org/a/EP1pXTWvy4z3OwUJaN4PH5FCh

Test case:

from clang.cindex import TranslationUnit, Diagnostic

def test_diagnostics_via_code_complete():

    files = [('fake.cpp', 'int f0() {x = 1}\n')]

    tu = TranslationUnit.from_source(
        'fake.cpp', ['-std=c++98', '-Wall', '-Wextra'], 
        unsaved_files=files)

    cr = tu.codeComplete(
        'fake.cpp', 1, 16,
        unsaved_files=files)

    assert cr is not None
    assert len(cr.diagnostics) == 1, len(cr.diagnostics)
    # Segfault here
    assert cr.diagnostics[0].severity == Diagnostic.Error
    assert cr.diagnostics[0].location.line == 1 
    assert cr.diagnostics[0].location.column == 11
    assert (cr.diagnostics[0].spelling == "use of undeclared identifier 'x'")

On my local machine I fixed this by replacing DiagnosticsIter.__getitem__ with this code:

    def __getitem__(self, key):
        diag = conf.lib.clang_codeCompleteGetDiagnostic(self.ccr, key)
        return Diagnostic(diag)

and change definition of clang_codeCompleteGetDiagnostic to this:

    (
        "clang_codeCompleteGetDiagnostic",
        [CodeCompletionResults, c_int],
        c_object_p # return type changed
    ),

p.s.: sorry, I don't figure out how to highlight code blocks here

llvmbot commented 1 year ago

@llvm/issue-subscribers-clang-frontend