CppComprehensionEngine.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "CppComprehensionEngine.h"
  7. #include <AK/Assertions.h>
  8. #include <AK/HashTable.h>
  9. #include <AK/OwnPtr.h>
  10. #include <LibCore/DirIterator.h>
  11. #include <LibCore/File.h>
  12. #include <LibCpp/AST.h>
  13. #include <LibCpp/Lexer.h>
  14. #include <LibCpp/Parser.h>
  15. #include <LibCpp/Preprocessor.h>
  16. #include <LibRegex/Regex.h>
  17. #include <Userland/DevTools/HackStudio/LanguageServers/ClientConnection.h>
  18. namespace LanguageServers::Cpp {
  19. CppComprehensionEngine::CppComprehensionEngine(const FileDB& filedb)
  20. : CodeComprehensionEngine(filedb, true)
  21. {
  22. }
  23. const CppComprehensionEngine::DocumentData* CppComprehensionEngine::get_or_create_document_data(const String& file)
  24. {
  25. auto absolute_path = filedb().to_absolute_path(file);
  26. if (!m_documents.contains(absolute_path)) {
  27. set_document_data(absolute_path, create_document_data_for(absolute_path));
  28. }
  29. return get_document_data(absolute_path);
  30. }
  31. const CppComprehensionEngine::DocumentData* CppComprehensionEngine::get_document_data(const String& file) const
  32. {
  33. auto absolute_path = filedb().to_absolute_path(file);
  34. auto document_data = m_documents.get(absolute_path);
  35. if (!document_data.has_value())
  36. return nullptr;
  37. return document_data.value();
  38. }
  39. OwnPtr<CppComprehensionEngine::DocumentData> CppComprehensionEngine::create_document_data_for(const String& file)
  40. {
  41. auto document = filedb().get_or_create_from_filesystem(file);
  42. if (!document)
  43. return {};
  44. return create_document_data(document->text(), file);
  45. }
  46. void CppComprehensionEngine::set_document_data(const String& file, OwnPtr<DocumentData>&& data)
  47. {
  48. m_documents.set(filedb().to_absolute_path(file), move(data));
  49. }
  50. Vector<GUI::AutocompleteProvider::Entry> CppComprehensionEngine::get_suggestions(const String& file, const GUI::TextPosition& autocomplete_position)
  51. {
  52. Cpp::Position position { autocomplete_position.line(), autocomplete_position.column() > 0 ? autocomplete_position.column() - 1 : 0 };
  53. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "CppComprehensionEngine position {}:{}", position.line, position.column);
  54. const auto* document_ptr = get_or_create_document_data(file);
  55. if (!document_ptr)
  56. return {};
  57. const auto& document = *document_ptr;
  58. auto containing_token = document.parser().token_at(position);
  59. if (containing_token.has_value() && containing_token->type() == Token::Type::IncludePath) {
  60. auto results = try_autocomplete_include(document, containing_token.value());
  61. if (results.has_value())
  62. return results.value();
  63. }
  64. auto node = document.parser().node_at(position);
  65. if (!node) {
  66. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "no node at position {}:{}", position.line, position.column);
  67. return {};
  68. }
  69. if (node->parent() && node->parent()->parent())
  70. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "node: {}, parent: {}, grandparent: {}", node->class_name(), node->parent()->class_name(), node->parent()->parent()->class_name());
  71. if (!node->parent())
  72. return {};
  73. auto results = try_autocomplete_property(document, *node, containing_token);
  74. if (results.has_value())
  75. return results.value();
  76. results = try_autocomplete_name(document, *node, containing_token);
  77. if (results.has_value())
  78. return results.value();
  79. return {};
  80. }
  81. Optional<Vector<GUI::AutocompleteProvider::Entry>> CppComprehensionEngine::try_autocomplete_name(const DocumentData& document, const ASTNode& node, Optional<Token> containing_token) const
  82. {
  83. auto partial_text = String::empty();
  84. if (containing_token.has_value() && containing_token.value().type() != Token::Type::ColonColon) {
  85. partial_text = containing_token.value().text();
  86. }
  87. return autocomplete_name(document, node, partial_text);
  88. }
  89. Optional<Vector<GUI::AutocompleteProvider::Entry>> CppComprehensionEngine::try_autocomplete_property(const DocumentData& document, const ASTNode& node, Optional<Token> containing_token) const
  90. {
  91. if (!containing_token.has_value())
  92. return {};
  93. if (!node.parent()->is_member_expression())
  94. return {};
  95. const auto& parent = static_cast<const MemberExpression&>(*node.parent());
  96. auto partial_text = String::empty();
  97. if (containing_token.value().type() != Token::Type::Dot) {
  98. if (&node != parent.property())
  99. return {};
  100. partial_text = containing_token.value().text();
  101. }
  102. return autocomplete_property(document, parent, partial_text);
  103. }
  104. Vector<GUI::AutocompleteProvider::Entry> CppComprehensionEngine::autocomplete_name(const DocumentData& document, const ASTNode& node, const String& partial_text) const
  105. {
  106. auto reference_scope = scope_of_reference_to_symbol(node);
  107. auto current_scope = scope_of_node(node);
  108. auto symbol_matches = [&](const Symbol& symbol) {
  109. if (!is_symbol_available(symbol, current_scope, reference_scope)) {
  110. return false;
  111. }
  112. if (!symbol.name.name.starts_with(partial_text))
  113. return false;
  114. if (symbol.is_local) {
  115. // If this symbol was declared bellow us in a function, it's not available to us.
  116. bool is_unavailable = symbol.is_local && symbol.declaration->start().line > node.start().line;
  117. if (is_unavailable)
  118. return false;
  119. }
  120. return true;
  121. };
  122. Vector<Symbol> matches;
  123. for_each_available_symbol(document, [&](const Symbol& symbol) {
  124. if (symbol_matches(symbol)) {
  125. matches.append(symbol);
  126. }
  127. return IterationDecision::Continue;
  128. });
  129. Vector<GUI::AutocompleteProvider::Entry> suggestions;
  130. for (auto& symbol : matches) {
  131. suggestions.append({ symbol.name.name, partial_text.length(), GUI::AutocompleteProvider::CompletionKind::Identifier });
  132. }
  133. if (reference_scope.is_empty()) {
  134. for (auto& preprocessor_name : document.preprocessor().definitions().keys()) {
  135. if (preprocessor_name.starts_with(partial_text)) {
  136. suggestions.append({ preprocessor_name.to_string(), partial_text.length(), GUI::AutocompleteProvider::CompletionKind::PreprocessorDefinition });
  137. }
  138. }
  139. }
  140. return suggestions;
  141. }
  142. Vector<StringView> CppComprehensionEngine::scope_of_reference_to_symbol(const ASTNode& node) const
  143. {
  144. const Name* name = nullptr;
  145. if (node.is_name()) {
  146. // FIXME It looks like this code path is never taken
  147. name = reinterpret_cast<const Name*>(&node);
  148. } else if (node.is_identifier()) {
  149. auto* parent = node.parent();
  150. if (!(parent && parent->is_name()))
  151. return {};
  152. name = reinterpret_cast<const Name*>(parent);
  153. } else {
  154. return {};
  155. }
  156. VERIFY(name->is_name());
  157. Vector<StringView> scope_parts;
  158. for (auto& scope_part : name->scope()) {
  159. scope_parts.append(scope_part.name());
  160. }
  161. return scope_parts;
  162. }
  163. Vector<GUI::AutocompleteProvider::Entry> CppComprehensionEngine::autocomplete_property(const DocumentData& document, const MemberExpression& parent, const String partial_text) const
  164. {
  165. VERIFY(parent.object());
  166. auto type = type_of(document, *parent.object());
  167. if (type.is_null()) {
  168. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "Could not infer type of object");
  169. return {};
  170. }
  171. Vector<GUI::AutocompleteProvider::Entry> suggestions;
  172. for (auto& prop : properties_of_type(document, type)) {
  173. if (prop.name.name.starts_with(partial_text)) {
  174. suggestions.append({ prop.name.name, partial_text.length(), GUI::AutocompleteProvider::CompletionKind::Identifier });
  175. }
  176. }
  177. return suggestions;
  178. }
  179. bool CppComprehensionEngine::is_property(const ASTNode& node) const
  180. {
  181. if (!node.parent()->is_member_expression())
  182. return false;
  183. auto& parent = verify_cast<MemberExpression>(*node.parent());
  184. return parent.property() == &node;
  185. }
  186. String CppComprehensionEngine::type_of_property(const DocumentData& document, const Identifier& identifier) const
  187. {
  188. auto& parent = verify_cast<MemberExpression>(*identifier.parent());
  189. VERIFY(parent.object());
  190. auto properties = properties_of_type(document, type_of(document, *parent.object()));
  191. for (auto& prop : properties) {
  192. if (prop.name.name != identifier.name())
  193. continue;
  194. const Type* type { nullptr };
  195. if (prop.declaration->is_variable_declaration()) {
  196. type = verify_cast<VariableDeclaration>(*prop.declaration).type();
  197. }
  198. if (!type)
  199. continue;
  200. if (!type->is_named_type())
  201. continue;
  202. VERIFY(verify_cast<NamedType>(*type).name());
  203. return verify_cast<NamedType>(*type).name()->full_name();
  204. }
  205. return {};
  206. }
  207. String CppComprehensionEngine::type_of_variable(const Identifier& identifier) const
  208. {
  209. const ASTNode* current = &identifier;
  210. while (current) {
  211. for (auto& decl : current->declarations()) {
  212. if (decl.is_variable_or_parameter_declaration()) {
  213. auto& var_or_param = verify_cast<VariableOrParameterDeclaration>(decl);
  214. if (var_or_param.name() == identifier.name() && var_or_param.type()->is_named_type()) {
  215. VERIFY(verify_cast<NamedType>(*var_or_param.type()).name());
  216. return verify_cast<NamedType>(*var_or_param.type()).name()->full_name();
  217. }
  218. }
  219. }
  220. current = current->parent();
  221. }
  222. return {};
  223. }
  224. String CppComprehensionEngine::type_of(const DocumentData& document, const Expression& expression) const
  225. {
  226. if (expression.is_member_expression()) {
  227. auto& member_expression = verify_cast<MemberExpression>(expression);
  228. VERIFY(member_expression.property());
  229. if (member_expression.property()->is_identifier())
  230. return type_of_property(document, static_cast<const Identifier&>(*member_expression.property()));
  231. return {};
  232. }
  233. const Identifier* identifier { nullptr };
  234. if (expression.is_name()) {
  235. identifier = static_cast<const Name&>(expression).name();
  236. } else if (expression.is_identifier()) {
  237. identifier = &static_cast<const Identifier&>(expression);
  238. } else {
  239. dbgln("expected identifier or name, got: {}", expression.class_name());
  240. VERIFY_NOT_REACHED(); // TODO
  241. }
  242. VERIFY(identifier);
  243. if (is_property(*identifier))
  244. return type_of_property(document, *identifier);
  245. return type_of_variable(*identifier);
  246. }
  247. Vector<CppComprehensionEngine::Symbol> CppComprehensionEngine::properties_of_type(const DocumentData& document, const String& type) const
  248. {
  249. auto type_symbol = SymbolName::create(type);
  250. auto decl = find_declaration_of(document, type_symbol);
  251. if (!decl) {
  252. dbgln("Couldn't find declaration of type: {}", type);
  253. return {};
  254. }
  255. if (!decl->is_struct_or_class()) {
  256. dbgln("Expected declaration of type: {} to be struct or class", type);
  257. return {};
  258. }
  259. auto& struct_or_class = verify_cast<StructOrClassDeclaration>(*decl);
  260. VERIFY(struct_or_class.name() == type_symbol.name);
  261. Vector<Symbol> properties;
  262. for (auto& member : struct_or_class.members()) {
  263. Vector<StringView> scope(type_symbol.scope);
  264. scope.append(type_symbol.name);
  265. // FIXME: We don't have to create the Symbol here, it should already exist in the 'm_symbol' table of some DocumentData we already parsed.
  266. properties.append(Symbol::create(member.name(), scope, member, Symbol::IsLocal::No));
  267. }
  268. return properties;
  269. }
  270. CppComprehensionEngine::Symbol CppComprehensionEngine::Symbol::create(StringView name, const Vector<StringView>& scope, NonnullRefPtr<Declaration> declaration, IsLocal is_local)
  271. {
  272. return { { name, scope }, move(declaration), is_local == IsLocal::Yes };
  273. }
  274. Vector<CppComprehensionEngine::Symbol> CppComprehensionEngine::get_child_symbols(const ASTNode& node) const
  275. {
  276. return get_child_symbols(node, {}, Symbol::IsLocal::No);
  277. }
  278. Vector<CppComprehensionEngine::Symbol> CppComprehensionEngine::get_child_symbols(const ASTNode& node, const Vector<StringView>& scope, Symbol::IsLocal is_local) const
  279. {
  280. Vector<Symbol> symbols;
  281. for (auto& decl : node.declarations()) {
  282. symbols.append(Symbol::create(decl.name(), scope, decl, is_local));
  283. bool should_recurse = decl.is_namespace() || decl.is_struct_or_class() || decl.is_function();
  284. bool are_child_symbols_local = decl.is_function();
  285. if (!should_recurse)
  286. continue;
  287. auto new_scope = scope;
  288. new_scope.append(decl.name());
  289. symbols.extend(get_child_symbols(decl, new_scope, are_child_symbols_local ? Symbol::IsLocal::Yes : is_local));
  290. }
  291. return symbols;
  292. }
  293. String CppComprehensionEngine::document_path_from_include_path(const StringView& include_path) const
  294. {
  295. static Regex<PosixExtended> library_include("<(.+)>");
  296. static Regex<PosixExtended> user_defined_include("\"(.+)\"");
  297. auto document_path_for_library_include = [&](const StringView& include_path) -> String {
  298. RegexResult result;
  299. if (!library_include.search(include_path, result))
  300. return {};
  301. auto path = result.capture_group_matches.at(0).at(0).view.string_view();
  302. return String::formatted("/usr/include/{}", path);
  303. };
  304. auto document_path_for_user_defined_include = [&](const StringView& include_path) -> String {
  305. RegexResult result;
  306. if (!user_defined_include.search(include_path, result))
  307. return {};
  308. return result.capture_group_matches.at(0).at(0).view.string_view();
  309. };
  310. auto result = document_path_for_library_include(include_path);
  311. if (result.is_null())
  312. result = document_path_for_user_defined_include(include_path);
  313. return result;
  314. }
  315. void CppComprehensionEngine::on_edit(const String& file)
  316. {
  317. set_document_data(file, create_document_data_for(file));
  318. }
  319. void CppComprehensionEngine::file_opened([[maybe_unused]] const String& file)
  320. {
  321. get_or_create_document_data(file);
  322. }
  323. Optional<GUI::AutocompleteProvider::ProjectLocation> CppComprehensionEngine::find_declaration_of(const String& filename, const GUI::TextPosition& identifier_position)
  324. {
  325. const auto* document_ptr = get_or_create_document_data(filename);
  326. if (!document_ptr)
  327. return {};
  328. const auto& document = *document_ptr;
  329. auto node = document.parser().node_at(Cpp::Position { identifier_position.line(), identifier_position.column() });
  330. if (!node) {
  331. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "no node at position {}:{}", identifier_position.line(), identifier_position.column());
  332. return {};
  333. }
  334. auto decl = find_declaration_of(document, *node);
  335. if (decl)
  336. return GUI::AutocompleteProvider::ProjectLocation { decl->filename(), decl->start().line, decl->start().column };
  337. return find_preprocessor_definition(document, identifier_position);
  338. }
  339. Optional<GUI::AutocompleteProvider::ProjectLocation> CppComprehensionEngine::find_preprocessor_definition(const DocumentData& document, const GUI::TextPosition& text_position)
  340. {
  341. Position cpp_position { text_position.line(), text_position.column() };
  342. // Search for a replaced preprocessor token that intersects with text_position
  343. for (auto& substitution : document.preprocessor().substitutions()) {
  344. if (substitution.original_token.start() > cpp_position)
  345. continue;
  346. if (substitution.original_token.end() < cpp_position)
  347. continue;
  348. return GUI::AutocompleteProvider::ProjectLocation { substitution.defined_value.filename, substitution.defined_value.line, substitution.defined_value.column };
  349. }
  350. return {};
  351. }
  352. struct TargetDeclaration {
  353. enum Type {
  354. Variable,
  355. Type,
  356. Function,
  357. Property
  358. } type;
  359. String name;
  360. };
  361. static Optional<TargetDeclaration> get_target_declaration(const ASTNode& node)
  362. {
  363. if (!node.is_identifier()) {
  364. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "node is not an identifier");
  365. return {};
  366. }
  367. String name = static_cast<const Identifier&>(node).name();
  368. if ((node.parent() && node.parent()->is_function_call()) || (node.parent()->is_name() && node.parent()->parent() && node.parent()->parent()->is_function_call())) {
  369. return TargetDeclaration { TargetDeclaration::Type::Function, name };
  370. }
  371. if ((node.parent() && node.parent()->is_type()) || (node.parent()->is_name() && node.parent()->parent() && node.parent()->parent()->is_type()))
  372. return TargetDeclaration { TargetDeclaration::Type::Type, name };
  373. if ((node.parent() && node.parent()->is_member_expression()))
  374. return TargetDeclaration { TargetDeclaration::Type::Property, name };
  375. return TargetDeclaration { TargetDeclaration::Type::Variable, name };
  376. }
  377. RefPtr<Declaration> CppComprehensionEngine::find_declaration_of(const DocumentData& document_data, const ASTNode& node) const
  378. {
  379. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "find_declaration_of: {} ({})", document_data.parser().text_of_node(node), node.class_name());
  380. if (!node.is_identifier()) {
  381. dbgln("node is not an identifier, can't find declaration");
  382. return {};
  383. }
  384. auto target_decl = get_target_declaration(node);
  385. if (!target_decl.has_value())
  386. return {};
  387. auto reference_scope = scope_of_reference_to_symbol(node);
  388. auto current_scope = scope_of_node(node);
  389. auto symbol_matches = [&](const Symbol& symbol) {
  390. bool match_function = target_decl.value().type == TargetDeclaration::Function && symbol.declaration->is_function();
  391. bool match_variable = target_decl.value().type == TargetDeclaration::Variable && symbol.declaration->is_variable_declaration();
  392. bool match_type = target_decl.value().type == TargetDeclaration::Type && symbol.declaration->is_struct_or_class();
  393. bool match_property = target_decl.value().type == TargetDeclaration::Property && symbol.declaration->parent()->is_declaration() && verify_cast<Declaration>(symbol.declaration->parent())->is_struct_or_class();
  394. bool match_parameter = target_decl.value().type == TargetDeclaration::Variable && symbol.declaration->is_parameter();
  395. if (match_property) {
  396. // FIXME: This is not really correct, we also need to check that the type of the struct/class matches (not just the property name)
  397. if (symbol.name.name == target_decl.value().name) {
  398. return true;
  399. }
  400. }
  401. if (!is_symbol_available(symbol, current_scope, reference_scope)) {
  402. return false;
  403. }
  404. if (match_function || match_type) {
  405. if (symbol.name.name == target_decl->name)
  406. return true;
  407. }
  408. if (match_variable || match_parameter) {
  409. // If this symbol was declared bellow us in a function, it's not available to us.
  410. bool is_unavailable = symbol.is_local && symbol.declaration->start().line > node.start().line;
  411. if (!is_unavailable && (symbol.name.name == target_decl->name)) {
  412. return true;
  413. }
  414. }
  415. return false;
  416. };
  417. Optional<Symbol> match;
  418. for_each_available_symbol(document_data, [&](const Symbol& symbol) {
  419. if (symbol_matches(symbol)) {
  420. match = symbol;
  421. return IterationDecision::Break;
  422. }
  423. return IterationDecision::Continue;
  424. });
  425. if (!match.has_value())
  426. return {};
  427. return match->declaration;
  428. }
  429. void CppComprehensionEngine::update_declared_symbols(DocumentData& document)
  430. {
  431. for (auto& symbol : get_child_symbols(*document.parser().root_node())) {
  432. document.m_symbols.set(symbol.name, move(symbol));
  433. }
  434. Vector<GUI::AutocompleteProvider::Declaration> declarations;
  435. for (auto& symbol_entry : document.m_symbols) {
  436. auto& symbol = symbol_entry.value;
  437. declarations.append({ symbol.name.name, { document.filename(), symbol.declaration->start().line, symbol.declaration->start().column }, type_of_declaration(symbol.declaration), symbol.name.scope_as_string() });
  438. }
  439. for (auto& definition : document.preprocessor().definitions()) {
  440. declarations.append({ definition.key, { document.filename(), definition.value.line, definition.value.column }, GUI::AutocompleteProvider::DeclarationType::PreprocessorDefinition, {} });
  441. }
  442. set_declarations_of_document(document.filename(), move(declarations));
  443. }
  444. void CppComprehensionEngine::update_todo_entries(DocumentData& document)
  445. {
  446. set_todo_entries_of_document(document.filename(), document.parser().get_todo_entries());
  447. }
  448. GUI::AutocompleteProvider::DeclarationType CppComprehensionEngine::type_of_declaration(const Declaration& decl)
  449. {
  450. if (decl.is_struct())
  451. return GUI::AutocompleteProvider::DeclarationType::Struct;
  452. if (decl.is_class())
  453. return GUI::AutocompleteProvider::DeclarationType::Class;
  454. if (decl.is_function())
  455. return GUI::AutocompleteProvider::DeclarationType::Function;
  456. if (decl.is_variable_declaration())
  457. return GUI::AutocompleteProvider::DeclarationType::Variable;
  458. if (decl.is_namespace())
  459. return GUI::AutocompleteProvider::DeclarationType::Namespace;
  460. if (decl.is_member())
  461. return GUI::AutocompleteProvider::DeclarationType::Member;
  462. return GUI::AutocompleteProvider::DeclarationType::Variable;
  463. }
  464. OwnPtr<CppComprehensionEngine::DocumentData> CppComprehensionEngine::create_document_data(String&& text, const String& filename)
  465. {
  466. auto document_data = make<DocumentData>();
  467. document_data->m_filename = filename;
  468. document_data->m_text = move(text);
  469. document_data->m_preprocessor = make<Preprocessor>(document_data->m_filename, document_data->text());
  470. document_data->preprocessor().set_ignore_unsupported_keywords(true);
  471. document_data->preprocessor().set_keep_include_statements(true);
  472. document_data->preprocessor().definitions_in_header_callback = [this](StringView include_path) -> Preprocessor::Definitions {
  473. auto included_document = get_or_create_document_data(document_path_from_include_path(include_path));
  474. if (!included_document)
  475. return {};
  476. return included_document->preprocessor().definitions();
  477. };
  478. auto tokens = document_data->preprocessor().process_and_lex();
  479. for (auto include_path : document_data->preprocessor().included_paths()) {
  480. auto include_fullpath = document_path_from_include_path(include_path);
  481. auto included_document = get_or_create_document_data(include_fullpath);
  482. if (!included_document)
  483. continue;
  484. document_data->m_available_headers.set(include_fullpath);
  485. for (auto& header : included_document->m_available_headers)
  486. document_data->m_available_headers.set(header);
  487. }
  488. document_data->m_parser = make<Parser>(move(tokens), filename);
  489. auto root = document_data->parser().parse();
  490. if constexpr (CPP_LANGUAGE_SERVER_DEBUG)
  491. root->dump();
  492. update_declared_symbols(*document_data);
  493. update_todo_entries(*document_data);
  494. return document_data;
  495. }
  496. Vector<StringView> CppComprehensionEngine::scope_of_node(const ASTNode& node) const
  497. {
  498. auto parent = node.parent();
  499. if (!parent)
  500. return {};
  501. auto parent_scope = scope_of_node(*parent);
  502. if (!parent->is_declaration())
  503. return parent_scope;
  504. auto& parent_decl = static_cast<Declaration&>(*parent);
  505. StringView containing_scope;
  506. if (parent_decl.is_namespace())
  507. containing_scope = static_cast<NamespaceDeclaration&>(parent_decl).name();
  508. if (parent_decl.is_struct_or_class())
  509. containing_scope = static_cast<StructOrClassDeclaration&>(parent_decl).name();
  510. if (parent_decl.is_function())
  511. containing_scope = static_cast<FunctionDeclaration&>(parent_decl).name();
  512. parent_scope.append(containing_scope);
  513. return parent_scope;
  514. }
  515. Optional<Vector<GUI::AutocompleteProvider::Entry>> CppComprehensionEngine::try_autocomplete_include(const DocumentData&, Token include_path_token)
  516. {
  517. VERIFY(include_path_token.type() == Token::Type::IncludePath);
  518. auto partial_include = include_path_token.text().trim_whitespace();
  519. String include_root;
  520. auto include_type = GUI::AutocompleteProvider::CompletionKind::ProjectInclude;
  521. if (partial_include.starts_with("<")) {
  522. include_root = "/usr/include/";
  523. include_type = GUI::AutocompleteProvider::CompletionKind::SystemInclude;
  524. } else if (partial_include.starts_with("\"")) {
  525. include_root = filedb().project_root();
  526. } else
  527. return {};
  528. auto last_slash = partial_include.find_last('/');
  529. auto include_dir = String::empty();
  530. auto partial_basename = partial_include.substring_view((last_slash.has_value() ? last_slash.value() : 0) + 1);
  531. if (last_slash.has_value()) {
  532. include_dir = partial_include.substring_view(1, last_slash.value());
  533. }
  534. auto full_dir = String::formatted("{}{}", include_root, include_dir);
  535. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "searching path: {}, partial_basename: {}", full_dir, partial_basename);
  536. Core::DirIterator it(full_dir, Core::DirIterator::Flags::SkipDots);
  537. Vector<GUI::AutocompleteProvider::Entry> options;
  538. while (it.has_next()) {
  539. auto path = it.next_path();
  540. if (!(path.ends_with(".h") || Core::File::is_directory(LexicalPath::join(full_dir, path).string())))
  541. continue;
  542. if (path.starts_with(partial_basename)) {
  543. options.append({ path, partial_basename.length(), include_type, GUI::AutocompleteProvider::Language::Cpp });
  544. }
  545. }
  546. return options;
  547. }
  548. RefPtr<Declaration> CppComprehensionEngine::find_declaration_of(const CppComprehensionEngine::DocumentData& document, const CppComprehensionEngine::SymbolName& target_symbol_name) const
  549. {
  550. RefPtr<Declaration> target_declaration;
  551. for_each_available_symbol(document, [&](const Symbol& symbol) {
  552. if (symbol.name == target_symbol_name) {
  553. target_declaration = symbol.declaration;
  554. return IterationDecision::Break;
  555. }
  556. return IterationDecision::Continue;
  557. });
  558. return target_declaration;
  559. }
  560. String CppComprehensionEngine::SymbolName::scope_as_string() const
  561. {
  562. if (scope.is_empty())
  563. return String::empty();
  564. StringBuilder builder;
  565. for (size_t i = 0; i < scope.size() - 1; ++i) {
  566. builder.appendff("{}::", scope[i]);
  567. }
  568. builder.append(scope.last());
  569. return builder.to_string();
  570. }
  571. CppComprehensionEngine::SymbolName CppComprehensionEngine::SymbolName::create(StringView name, Vector<StringView>&& scope)
  572. {
  573. return { name, move(scope) };
  574. }
  575. CppComprehensionEngine::SymbolName CppComprehensionEngine::SymbolName::create(StringView qualified_name)
  576. {
  577. auto parts = qualified_name.split_view("::");
  578. VERIFY(!parts.is_empty());
  579. auto name = parts.take_last();
  580. return SymbolName::create(name, move(parts));
  581. }
  582. String CppComprehensionEngine::SymbolName::to_string() const
  583. {
  584. if (scope.is_empty())
  585. return name;
  586. return String::formatted("{}::{}", scope_as_string(), name);
  587. }
  588. bool CppComprehensionEngine::is_symbol_available(const Symbol& symbol, const Vector<StringView>& current_scope, const Vector<StringView>& reference_scope)
  589. {
  590. if (!reference_scope.is_empty()) {
  591. return reference_scope == symbol.name.scope;
  592. }
  593. // FIXME: Consider "using namespace ..."
  594. // Check if current_scope starts with symbol's scope
  595. if (symbol.name.scope.size() > current_scope.size())
  596. return false;
  597. for (size_t i = 0; i < symbol.name.scope.size(); ++i) {
  598. if (current_scope[i] != symbol.name.scope[i])
  599. return false;
  600. }
  601. return true;
  602. }
  603. Optional<CodeComprehensionEngine::FunctionParamsHint> CppComprehensionEngine::get_function_params_hint(const String& filename, const GUI::TextPosition& identifier_position)
  604. {
  605. const auto* document_ptr = get_or_create_document_data(filename);
  606. if (!document_ptr)
  607. return {};
  608. const auto& document = *document_ptr;
  609. Cpp::Position cpp_position { identifier_position.line(), identifier_position.column() };
  610. auto node = document.parser().node_at(cpp_position);
  611. if (!node) {
  612. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "no node at position {}:{}", identifier_position.line(), identifier_position.column());
  613. return {};
  614. }
  615. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "node type: {}", node->class_name());
  616. FunctionCall* call_node { nullptr };
  617. if (node->is_function_call()) {
  618. call_node = verify_cast<FunctionCall>(node.ptr());
  619. auto token = document.parser().token_at(cpp_position);
  620. // If we're in a function call with 0 arguments
  621. if (token.has_value() && (token->type() == Token::Type::LeftParen || token->type() == Token::Type::RightParen)) {
  622. return get_function_params_hint(document, *call_node, call_node->arguments().is_empty() ? 0 : call_node->arguments().size() - 1);
  623. }
  624. }
  625. // Walk upwards in the AST to find a FunctionCall node
  626. while (!call_node && node) {
  627. auto parent_is_call = node->parent() && node->parent()->is_function_call();
  628. if (parent_is_call) {
  629. call_node = verify_cast<FunctionCall>(node->parent());
  630. break;
  631. }
  632. node = node->parent();
  633. }
  634. if (!call_node) {
  635. dbgln("did not find function call");
  636. return {};
  637. }
  638. Optional<size_t> invoked_arg_index;
  639. for (size_t arg_index = 0; arg_index < call_node->arguments().size(); ++arg_index) {
  640. if (&call_node->arguments()[arg_index] == node.ptr()) {
  641. invoked_arg_index = arg_index;
  642. break;
  643. }
  644. }
  645. if (!invoked_arg_index.has_value()) {
  646. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "could not find argument index, defaulting to the last argument");
  647. invoked_arg_index = call_node->arguments().is_empty() ? 0 : call_node->arguments().size() - 1;
  648. }
  649. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "arg index: {}", invoked_arg_index.value());
  650. return get_function_params_hint(document, *call_node, invoked_arg_index.value());
  651. }
  652. Optional<CppComprehensionEngine::FunctionParamsHint> CppComprehensionEngine::get_function_params_hint(
  653. DocumentData const& document,
  654. FunctionCall& call_node,
  655. size_t argument_index)
  656. {
  657. const Identifier* callee = nullptr;
  658. VERIFY(call_node.callee());
  659. if (call_node.callee()->is_identifier()) {
  660. callee = verify_cast<Identifier>(call_node.callee());
  661. } else if (call_node.callee()->is_name()) {
  662. callee = verify_cast<Name>(*call_node.callee()).name();
  663. } else if (call_node.callee()->is_member_expression()) {
  664. auto& member_exp = verify_cast<MemberExpression>(*call_node.callee());
  665. VERIFY(member_exp.property());
  666. if (member_exp.property()->is_identifier()) {
  667. callee = verify_cast<Identifier>(member_exp.property());
  668. }
  669. }
  670. if (!callee) {
  671. dbgln("unexpected node type for function call: {}", call_node.callee()->class_name());
  672. return {};
  673. }
  674. VERIFY(callee);
  675. auto decl = find_declaration_of(document, *callee);
  676. if (!decl) {
  677. dbgln("func decl not found");
  678. return {};
  679. }
  680. if (!decl->is_function()) {
  681. dbgln("declaration is not a function");
  682. return {};
  683. }
  684. auto& func_decl = verify_cast<FunctionDeclaration>(*decl);
  685. auto document_of_declaration = get_document_data(func_decl.filename());
  686. FunctionParamsHint hint {};
  687. hint.current_index = argument_index;
  688. for (auto& arg : func_decl.parameters()) {
  689. Vector<StringView> tokens_text;
  690. for (auto token : document_of_declaration->parser().tokens_in_range(arg.start(), arg.end())) {
  691. tokens_text.append(token.text());
  692. }
  693. hint.params.append(String::join(" ", tokens_text));
  694. }
  695. return hint;
  696. }
  697. }