CppComprehensionEngine.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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.m_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.parser().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->m_scope) {
  159. scope_parts.append(scope_part.m_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. auto type = type_of(document, *parent.m_object);
  166. if (type.is_null()) {
  167. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "Could not infer type of object");
  168. return {};
  169. }
  170. Vector<GUI::AutocompleteProvider::Entry> suggestions;
  171. for (auto& prop : properties_of_type(document, type)) {
  172. if (prop.name.name.starts_with(partial_text)) {
  173. suggestions.append({ prop.name.name, partial_text.length(), GUI::AutocompleteProvider::CompletionKind::Identifier });
  174. }
  175. }
  176. return suggestions;
  177. }
  178. bool CppComprehensionEngine::is_property(const ASTNode& node) const
  179. {
  180. if (!node.parent()->is_member_expression())
  181. return false;
  182. auto& parent = (MemberExpression&)(*node.parent());
  183. return parent.m_property.ptr() == &node;
  184. }
  185. String CppComprehensionEngine::type_of_property(const DocumentData& document, const Identifier& identifier) const
  186. {
  187. auto& parent = (const MemberExpression&)(*identifier.parent());
  188. auto properties = properties_of_type(document, type_of(document, *parent.m_object));
  189. for (auto& prop : properties) {
  190. if (prop.name.name != identifier.m_name)
  191. continue;
  192. Type* type { nullptr };
  193. if (prop.declaration->is_variable_declaration()) {
  194. type = ((VariableDeclaration&)*prop.declaration).m_type.ptr();
  195. }
  196. if (!type)
  197. continue;
  198. if (!type->is_named_type())
  199. continue;
  200. return ((NamedType&)*type).m_name->full_name();
  201. }
  202. return {};
  203. }
  204. String CppComprehensionEngine::type_of_variable(const Identifier& identifier) const
  205. {
  206. const ASTNode* current = &identifier;
  207. while (current) {
  208. for (auto& decl : current->declarations()) {
  209. if (decl.is_variable_or_parameter_declaration()) {
  210. auto& var_or_param = (VariableOrParameterDeclaration&)decl;
  211. if (var_or_param.m_name == identifier.m_name && var_or_param.m_type->is_named_type()) {
  212. return ((NamedType&)(*var_or_param.m_type)).m_name->full_name();
  213. }
  214. }
  215. }
  216. current = current->parent();
  217. }
  218. return {};
  219. }
  220. String CppComprehensionEngine::type_of(const DocumentData& document, const Expression& expression) const
  221. {
  222. if (expression.is_member_expression()) {
  223. auto& member_expression = (const MemberExpression&)expression;
  224. if (member_expression.m_property->is_identifier())
  225. return type_of_property(document, static_cast<const Identifier&>(*member_expression.m_property));
  226. return {};
  227. }
  228. const Identifier* identifier { nullptr };
  229. if (expression.is_name()) {
  230. identifier = static_cast<const Name&>(expression).m_name.ptr();
  231. } else if (expression.is_identifier()) {
  232. identifier = &static_cast<const Identifier&>(expression);
  233. } else {
  234. dbgln("expected identifier or name, got: {}", expression.class_name());
  235. VERIFY_NOT_REACHED(); // TODO
  236. }
  237. VERIFY(identifier);
  238. if (is_property(*identifier))
  239. return type_of_property(document, *identifier);
  240. return type_of_variable(*identifier);
  241. }
  242. Vector<CppComprehensionEngine::Symbol> CppComprehensionEngine::properties_of_type(const DocumentData& document, const String& type) const
  243. {
  244. auto type_symbol = SymbolName::create(type);
  245. auto decl = find_declaration_of(document, type_symbol);
  246. if (!decl) {
  247. dbgln("Couldn't find declaration of type: {}", type);
  248. return {};
  249. }
  250. if (!decl->is_struct_or_class()) {
  251. dbgln("Expected declaration of type: {} to be struct or class", type);
  252. return {};
  253. }
  254. auto& struct_or_class = (StructOrClassDeclaration&)*decl;
  255. VERIFY(struct_or_class.name() == type_symbol.name);
  256. Vector<Symbol> properties;
  257. for (auto& member : struct_or_class.m_members) {
  258. Vector<StringView> scope(type_symbol.scope);
  259. scope.append(type_symbol.name);
  260. // 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.
  261. properties.append(Symbol::create(member.m_name, scope, member, Symbol::IsLocal::No));
  262. }
  263. return properties;
  264. }
  265. CppComprehensionEngine::Symbol CppComprehensionEngine::Symbol::create(StringView name, const Vector<StringView>& scope, NonnullRefPtr<Declaration> declaration, IsLocal is_local)
  266. {
  267. return { { name, scope }, move(declaration), is_local == IsLocal::Yes };
  268. }
  269. Vector<CppComprehensionEngine::Symbol> CppComprehensionEngine::get_child_symbols(const ASTNode& node) const
  270. {
  271. return get_child_symbols(node, {}, Symbol::IsLocal::No);
  272. }
  273. Vector<CppComprehensionEngine::Symbol> CppComprehensionEngine::get_child_symbols(const ASTNode& node, const Vector<StringView>& scope, Symbol::IsLocal is_local) const
  274. {
  275. Vector<Symbol> symbols;
  276. for (auto& decl : node.declarations()) {
  277. symbols.append(Symbol::create(decl.name(), scope, decl, is_local));
  278. bool should_recurse = decl.is_namespace() || decl.is_struct_or_class() || decl.is_function();
  279. bool are_child_symbols_local = decl.is_function();
  280. if (!should_recurse)
  281. continue;
  282. auto new_scope = scope;
  283. new_scope.append(decl.name());
  284. symbols.extend(get_child_symbols(decl, new_scope, are_child_symbols_local ? Symbol::IsLocal::Yes : is_local));
  285. }
  286. return symbols;
  287. }
  288. String CppComprehensionEngine::document_path_from_include_path(const StringView& include_path) const
  289. {
  290. static Regex<PosixExtended> library_include("<(.+)>");
  291. static Regex<PosixExtended> user_defined_include("\"(.+)\"");
  292. auto document_path_for_library_include = [&](const StringView& include_path) -> String {
  293. RegexResult result;
  294. if (!library_include.search(include_path, result))
  295. return {};
  296. auto path = result.capture_group_matches.at(0).at(0).view.u8view();
  297. return String::formatted("/usr/include/{}", path);
  298. };
  299. auto document_path_for_user_defined_include = [&](const StringView& include_path) -> String {
  300. RegexResult result;
  301. if (!user_defined_include.search(include_path, result))
  302. return {};
  303. return result.capture_group_matches.at(0).at(0).view.u8view();
  304. };
  305. auto result = document_path_for_library_include(include_path);
  306. if (result.is_null())
  307. result = document_path_for_user_defined_include(include_path);
  308. return result;
  309. }
  310. void CppComprehensionEngine::on_edit(const String& file)
  311. {
  312. set_document_data(file, create_document_data_for(file));
  313. }
  314. void CppComprehensionEngine::file_opened([[maybe_unused]] const String& file)
  315. {
  316. get_or_create_document_data(file);
  317. }
  318. Optional<GUI::AutocompleteProvider::ProjectLocation> CppComprehensionEngine::find_declaration_of(const String& filename, const GUI::TextPosition& identifier_position)
  319. {
  320. const auto* document_ptr = get_or_create_document_data(filename);
  321. if (!document_ptr)
  322. return {};
  323. const auto& document = *document_ptr;
  324. auto node = document.parser().node_at(Cpp::Position { identifier_position.line(), identifier_position.column() });
  325. if (!node) {
  326. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "no node at position {}:{}", identifier_position.line(), identifier_position.column());
  327. return {};
  328. }
  329. auto decl = find_declaration_of(document, *node);
  330. if (decl)
  331. return GUI::AutocompleteProvider::ProjectLocation { decl->filename(), decl->start().line, decl->start().column };
  332. return find_preprocessor_definition(document, identifier_position);
  333. }
  334. Optional<GUI::AutocompleteProvider::ProjectLocation> CppComprehensionEngine::find_preprocessor_definition(const DocumentData& document, const GUI::TextPosition& text_position)
  335. {
  336. Position cpp_position { text_position.line(), text_position.column() };
  337. // Search for a replaced preprocessor token that intersects with text_position
  338. for (auto& replaced_token : document.parser().replaced_preprocessor_tokens()) {
  339. if (replaced_token.token.start() > cpp_position)
  340. continue;
  341. if (replaced_token.token.end() < cpp_position)
  342. continue;
  343. return GUI::AutocompleteProvider::ProjectLocation { replaced_token.preprocessor_value.filename, replaced_token.preprocessor_value.line, replaced_token.preprocessor_value.column };
  344. }
  345. return {};
  346. }
  347. struct TargetDeclaration {
  348. enum Type {
  349. Variable,
  350. Type,
  351. Function,
  352. Property
  353. } type;
  354. String name;
  355. };
  356. static Optional<TargetDeclaration> get_target_declaration(const ASTNode& node)
  357. {
  358. if (!node.is_identifier()) {
  359. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "node is not an identifier");
  360. return {};
  361. }
  362. String name = static_cast<const Identifier&>(node).m_name;
  363. if ((node.parent() && node.parent()->is_function_call()) || (node.parent()->is_name() && node.parent()->parent() && node.parent()->parent()->is_function_call())) {
  364. return TargetDeclaration { TargetDeclaration::Type::Function, name };
  365. }
  366. if ((node.parent() && node.parent()->is_type()) || (node.parent()->is_name() && node.parent()->parent() && node.parent()->parent()->is_type()))
  367. return TargetDeclaration { TargetDeclaration::Type::Type, name };
  368. if ((node.parent() && node.parent()->is_member_expression()))
  369. return TargetDeclaration { TargetDeclaration::Type::Property, name };
  370. return TargetDeclaration { TargetDeclaration::Type::Variable, name };
  371. }
  372. RefPtr<Declaration> CppComprehensionEngine::find_declaration_of(const DocumentData& document_data, const ASTNode& node) const
  373. {
  374. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "find_declaration_of: {} ({})", document_data.parser().text_of_node(node), node.class_name());
  375. if (!node.is_identifier()) {
  376. dbgln("node is not an identifier, can't find declaration");
  377. return {};
  378. }
  379. auto target_decl = get_target_declaration(node);
  380. if (!target_decl.has_value())
  381. return {};
  382. auto reference_scope = scope_of_reference_to_symbol(node);
  383. auto current_scope = scope_of_node(node);
  384. auto symbol_matches = [&](const Symbol& symbol) {
  385. bool match_function = target_decl.value().type == TargetDeclaration::Function && symbol.declaration->is_function();
  386. bool match_variable = target_decl.value().type == TargetDeclaration::Variable && symbol.declaration->is_variable_declaration();
  387. bool match_type = target_decl.value().type == TargetDeclaration::Type && symbol.declaration->is_struct_or_class();
  388. bool match_property = target_decl.value().type == TargetDeclaration::Property && symbol.declaration->parent()->is_declaration() && ((Declaration*)symbol.declaration->parent())->is_struct_or_class();
  389. bool match_parameter = target_decl.value().type == TargetDeclaration::Variable && symbol.declaration->is_parameter();
  390. if (match_property) {
  391. // FIXME: This is not really correct, we also need to check that the type of the struct/class matches (not just the property name)
  392. if (symbol.name.name == target_decl.value().name) {
  393. return true;
  394. }
  395. }
  396. if (!is_symbol_available(symbol, current_scope, reference_scope)) {
  397. return false;
  398. }
  399. if (match_function || match_type) {
  400. if (symbol.name.name == target_decl->name)
  401. return true;
  402. }
  403. if (match_variable || match_parameter) {
  404. // If this symbol was declared bellow us in a function, it's not available to us.
  405. bool is_unavailable = symbol.is_local && symbol.declaration->start().line > node.start().line;
  406. if (!is_unavailable && (symbol.name.name == target_decl->name)) {
  407. return true;
  408. }
  409. }
  410. return false;
  411. };
  412. Optional<Symbol> match;
  413. for_each_available_symbol(document_data, [&](const Symbol& symbol) {
  414. if (symbol_matches(symbol)) {
  415. match = symbol;
  416. return IterationDecision::Break;
  417. }
  418. return IterationDecision::Continue;
  419. });
  420. if (!match.has_value())
  421. return {};
  422. return match->declaration;
  423. }
  424. void CppComprehensionEngine::update_declared_symbols(DocumentData& document)
  425. {
  426. for (auto& symbol : get_child_symbols(*document.parser().root_node())) {
  427. document.m_symbols.set(symbol.name, move(symbol));
  428. }
  429. Vector<GUI::AutocompleteProvider::Declaration> declarations;
  430. for (auto& symbol_entry : document.m_symbols) {
  431. auto& symbol = symbol_entry.value;
  432. 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() });
  433. }
  434. for (auto& definition : document.preprocessor().definitions()) {
  435. declarations.append({ definition.key, { document.filename(), definition.value.line, definition.value.column }, GUI::AutocompleteProvider::DeclarationType::PreprocessorDefinition, {} });
  436. }
  437. set_declarations_of_document(document.filename(), move(declarations));
  438. }
  439. void CppComprehensionEngine::update_todo_entries(DocumentData& document)
  440. {
  441. set_todo_entries_of_document(document.filename(), document.parser().get_todo_entries());
  442. }
  443. GUI::AutocompleteProvider::DeclarationType CppComprehensionEngine::type_of_declaration(const Declaration& decl)
  444. {
  445. if (decl.is_struct())
  446. return GUI::AutocompleteProvider::DeclarationType::Struct;
  447. if (decl.is_class())
  448. return GUI::AutocompleteProvider::DeclarationType::Class;
  449. if (decl.is_function())
  450. return GUI::AutocompleteProvider::DeclarationType::Function;
  451. if (decl.is_variable_declaration())
  452. return GUI::AutocompleteProvider::DeclarationType::Variable;
  453. if (decl.is_namespace())
  454. return GUI::AutocompleteProvider::DeclarationType::Namespace;
  455. if (decl.is_member())
  456. return GUI::AutocompleteProvider::DeclarationType::Member;
  457. return GUI::AutocompleteProvider::DeclarationType::Variable;
  458. }
  459. OwnPtr<CppComprehensionEngine::DocumentData> CppComprehensionEngine::create_document_data(String&& text, const String& filename)
  460. {
  461. auto document_data = make<DocumentData>();
  462. document_data->m_filename = filename;
  463. document_data->m_text = move(text);
  464. document_data->m_preprocessor = make<Preprocessor>(document_data->m_filename, document_data->text());
  465. document_data->preprocessor().set_ignore_unsupported_keywords(true);
  466. document_data->preprocessor().set_keep_include_statements(true);
  467. document_data->preprocessor().process();
  468. Preprocessor::Definitions preprocessor_definitions;
  469. for (auto item : document_data->preprocessor().definitions())
  470. preprocessor_definitions.set(move(item.key), move(item.value));
  471. for (auto include_path : document_data->preprocessor().included_paths()) {
  472. auto include_fullpath = document_path_from_include_path(include_path);
  473. auto included_document = get_or_create_document_data(include_fullpath);
  474. if (!included_document)
  475. continue;
  476. document_data->m_available_headers.set(include_fullpath);
  477. for (auto& header : included_document->m_available_headers)
  478. document_data->m_available_headers.set(header);
  479. for (auto item : included_document->parser().preprocessor_definitions())
  480. preprocessor_definitions.set(move(item.key), move(item.value));
  481. }
  482. document_data->m_parser = make<Parser>(document_data->preprocessor().processed_text(), filename, move(preprocessor_definitions));
  483. auto root = document_data->parser().parse();
  484. if constexpr (CPP_LANGUAGE_SERVER_DEBUG)
  485. root->dump();
  486. update_declared_symbols(*document_data);
  487. update_todo_entries(*document_data);
  488. return document_data;
  489. }
  490. Vector<StringView> CppComprehensionEngine::scope_of_node(const ASTNode& node) const
  491. {
  492. auto parent = node.parent();
  493. if (!parent)
  494. return {};
  495. auto parent_scope = scope_of_node(*parent);
  496. if (!parent->is_declaration())
  497. return parent_scope;
  498. auto& parent_decl = static_cast<Declaration&>(*parent);
  499. StringView containing_scope;
  500. if (parent_decl.is_namespace())
  501. containing_scope = static_cast<NamespaceDeclaration&>(parent_decl).m_name;
  502. if (parent_decl.is_struct_or_class())
  503. containing_scope = static_cast<StructOrClassDeclaration&>(parent_decl).name();
  504. if (parent_decl.is_function())
  505. containing_scope = static_cast<FunctionDeclaration&>(parent_decl).name();
  506. parent_scope.append(containing_scope);
  507. return parent_scope;
  508. }
  509. Optional<Vector<GUI::AutocompleteProvider::Entry>> CppComprehensionEngine::try_autocomplete_include(const DocumentData&, Token include_path_token)
  510. {
  511. VERIFY(include_path_token.type() == Token::Type::IncludePath);
  512. auto partial_include = include_path_token.text().trim_whitespace();
  513. String include_root;
  514. auto include_type = GUI::AutocompleteProvider::CompletionKind::ProjectInclude;
  515. if (partial_include.starts_with("<")) {
  516. include_root = "/usr/include/";
  517. include_type = GUI::AutocompleteProvider::CompletionKind::SystemInclude;
  518. } else if (partial_include.starts_with("\"")) {
  519. include_root = filedb().project_root();
  520. } else
  521. return {};
  522. auto last_slash = partial_include.find_last('/');
  523. auto include_dir = String::empty();
  524. auto partial_basename = partial_include.substring_view((last_slash.has_value() ? last_slash.value() : 0) + 1);
  525. if (last_slash.has_value()) {
  526. include_dir = partial_include.substring_view(1, last_slash.value());
  527. }
  528. auto full_dir = String::formatted("{}{}", include_root, include_dir);
  529. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "searching path: {}, partial_basename: {}", full_dir, partial_basename);
  530. Core::DirIterator it(full_dir, Core::DirIterator::Flags::SkipDots);
  531. Vector<GUI::AutocompleteProvider::Entry> options;
  532. while (it.has_next()) {
  533. auto path = it.next_path();
  534. if (!(path.ends_with(".h") || Core::File::is_directory(LexicalPath::join(full_dir, path).string())))
  535. continue;
  536. if (path.starts_with(partial_basename)) {
  537. options.append({ path, partial_basename.length(), include_type, GUI::AutocompleteProvider::Language::Cpp });
  538. }
  539. }
  540. return options;
  541. }
  542. RefPtr<Declaration> CppComprehensionEngine::find_declaration_of(const CppComprehensionEngine::DocumentData& document, const CppComprehensionEngine::SymbolName& target_symbol_name) const
  543. {
  544. RefPtr<Declaration> target_declaration;
  545. for_each_available_symbol(document, [&](const Symbol& symbol) {
  546. if (symbol.name == target_symbol_name) {
  547. target_declaration = symbol.declaration;
  548. return IterationDecision::Break;
  549. }
  550. return IterationDecision::Continue;
  551. });
  552. return target_declaration;
  553. }
  554. String CppComprehensionEngine::SymbolName::scope_as_string() const
  555. {
  556. if (scope.is_empty())
  557. return String::empty();
  558. StringBuilder builder;
  559. for (size_t i = 0; i < scope.size() - 1; ++i) {
  560. builder.appendff("{}::", scope[i]);
  561. }
  562. builder.append(scope.last());
  563. return builder.to_string();
  564. }
  565. CppComprehensionEngine::SymbolName CppComprehensionEngine::SymbolName::create(StringView name, Vector<StringView>&& scope)
  566. {
  567. return { name, move(scope) };
  568. }
  569. CppComprehensionEngine::SymbolName CppComprehensionEngine::SymbolName::create(StringView qualified_name)
  570. {
  571. auto parts = qualified_name.split_view("::");
  572. VERIFY(!parts.is_empty());
  573. auto name = parts.take_last();
  574. return SymbolName::create(name, move(parts));
  575. }
  576. String CppComprehensionEngine::SymbolName::to_string() const
  577. {
  578. if (scope.is_empty())
  579. return name;
  580. return String::formatted("{}::{}", scope_as_string(), name);
  581. }
  582. bool CppComprehensionEngine::is_symbol_available(const Symbol& symbol, const Vector<StringView>& current_scope, const Vector<StringView>& reference_scope)
  583. {
  584. if (!reference_scope.is_empty()) {
  585. return reference_scope == symbol.name.scope;
  586. }
  587. // FIXME: Consider "using namespace ..."
  588. // Check if current_scope starts with symbol's scope
  589. if (symbol.name.scope.size() > current_scope.size())
  590. return false;
  591. for (size_t i = 0; i < symbol.name.scope.size(); ++i) {
  592. if (current_scope[i] != symbol.name.scope[i])
  593. return false;
  594. }
  595. return true;
  596. }
  597. Optional<CodeComprehensionEngine::FunctionParamsHint> CppComprehensionEngine::get_function_params_hint(const String& filename, const GUI::TextPosition& identifier_position)
  598. {
  599. const auto* document_ptr = get_or_create_document_data(filename);
  600. if (!document_ptr)
  601. return {};
  602. const auto& document = *document_ptr;
  603. Cpp::Position cpp_position { identifier_position.line(), identifier_position.column() };
  604. auto node = document.parser().node_at(cpp_position);
  605. if (!node) {
  606. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "no node at position {}:{}", identifier_position.line(), identifier_position.column());
  607. return {};
  608. }
  609. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "node type: {}", node->class_name());
  610. FunctionCall* call_node { nullptr };
  611. if (node->is_function_call()) {
  612. call_node = ((FunctionCall*)node.ptr());
  613. auto token = document.parser().token_at(cpp_position);
  614. // If we're in a function call with 0 arguments
  615. if (token.has_value() && (token->type() == Token::Type::LeftParen || token->type() == Token::Type::RightParen)) {
  616. return get_function_params_hint(document, *call_node, call_node->m_arguments.is_empty() ? 0 : call_node->m_arguments.size() - 1);
  617. }
  618. }
  619. // Walk upwards in the AST to find a FunctionCall node
  620. while (!call_node && node) {
  621. auto parent_is_call = node->parent() && node->parent()->is_function_call();
  622. if (parent_is_call) {
  623. call_node = (FunctionCall*)node->parent();
  624. break;
  625. }
  626. node = node->parent();
  627. }
  628. if (!call_node) {
  629. dbgln("did not find function call");
  630. return {};
  631. }
  632. Optional<size_t> invoked_arg_index;
  633. for (size_t arg_index = 0; arg_index < call_node->m_arguments.size(); ++arg_index) {
  634. if (&call_node->m_arguments[arg_index] == node.ptr()) {
  635. invoked_arg_index = arg_index;
  636. break;
  637. }
  638. }
  639. if (!invoked_arg_index.has_value()) {
  640. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "could not find argument index, defaulting to the last argument");
  641. invoked_arg_index = call_node->m_arguments.is_empty() ? 0 : call_node->m_arguments.size() - 1;
  642. }
  643. dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "arg index: {}", invoked_arg_index.value());
  644. return get_function_params_hint(document, *call_node, invoked_arg_index.value());
  645. }
  646. Optional<CppComprehensionEngine::FunctionParamsHint> CppComprehensionEngine::get_function_params_hint(
  647. DocumentData const& document,
  648. FunctionCall& call_node,
  649. size_t argument_index)
  650. {
  651. Identifier* callee = nullptr;
  652. if (call_node.m_callee->is_identifier()) {
  653. callee = (Identifier*)call_node.m_callee.ptr();
  654. } else if (call_node.m_callee->is_name()) {
  655. callee = ((Name&)*call_node.m_callee).m_name.ptr();
  656. } else if (call_node.m_callee->is_member_expression()) {
  657. auto& member_exp = ((MemberExpression&)*call_node.m_callee);
  658. if (member_exp.m_property->is_identifier()) {
  659. callee = (Identifier*)member_exp.m_property.ptr();
  660. }
  661. }
  662. if (!callee) {
  663. dbgln("unexpected node type for function call: {}", call_node.m_callee->class_name());
  664. return {};
  665. }
  666. VERIFY(callee);
  667. auto decl = find_declaration_of(document, *callee);
  668. if (!decl) {
  669. dbgln("func decl not found");
  670. return {};
  671. }
  672. if (!decl->is_function()) {
  673. dbgln("declaration is not a function");
  674. return {};
  675. }
  676. auto& func_decl = (FunctionDeclaration&)*decl;
  677. auto document_of_declaration = get_document_data(func_decl.filename());
  678. FunctionParamsHint hint {};
  679. hint.current_index = argument_index;
  680. for (auto& arg : func_decl.m_parameters) {
  681. Vector<StringView> tokens_text;
  682. for (auto token : document_of_declaration->parser().tokens_in_range(arg.start(), arg.end())) {
  683. tokens_text.append(token.text());
  684. }
  685. hint.params.append(String::join(" ", tokens_text));
  686. }
  687. return hint;
  688. }
  689. }