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

Avoid type collision by renaming callback variable #2688

Merged
merged 1 commit into from Sep 16, 2016

Conversation

artokin
Copy link
Contributor

@artokin artokin commented Sep 13, 2016

Description

Jenkins jobs are failing due build error:

Compile: arm_hal_timer.cpp
[Error] arm_hal_timer.cpp@28,9: reference to 'callback' is ambiguous
[Error] arm_hal_timer.cpp@50,5: reference to 'callback' is ambiguous
[Warning] arm_hal_timer.cpp@19,15: 'callback' defined but not used [-Wunused-variable]
[ERROR] ./mbed-os/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp: In function 'void timer_thread(const void*)':
./mbed-os/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp:28:9: error: reference to 'callback' is ambiguous
         callback();
         ^
./mbed-os/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp:19:15: note: candidates are: void (* callback)()
 static void (*callback)(void);
               ^
In file included from ./mbed-os/rtos/rtos/Thread.h:27:0,
                 from ./mbed-os/rtos/rtos/rtos.h:25,
                 from ./mbed-os/hal/api/mbed.h:22,
                 from ./mbed-os/features/FEATURE_COMMON_PAL/nanostack-hal-mbed-cmsis-rtos/arm_hal_timer.cpp:9:
./mbed-os/hal/api/Callback.h:3524:33: note:                 template<class T, class R, class A0, class A1, class A2, class A3, class A4> mbed::Callback<R(A0, A1, A2, A3, A4)> mbed::callback(const volatile T*, R (T::*)(A0, A1, A2, A3, A4) const volatile)
 Callback<R(A0, A1, A2, A3, A4)> callback(const volatile T *obj, R (T::*func)(A0, A1, A2, A3, A4) const volatile) {

Fix the error by renaming callback as arm_hal_callback.

Build jobs are failing due build error "arm_hal_timer.cpp:50:5:
error: reference to 'callback' is ambiguous".

Fix the build error by renaming callback to arm_hal_callback to
avoid collision with callback defined in ./mbed-os/hal/api/Callback.h
@artokin
Copy link
Contributor Author

artokin commented Sep 13, 2016

Originally this change was made to ARMmbed/nanostack-hal-mbed-cmsis-rtos#12 but that repository is going to be retired.

@kjbracey-arm would you please check again.

@kjbracey
Copy link
Contributor

I'm fine with it, but can't help but feel that mbed.h adding "callback" into the global namespace is somewhat asking for trouble with a wide range of possible applications and libraries.

(Could there be some way to stop mbed.h doing "using namespace mbed")?

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 13, 2016

@geky @sg- I think this alternative is the easiest to fix the failure (compare to renaming callback in the Callback API, or changing mbed.h file). What do you think?

(Could there be some way to stop mbed.h doing "using namespace mbed")?

I personally also would like to remove using from any header file. That's something for a discussion that should happen soon, most likely be fixed for Q4 release.

@geky
Copy link
Contributor

geky commented Sep 13, 2016

related issue #2653

@pan-
Copy link
Member

pan- commented Sep 13, 2016

Another possible fix would be to take advantages of the C++ namespace rules.

There is two way to do it:

  • When callback is used, indicate explicitly that it is the one from the global namespace by prefixing it with ::
static void timer_thread(const void *)
{
    for (;;) {
        osSignalWait(1, osWaitForever);
        // !!! We don't do our own enter/exit critical - we rely on callback
        // doing it (ns_timer_interrupt_handler does)
        //platform_enter_critical();

        // USE CALLBACK FROM THE GLOBAL NAMESPACE
        ::callback();
        //platform_exit_critical();
    }
}

// Not called while running, fortunately
void platform_timer_set_cb(void (*new_fp)(void))
{
    // USE CALLBACK FROM THE GLOBAL NAMESPACE
    ::callback= new_fp;
}
  • Import only the symbols name needed by this file:
//#include "mbed.h"
// DO NOT import mbed.h, pick only the symbols needed
// Include timeout and timer instead
#include "Timeout.h"
#include "Timer.h"

 #include "platform/arm_hal_timer.h"
 #include "platform/arm_hal_interrupt.h"

// bring useful symbols in the global namespace
using mbed::Timeout;
using mbed::Timer;

@sg-
Copy link
Contributor

sg- commented Sep 14, 2016

I think this can be merged given it was previously but we also have a lot of work to do in general to rid this. More to come on this soon.

@sg-
Copy link
Contributor

sg- commented Sep 14, 2016

/morph test

@mbed-bot
Copy link

Result: SUCCESS

Your command has finished executing! Here's what you wrote!

/morph test

Output

mbed Build Number: 855

All builds and test passed!

@kjbracey
Copy link
Contributor

@pan- Does ::callback work? I thought the using directive had added callback to the global namespace, so that would remain ambiguous.

A name prefixed by the unary scope operator :: (5.1) is looked up in global scope,
in the translation unit where it is used. The name shall be declared in global
namespace scope or shall be a name whose declaration is
visible in global scope because of a using-directive (3.4.3.2).

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 14, 2016

As @pan- highlighted this brings a question if mbed-os files should include mbed header file. I would say no, go with his second proposal, to include only what is required. Thoughts?

@pan-
Copy link
Member

pan- commented Sep 14, 2016

@kjbracey-arm

The standard can be confusing on this point.
Basically a using directive add all namespace enclosed name into the current scope for unqualified lookup while a using declaration add the name specified into the current scope for qualified and unqualified lookup.

The section 7.4 explain this:

A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-
directive appears after the using-directive. During unqualified name lookup (3.4.1), the names appear as if they were
declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace.
[Note: in this context, “contains” means “contains directly or indirectly”. — end note ]

More importantly:

3 A using-directive does not add any members to the declarative region in which it appears.

And finally there is an example of this in the standard:

namespace D {
  int d1 ;
  void f( char );
}

using namespace D;
int d1 ; // OK: no conflict with D::d1

namespace E {
  int e ;
  void f( int );
}
namespace D { // namespace extension
  int d2 ;
  using namespace E;
  void f( int );
}

void f ()
{
  d1++; // error: ambiguous ::d1 or D::d1?
  ::d1++; // OK
  D::d1++; //OK
  d2++; // OK: D::d2
  e++; // OK: E::e
  f(1); // error: ambiguous: D::f(int) or E::f(int)?
  f('a’); // OK: D::f(char)
}

@kjbracey
Copy link
Contributor

kjbracey commented Sep 14, 2016

Right, I didn't know that distinction.

So after using mbed::foo;, ::foo would actually be mbed::foo, so a global definition of foo would be an error.

But after using namespace mbed;, we can also define our own global foo, and disambiguate references to it via ::foo.

@pan-
Copy link
Member

pan- commented Sep 14, 2016

@0xc0170 Explicitly stating which name from another namespace is used is the best way to minimize name collision. In python we all know that we shouldn't write from X import * but it is what mbed.h does with the standard library and mbed namespace. The worse part is that every file including mbed.h became polluted by these using directives.

There is an old but still interesting article from Herb Sutter explaining how to properly use namespaces: http://www.gotw.ca/publications/migrating_to_namespaces.htm .

@kjbracey-arm Correct, that's the big difference between using directive and using declaration. A using declaration add the name in the declarative region of the current scope; using directive doesn't.

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 15, 2016

As @pan- highlighted this brings a question if mbed-os files should include mbed header file. I would say no, go with his second proposal, to include only what is required. Thoughts?

Any update for this PR, ppl happy with it as it is or we follow one of the suggestions @pan- did above?

@kjbracey
Copy link
Contributor

I'm okay with this, as a local change, it's just as reasonable as the suggestions above.

But #2663 seems like it would solve the problem more generally. If that was in, this wouldn't need to be.

@sg-
Copy link
Contributor

sg- commented Sep 15, 2016

Will land this and something that looks like #2663 will also land for mbed OS.

@sg- sg- merged commit e4c8d76 into ARMmbed:master Sep 16, 2016
artokin added a commit to artokin/mbed-os that referenced this pull request Sep 23, 2021
…..225a4af

225a4af Remove files from tests folder
58d2c8f Merge remote-tracking branch 'origin/release_internal' into release_external
921b4b3 Wi-SUN FAN 1.1 dynamic MDR data request enabler
b8722e8 Corrected BR removing of waiting list entry when supplicant is in key storage
0d54d7a Adjust trace levels (ARMmbed#2692)
681d9ea Added reset for pan id and version to BR network start
30d4fb2 Renaming and cleaning ws bootstrap (ARMmbed#2688)
e0da19d Add Wi-SUN host configuration (ARMmbed#2690)
50ecc3d Refactoring Wi-SUN stack (ARMmbed#2686)
9d2386d Renamed operation mode to operating mode.
2f755bc RF config resolver and some refactoring (ARMmbed#2683)
86c6d19 Fixed WS IE PCAP read operation wrong length usage.
cd3a4c2 Config: Remove additional HAVE_WS_ROUTER (ARMmbed#2684)
cdd7f2d Added API for configure supported Phy capability.
a00a3c0 Wi-SUN FAN 1.1 PCAP IE update
2d063d3 Moved State machine and timer functions to own files
edb8bec Corrected system time check function return values
85358a6 Moved Wi-SUN Bootstrap Event handling to separate device handlers
61cbdde MAC to support mode switch on single channel (ARMmbed#2678)
1006d29 Added storing of PAN ID to NVM in BBR
7bf0028 Corrected system time jump detection on BR startup
e60974d Split Wi-SUN bootstrap to device types
a3f3412 MAC data req: API to support mode switch (ARMmbed#2674)
cad5122 Removed automatic network size configuration (ARMmbed#2673)
35d3132 MAC: Callback set to resolve PHY mode ID (ARMmbed#2672)
0c5faca Added support for large system time changes (e.g. due to NTP) (ARMmbed#2670)
c94b306 LFN version and LGTK Hash IE advertisment and learn
8e07511 Use FAN version constant  instead of pure number
a5566b2 Channel Plan 2 validation and FAN 1.0 reject
42dba41 Wi-Sun IE FAN 1.1 update
1d56070 EU channel plan ids (FAN 1.1) supported (ARMmbed#2668)
fc4f41f Add test API empty function
37efc7e Add version 1.1 basic support
e1558fb Implemented mode switch PHR build and parse (ARMmbed#2665)
cbd8a15 Corrected frame counter storing threshold check
37f7ae9 Time configuration distribution using DHCPv6 vendor data
7415bc7 Added checks for Border Router frame counter space exhaustion (ARMmbed#2660)
f1a65ec Mode switch PHY API (ARMmbed#2663)
e54231b Do not check buffer age when virtual RF driver used (ARMmbed#2662)
cc8c7bd arm_network_certificate_chain_set() returns -2 when PANA is disabled
319dd91 Fix dubious semicolon in #define
2ff51ab Remove extra '\n' in traces
19376c8 Simplify array indexes
c808661 Fix ASAN warnings about overflows in bit shifts
f998008 Fix use-after-free in mac_helper_coordinator_address_set()
4d04541 Wi-SUN header and Paylod IE element lenght future proof update.
935898b Medium network PAN_TIMEOUT changed to 30 minutes
1af7cfe Updated nanostack to be compatible with mbed TLS 3.0 (ARMmbed#2657)
29744e0 If Router Solicitation creation fails no longer tries to retry the RS right away (ARMmbed#2655)
2b889e9 Added automatic test procedure triggering during bootstrap
ed9eb05 GTKs are removed only when fresh GTK hash is received
81ecdc2 Added empty function for test procedure trigger
14439b4 Added support for triggering test procedures
b8a67a9 Update CHANGELOG.md for Nanostack 14.0.0 (ARMmbed#2649)

git-subtree-dir: features/nanostack/sal-stack-nanostack
git-subtree-split: 225a4af
artokin added a commit to artokin/mbed-os that referenced this pull request Sep 23, 2021
…..225a4af

225a4af Remove files from tests folder
58d2c8f Merge remote-tracking branch 'origin/release_internal' into release_external
921b4b3 Wi-SUN FAN 1.1 dynamic MDR data request enabler
b8722e8 Corrected BR removing of waiting list entry when supplicant is in key storage
0d54d7a Adjust trace levels (ARMmbed#2692)
681d9ea Added reset for pan id and version to BR network start
30d4fb2 Renaming and cleaning ws bootstrap (ARMmbed#2688)
e0da19d Add Wi-SUN host configuration (ARMmbed#2690)
50ecc3d Refactoring Wi-SUN stack (ARMmbed#2686)
9d2386d Renamed operation mode to operating mode.
2f755bc RF config resolver and some refactoring (ARMmbed#2683)
86c6d19 Fixed WS IE PCAP read operation wrong length usage.
cd3a4c2 Config: Remove additional HAVE_WS_ROUTER (ARMmbed#2684)
cdd7f2d Added API for configure supported Phy capability.
a00a3c0 Wi-SUN FAN 1.1 PCAP IE update
2d063d3 Moved State machine and timer functions to own files
edb8bec Corrected system time check function return values
85358a6 Moved Wi-SUN Bootstrap Event handling to separate device handlers
61cbdde MAC to support mode switch on single channel (ARMmbed#2678)
1006d29 Added storing of PAN ID to NVM in BBR
7bf0028 Corrected system time jump detection on BR startup
e60974d Split Wi-SUN bootstrap to device types
a3f3412 MAC data req: API to support mode switch (ARMmbed#2674)
cad5122 Removed automatic network size configuration (ARMmbed#2673)
35d3132 MAC: Callback set to resolve PHY mode ID (ARMmbed#2672)
0c5faca Added support for large system time changes (e.g. due to NTP) (ARMmbed#2670)
c94b306 LFN version and LGTK Hash IE advertisment and learn
8e07511 Use FAN version constant  instead of pure number
a5566b2 Channel Plan 2 validation and FAN 1.0 reject
42dba41 Wi-Sun IE FAN 1.1 update
1d56070 EU channel plan ids (FAN 1.1) supported (ARMmbed#2668)
fc4f41f Add test API empty function
37efc7e Add version 1.1 basic support
e1558fb Implemented mode switch PHR build and parse (ARMmbed#2665)
cbd8a15 Corrected frame counter storing threshold check
37f7ae9 Time configuration distribution using DHCPv6 vendor data
7415bc7 Added checks for Border Router frame counter space exhaustion (ARMmbed#2660)
f1a65ec Mode switch PHY API (ARMmbed#2663)
e54231b Do not check buffer age when virtual RF driver used (ARMmbed#2662)
cc8c7bd arm_network_certificate_chain_set() returns -2 when PANA is disabled
319dd91 Fix dubious semicolon in #define
2ff51ab Remove extra '\n' in traces
19376c8 Simplify array indexes
c808661 Fix ASAN warnings about overflows in bit shifts
f998008 Fix use-after-free in mac_helper_coordinator_address_set()
4d04541 Wi-SUN header and Paylod IE element lenght future proof update.
935898b Medium network PAN_TIMEOUT changed to 30 minutes
1af7cfe Updated nanostack to be compatible with mbed TLS 3.0 (ARMmbed#2657)
29744e0 If Router Solicitation creation fails no longer tries to retry the RS right away (ARMmbed#2655)
2b889e9 Added automatic test procedure triggering during bootstrap
ed9eb05 GTKs are removed only when fresh GTK hash is received
81ecdc2 Added empty function for test procedure trigger
14439b4 Added support for triggering test procedures
b8a67a9 Update CHANGELOG.md for Nanostack 14.0.0 (ARMmbed#2649)

git-subtree-dir: features/nanostack/sal-stack-nanostack
git-subtree-split: 225a4af
artokin added a commit to artokin/mbed-os that referenced this pull request Sep 23, 2021
…a3c5c5..225a4af

225a4af Remove files from tests folder
58d2c8f Merge remote-tracking branch 'origin/release_internal' into release_external
921b4b3 Wi-SUN FAN 1.1 dynamic MDR data request enabler
b8722e8 Corrected BR removing of waiting list entry when supplicant is in key storage
0d54d7a Adjust trace levels (ARMmbed#2692)
681d9ea Added reset for pan id and version to BR network start
30d4fb2 Renaming and cleaning ws bootstrap (ARMmbed#2688)
e0da19d Add Wi-SUN host configuration (ARMmbed#2690)
50ecc3d Refactoring Wi-SUN stack (ARMmbed#2686)
9d2386d Renamed operation mode to operating mode.
2f755bc RF config resolver and some refactoring (ARMmbed#2683)
86c6d19 Fixed WS IE PCAP read operation wrong length usage.
cd3a4c2 Config: Remove additional HAVE_WS_ROUTER (ARMmbed#2684)
cdd7f2d Added API for configure supported Phy capability.
a00a3c0 Wi-SUN FAN 1.1 PCAP IE update
2d063d3 Moved State machine and timer functions to own files
edb8bec Corrected system time check function return values
85358a6 Moved Wi-SUN Bootstrap Event handling to separate device handlers
61cbdde MAC to support mode switch on single channel (ARMmbed#2678)
1006d29 Added storing of PAN ID to NVM in BBR
7bf0028 Corrected system time jump detection on BR startup
e60974d Split Wi-SUN bootstrap to device types
a3f3412 MAC data req: API to support mode switch (ARMmbed#2674)
cad5122 Removed automatic network size configuration (ARMmbed#2673)
35d3132 MAC: Callback set to resolve PHY mode ID (ARMmbed#2672)
0c5faca Added support for large system time changes (e.g. due to NTP) (ARMmbed#2670)
c94b306 LFN version and LGTK Hash IE advertisment and learn
8e07511 Use FAN version constant  instead of pure number
a5566b2 Channel Plan 2 validation and FAN 1.0 reject
42dba41 Wi-Sun IE FAN 1.1 update
1d56070 EU channel plan ids (FAN 1.1) supported (ARMmbed#2668)
fc4f41f Add test API empty function
37efc7e Add version 1.1 basic support
e1558fb Implemented mode switch PHR build and parse (ARMmbed#2665)
cbd8a15 Corrected frame counter storing threshold check
37f7ae9 Time configuration distribution using DHCPv6 vendor data
7415bc7 Added checks for Border Router frame counter space exhaustion (ARMmbed#2660)
f1a65ec Mode switch PHY API (ARMmbed#2663)
e54231b Do not check buffer age when virtual RF driver used (ARMmbed#2662)
cc8c7bd arm_network_certificate_chain_set() returns -2 when PANA is disabled
319dd91 Fix dubious semicolon in #define
2ff51ab Remove extra '\n' in traces
19376c8 Simplify array indexes
c808661 Fix ASAN warnings about overflows in bit shifts
f998008 Fix use-after-free in mac_helper_coordinator_address_set()
4d04541 Wi-SUN header and Paylod IE element lenght future proof update.
935898b Medium network PAN_TIMEOUT changed to 30 minutes
1af7cfe Updated nanostack to be compatible with mbed TLS 3.0 (ARMmbed#2657)
29744e0 If Router Solicitation creation fails no longer tries to retry the RS right away (ARMmbed#2655)
2b889e9 Added automatic test procedure triggering during bootstrap
ed9eb05 GTKs are removed only when fresh GTK hash is received
81ecdc2 Added empty function for test procedure trigger
14439b4 Added support for triggering test procedures
b8a67a9 Update CHANGELOG.md for Nanostack 14.0.0 (ARMmbed#2649)

git-subtree-dir: connectivity/nanostack/sal-stack-nanostack
git-subtree-split: 225a4af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants