Allow for auto as type (C++ >= 11)
Description
Since C++11, auto
can be used as a type instead of a storage qualifier (https://en.cppreference.com/w/cpp/language/auto). The auto
type tells the compiler that the actual type of the variable being declared must be inferred from its initializer. It can be understood as a placeholder, asking the compiler to type the variable for us.
Need
A request to handle auto
as a placeholder type has been emitted in the thread https://systeme.lip6.fr/pipermail/cocci/2020-February/006812.html
Design
In order to handle it properly, 5 different things are necessary:
- Allow
auto
as a type on the C side (when the--c++
option is provided). - Allow
auto
as a type on the SmPl side (when the--c++
option is provided).
The first 2 points combined allow for a simple match like we already do for int
and other types.
-
Update the
--c++
option to optionally take an argument indicating the version of C++ we are working on -
Disallow
auto
as storage qualifier when--c++=11
or later is being processed.
These 2 points ensure that we stay compatible with C++ prior to C++11, which uses auto
as storage qualifier (as in C), while avoiding breaking any existing user configuration and enforcing the code to respect either of the semantics.
- Infer which type
auto
is being used for.
This last point would allow to create an isomorphism from auto to its inferred type in order to have :
@@
@@
* int x
be matched by both variable declarations explicitly stating that x
is an int
and those where x
was declared as auto
where auto
is inferred to be an int
.
- Since
auto
is only allowed in a declaration where an initializer is also provided, the inference (if we succeed to do it) is simply a matter of looking at what the initializer's type is. - This feature has not been requested and can be seen as optional, but it seems better and easier to implement it on the way than to come back to it later, potentially changing some internal representation of things.
Test
The following example (files attached) should pass using spatch --cocci-file test.cocci --c++11 test.cpp
with test.res
as the expected result
int main() {
auto x = 2;
const auto& y = x;
return 0;
}
@@
type t;
identifier x;
identifier y;
@@
- t y = x;
+ t x = y;
@@
type t;
identifier x;
@@
- t x = 2;
+ auto y = 2;
int main() {
auto y = 2;
const auto& x = y;
return 0;
}