Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle loaded source code as shared objects #3041

Merged
merged 6 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SOURCES = \
units.cpp \
values.cpp \
plugins.cpp \
source.cpp \
position.cpp \
lexer.cpp \
parser.cpp \
Expand Down
2 changes: 1 addition & 1 deletion src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace Sass {

void AST_Node::update_pstate(const SourceSpan& pstate)
{
pstate_.offset += pstate - pstate_ + pstate.offset;
pstate_.offset += pstate.position - pstate_.position + pstate.offset;
}

sass::string AST_Node::to_string(Sass_Inspect_Options opt) const
Expand Down
10 changes: 4 additions & 6 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,18 @@ namespace Sass {
virtual sass::string to_string() const;
virtual void cloneChildren() {};
// generic find function (not fully implemented yet)
// ToDo: add specific implementions to all children
// ToDo: add specific implementations to all children
virtual bool find ( bool (*f)(AST_Node_Obj) ) { return f(this); };
void update_pstate(const SourceSpan& pstate);
Offset off() { return pstate(); }
Position pos() { return pstate(); }

// Some obects are not meant to be compared
// ToDo: maybe fallback to pointer comparison?
// Some objects are not meant to be compared
// ToDo: maybe fall-back to pointer comparison?
virtual bool operator== (const AST_Node& rhs) const {
throw std::runtime_error("operator== not implemented");
}

// We can give some reasonable implementations by using
// inverst operators on the specialized implementations
// invert operators on the specialized implementations
virtual bool operator!= (const AST_Node& rhs) const {
// Unequal if not equal
return !(*this == rhs);
Expand Down
10 changes: 10 additions & 0 deletions src/ast_fwd_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
/////////////////////////////////////////////
namespace Sass {

class SourceData;
class SourceFile;
class SynthFile;
class ItplFile;

class AST_Node;

class ParentStatement;
Expand Down Expand Up @@ -127,6 +132,11 @@ namespace Sass {
typedef SharedImpl<type> type##Obj; \
typedef SharedImpl<type> type##_Obj; \

IMPL_MEM_OBJ(SourceData);
IMPL_MEM_OBJ(SourceFile);
IMPL_MEM_OBJ(SynthFile);
IMPL_MEM_OBJ(ItplFile);

IMPL_MEM_OBJ(AST_Node);
IMPL_MEM_OBJ(Statement);
IMPL_MEM_OBJ(Block);
Expand Down
10 changes: 5 additions & 5 deletions src/backtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ namespace Sass {
const Backtrace& trace = traces[i];

// make path relative to the current directory
sass::string rel_path(File::abs2rel(trace.pstate.path, cwd, cwd));
sass::string rel_path(File::abs2rel(trace.pstate.getPath(), cwd, cwd));

// skip functions on error cases (unsure why ruby sass does this)
// if (trace.caller.substr(0, 6) == ", in f") continue;

if (first) {
ss << indent;
ss << "on line ";
ss << trace.pstate.line + 1;
ss << trace.pstate.getLine();
ss << ":";
ss << trace.pstate.column + 1;
ss << trace.pstate.getColumn();
ss << " of " << rel_path;
// ss << trace.caller;
first = false;
Expand All @@ -34,9 +34,9 @@ namespace Sass {
ss << std::endl;
ss << indent;
ss << "from line ";
ss << trace.pstate.line + 1;
ss << trace.pstate.getLine();
ss << ":";
ss << trace.pstate.column + 1;
ss << trace.pstate.getColumn();
ss << " of " << rel_path;
}

Expand Down
13 changes: 7 additions & 6 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "expand.hpp"
#include "parser.hpp"
#include "cssize.hpp"
#include "source.hpp"

namespace Sass {
using namespace Constants;
Expand Down Expand Up @@ -273,11 +274,11 @@ namespace Sass {

// get pointer to the loaded content
const char* contents = resources[idx].contents;
// keep a copy of the path around (for parserstates)
// ToDo: we clean it, but still not very elegant!?
strings.push_back(sass_copy_c_string(inc.abs_path.c_str()));
SourceFileObj source = SASS_MEMORY_NEW(SourceFile,
inc.abs_path.c_str(), contents, idx);

// create the initial parser state from resource
SourceSpan pstate(strings.back(), contents, idx);
SourceSpan pstate(source);

// check existing import stack for possible recursion
for (size_t i = 0; i < import_stack.size() - 2; ++i) {
Expand All @@ -298,7 +299,7 @@ namespace Sass {
}

// create a parser instance from the given c_str buffer
Parser p(Parser::from_c_str(contents, *this, traces, pstate));
Parser p(source, *this, traces);
// do not yet dispose these buffers
sass_import_take_source(import);
sass_import_take_srcmap(import);
Expand Down Expand Up @@ -441,7 +442,7 @@ namespace Sass {
if (const char* err_message = sass_import_get_error_message(include_ent)) {
if (source || srcmap) register_resource({ importer, uniq_path }, { source, srcmap }, pstate);
if (line == sass::string::npos && column == sass::string::npos) error(err_message, pstate, traces);
else error(err_message, SourceSpan(ctx_path, source, Position(line, column)), traces);
else { error(err_message, { pstate.source, { line, column } }, traces); }
}
// content for import was set
else if (source) {
Expand Down
21 changes: 10 additions & 11 deletions src/debugger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,10 @@ inline sass::string longToHex(long long t) {
inline sass::string pstate_source_position(AST_Node* node)
{
sass::sstream str;
Position start(node->pstate());
Position end(start + node->pstate().offset);
str << (start.file == sass::string::npos ? 99999999 : start.file)
Offset start(node->pstate().position);
Offset end(start + node->pstate().offset);
size_t file = node->pstate().getSrcId();
str << (file == sass::string::npos ? 99999999 : file)
<< "@[" << start.line << ":" << start.column << "]"
<< "-[" << end.line << ":" << end.column << "]";
#ifdef DEBUG_SHARED_PTR
Expand Down Expand Up @@ -419,7 +420,7 @@ inline void debug_ast(AST_Node* node, sass::string ind, Env* env)
std::cerr << ind << "Parent_Reference " << selector;
std::cerr << " (" << pstate_source_position(node) << ")";
std::cerr << " <" << selector->hash() << ">";
std::cerr << " <" << prettyprint(selector->pstate().token.ws_before()) << ">" << std::endl;
std::cerr << std::endl;

} else if (Cast<PseudoSelector>(node)) {
PseudoSelector* selector = Cast<PseudoSelector>(node);
Expand Down Expand Up @@ -460,7 +461,6 @@ inline void debug_ast(AST_Node* node, sass::string ind, Env* env)
std::cerr << " (" << pstate_source_position(node) << ")";
std::cerr << " <" << selector->hash() << ">";
std::cerr << " <<" << selector->ns_name() << ">>";
std::cerr << " <" << prettyprint(selector->pstate().token.ws_before()) << ">";
std::cerr << std::endl;
} else if (Cast<PlaceholderSelector>(node)) {

Expand Down Expand Up @@ -600,8 +600,7 @@ inline void debug_ast(AST_Node* node, sass::string ind, Env* env)
Comment* block = Cast<Comment>(node);
std::cerr << ind << "Comment " << block;
std::cerr << " (" << pstate_source_position(node) << ")";
std::cerr << " " << block->tabs() <<
" <" << prettyprint(block->pstate().token.ws_before()) << ">" << std::endl;
std::cerr << " " << block->tabs() << std::endl;
debug_ast(block->text(), ind + "// ", env);
} else if (Cast<If>(node)) {
If* block = Cast<If>(node);
Expand Down Expand Up @@ -881,7 +880,7 @@ inline void debug_ast(AST_Node* node, sass::string ind, Env* env)
if (expression->is_delayed()) std::cerr << " [delayed]";
if (expression->is_interpolant()) std::cerr << " [interpolant]";
if (expression->quote_mark()) std::cerr << " [quote_mark: " << expression->quote_mark() << "]";
std::cerr << " <" << prettyprint(expression->pstate().token.ws_before()) << ">" << std::endl;
std::cerr << std::endl;
} else if (Cast<String_Constant>(node)) {
String_Constant* expression = Cast<String_Constant>(node);
std::cerr << ind << "String_Constant " << expression;
Expand All @@ -892,7 +891,7 @@ inline void debug_ast(AST_Node* node, sass::string ind, Env* env)
std::cerr << " [" << prettyprint(expression->value()) << "]";
if (expression->is_delayed()) std::cerr << " [delayed]";
if (expression->is_interpolant()) std::cerr << " [interpolant]";
std::cerr << " <" << prettyprint(expression->pstate().token.ws_before()) << ">" << std::endl;
std::cerr << std::endl;
} else if (Cast<String_Schema>(node)) {
String_Schema* expression = Cast<String_Schema>(node);
std::cerr << ind << "String_Schema " << expression;
Expand All @@ -905,15 +904,15 @@ inline void debug_ast(AST_Node* node, sass::string ind, Env* env)
if (expression->has_interpolant()) std::cerr << " [has interpolant]";
if (expression->is_left_interpolant()) std::cerr << " [left interpolant] ";
if (expression->is_right_interpolant()) std::cerr << " [right interpolant] ";
std::cerr << " <" << prettyprint(expression->pstate().token.ws_before()) << ">" << std::endl;
std::cerr << std::endl;
for(const auto& i : expression->elements()) { debug_ast(i, ind + " ", env); }
} else if (Cast<String>(node)) {
String* expression = Cast<String>(node);
std::cerr << ind << "String " << expression;
std::cerr << " " << expression->concrete_type();
std::cerr << " (" << pstate_source_position(node) << ")";
if (expression->is_interpolant()) std::cerr << " [interpolant]";
std::cerr << " <" << prettyprint(expression->pstate().token.ws_before()) << ">" << std::endl;
std::cerr << std::endl;
} else if (Cast<Expression>(node)) {
Expression* expression = Cast<Expression>(node);
std::cerr << ind << "Expression " << expression;
Expand Down
38 changes: 19 additions & 19 deletions src/error_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ namespace Sass {
prefix("Error"), pstate(pstate), traces(traces)
{ }

InvalidSass::InvalidSass(SourceSpan pstate, Backtraces traces, sass::string msg, char* owned_src)
: Base(pstate, msg, traces), owned_src(owned_src)
InvalidSass::InvalidSass(SourceSpan pstate, Backtraces traces, sass::string msg)
: Base(pstate, msg, traces)
{ }


Expand Down Expand Up @@ -164,11 +164,11 @@ namespace Sass {
void warning(sass::string msg, SourceSpan pstate)
{
sass::string cwd(Sass::File::get_cwd());
sass::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.path));
sass::string abs_path(Sass::File::rel2abs(pstate.getPath(), cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.getPath(), cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.getPath()));

std::cerr << "WARNING on line " << pstate.line+1 << ", column " << pstate.column+1 << " of " << output_path << ":" << std::endl;
std::cerr << "WARNING on line " << pstate.getLine() << ", column " << pstate.getColumn() << " of " << output_path << ":" << std::endl;
std::cerr << msg << std::endl << std::endl;
}

Expand All @@ -180,24 +180,24 @@ namespace Sass {
void deprecated_function(sass::string msg, SourceSpan pstate)
{
sass::string cwd(Sass::File::get_cwd());
sass::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.path));
sass::string abs_path(Sass::File::rel2abs(pstate.getPath(), cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.getPath(), cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.getPath()));

std::cerr << "DEPRECATION WARNING: " << msg << std::endl;
std::cerr << "will be an error in future versions of Sass." << std::endl;
std::cerr << " on line " << pstate.line+1 << " of " << output_path << std::endl;
std::cerr << " on line " << pstate.getLine() << " of " << output_path << std::endl;
}

void deprecated(sass::string msg, sass::string msg2, bool with_column, SourceSpan pstate)
{
sass::string cwd(Sass::File::get_cwd());
sass::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, pstate.path, pstate.path));
sass::string abs_path(Sass::File::rel2abs(pstate.getPath(), cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.getPath(), cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, pstate.getPath(), pstate.getPath()));

std::cerr << "DEPRECATION WARNING on line " << pstate.line + 1;
if (with_column) std::cerr << ", column " << pstate.column + pstate.offset.column + 1;
std::cerr << "DEPRECATION WARNING on line " << pstate.getLine();
// if (with_column) std::cerr << ", column " << pstate.column + pstate.offset.column + 1;
if (output_path.length()) std::cerr << " of " << output_path;
std::cerr << ":" << std::endl;
std::cerr << msg << std::endl;
Expand All @@ -208,12 +208,12 @@ namespace Sass {
void deprecated_bind(sass::string msg, SourceSpan pstate)
{
sass::string cwd(Sass::File::get_cwd());
sass::string abs_path(Sass::File::rel2abs(pstate.path, cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.path, cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.path));
sass::string abs_path(Sass::File::rel2abs(pstate.getPath(), cwd, cwd));
sass::string rel_path(Sass::File::abs2rel(pstate.getPath(), cwd, cwd));
sass::string output_path(Sass::File::path_for_console(rel_path, abs_path, pstate.getPath()));

std::cerr << "WARNING: " << msg << std::endl;
std::cerr << " on line " << pstate.line+1 << " of " << output_path << std::endl;
std::cerr << " on line " << pstate.getLine() << " of " << output_path << std::endl;
std::cerr << "This will be an error in future versions of Sass." << std::endl;
}

Expand Down
16 changes: 2 additions & 14 deletions src/error_handling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,8 @@ namespace Sass {

class InvalidSass : public Base {
public:
InvalidSass(InvalidSass& other) : Base(other), owned_src(other.owned_src) {
// Assumes that `this` will outlive `other`.
other.owned_src = nullptr;
}

// Required because the copy constructor's argument is not const.
// Can't use `std::move` here because we build on Visual Studio 2013.
InvalidSass(InvalidSass &&other) : Base(other), owned_src(other.owned_src) {
other.owned_src = nullptr;
}

InvalidSass(SourceSpan pstate, Backtraces traces, sass::string msg, char* owned_src = nullptr);
virtual ~InvalidSass() throw() { sass_free_memory(owned_src); };
char *owned_src;
InvalidSass(SourceSpan pstate, Backtraces traces, sass::string msg);
virtual ~InvalidSass() throw() {};
};

class InvalidParent : public Base {
Expand Down
Loading