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

Parse bounds declarations for variable declarations. #18

Closed
secure-sw-dev-bot opened this issue Jan 16, 2022 · 3 comments
Closed

Parse bounds declarations for variable declarations. #18

secure-sw-dev-bot opened this issue Jan 16, 2022 · 3 comments

Comments

@secure-sw-dev-bot
Copy link

This issue was copied from checkedc/checkedc-clang#18


This change adds parsing of variable declarations with bounds declarations (issue #13).
For each declarator in a declaration, the declarator is parsed and then the
optional Checked C bounds declaration is parsed. The bounds declaration is
parsed before the optional initializing expression for the declarator. Because
the declarator has already been parsed and added to the current scope, the
bounds expression can be eagerly parsed.

One surprise with clang was that placing declarators for a declaration
on multiple lines caused a parsing error in the initial implementation,
while having all the declarators on one line did not. I traced this back to
special case code that looks for typographical mistakes
at line endings by calling MightBeDeclarator and generating an
error if MightBeDeclarator is false. MightBeDeclarator returns true
for syntactic items that might start a declarator. It has special
case checks to make sure that an identifier is followed by something
that might also be part of a declarator. For Checked C, an identifier
that starts a declarator may be followed by ':' and a bounds expression,
so allow ':' when the language options include Checked C.

This change also improves error handling during the parsing of bounds
expressions.

  • When an error occurs after having parsed an identifier and a left parenthesis,
    always scan for the matching right parenthesis. The scan for the matching
    right parenthesis was only happening in one specific case, leading to
    hard-to-understand spurious parsing errors.
  • Make a best effort to continue if an error occurs while parsing a
    bounds expression of the form bounds '(' e1 ',' e2, ')'. clang does not
    differentiate during parsing of expressions between semantic errors and
    parsing failures. It is important to continue parsing so that a semantic
    error does not cause a cascade of parsing errors.

These problems were uncovered during testing of parsing of variable declarations
with bounds expressions. Specifically, using an incorrect bounds expression
in a variable declaration with an initializer caused a spurious parsing
errors.

Testing:

  • Created a new feature test for parsing of declarations with bounds
    (parsing_bounds_var_declarations.c). This will be committed separately to the
    checkedc repo.
  • Passes existing Checked C tests.
  • Passes existing clang base line tests.
@secure-sw-dev-bot
Copy link
Author

Comment from @msftclas:

Hi @dtarditi, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!


It looks like you're a Microsoft contributor (David Tarditi). If you're full-time, we DON'T require a Contribution License Agreement. If you are a vendor, please DO sign the electronic Contribution License Agreement. It will take 2 minutes and there's no faxing! https://cla.microsoft.com.

TTYL, MSBOT;

@secure-sw-dev-bot
Copy link
Author

Comment from @reubeno:

Modulo my one comment comment :), looks good to me!

@secure-sw-dev-bot
Copy link
Author

Comment from @dtarditi:

Good idea - I've added a comment explaining the case for Checked C.

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

No branches or pull requests

1 participant