mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-09 00:50:47 +00:00
LibCpp: Convert ScopeLogger to use AK:SourceLocation
Utilize AK::SourceLocation to get function information into the scope logger, instead of relying on pre-processor macros.
This commit is contained in:
parent
357a13b404
commit
7a73f11005
|
@ -6,21 +6,21 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/SourceLocation.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
|
|
||||||
#ifdef DEBUG_SPAM
|
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
class ScopeLogger {
|
class ScopeLogger {
|
||||||
public:
|
public:
|
||||||
ScopeLogger(StringView&& fun)
|
#ifdef DEBUG_SPAM
|
||||||
: m_fun(fun)
|
ScopeLogger(const SourceLocation& location = SourceLocation::current())
|
||||||
|
: m_location(location)
|
||||||
{
|
{
|
||||||
StringBuilder sb;
|
StringBuilder sb;
|
||||||
|
|
||||||
for (auto indent = m_depth++; indent > 0; indent--)
|
for (auto indent = m_depth++; indent > 0; indent--)
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
dbgln("\033[1;{}m{}entering {}\033[0m", m_depth % 8 + 30, sb.to_string(), m_fun);
|
dbgln("\033[1;{}m{}entering {}\033[0m", m_depth % 8 + 30, sb.to_string(), m_location);
|
||||||
}
|
}
|
||||||
~ScopeLogger()
|
~ScopeLogger()
|
||||||
{
|
{
|
||||||
|
@ -28,18 +28,16 @@ public:
|
||||||
|
|
||||||
for (auto indent = --m_depth; indent > 0; indent--)
|
for (auto indent = --m_depth; indent > 0; indent--)
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
dbgln("\033[1;{}m{}leaving {}\033[0m", (m_depth + 1) % 8 + 30, sb.to_string(), m_fun);
|
dbgln("\033[1;{}m{}leaving {}\033[0m", (m_depth + 1) % 8 + 30, sb.to_string(), m_location);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static inline size_t m_depth = 0;
|
static inline size_t m_depth = 0;
|
||||||
StringView m_fun;
|
SourceLocation m_location;
|
||||||
|
#else
|
||||||
|
ScopeLogger() = default;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
using AK::ScopeLogger;
|
using AK::ScopeLogger;
|
||||||
# define SCOPE_LOGGER() auto tmp##__COUNTER__ = ScopeLogger(__PRETTY_FUNCTION__);
|
|
||||||
|
|
||||||
#else
|
|
||||||
# define SCOPE_LOGGER()
|
|
||||||
#endif
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ void Parser::initialize_program_tokens(const StringView& program)
|
||||||
|
|
||||||
NonnullRefPtr<TranslationUnit> Parser::parse()
|
NonnullRefPtr<TranslationUnit> Parser::parse()
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
if (m_tokens.is_empty())
|
if (m_tokens.is_empty())
|
||||||
return create_root_ast_node({}, {});
|
return create_root_ast_node({}, {});
|
||||||
auto unit = create_root_ast_node(m_tokens.first().start(), m_tokens.last().end());
|
auto unit = create_root_ast_node(m_tokens.first().start(), m_tokens.last().end());
|
||||||
|
@ -156,7 +156,7 @@ NonnullRefPtr<FunctionDeclaration> Parser::parse_function_declaration(ASTNode& p
|
||||||
|
|
||||||
NonnullRefPtr<FunctionDefinition> Parser::parse_function_definition(ASTNode& parent)
|
NonnullRefPtr<FunctionDefinition> Parser::parse_function_definition(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto func = create_ast_node<FunctionDefinition>(parent, position(), {});
|
auto func = create_ast_node<FunctionDefinition>(parent, position(), {});
|
||||||
consume(Token::Type::LeftCurly);
|
consume(Token::Type::LeftCurly);
|
||||||
while (!eof() && peek().type() != Token::Type::RightCurly) {
|
while (!eof() && peek().type() != Token::Type::RightCurly) {
|
||||||
|
@ -170,7 +170,7 @@ NonnullRefPtr<FunctionDefinition> Parser::parse_function_definition(ASTNode& par
|
||||||
|
|
||||||
NonnullRefPtr<Statement> Parser::parse_statement(ASTNode& parent)
|
NonnullRefPtr<Statement> Parser::parse_statement(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
ArmedScopeGuard consume_semicolon([this]() {
|
ArmedScopeGuard consume_semicolon([this]() {
|
||||||
consume(Token::Type::Semicolon);
|
consume(Token::Type::Semicolon);
|
||||||
});
|
});
|
||||||
|
@ -222,7 +222,7 @@ bool Parser::match_block_statement()
|
||||||
|
|
||||||
NonnullRefPtr<BlockStatement> Parser::parse_block_statement(ASTNode& parent)
|
NonnullRefPtr<BlockStatement> Parser::parse_block_statement(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto block_statement = create_ast_node<BlockStatement>(parent, position(), {});
|
auto block_statement = create_ast_node<BlockStatement>(parent, position(), {});
|
||||||
consume(Token::Type::LeftCurly);
|
consume(Token::Type::LeftCurly);
|
||||||
while (!eof() && peek().type() != Token::Type::RightCurly) {
|
while (!eof() && peek().type() != Token::Type::RightCurly) {
|
||||||
|
@ -269,7 +269,7 @@ bool Parser::match_template_arguments()
|
||||||
|
|
||||||
NonnullRefPtrVector<Type> Parser::parse_template_arguments(ASTNode& parent)
|
NonnullRefPtrVector<Type> Parser::parse_template_arguments(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
|
|
||||||
consume(Token::Type::Less);
|
consume(Token::Type::Less);
|
||||||
|
|
||||||
|
@ -285,7 +285,7 @@ NonnullRefPtrVector<Type> Parser::parse_template_arguments(ASTNode& parent)
|
||||||
|
|
||||||
bool Parser::match_variable_declaration()
|
bool Parser::match_variable_declaration()
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
save_state();
|
save_state();
|
||||||
ScopeGuard state_guard = [this] { load_state(); };
|
ScopeGuard state_guard = [this] { load_state(); };
|
||||||
|
|
||||||
|
@ -316,7 +316,7 @@ bool Parser::match_variable_declaration()
|
||||||
|
|
||||||
NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(ASTNode& parent, bool expect_semicolon)
|
NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(ASTNode& parent, bool expect_semicolon)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto var = create_ast_node<VariableDeclaration>(parent, position(), {});
|
auto var = create_ast_node<VariableDeclaration>(parent, position(), {});
|
||||||
if (!match_variable_declaration()) {
|
if (!match_variable_declaration()) {
|
||||||
error("unexpected token for variable type");
|
error("unexpected token for variable type");
|
||||||
|
@ -344,7 +344,7 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(ASTNode& p
|
||||||
|
|
||||||
NonnullRefPtr<Expression> Parser::parse_expression(ASTNode& parent)
|
NonnullRefPtr<Expression> Parser::parse_expression(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto expression = parse_primary_expression(parent);
|
auto expression = parse_primary_expression(parent);
|
||||||
// TODO: remove eof() logic, should still work without it
|
// TODO: remove eof() logic, should still work without it
|
||||||
if (eof() || match(Token::Type::Semicolon)) {
|
if (eof() || match(Token::Type::Semicolon)) {
|
||||||
|
@ -406,7 +406,7 @@ bool Parser::match_secondary_expression()
|
||||||
|
|
||||||
NonnullRefPtr<Expression> Parser::parse_primary_expression(ASTNode& parent)
|
NonnullRefPtr<Expression> Parser::parse_primary_expression(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
// TODO: remove eof() logic, should still work without it
|
// TODO: remove eof() logic, should still work without it
|
||||||
if (eof()) {
|
if (eof()) {
|
||||||
auto node = create_ast_node<Identifier>(parent, position(), position());
|
auto node = create_ast_node<Identifier>(parent, position(), position());
|
||||||
|
@ -536,7 +536,7 @@ NonnullRefPtr<Expression> Parser::parse_literal(ASTNode& parent)
|
||||||
|
|
||||||
NonnullRefPtr<Expression> Parser::parse_secondary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs)
|
NonnullRefPtr<Expression> Parser::parse_secondary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
switch (peek().type()) {
|
switch (peek().type()) {
|
||||||
case Token::Type::Plus:
|
case Token::Type::Plus:
|
||||||
return parse_binary_expression(parent, lhs, BinaryOp::Addition);
|
return parse_binary_expression(parent, lhs, BinaryOp::Addition);
|
||||||
|
@ -689,7 +689,7 @@ bool Parser::match_function_declaration()
|
||||||
|
|
||||||
Optional<NonnullRefPtrVector<Parameter>> Parser::parse_parameter_list(ASTNode& parent)
|
Optional<NonnullRefPtrVector<Parameter>> Parser::parse_parameter_list(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
NonnullRefPtrVector<Parameter> parameters;
|
NonnullRefPtrVector<Parameter> parameters;
|
||||||
while (peek().type() != Token::Type::RightParen && !eof()) {
|
while (peek().type() != Token::Type::RightParen && !eof()) {
|
||||||
if (match_ellipsis()) {
|
if (match_ellipsis()) {
|
||||||
|
@ -739,7 +739,7 @@ bool Parser::match_preprocessor()
|
||||||
|
|
||||||
void Parser::consume_preprocessor()
|
void Parser::consume_preprocessor()
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
switch (peek().type()) {
|
switch (peek().type()) {
|
||||||
case Token::Type::PreprocessorStatement:
|
case Token::Type::PreprocessorStatement:
|
||||||
consume();
|
consume();
|
||||||
|
@ -756,7 +756,7 @@ void Parser::consume_preprocessor()
|
||||||
|
|
||||||
Optional<Token> Parser::consume_whitespace()
|
Optional<Token> Parser::consume_whitespace()
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
return consume(Token::Type::Whitespace);
|
return consume(Token::Type::Whitespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -832,7 +832,7 @@ String Parser::text_in_range(Position start, Position end) const
|
||||||
|
|
||||||
void Parser::error(StringView message)
|
void Parser::error(StringView message)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
if (message.is_null() || message.is_empty())
|
if (message.is_null() || message.is_empty())
|
||||||
message = "<empty>";
|
message = "<empty>";
|
||||||
String formatted_message;
|
String formatted_message;
|
||||||
|
@ -937,7 +937,7 @@ void Parser::print_tokens() const
|
||||||
|
|
||||||
NonnullRefPtr<StringLiteral> Parser::parse_string_literal(ASTNode& parent)
|
NonnullRefPtr<StringLiteral> Parser::parse_string_literal(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
Optional<size_t> start_token_index;
|
Optional<size_t> start_token_index;
|
||||||
Optional<size_t> end_token_index;
|
Optional<size_t> end_token_index;
|
||||||
while (!eof()) {
|
while (!eof()) {
|
||||||
|
@ -971,7 +971,7 @@ NonnullRefPtr<StringLiteral> Parser::parse_string_literal(ASTNode& parent)
|
||||||
|
|
||||||
NonnullRefPtr<ReturnStatement> Parser::parse_return_statement(ASTNode& parent)
|
NonnullRefPtr<ReturnStatement> Parser::parse_return_statement(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto return_statement = create_ast_node<ReturnStatement>(parent, position(), {});
|
auto return_statement = create_ast_node<ReturnStatement>(parent, position(), {});
|
||||||
consume(Token::Type::Keyword);
|
consume(Token::Type::Keyword);
|
||||||
if (!peek(Token::Type::Semicolon).has_value()) {
|
if (!peek(Token::Type::Semicolon).has_value()) {
|
||||||
|
@ -984,7 +984,7 @@ NonnullRefPtr<ReturnStatement> Parser::parse_return_statement(ASTNode& parent)
|
||||||
|
|
||||||
NonnullRefPtr<EnumDeclaration> Parser::parse_enum_declaration(ASTNode& parent)
|
NonnullRefPtr<EnumDeclaration> Parser::parse_enum_declaration(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto enum_decl = create_ast_node<EnumDeclaration>(parent, position(), {});
|
auto enum_decl = create_ast_node<EnumDeclaration>(parent, position(), {});
|
||||||
consume_keyword("enum");
|
consume_keyword("enum");
|
||||||
auto name_token = consume(Token::Type::Identifier);
|
auto name_token = consume(Token::Type::Identifier);
|
||||||
|
@ -1031,7 +1031,7 @@ bool Parser::match_keyword(const String& keyword)
|
||||||
|
|
||||||
NonnullRefPtr<StructOrClassDeclaration> Parser::parse_struct_or_class_declaration(ASTNode& parent, StructOrClassDeclaration::Type type)
|
NonnullRefPtr<StructOrClassDeclaration> Parser::parse_struct_or_class_declaration(ASTNode& parent, StructOrClassDeclaration::Type type)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto decl = create_ast_node<StructOrClassDeclaration>(parent, position(), {}, type);
|
auto decl = create_ast_node<StructOrClassDeclaration>(parent, position(), {}, type);
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case StructOrClassDeclaration::Type::Struct:
|
case StructOrClassDeclaration::Type::Struct:
|
||||||
|
@ -1058,7 +1058,7 @@ NonnullRefPtr<StructOrClassDeclaration> Parser::parse_struct_or_class_declaratio
|
||||||
|
|
||||||
NonnullRefPtr<MemberDeclaration> Parser::parse_member_declaration(ASTNode& parent)
|
NonnullRefPtr<MemberDeclaration> Parser::parse_member_declaration(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto member_decl = create_ast_node<MemberDeclaration>(parent, position(), {});
|
auto member_decl = create_ast_node<MemberDeclaration>(parent, position(), {});
|
||||||
member_decl->m_type = parse_type(*member_decl);
|
member_decl->m_type = parse_type(*member_decl);
|
||||||
|
|
||||||
|
@ -1077,7 +1077,7 @@ NonnullRefPtr<MemberDeclaration> Parser::parse_member_declaration(ASTNode& paren
|
||||||
|
|
||||||
NonnullRefPtr<BooleanLiteral> Parser::parse_boolean_literal(ASTNode& parent)
|
NonnullRefPtr<BooleanLiteral> Parser::parse_boolean_literal(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto token = consume(Token::Type::Keyword);
|
auto token = consume(Token::Type::Keyword);
|
||||||
auto text = text_of_token(token);
|
auto text = text_of_token(token);
|
||||||
// text == "true" || text == "false";
|
// text == "true" || text == "false";
|
||||||
|
@ -1096,7 +1096,7 @@ bool Parser::match_boolean_literal()
|
||||||
|
|
||||||
NonnullRefPtr<Type> Parser::parse_type(ASTNode& parent)
|
NonnullRefPtr<Type> Parser::parse_type(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
|
|
||||||
if (!match_type()) {
|
if (!match_type()) {
|
||||||
auto token = consume();
|
auto token = consume();
|
||||||
|
@ -1134,7 +1134,7 @@ NonnullRefPtr<Type> Parser::parse_type(ASTNode& parent)
|
||||||
|
|
||||||
NonnullRefPtr<ForStatement> Parser::parse_for_statement(ASTNode& parent)
|
NonnullRefPtr<ForStatement> Parser::parse_for_statement(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto for_statement = create_ast_node<ForStatement>(parent, position(), {});
|
auto for_statement = create_ast_node<ForStatement>(parent, position(), {});
|
||||||
consume(Token::Type::Keyword);
|
consume(Token::Type::Keyword);
|
||||||
consume(Token::Type::LeftParen);
|
consume(Token::Type::LeftParen);
|
||||||
|
@ -1158,7 +1158,7 @@ NonnullRefPtr<ForStatement> Parser::parse_for_statement(ASTNode& parent)
|
||||||
|
|
||||||
NonnullRefPtr<IfStatement> Parser::parse_if_statement(ASTNode& parent)
|
NonnullRefPtr<IfStatement> Parser::parse_if_statement(ASTNode& parent)
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
auto if_statement = create_ast_node<IfStatement>(parent, position(), {});
|
auto if_statement = create_ast_node<IfStatement>(parent, position(), {});
|
||||||
consume(Token::Type::Keyword);
|
consume(Token::Type::Keyword);
|
||||||
consume(Token::Type::LeftParen);
|
consume(Token::Type::LeftParen);
|
||||||
|
@ -1176,7 +1176,7 @@ NonnullRefPtr<IfStatement> Parser::parse_if_statement(ASTNode& parent)
|
||||||
}
|
}
|
||||||
Vector<StringView> Parser::parse_type_qualifiers()
|
Vector<StringView> Parser::parse_type_qualifiers()
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
Vector<StringView> qualifiers;
|
Vector<StringView> qualifiers;
|
||||||
while (!eof()) {
|
while (!eof()) {
|
||||||
auto token = peek();
|
auto token = peek();
|
||||||
|
@ -1195,7 +1195,7 @@ Vector<StringView> Parser::parse_type_qualifiers()
|
||||||
|
|
||||||
Vector<StringView> Parser::parse_function_qualifiers()
|
Vector<StringView> Parser::parse_function_qualifiers()
|
||||||
{
|
{
|
||||||
SCOPE_LOGGER();
|
ScopeLogger logger {};
|
||||||
Vector<StringView> qualifiers;
|
Vector<StringView> qualifiers;
|
||||||
while (!eof()) {
|
while (!eof()) {
|
||||||
auto token = peek();
|
auto token = peek();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user