[cfe-users] inconsistent compilation error inside constexpr if
Hello, In the below code the compiler throws "undeclared identifier" when the commented line is uncommented. Whereas the line just before compiles fine. Regards, Manu typedef bool (* DummyFunc) (); bool ExecDummy (DummyFunc fptr) { if (fptr) return fptr (); return false; } constexpr unsigned int IsMyAppClient = 0; constexpr bool CheckForTypeClient (unsigned int pAppType) { return ((pAppType & IsMyAppClient) != 0); } class MyAppTemplate { public: template staticboolMyAppInit (); }; template bool MyAppTemplate::MyAppInit () { if constexpr (CheckForTypeClient(T)) { return ClientMain ();// no error //return ExecDummy(ClientMain);// error: use of undeclared identifier 'ClientMain' } return false; } int __cdecl main (int pArgc, char* pArgv[]) { constexpr int TVal = 3; MyAppTemplate::MyAppInit (); return 0; } ___ cfe-users mailing list cfe-users@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
Re: [cfe-users] inconsistent compilation error inside constexpr if
Hi David, Thanks for the reply. I missed to mention the environment and what i am trying to do. 1. Environment is ClangCL (version 10.0), Microsoft Visual Studio. 2. I assumed that since the 'constexpr if' evaluates to false, the external function's presence would not be checked. So, if my application is not of type 'client' ClientMain would not be called. My actual code inside the constexpr if is the commented line of code that does not compile. To experiment i simply invoked ClientMain directly which compiled fine. Here is the assembly generated from the successful compilation (with the first line inside 'constexpr if' enabled). "??$MyAppInit@$02@MyAppTemplate@@SQ_NXZ": # @"??$MyAppInit@$02@MyAppTemplate@@SQ_NXZ" .Lfunc_begin2: .cv_func_id 2 .cv_loc 2 1 32 0# Main.cpp:32:0 # %bb.0: .cv_loc 2 1 39 0# Main.cpp:39:0 xor eax, eax # kill: def $al killed $al killed $eax .Ltmp6: ret .Ltmp7: .Lfunc_end2: Here is how the compiler is invoked: E:\LLVM\bin\clang-cl.exe /c /Z7 /nologo /W3 /WX- /diagnostics:column /Od /D _DEBUG /D _CONSOLE /D _UNICODE /D UNICODE /EHsc /MTd /GS /fp:precise /permissive- /std:c++17 /FA /Fa"x64\Debug\\" /Fo"x64\Debug\\" /Gv /TP -m64 Main.cpp 1> Done executing task "CL". Is my understanding of 'constexpr if' wrong ? won't the body get discarded in case it gets evaluated to false? Regards, Manu From: David Blaikie Sent: Saturday, August 22, 2020 2:14 AM To: Manu agarwal Cc: cfe-users@lists.llvm.org Subject: Re: [cfe-users] inconsistent compilation error inside constexpr if >From what I could test on godbolt, using LLVM evrsions back to 5.0, Clang does reject the "return ClientMain();" call you aren't seeing an error on. So I'm not sure what compiler/version/situation you're running, but at least at first blush it doesn't look like clang. On Fri, Aug 21, 2020 at 1:29 PM Manu agarwal via cfe-users wrote: > > Hello, > > In the below code the compiler throws "undeclared identifier" when the > commented line is uncommented. Whereas the line just before compiles fine. > > Regards, > Manu > > typedef bool (* DummyFunc) (); > > > bool ExecDummy (DummyFunc fptr) { > > if (fptr) > return fptr (); > > return false; > } > > constexpr unsigned int IsMyAppClient = 0; > > constexpr bool CheckForTypeClient (unsigned int pAppType) > { > return ((pAppType & IsMyAppClient) != 0); > } > > > class MyAppTemplate > { > public: > template > staticboolMyAppInit (); > }; > > template > bool > MyAppTemplate::MyAppInit () > { > if constexpr (CheckForTypeClient(T)) { > > return ClientMain ();// no error > //return ExecDummy(ClientMain);// error: use of > undeclared identifier 'ClientMain' > } > > return false; > } > > int __cdecl > main (int pArgc, char* pArgv[]) > { > constexpr int TVal = 3; > > MyAppTemplate::MyAppInit (); > > return 0; > } > > > ___ > cfe-users mailing list > cfe-users@lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users ___ cfe-users mailing list cfe-users@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
Re: [cfe-users] inconsistent compilation error inside constexpr if
Thanks Richard and David for your replies. That was helpful. Regards, Manu From: Richard Smith Sent: Sunday, August 23, 2020 11:56 PM To: Manu agarwal Cc: cfe-users@lists.llvm.org Subject: Re: [cfe-users] inconsistent compilation error inside constexpr if On Fri, 21 Aug 2020 at 13:29, Manu agarwal via cfe-users mailto:cfe-users@lists.llvm.org>> wrote: Hello, In the below code the compiler throws "undeclared identifier" when the commented line is uncommented. Whereas the line just before compiles fine. Regards, Manu typedef bool (* DummyFunc) (); bool ExecDummy (DummyFunc fptr) { if (fptr) return fptr (); return false; } constexpr unsigned int IsMyAppClient = 0; constexpr bool CheckForTypeClient (unsigned int pAppType) { return ((pAppType & IsMyAppClient) != 0); } class MyAppTemplate { public: template staticboolMyAppInit (); }; template bool MyAppTemplate::MyAppInit () { if constexpr (CheckForTypeClient(T)) { return ClientMain ();// no error //return ExecDummy(ClientMain);// error: use of undeclared identifier 'ClientMain' } This code is invalid, as David Blaikie explains. However, in MSVC-compatible mode (under -fms-compatibility, which is enabled by default for some Windows-targeting modes), Clang attempts to to accept certain invalid code that MSVC has historically accepted, including this case, where we allow the call to ClientMain() to find declarations of a ClientMain function that appear after the template definition. That recovery from invalid code is only done for certain syntactic patterns, such as unqualified function calls -- so it applies to ClientMain() but not to (ClientMain). return false; } int __cdecl main (int pArgc, char* pArgv[]) { constexpr int TVal = 3; MyAppTemplate::MyAppInit (); return 0; } ___ cfe-users mailing list cfe-users@lists.llvm.org<mailto:cfe-users@lists.llvm.org> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users ___ cfe-users mailing list cfe-users@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users
[cfe-users] -Winvalid-noreturn warning
Hi, We have the below code where this warning gets generated. #include [[noreturn]] inline void abc () noexcept { std::terminate (); } using MyNoReturn = void (*) () noexcept; //using MyNoReturn = std::add_pointer_t;// even this declaration, instead of the above, gives the same warning [[noreturn]] inline void IDontReturn () noexcept { std::terminate (); } static MyNoReturn gvar {IDontReturn}; [[noreturn]] inline void CallIDontReturn () noexcept { gvar (); // here is where clang gives warning: function declared 'noreturn' should not return [-Winvalid-noreturn] } We have the used the below command to compile: clang++ -std=c++17 -Wall -o runusing usingst.cpp gcc does not show this warning. Please let us know how we can use 'using' clause to declare function pointer to a function that does not return, so that this warning does not show up. Regards, Manu ___ cfe-users mailing list cfe-users@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users