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

Places where we have const* arguments in source, optionally mark the value as const as well. #1261

Merged
4 commits merged into from
Sep 24, 2020

Conversation

ahsonkhan
Copy link
Member

@ahsonkhan ahsonkhan commented Sep 12, 2020

Used this regex:
const\* (?!const) -> const* const on *.c files excluding sdk\tests, sdk\samples, and intentionally skipping *.h files

Copy link
Contributor

@momuno momuno left a comment

Choose a reason for hiding this comment

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

Side Q: I'm intrigued that this compiles without changing header declarations. Do you know why that is?

sdk/src/azure/core/az_log.c Outdated Show resolved Hide resolved
sdk/src/azure/core/az_context.c Show resolved Hide resolved
sdk/src/azure/core/az_context.c Show resolved Hide resolved
sdk/src/azure/core/az_http_policy_logging.c Show resolved Hide resolved
@ahsonkhan
Copy link
Member Author

ahsonkhan commented Sep 14, 2020

Side Q: I'm intrigued that this compiles without changing header declarations. Do you know why that is?

I will quote @BillyONeal

in C, f(char*) and f(char* const) are the same function

I don't know the underlying compiler or language reason to exactly why that is (or the mechanism behind it), but if I had to guess, the generated assembly is going to be identical and the second const is just an implementation detail hint used by the compiler in some way to validate assignments.

@BillyONeal
Copy link
Contributor

I don't know the underlying compiler or language reason to exactly why that is

C11 6.7.6.3 "Function declarators (including prototypes)"/15:

image

The part I highlighted says that type qualifiers are removed from the declared type when determining whether function types are compatible. Note that the const in const T* is not a qualifier on the pointer's type, it's on the pointee's type. C only strips the top level qualifier, it doesn't "reach inside" to remove the inner const.

@momuno
Copy link
Contributor

momuno commented Sep 14, 2020

I don't know the underlying compiler or language reason to exactly why that is

C11 6.7.6.3 "Function declarators (including prototypes)"/15:

image

The part I highlighted says that type qualifiers are removed from the declared type when determining whether function types are compatible. Note that the const in const T* is not a qualifier on the pointer's type, it's on the pointee's type. C only strips the top level qualifier, it doesn't "reach inside" to remove the inner const.

Thanks for this info. A spot I'm still a little confused on is the removing of qualifiers to determine if function types are compatible. Here's my question: If the qualifier (in this case const on the pointee) is removed prior to compatibility testing, then wouldn't that make int function_name(int const* var1) the same as int function_name(int* var1) ? However, I have found that the compiler will complain if the decalaration and definition of a function differ in this respect.

@momuno
Copy link
Contributor

momuno commented Sep 14, 2020

@ahsonkhan are you thinking to also update the header files in a separate PR?

@BillyONeal
Copy link
Contributor

If the qualifier (in this case const on the pointee)

Qualifiers on the pointee aren't qualifiers on the function argument, so they are not adjusted by the above wording.

then wouldn't that make int function_name(int const* var1) the same as int function_name(int* var1) ?

No, because the const in "int const*" is not a type qualification of the function parameter. The function parameter's type is a pointer, not a const or non-const int. The const in int *const is a type qualification of the function parameter, since the const refers to the function's parameter, which is a pointer.

@momuno
Copy link
Contributor

momuno commented Sep 14, 2020

If the qualifier (in this case const on the pointee)

Qualifiers on the pointee aren't qualifiers on the function argument, so they are not adjusted by the above wording.

then wouldn't that make int function_name(int const* var1) the same as int function_name(int* var1) ?

No, because the const in "int const*" is not a type qualification of the function parameter. The function parameter's type is a pointer, not a const or non-const int. The const in int *const is a type qualification of the function parameter, since the const refers to the function's parameter, which is a pointer.

Oh I get it now. I had it flipped. Thank you!

@ahsonkhan
Copy link
Member Author

ahsonkhan commented Sep 15, 2020

are you thinking to also update the header files in a separate PR?

No. That is intentionally not something we want to do as per our discussion/meeting last Friday. We don't want redundant use of const in the public headers to increase cognitive burden on the end user. Having it optionally used in the source files, to try to validate correctness, is OK.

Quoting @CIPop

In SRC we can have the const on all parameters that are const in this version of the implementation. The public API (include files) do not need to have the consts since they do not affect in any way the application (taking a dependency on our sources).

http://www.gotw.ca/gotw/006.htm
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#exception-49
https://stackoverflow.com/questions/117293/use-of-const-for-function-parameters/11036371#11036371
https://stackoverflow.com/questions/1554750/c-const-keyword-use-liberally/1554820#1554820

@momuno
Copy link
Contributor

momuno commented Sep 15, 2020

are you thinking to also update the header files in a separate PR?

No. That is intentionally not something we want to do as per our discussion/meeting last Friday. We don't want redundant use of const in the public headers to increase cognitive burden on the end user. Having it optionally used in the source files, to try to validate correctness, is OK.

Quoting @CIPop

In SRC we can have the const on all parameters that are const in this version of the implementation. The public API (include files) do not need to have the consts since they do not affect in any way the application (taking a dependency on our sources).

http://www.gotw.ca/gotw/006.htm
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#exception-49
https://stackoverflow.com/questions/117293/use-of-const-for-function-parameters/11036371#11036371
https://stackoverflow.com/questions/1554750/c-const-keyword-use-liberally/1554820#1554820

Sounds good. Tbh, there was so much back and forth, repetition and circling back around in that debate, I wasn't clear on the end result.

@ahsonkhan
Copy link
Member Author

@RickWinter, @ericwol-msft, @barcharcraz, @CIPop - please review.

@ahsonkhan
Copy link
Member Author

Got confirmation from @ericwol-msft offline that this should wait until after GA. Please hold off on merging for now.

@ghost
Copy link

ghost commented Sep 24, 2020

Hello @ahsonkhan!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 5723678 into Azure:master Sep 24, 2020
@ahsonkhan ahsonkhan deleted the UseConstInSrc branch September 24, 2020 01:17
danewalton added a commit to danewalton/azure-sdk-for-c that referenced this pull request Nov 13, 2020
…ark the value as const as well. (Azure#1261)"

This reverts commit 5723678.
danewalton added a commit that referenced this pull request Nov 14, 2020
…ark the value as const as well. (#1261)" (#1517)

This reverts commit 5723678.
danewalton added a commit that referenced this pull request Nov 16, 2020
* Make sure we always log in as app or provisioner (#1480)

This also adds previously-required parameters back into
@PSBoundParameters to pass down to pre- and post-scripts.

Co-authored-by: Heath Stewart <heaths@microsoft.com>

* update changelog for beta 2 (#1484)

* Sync eng/common directory with azure-sdk-tools for PR 1163 (#1483)

* Update subscription configuration schema to include new parameters

* Support platform specific arm template parameters and legacy hashtable format

* Update arm template parameter comment to include top level key

* Restore AdditionalParameters. Merge ArmTemplateParameters from stringified hash literal

* Handle duplicate keys more explicitly for arm and env vars

* Regenerate New-TestResources.ps1 markdown

* revert variable name to environmentVariables to fix post-scripts

* Handle empty arm template parameters better

* Remove arm template parameter merge logic from deploy template

* Add merge hashes function to New-TestResources.ps1

* Add merge hashes function to New-TestResources.ps1

* Add env variable overwrite warning. Use ContainsKey checks

* Temporarily manually fix invalid generated markdown links

Co-authored-by: Ben Broderick Phillips <bebroder@microsoft.com>

* Sync eng/common directory with azure-sdk-tools for PR 1178 (#1487)

* Refactor eng/common/README.md

* Add doc directory

Co-authored-by: Chidozie Ononiwu <chononiw@microsoft.com>

* Fix broken eng/common doc link (#1489)

Co-authored-by: Chidozie Ononiwu <chononiw@microsoft.com>

* Explicitly ignore return of _az_span_token on c2d parsing (#1482) (#1490)

* add latest static analyzer fix to changelog (#1491)

* Sync eng/common directory with azure-sdk-tools for PR 1188 (#1502)

* Add debug flag to arm deployment command

* Only set debug preference when $CI is true

Co-authored-by: Ben Broderick Phillips <bebroder@microsoft.com>

* Remove invalid characters in basename sourced from username (#1503)

Co-authored-by: Ben Broderick Phillips <bebroder@microsoft.com>

* Sync eng/common directory with azure-sdk-tools for PR 1170 (#1481)

* Added the preprocess scripts.

* string array to string

Co-authored-by: Sima Zhu <sizhu@microsoft.com>

* Reorganization of samples readme (#1505)

* Completion of Sample Readme update (#1508)

* Add Invoke-DevOpsAPI.ps1, Add functions for Canceling and Listing Builds (#1474)

Co-authored-by: Chidozie Ononiwu <chononiw@microsoft.com>

* Change live test resource DeleteAfterHours tag to 8 hours (#1509)

Co-authored-by: Ben Broderick Phillips <bebroder@microsoft.com>

* adding notes about security on readme file (#1498)

* adding notes about security on readme file

* Update sdk/samples/iot/docs/how_to_iot_hub_esp8266_nodemcu.md

Co-authored-by: Mollie Munoz <mollie.munoz@microsoft.com>

* Update sdk/samples/iot/docs/how_to_iot_hub_esp8266_nodemcu.md

Co-authored-by: Ahson Khan <ahson_ahmedk@yahoo.com>

* Update sdk/samples/iot/docs/how_to_iot_hub_esp8266_nodemcu.md

Co-authored-by: Ahson Khan <ahson_ahmedk@yahoo.com>

* Update sdk/samples/iot/docs/how_to_iot_hub_esp8266_nodemcu.md

Co-authored-by: Mollie Munoz <mollie.munoz@microsoft.com>

* Update sdk/samples/iot/docs/how_to_iot_hub_esp8266_nodemcu.md

Co-authored-by: Ahson Khan <ahson_ahmedk@yahoo.com>

* Update sdk/samples/iot/docs/how_to_iot_hub_esp8266_nodemcu.md

Co-authored-by: Mollie Munoz <mollie.munoz@microsoft.com>

* Update how_to_iot_hub_esp8266_nodemcu.md

* Update sdk/samples/iot/docs/how_to_iot_hub_esp8266_nodemcu.md

Co-authored-by: Eric Wolz <ericwol@microsoft.com>

* Update sdk/samples/iot/docs/how_to_iot_hub_esp8266_nodemcu.md

Co-authored-by: Eric Wolz <ericwol@microsoft.com>

* Update sdk/samples/iot/docs/how_to_iot_hub_esp8266_nodemcu.md

Co-authored-by: Eric Wolz <ericwol@microsoft.com>

Co-authored-by: Mollie Munoz <mollie.munoz@microsoft.com>
Co-authored-by: Ahson Khan <ahson_ahmedk@yahoo.com>
Co-authored-by: Eric Wolz <ericwol@microsoft.com>

* Sync eng/common directory with azure-sdk-tools for PR 1202 (#1510)

* Add debugging link on resource deployment failures to log output

* Update aka link for live test help docs. Use here string and empty throw.

Co-authored-by: Ben Broderick Phillips <bebroder@microsoft.com>

* Sync eng/common directory with azure-sdk-tools for PR 1153 (#1513)

* Improve Update-ChangeLog Logic

* Updates to ChangeLog-Operations.ps1, copy-docs-to-blobstorage.ps1, Invoke-GitHubAPI.ps1 and Package-Properties.ps1

* More changeLog Logic Improvements

* Update date parsing

Co-authored-by: Chidozie Ononiwu <chononiw@microsoft.com>

* Revert "Places where we have const* arguments in source, optionally mark the value as const as well. (#1261)" (#1517)

This reverts commit 5723678.

* Update AddAzureSDKforC.cmake to reference the latest tagged release of the SDK (#1495)

* Update AddAzureSDKforC.cmake to reference the latest release of the SDK

* Remove pnp from the tag being referenced.

* Incrementing master CL and version to be above the next release from the feature branch (#1506)

* Update LanguageSetting.ps1 (#1518)

Co-authored-by: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com>
Co-authored-by: Heath Stewart <heaths@microsoft.com>
Co-authored-by: Ben Broderick Phillips <bebroder@microsoft.com>
Co-authored-by: Chidozie Ononiwu <chononiw@microsoft.com>
Co-authored-by: ewertons <ewertons@microsoft.com>
Co-authored-by: Sima Zhu <sizhu@microsoft.com>
Co-authored-by: Mollie Munoz <mollie.munoz@microsoft.com>
Co-authored-by: Wellington Duraes <wellington.duraes@gmail.com>
Co-authored-by: Ahson Khan <ahson_ahmedk@yahoo.com>
Co-authored-by: Eric Wolz <ericwol@microsoft.com>
Co-authored-by: Chidozie Ononiwu <31145988+chidozieononiwu@users.noreply.github.com>
This pull request was closed.
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.

4 participants