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

Parsing error when using template type parameter with assignment #2317

Closed
slnj opened this issue Feb 20, 2022 · 1 comment · Fixed by #2321
Closed

Parsing error when using template type parameter with assignment #2317

slnj opened this issue Feb 20, 2022 · 1 comment · Fixed by #2321
Assignees
Labels
Milestone

Comments

@slnj
Copy link

slnj commented Feb 20, 2022

Hello!

When using a template parameter with regular type parameters with default value assignment, a parsing error is reported.
This cases are valid and generate parsing errors:

template < typename T,  size_t s = 15 >
void f(){}
template < values v = v::ok >   // where values is an enum
void f(){}
template<class T, T::type n = 0>  //Where T::type is an integral type
class X;

The type could be:
integral type
enumerator
lvalue reference
nullptr
pointer

An a default value could be assigned

Also, the default value could be an expression like: (sizeof(T)*2)

Regards

@guwirth guwirth added the bug label Feb 21, 2022
@guwirth
Copy link
Collaborator

guwirth commented Feb 22, 2022

Hello @slnj,

will have a look to it.

For your explanation:

General problem is that the cxx plugin is only parsing the grammar (syntax analysis) without knowing the semantic.

Below is a good sample: First case works because int is an integral type. In second case size_t is for the parser just a identifier and only from the position the parser can guess that it is a type.

template < typename T,  int s = 15 > void f(){} // works: int is integral type
template < typename T,  size_t s = 15 > void f(){} // fails

Our second problem is that we are using SonarSource Language Recognizer (SSLR) which implements PEG parsing. Syntactically, PEGs also look similar to context-free grammars (CFGs), but they have a different interpretation: the choice operator selects the first match in PEG, while it is ambiguous in CFG. That means we have to change the order of the rules in the grammar in some places. This is ultimately a lot of "try and error", especially in such a complex grammar as C++.

Regards,

guwirth added a commit to guwirth/sonar-cxx that referenced this issue Feb 22, 2022
- close SonarOpenCommunity#2317

There is a PEG issue with the order of
```
template-parameter:
   type-parameter
   parameter-declaration
```
`type-parameter type-constraint' rule matches in case of non integral data type.

Samples:
```C++
template <typename T, size_t s = 15 > void f() {}
template <typename T, size_t s = sizeof(T)*2 > void f() {}
template <typename T, size_t s = 1>2 > void f() {}
template <values v = v::ok> void f() {}
template<class T, T::type n = 0> class X;
```
guwirth added a commit to guwirth/sonar-cxx that referenced this issue Feb 23, 2022
- update to SQ 8.9.7.52159 for testing
- close SonarOpenCommunity#2317

There is a PEG issue with the order of
```
template-parameter:
   type-parameter
   parameter-declaration
```
`type-parameter type-constraint' rule matches in case of non integral data type.

Samples:
```C++
template <typename T, size_t s = 15 > void f() {}
template <typename T, size_t s = sizeof(T)*2 > void f() {}
template <typename T, size_t s = 1>2 > void f() {}
template <values v = v::ok> void f() {}
template<class T, T::type n = 0> class X;
```
guwirth added a commit to guwirth/sonar-cxx that referenced this issue Feb 23, 2022
- update to SQ 8.9.7.52159 for testing
- use Start-FileDownload in AppVeyor
- close SonarOpenCommunity#2317

There is a PEG issue with the order of
```
template-parameter:
   type-parameter
   parameter-declaration
```
`type-parameter type-constraint' rule matches in case of non integral data type.

Samples:
```C++
template <typename T, size_t s = 15 > void f() {}
template <typename T, size_t s = sizeof(T)*2 > void f() {}
template <typename T, size_t s = 1>2 > void f() {}
template <values v = v::ok> void f() {}
template<class T, T::type n = 0> class X;
```
guwirth added a commit to guwirth/sonar-cxx that referenced this issue Feb 23, 2022
- update to SQ 8.9.7.52159 for testing
- update to Maven 3.8.4
- use Start-FileDownload in AppVeyor
- close SonarOpenCommunity#2317

There is a PEG issue with the order of
```
template-parameter:
   type-parameter
   parameter-declaration
```
`type-parameter type-constraint' rule matches in case of non integral data type.

Samples:
```C++
template <typename T, size_t s = 15 > void f() {}
template <typename T, size_t s = sizeof(T)*2 > void f() {}
template <typename T, size_t s = 1>2 > void f() {}
template <values v = v::ok> void f() {}
template<class T, T::type n = 0> class X;
```
guwirth added a commit to guwirth/sonar-cxx that referenced this issue Feb 23, 2022
- update to SQ 8.9.7.52159 for testing
- update to Maven 3.8.4
- use curl in AppVeyor
- close SonarOpenCommunity#2317

There is a PEG issue with the order of
```
template-parameter:
   type-parameter
   parameter-declaration
```
`type-parameter type-constraint' rule matches in case of non integral data type.

Samples:
```C++
template <typename T, size_t s = 15 > void f() {}
template <typename T, size_t s = sizeof(T)*2 > void f() {}
template <typename T, size_t s = 1>2 > void f() {}
template <values v = v::ok> void f() {}
template<class T, T::type n = 0> class X;
```
guwirth added a commit to guwirth/sonar-cxx that referenced this issue Feb 23, 2022
- update to SQ 8.9.7.52159 for testing
- update to Maven 3.8.4
- use curl in AppVeyor
- close SonarOpenCommunity#2317

There is a PEG issue with the order of
```
template-parameter:
   type-parameter
   parameter-declaration
```
`type-parameter type-constraint' rule matches in case of non integral data type.

Samples:
```C++
template <typename T, size_t s = 15 > void f() {}
template <typename T, size_t s = sizeof(T)*2 > void f() {}
template <typename T, size_t s = 1>2 > void f() {}
template <values v = v::ok> void f() {}
template<class T, T::type n = 0> class X;
```
guwirth added a commit to guwirth/sonar-cxx that referenced this issue Feb 23, 2022
- update to SQ 8.9.7.52159 for testing
- update to Maven 3.8.4
- use curl in AppVeyor
- close SonarOpenCommunity#2317

There is a PEG issue with the order of
```
template-parameter:
   type-parameter
   parameter-declaration
```
`type-parameter type-constraint' rule matches in case of non integral data type.

Samples:
```C++
template <typename T, size_t s = 15 > void f() {}
template <typename T, size_t s = sizeof(T)*2 > void f() {}
template <typename T, size_t s = 1>2 > void f() {}
template <values v = v::ok> void f() {}
template<class T, T::type n = 0> class X;
```
@guwirth guwirth self-assigned this Feb 24, 2022
@guwirth guwirth added this to the 2.0.7 milestone Feb 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants