1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 14:47:46 +00:00

LanguageServers/Cpp: Support jumping to declaration of preprocessor

.. definitions.
This commit is contained in:
Itamar 2021-03-13 10:37:23 +02:00 committed by Andreas Kling
parent 7bf6eca9d8
commit 8688259ed9
8 changed files with 43 additions and 10 deletions

View file

@ -54,6 +54,7 @@ Parser::Parser(const StringView& program, const String& filename, Preprocessor::
}
#endif
}
void Parser::initialize_program_tokens(const StringView& program)
{
Lexer lexer(program);
@ -63,6 +64,7 @@ void Parser::initialize_program_tokens(const StringView& program)
if (token.type() == Token::Type::Identifier) {
if (auto defined_value = m_definitions.find(text_of_token(token)); defined_value != m_definitions.end()) {
add_tokens_for_preprocessor(token, defined_value->value);
m_replaced_preprocessor_tokens.append({ token, defined_value->value });
continue;
}
}

View file

@ -56,6 +56,12 @@ public:
Vector<String> errors() const { return m_errors; }
const Preprocessor::Definitions& definitions() const { return m_definitions; }
struct TokenAndPreprocessorDefinition {
Token token;
Preprocessor::DefinedValue preprocessor_value;
};
const Vector<TokenAndPreprocessorDefinition>& replaced_preprocessor_tokens() const { return m_replaced_preprocessor_tokens; }
private:
enum class DeclarationType {
Function,
@ -171,6 +177,8 @@ private:
RefPtr<TranslationUnit> m_root_node;
NonnullRefPtrVector<ASTNode> m_nodes;
Vector<String> m_errors;
Vector<TokenAndPreprocessorDefinition> m_replaced_preprocessor_tokens;
};
}

View file

@ -31,8 +31,9 @@
#include <ctype.h>
namespace Cpp {
Preprocessor::Preprocessor(const StringView& program)
: m_program(program)
Preprocessor::Preprocessor(const String& filename, const StringView& program)
: m_filename(filename)
, m_program(program)
{
m_lines = m_program.split_view('\n', true);
}
@ -107,6 +108,7 @@ void Preprocessor::handle_preprocessor_line(const StringView& line)
consume_whitespace();
DefinedValue value;
value.filename = m_filename;
value.line = m_line_index;
auto string_value = lexer.consume_all();

View file

@ -26,6 +26,7 @@
#pragma once
#include <AK/FlyString.h>
#include <AK/HashMap.h>
#include <AK/Optional.h>
#include <AK/String.h>
@ -36,15 +37,16 @@ namespace Cpp {
class Preprocessor {
public:
explicit Preprocessor(const StringView&);
explicit Preprocessor(const String& filename, const StringView& program);
const String& process();
const String& processed_text();
Vector<StringView> included_paths() const { return m_included_paths; }
struct DefinedValue {
Optional<StringView> value;
size_t line {0};
size_t column {0};
FlyString filename;
size_t line { 0 };
size_t column { 0 };
};
using Definitions = HashMap<StringView, DefinedValue>;
@ -54,6 +56,7 @@ private:
void handle_preprocessor_line(const StringView&);
Definitions m_definitions;
const String m_filename;
const StringView m_program;
StringBuilder m_builder;
Vector<StringView> m_lines;