From 71d2f3a5e336a1b24b360fb97d78bfb633e11643 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Thu, 1 Jun 2023 00:38:47 +0200 Subject: [PATCH] Add SourceLocation to store C++ or pgs source locations --- src/language/ast/ASTBacktrace.cpp | 9 +++++ src/language/ast/ASTBacktrace.hpp | 3 ++ src/utils/SourceLocation.hpp | 58 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/utils/SourceLocation.hpp diff --git a/src/language/ast/ASTBacktrace.cpp b/src/language/ast/ASTBacktrace.cpp index f34eb0273..18442ec43 100644 --- a/src/language/ast/ASTBacktrace.cpp +++ b/src/language/ast/ASTBacktrace.cpp @@ -26,6 +26,15 @@ ASTBacktrace::errorMessageAt(const std::string& message) const return error_msg.str(); } +SourceLocation +ASTBacktrace::sourceLocation() const +{ + auto& stack = ASTBacktrace::getInstance().m_stack; + + auto p = stack[stack.size() - 1]->begin(); + return SourceLocation(p.source, p.line, p.column); +} + void ASTBacktrace::create() { diff --git a/src/language/ast/ASTBacktrace.hpp b/src/language/ast/ASTBacktrace.hpp index add4bc8b0..6ea9a5364 100644 --- a/src/language/ast/ASTBacktrace.hpp +++ b/src/language/ast/ASTBacktrace.hpp @@ -2,6 +2,7 @@ #define AST_BACTRACE_HPP #include <utils/PugsAssert.hpp> +#include <utils/SourceLocation.hpp> #include <pegtl/file_input.hpp> #include <string> @@ -24,6 +25,8 @@ class ASTBacktrace public: std::string errorMessageAt(const std::string& error_msg) const; + SourceLocation sourceLocation() const; + void push(const ASTNode* node) { diff --git a/src/utils/SourceLocation.hpp b/src/utils/SourceLocation.hpp new file mode 100644 index 000000000..d57116c8c --- /dev/null +++ b/src/utils/SourceLocation.hpp @@ -0,0 +1,58 @@ +#ifndef SOURCE_LOCATION_HPP +#define SOURCE_LOCATION_HPP + +#include <experimental/source_location> +#include <string> + +class SourceLocation +{ + private: + std::string m_filename; + std::string m_function; + size_t m_line; + size_t m_column; + + public: + const std::string& + filename() const + { + return m_filename; + } + + const std::string& + function() const + { + return m_function; + } + + size_t + line() const + { + return m_line; + } + + size_t + column() const + { + return m_column; + } + + SourceLocation( + const std::experimental::source_location& source_location = std::experimental::source_location::current()) + : m_filename{source_location.file_name()}, + m_function{source_location.function_name()}, + m_line{source_location.line()}, + m_column{source_location.column()} + {} + + SourceLocation(std::string filename, size_t line, size_t column, std::string function = "") + : m_filename{filename}, m_function{function}, m_line{line}, m_column{column} + {} + + SourceLocation(SourceLocation&&) = default; + SourceLocation(const SourceLocation&) = default; + + ~SourceLocation() = default; +}; + +#endif // SOURCE_LOCATION_HPP -- GitLab