Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / bindings / python / tests / cindex / test_code_completion.py
blobca52fc6f73e1d2c973a5515951e9be638a032d44
1 import os
2 from clang.cindex import Config
4 if "CLANG_LIBRARY_PATH" in os.environ:
5 Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
7 from clang.cindex import TranslationUnit
9 import unittest
10 from .util import skip_if_no_fspath
11 from .util import str_to_path
14 class TestCodeCompletion(unittest.TestCase):
15 def check_completion_results(self, cr, expected):
16 self.assertIsNotNone(cr)
17 self.assertEqual(len(cr.diagnostics), 0)
19 completions = [str(c) for c in cr.results]
21 for c in expected:
22 self.assertIn(c, completions)
24 def test_code_complete(self):
25 files = [
27 "fake.c",
28 """
29 /// Aaa.
30 int test1;
32 /// Bbb.
33 void test2(void);
35 void f() {
38 """,
42 tu = TranslationUnit.from_source(
43 "fake.c",
44 ["-std=c99"],
45 unsaved_files=files,
46 options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION,
49 cr = tu.codeComplete(
50 "fake.c", 9, 1, unsaved_files=files, include_brief_comments=True
53 expected = [
54 "{'int', ResultType} | {'test1', TypedText} || Priority: 50 || Availability: Available || Brief comment: Aaa.",
55 "{'void', ResultType} | {'test2', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 50 || Availability: Available || Brief comment: Bbb.",
56 "{'return', TypedText} | {';', SemiColon} || Priority: 40 || Availability: Available || Brief comment: None",
58 self.check_completion_results(cr, expected)
60 @skip_if_no_fspath
61 def test_code_complete_pathlike(self):
62 files = [
64 str_to_path("fake.c"),
65 """
66 /// Aaa.
67 int test1;
69 /// Bbb.
70 void test2(void);
72 void f() {
75 """,
79 tu = TranslationUnit.from_source(
80 str_to_path("fake.c"),
81 ["-std=c99"],
82 unsaved_files=files,
83 options=TranslationUnit.PARSE_INCLUDE_BRIEF_COMMENTS_IN_CODE_COMPLETION,
86 cr = tu.codeComplete(
87 str_to_path("fake.c"),
90 unsaved_files=files,
91 include_brief_comments=True,
94 expected = [
95 "{'int', ResultType} | {'test1', TypedText} || Priority: 50 || Availability: Available || Brief comment: Aaa.",
96 "{'void', ResultType} | {'test2', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 50 || Availability: Available || Brief comment: Bbb.",
97 "{'return', TypedText} | {';', SemiColon} || Priority: 40 || Availability: Available || Brief comment: None",
99 self.check_completion_results(cr, expected)
101 def test_code_complete_availability(self):
102 files = [
104 "fake.cpp",
106 class P {
107 protected:
108 int member;
111 class Q : public P {
112 public:
113 using P::member;
116 void f(P x, Q y) {
117 x.; // member is inaccessible
118 y.; // member is accessible
120 """,
124 tu = TranslationUnit.from_source(
125 "fake.cpp", ["-std=c++98"], unsaved_files=files
128 cr = tu.codeComplete("fake.cpp", 12, 5, unsaved_files=files)
130 expected = [
131 "{'const', TypedText} || Priority: 50 || Availability: Available || Brief comment: None",
132 "{'volatile', TypedText} || Priority: 50 || Availability: Available || Brief comment: None",
133 "{'operator', TypedText} || Priority: 40 || Availability: Available || Brief comment: None",
134 "{'P', TypedText} || Priority: 50 || Availability: Available || Brief comment: None",
135 "{'Q', TypedText} || Priority: 50 || Availability: Available || Brief comment: None",
137 self.check_completion_results(cr, expected)
139 cr = tu.codeComplete("fake.cpp", 13, 5, unsaved_files=files)
140 expected = [
141 "{'P', TypedText} | {'::', Text} || Priority: 75 || Availability: Available || Brief comment: None",
142 "{'P &', ResultType} | {'operator=', TypedText} | {'(', LeftParen} | {'const P &', Placeholder} | {')', RightParen} || Priority: 79 || Availability: Available || Brief comment: None",
143 "{'int', ResultType} | {'member', TypedText} || Priority: 35 || Availability: NotAccessible || Brief comment: None",
144 "{'void', ResultType} | {'~P', TypedText} | {'(', LeftParen} | {')', RightParen} || Priority: 79 || Availability: Available || Brief comment: None",
146 self.check_completion_results(cr, expected)