Skip to content

Add support for enums with underlying type specifications to the parser #5431

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

Merged
merged 3 commits into from
Jul 23, 2020

Conversation

danpoe
Copy link
Contributor

@danpoe danpoe commented Jul 22, 2020

This adds support for parsing enums that specify the underlying type (a clang extension) to cbmc. For example, a usage would look like enum enum1 : unsigned {X, Y}.

The change to the grammar however introduces an additional shift/reduce conflict. The issue seems to be that the new syntax leads to ambiguity with the syntax for bitfields (where the bit portion also starts with a :). That is, for enum enum1 : unsigned, when enum enum1 has been read and : becomes the lookahead token, there is a choice between reducing (thus enum enum1 becomes the portion matched by the enum_name rule), or shifting, in which case : unsigned will become part of the portion matched by the rule as well. Any suggestions on how or if this can be fixed would be appreciated.

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • White-space or formatting changes outside the feature-related changed lines are in commits of their own.

@danpoe danpoe changed the title Feature/enums with underlying type Add support for enums with underlying type specifications to the parser Jul 22, 2020
@danpoe danpoe force-pushed the feature/enums-with-underlying-type branch from 919a6c3 to 6abdb89 Compare July 22, 2020 13:18

int main()
{
enum my_enum2 : unsigned enum_var1;
Copy link
Contributor

@hannes-steffenhagen-diffblue hannes-steffenhagen-diffblue Jul 22, 2020

Choose a reason for hiding this comment

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

Huh? Any ideas on what this means? Seems like clang allows this notation, but it appears to not actually mean anything, for instance

#include <stdio.h>

enum my_enum : signed char {
  A, B
};


int main(void) {
  enum my_enum : unsigned long long  X = 255;
  printf("%d\n", X);
}

Seems to compile just fine and works as if the second : unsigned long long wasn’t there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah it seems to just be ignored by clang. My guess would be that it might have been overly complicated for clang to exclude this case in the grammar, which would also be true for cbmc. I think we should report this as an error during conversion/typechecking.

@codecov
Copy link

codecov bot commented Jul 22, 2020

Codecov Report

Merging #5431 into develop will decrease coverage by 0.84%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #5431      +/-   ##
===========================================
- Coverage    68.21%   67.36%   -0.85%     
===========================================
  Files         1178     1178              
  Lines        97550    97551       +1     
===========================================
- Hits         66544    65720     -824     
- Misses       31006    31831     +825     
Flag Coverage Δ
#cproversmt2 ?
#regression 65.38% <100.00%> (+<0.01%) ⬆️
#unit 32.26% <66.66%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/ansi-c/parser.y 77.91% <100.00%> (+0.02%) ⬆️
src/solvers/smt2/smt2irep.cpp 32.00% <0.00%> (-56.00%) ⬇️
src/solvers/smt2/smt2_conv.cpp 31.46% <0.00%> (-26.23%) ⬇️
src/solvers/floatbv/float_bv.cpp 55.25% <0.00%> (-18.88%) ⬇️
src/solvers/smt2/smt2_dec.cpp 61.53% <0.00%> (-16.93%) ⬇️
src/solvers/smt2/smt2_tokenizer.cpp 69.02% <0.00%> (-16.82%) ⬇️
src/solvers/flattening/pointer_logic.cpp 81.81% <0.00%> (-10.61%) ⬇️
src/solvers/smt2/smt2_parser.cpp 58.91% <0.00%> (-8.07%) ⬇️
src/util/expr_util.cpp 80.50% <0.00%> (-4.24%) ⬇️
src/solvers/smt2/smt2_format.cpp 70.37% <0.00%> (-3.71%) ⬇️
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a743af3...6abdb89. Read the comment docs.

@codecov
Copy link

codecov bot commented Jul 22, 2020

Codecov Report

Merging #5431 into develop will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #5431   +/-   ##
========================================
  Coverage    68.21%   68.21%           
========================================
  Files         1178     1178           
  Lines        97550    97561   +11     
========================================
+ Hits         66544    66554   +10     
- Misses       31006    31007    +1     
Flag Coverage Δ
#cproversmt2 42.77% <68.75%> (-0.01%) ⬇️
#regression 65.38% <100.00%> (+<0.01%) ⬆️
#unit 32.26% <43.75%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/ansi-c/c_typecheck_type.cpp 76.51% <100.00%> (+0.15%) ⬆️
src/ansi-c/parser.y 78.03% <100.00%> (+0.14%) ⬆️
src/solvers/flattening/pointer_logic.cpp 89.39% <0.00%> (-3.04%) ⬇️
src/pointer-analysis/value_set_fi.h 95.12% <0.00%> (-2.44%) ⬇️
src/solvers/strings/string_builtin_function.h 76.82% <0.00%> (+2.43%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a743af3...75f9c49. Read the comment docs.

Copy link
Contributor

@chrisr-diffblue chrisr-diffblue left a comment

Choose a reason for hiding this comment

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

I think this looks OK, though its not my absolute area of expertise. I assume you've already taken a look at Parser::ParseEnumSpecifier in https://clang.llvm.org/doxygen/ParseDecl_8cpp_source.html to check what Clang is doing - though of course they have a hand-written parser which gives them a bit more flexibility.

@danpoe danpoe force-pushed the feature/enums-with-underlying-type branch 2 times, most recently from f5bc54d to cd77e54 Compare July 23, 2020 11:53
@danpoe danpoe marked this pull request as ready for review July 23, 2020 11:54
Copy link
Contributor

@chrisr-diffblue chrisr-diffblue left a comment

Choose a reason for hiding this comment

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

LGTM, with one small query around naming - but don't want to bike shed :-)

@@ -212,6 +212,7 @@ IREP_ID_ONE(NULL)
IREP_ID_ONE(null)
IREP_ID_ONE(nullptr)
IREP_ID_ONE(c_enum)
IREP_ID_ONE(underlying_type)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be something like enum_underlying_type or is there a reason is should not be specific to enums?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No particular reason, I've changed it to enum_underlying_type.

@danpoe danpoe force-pushed the feature/enums-with-underlying-type branch from cd77e54 to ab2b220 Compare July 23, 2020 12:21
@danpoe danpoe force-pushed the feature/enums-with-underlying-type branch from ab2b220 to 0bb02be Compare July 23, 2020 13:27
@danpoe danpoe force-pushed the feature/enums-with-underlying-type branch from 0bb02be to 75f9c49 Compare July 23, 2020 14:28
@danpoe danpoe merged commit 9b607d8 into diffblue:develop Jul 23, 2020
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.

3 participants