From 0c4dc00f01aa283e08bb04a6e10f9bc6627bdb25 Mon Sep 17 00:00:00 2001 From: Itamar Date: Fri, 6 Aug 2021 11:15:57 +0300 Subject: LibCpp: Import definitions from headers while processing When the preprocessor encounters an #include statement it now adds the preprocessor definitions that exist in the included header to its own set of definitions. We previously only aggregated the definitions from headers after processing the source, which was less correct. (For example, there could be an #ifdef that depends on a definition from another header). --- .../LanguageServers/Cpp/CppComprehensionEngine.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'Userland/DevTools/HackStudio') diff --git a/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.cpp b/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.cpp index fd07b4f370..84c776faff 100644 --- a/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.cpp +++ b/Userland/DevTools/HackStudio/LanguageServers/Cpp/CppComprehensionEngine.cpp @@ -569,11 +569,16 @@ OwnPtr CppComprehensionEngine::create_docu document_data->m_preprocessor = make(document_data->m_filename, document_data->text()); document_data->preprocessor().set_ignore_unsupported_keywords(true); document_data->preprocessor().set_keep_include_statements(true); - auto tokens = document_data->preprocessor().process_and_lex(); - Preprocessor::Definitions preprocessor_definitions; - for (auto item : document_data->preprocessor().definitions()) - preprocessor_definitions.set(move(item.key), move(item.value)); + document_data->preprocessor().definitions_in_header_callback = [this](StringView include_path) -> Preprocessor::Definitions { + auto included_document = get_or_create_document_data(document_path_from_include_path(include_path)); + if (!included_document) + return {}; + + return included_document->preprocessor().definitions(); + }; + + auto tokens = document_data->preprocessor().process_and_lex(); for (auto include_path : document_data->preprocessor().included_paths()) { auto include_fullpath = document_path_from_include_path(include_path); @@ -585,12 +590,9 @@ OwnPtr CppComprehensionEngine::create_docu for (auto& header : included_document->m_available_headers) document_data->m_available_headers.set(header); - - for (auto item : included_document->parser().preprocessor_definitions()) - preprocessor_definitions.set(move(item.key), move(item.value)); } - document_data->m_parser = make(move(tokens), filename, move(preprocessor_definitions)); + document_data->m_parser = make(move(tokens), filename, document_data->preprocessor().definitions()); auto root = document_data->parser().parse(); -- cgit v1.2.3