Skip to content

Commit

Permalink
Handle circular includes, take 2
Browse files Browse the repository at this point in the history
We can't rely on path equivalence to determine the old file, need to
count the level instead.

Fixes #150, again.
  • Loading branch information
foonathan committed Nov 24, 2022
1 parent 282f05e commit ba6fd12
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/libclang/preprocessor.cpp
Expand Up @@ -505,7 +505,7 @@ class position
{
public:
position(ts::object_ref<std::string> result, const char* ptr) noexcept
: result_(result), cur_line_(1u), cur_column_(0u), ptr_(ptr), write_(true)
: result_(result), cur_line_(1u), cur_column_(0u), ptr_(ptr), write_disabled_count_(0)
{
// We strip all conditional defines and pragmas from the input, which includes the include
// guard. If the source includes a file, which includes itself again (for some reason), this
Expand All @@ -518,7 +518,7 @@ class position

void set_line(unsigned line)
{
if (cur_line_ != line)
if (write_enabled() && cur_line_ != line)
{
*result_ += "#line " + std::to_string(line) + "\n";
cur_line_ = line;
Expand All @@ -528,8 +528,9 @@ class position

void write_str(std::string str)
{
if (write_ == false)
if (!write_enabled())
return;

for (auto c : str)
{
*result_ += c;
Expand All @@ -545,7 +546,7 @@ class position

void bump() noexcept
{
if (write_ == true)
if (write_enabled())
{
result_->push_back(*ptr_);
++cur_column_;
Expand All @@ -561,13 +562,15 @@ class position

void bump(std::size_t offset) noexcept
{
if (write_ == true)
if (write_enabled())
{
for (std::size_t i = 0u; i != offset; ++i)
bump();
}
else
{
skip(offset);
}
}

// no write, no newline detection
Expand All @@ -578,7 +581,7 @@ class position

void skip_with_linecount() noexcept
{
if (write_ == true)
if (write_enabled())
{
++cur_column_;
if (*ptr_ == '\n')
Expand All @@ -594,17 +597,18 @@ class position

void enable_write() noexcept
{
write_.set();
DEBUG_ASSERT(write_disabled_count_ > 0, detail::assert_handler{});
--write_disabled_count_;
}

void disable_write() noexcept
{
write_.try_reset();
++write_disabled_count_;
}

bool write_enabled() const noexcept
{
return write_ == true;
return write_disabled_count_ == 0;
}

explicit operator bool() const noexcept
Expand Down Expand Up @@ -636,7 +640,7 @@ class position
ts::object_ref<std::string> result_;
unsigned cur_line_, cur_column_;
const char* ptr_;
ts::flag write_;
unsigned write_disabled_count_;
};

bool starts_with(const position& p, const char* str, std::size_t len)
Expand Down Expand Up @@ -1204,11 +1208,8 @@ detail::preprocessor_output detail::preprocess(const libclang_compile_config& co
}
else if (lm.value().flag == linemarker::enter_old)
{
if (lm.value().file == path)
{
p.enable_write();
p.set_line(lm.value().line);
}
p.enable_write();
p.set_line(lm.value().line);
}
else if (lm.value().flag == linemarker::line_directive && p.write_enabled())
{
Expand Down

0 comments on commit ba6fd12

Please sign in to comment.