Skip to content
Snippets Groups Projects
Commit 71d2f3a5 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add SourceLocation to store C++ or pgs source locations

parent 776e0c92
No related branches found
No related tags found
1 merge request!168Simplify backtrace output
...@@ -26,6 +26,15 @@ ASTBacktrace::errorMessageAt(const std::string& message) const ...@@ -26,6 +26,15 @@ ASTBacktrace::errorMessageAt(const std::string& message) const
return error_msg.str(); 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 void
ASTBacktrace::create() ASTBacktrace::create()
{ {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define AST_BACTRACE_HPP #define AST_BACTRACE_HPP
#include <utils/PugsAssert.hpp> #include <utils/PugsAssert.hpp>
#include <utils/SourceLocation.hpp>
#include <pegtl/file_input.hpp> #include <pegtl/file_input.hpp>
#include <string> #include <string>
...@@ -24,6 +25,8 @@ class ASTBacktrace ...@@ -24,6 +25,8 @@ class ASTBacktrace
public: public:
std::string errorMessageAt(const std::string& error_msg) const; std::string errorMessageAt(const std::string& error_msg) const;
SourceLocation sourceLocation() const;
void void
push(const ASTNode* node) push(const ASTNode* node)
{ {
......
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment