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

Rewrite parameter uses when rewriting function types. #484

Closed
dtarditi opened this issue Apr 26, 2018 · 1 comment
Closed

Rewrite parameter uses when rewriting function types. #484

dtarditi opened this issue Apr 26, 2018 · 1 comment

Comments

@dtarditi
Copy link
Member

dtarditi commented Apr 26, 2018

In checked scopes , we rewrite function types with bounds-safe interfaces to be fully checked. We need to rewrite declarations of parameters and uses of parameters that have bounds-safe interfaces.

The following example illustrates the problem. We define 3 C typedefs:

  1. one that defines a function pointer with a bounds-safe interface on a parameter.
  2. one that defines the bounds-safe interface type the first one.
  3. one that defined a fully checked version.

We define a function that returns 1, with a bounds-safe interface of 2. We try to assign the result to 3 and get a compiler error that makes no sense on the face of it:

#include <stdchecked.h>

#pragma CHECKED_SCOPE ON

typedef int (*callback_fn3)(int *a : count(n), int n);
typedef  ptr<int (int *a : bounds(a, a + n), int n)> bsi_callback_fn3;
typedef  ptr<int (array_ptr<int> a : bounds(a, a + n), int n)> checked_callback_fn3;

checked callback_fn3 return_function_pointer(void) : itype(bsi_callback_fn3);

checked void test_function_pointer_return(void) {
  checked_callback_fn3 fn = 0;
  fn = return_function_pointer();
}

Here is the compiler error:

tmp.c:13:6: error: assigning to 'checked_callback_fn3' (aka '_Ptr<int (_Array_ptr<int> : bounds(arg #0, arg #0 + arg
      #1), int)>') from incompatible type '_Ptr<int (_Array_ptr<int> : bounds(arg #0, arg #0 + arg #1), int)>'
  fn = return_function_pointer();
     ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

My guess is that the uses of a in bsi_callback_fn3 weren't rewritten, causing the types to differ for reasons that aren't apparent in the error message.

@dtarditi dtarditi changed the title Rewriter parameter uses when rewriting function types. Rewrite parameter uses when rewriting function types. Apr 26, 2018
dtarditi added a commit that referenced this issue Oct 19, 2018
Change the transformation of positional parameters in TreeTransform.h
to use the transformed types for parameters.  Do this by using the
types passed into TransformExtendedParameterInfo.

Before this, the code was just transforming the type for the positional
parameter based on however types were being transformed.,  This worked fine
for a type-only transform, but didn't work properly when the parameter type
was transformed based on some other informaton in the program (such as a
bounds-safe interface).

This fixes issue #484.

Testing:
- Add a regression test based on the case described in issue #484.
dtarditi added a commit that referenced this issue Oct 20, 2018
In TreeTransform.h, track the transformed types for parameters.  Use
this information to update the types for positional parameter expressions. 
This fixes issue #484, which was a typechecking error caused by different types
for uses of parameters in bounds expressions in function types.

The tracking is straightforward: we add a vector of types for parameters.
Positional parameters only occur in bounds expressions in types.  When
we transform a function prototype type, we record the transformed types for
parameters before transforming the bounds expressions in the types.

This change also contains a few clean up changes:
1. While debugging an alternative approach, I noticed that TreeTransform
had some code that is flagged as specific to template instantiation. It
was mostly code for marking uses.  The code in turn called into code that
checked for detecting references to local variables defined in other lexical
contexts (for example, block scopes).  Those checks depend on the lexical context
state for Sema.  We don't want any of this code running for the kinds
of transforms that we are doing, so I added a new method to TreeTransform.h that
can be overridden to suppress this code.
2. Clean up and improve some comments in TreeTransform.h.
3. Improve the parameters for TransformExtendedParameterInfo, marking some
as const and changing the order of parameters to group related parameters
together.

Testing:

- Added new regression test for issue 484.
- Passed local testing on Windows.
- Passed automated testing.
@dtarditi
Copy link
Member Author

dtarditi commented Oct 20, 2018

Fixed by PR #576.

sulekhark pushed a commit that referenced this issue Jul 8, 2021
Before this change, types for function pointer parameters were generated
from a QualType object even when the specific parameter was not changed
by 3c. Now parameters that 3c does not convert will be rewritten using
their original source representation.

This fixes #484 by using va_list for the parameter (how it's written in the
source) instead of expanding it to struct __va_list_tag *. As a side effect,
this preserves formatting in function pointer types slightly better than before.
This shows up mostly as spaces being deleted from our generated tests.
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