Skip to content

Handle explicit template specializations in sg.pl#134

Open
Ozaq wants to merge 1 commit intodevelopfrom
fix/sg_pl
Open

Handle explicit template specializations in sg.pl#134
Ozaq wants to merge 1 commit intodevelopfrom
fix/sg_pl

Conversation

@Ozaq
Copy link
Member

@Ozaq Ozaq commented Feb 26, 2026

Description

sg.pl crashes when a header contains an explicit template specialization (template <>) because template_args() expects class/bool/int after '<' but gets '>'.

Return an empty parameter list from template_args() on 'template <>' and skip the entire specialization in parse_template(), since explicit specializations are not new class definitions and should not generate .b files.

Contributor Declaration

By opening this pull request, I affirm the following:

  • All authors agree to the Contributor License Agreement.
  • The code follows the project's coding standards.
  • I have performed self-review and added comments where needed.
  • I have added or updated tests to verify that my changes are effective and functional.
  • I have run all existing tests and confirmed they pass.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Prevents sg.pl from crashing on explicit template specializations (template <>) by detecting the empty parameter list case and skipping parsing of the specialization body.

Changes:

  • Return an empty parameter list from template_args() when encountering template <>.
  • In parse_template(), detect explicit specializations and skip the entire declaration/body so they don’t generate .b outputs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

my @tmp;
expect_next("<");
if(next_is(">"))
{
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template_args() relies on next_is(\">\") consuming the token; if next_is is implemented as a pure lookahead in this parser, the > will remain in the stream and the subsequent parsing will desync. Consider replacing this with an explicit consume (e.g., expect_next(\">\") or a dedicated consume function) before returning the empty list, so the behavior doesn’t depend on next_is side-effects.

Suggested change
{
{
# Empty template parameter list: consume '>' explicitly so parsing stays in sync
expect_next(">");

Copilot uses AI. Check for mistakes.
Comment on lines +217 to +218
consume_block('{','}');
next_is(";");
Copy link

Copilot AI Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semicolon handling after consume_block is currently implicit and ignores the return value of next_is(\";\"). If the goal is to optionally consume a trailing ;, consider using a function/name that’s explicitly "consume if present" (or check the return value), to avoid depending on undocumented next_is behavior and to make the intent clearer.

Copilot uses AI. Check for mistakes.
sg.pl crashes when a header contains an explicit template
specialization (template <>) because template_args() expects
class/bool/int after '<' but gets '>'.

Return an empty parameter list from template_args() on 'template <>'
and skip the entire specialization in parse_template(), since explicit
specializations are not new class definitions and should not generate
.b files.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@wdeconinck
Copy link
Member

Hi @Ozaq could you please provide a test that was previously crashing?

@marcosbento
Copy link
Collaborator

marcosbento commented Feb 27, 2026

If I understood correctly, the issue occurs when the file contains something like:

// Forwarded classes
template <> class TheNuisance< std::string >;

class X {};

which ends with the failure > is not (class|bool|int) at cmake/sg.pl line 45. upon finding the <> of TheNuisance, where normally the whole construct would be ignored as it is just a class forward declaration.

Assuming this is indeed the problem the issue is fixed! But this certainly deserves a test to ensure the use case is handled properly

Copy link
Collaborator

@marcosbento marcosbento left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test to cover the failure test that triggered these changes.

# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.

# ==========================================================================
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding this description. It is very useful!

grep { length($_); } # drop empty tokens
map { /\W/ ? split('',$_) : $_; } # split non-word tokens into chars
map { s/\s//g; $_; } # strip whitespace within tokens
split(/\b/, $x ); # split on word boundaries
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please align the # "column"

Copy link
Member

@wdeconinck wdeconinck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test requested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants