From marek.krzynowek at rbccm.com Fri Oct 1 07:43:57 2021 From: marek.krzynowek at rbccm.com (Krzynowek, Marek (CWM-NR)) Date: Fri Oct 1 07:42:25 2021 Subject: [xsd-users] Linker problem caused by using std::ifstream/std::ofstream/std::fstream together with xsd::cxx::tree::string In-Reply-To: References: <93498d8e048942c49fa49367c347394c@SXLOE207.fg.rbc.com> Message-ID: <9284ca73fb514b889117d323cc71f468@SXLOE207.fg.rbc.com> Hi, >From Piotr Martynowicz who is helping me with the C++ side of the update: In our solution we have a DLL project, and in that project we have a file, let's call it utils.cpp, which includes codesynthesis/base_xml_schema/base/xml_schema.hxx and defines a constructor of class X in which it creates an std::ifstream instance: X::X(std::string& filePath) { std::ifstream inFile; // read from file } xml_schema.hxx starts with a comment: // Copyright (c) 2005-2014 Code Synthesis Tools CC // // This program was generated by CodeSynthesis XSD, an XML Schema to // C++ data binding compiler. and at the end it contains lines: namespace xsd { namespace cxx { namespace tree { template class XML_SCHEMA_API simple_type< char, ::xml_schema::Type >; template class XML_SCHEMA_API string< char, ::xml_schema::SimpleType >; template class XML_SCHEMA_API normalized_string< char, ::xml_schema::String >; template class XML_SCHEMA_API token< char, ::xml_schema::NormalizedString >; template class XML_SCHEMA_API name< char, ::xml_schema::Token >; template class XML_SCHEMA_API nmtoken< char, ::xml_schema::Token >; ... } } } This is where the quoted line came from. XML_SCHEMA_API is defined in codesynthesis/base_xml_schema/export.hxx: #ifndef HAVE_XML_SCHEMA_EXPORT_HXX #define HAVE_XML_SCHEMA_EXPORT_HXX #ifdef _MSC_VER #if defined( BASE_XML_EXPORTS ) # define XML_SCHEMA_API __declspec(dllexport) // Magic trick to work around npos export VC bug. #if _MSC_VER >= 1600 #ifdef _M_IX86 #pragma comment(linker, "/include:_magic_magic_magic") #else #pragma comment(linker, "/include:magic_magic_magic") #endif // x86 #endif #else # define XML_SCHEMA_API __declspec(dllimport) #endif // msc compiler #else #define XML_SCHEMA_API #endif #endif // HAVE_XML_SCHEMA_EXPORT_HXX The linker issue arises when our utils.cpp includes xml_schema.hxx AND creates an instance of either std::ifstream or std::ofstream or std::fstream. More specifically, it looks like it has something to do with xsd::cxx::tree::string inheriting from std::basic_string. I'm not sure but it looks like xml_schema.hxx is either your code or code generated by your software (as the comment suggests). But let us know if that's not the case. The issue didn't appear in Visual Studio 16.6 but it appeared after updating to 16.11.3. Best regards, Marek -----Original Message----- From: Boris Kolpackov Sent: 29 September 2021 15:49 To: Krzynowek, Marek (CWM-NR) Cc: xsd-users@codesynthesis.com Subject: Re: [xsd-users] Linker problem caused by using std::ifstream/std::ofstream/std::fstream together with xsd::cxx::tree::string [External] Krzynowek, Marek (CWM-NR) writes: > template class __declspec(dllimport) xsd::cxx::tree::string SimpleType>; Can you elaborate on how you end up with this situation in your project (I have my suspicions but it would be helpful to clarify this). ______________________________________________________________________ This email is intended only for the use of the individual(s) to whom it is addressed and may be privileged and confidential. Unauthorised use or disclosure is prohibited. If you receive this e-mail in error, please advise immediately and delete the original message. This message may have been altered without your or our knowledge and the sender does not accept any liability for any errors or omissions in the message. Emails are monitored by supervisory personnel in jurisdictions where monitoring is permitted. Such communications are retained and may be produced to regulatory authorities or others with legal rights to the information. Please see link for RBCCM disclosures. https://www.rbccm.com/rbccm/policies-disclaimers.page From boris at codesynthesis.com Tue Oct 5 10:26:56 2021 From: boris at codesynthesis.com (Boris Kolpackov) Date: Tue Oct 5 10:25:08 2021 Subject: [xsd-users] Linker problem caused by using std::ifstream/std::ofstream/std::fstream together with xsd::cxx::tree::string In-Reply-To: <9284ca73fb514b889117d323cc71f468@SXLOE207.fg.rbc.com> References: <93498d8e048942c49fa49367c347394c@SXLOE207.fg.rbc.com> <9284ca73fb514b889117d323cc71f468@SXLOE207.fg.rbc.com> Message-ID: Krzynowek, Marek (CWM-NR) writes: > In our solution we have a DLL project, and in that project we have a file, > let's call it utils.cpp, which includes xml_schema.hxx [...] > > xml_schema.hxx starts with a comment: > // Copyright (c) 2005-2014 Code Synthesis Tools CC > // > // This program was generated by CodeSynthesis XSD, an XML Schema to > // C++ data binding compiler. > > and at the end it contains lines: > namespace xsd > { > namespace cxx > { > namespace tree > { > template class XML_SCHEMA_API simple_type< char, ::xml_schema::Type >; > template class XML_SCHEMA_API string< char, ::xml_schema::SimpleType >; > template class XML_SCHEMA_API normalized_string< char, ::xml_schema::String >; > template class XML_SCHEMA_API token< char, ::xml_schema::NormalizedString >; > template class XML_SCHEMA_API name< char, ::xml_schema::Token >; > template class XML_SCHEMA_API nmtoken< char, ::xml_schema::Token >; > ... > } > } > } > > #ifdef _MSC_VER > #if defined( BASE_XML_EXPORTS ) > # define XML_SCHEMA_API __declspec(dllexport) > [...] > #else > # define XML_SCHEMA_API __declspec(dllimport) > #endif // msc compiler > #else > #define XML_SCHEMA_API > #endif > > The linker issue arises when our utils.cpp includes xml_schema.hxx AND > creates an instance of either std::ifstream or std::ofstream or > std::fstream. More specifically, it looks like it has something to > do with xsd::cxx::tree::string inheriting from std::basic_string. Ok, let me describe the underlying problem so that we hopefully have an idea about what's going on here: 1. XSD built-in types (e.g., xml_schema::string) derive from std::string (this is done so that you can "seamlessly" pass xml_schema::string where std::string is expected). 2. In VC++, when you export a class (xml_schema::string in our case, caused by the --export-xml-schema option), it automatically exports the base class. So in our case std::string (aka std::basic_string) gets exported as well. There doesn't seem to be any way to disable exporting of or "unexport" a base class in VC++. 3. When you build your executable (or another DLL), there could be some source files that include "xml-schema.hxx" and therefore "see" std::string as imported from the DLL. In this case, you need to make sure to link the DLL where xml-schema.hxx exports its symbols (i.e., where XML_SCHEMA_API is defined as __declspec(dllexport)). Here are some previous post on the same topic, as a reference: http://www.codesynthesis.com/pipermail/xsd-users/2010-September/003011.html https://www.codesynthesis.com/pipermail/xsd-users/2010-September/003019.html From Erik.Tempelaar at vanoord.com Tue Oct 12 05:17:31 2021 From: Erik.Tempelaar at vanoord.com (Tempelaar E. (Erik)) Date: Tue Oct 12 09:30:13 2021 Subject: [xsd-users] Howto avoid slicing with xsd-types to std::string Message-ID: Hello, Our codebase gets analyzed by sonarcloud and recently they upgraded the analyzer to warn about slicing. As a result we get warnings for code that interacts with xsd-generated types with assignments where the resulting type is simply a std::string. (xsd:simpleType with xsd::restriction base="xsd:string") https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es63-dont-slice The guidelines suggest to define an explicit operation to do so, but I could not find it. Any suggestions? Thanks in advance. Erik Disclaimer: This mail transmission and any attached files are confidential and are intended for the addressee only. If you are not the person or organization to whom it is addressed, you must not copy, disclose, distribute or take any action in reliance upon it. If you have received this message in error, please contact the sender by email and delete all copies of this message and all copies of any attached files. From boris at codesynthesis.com Tue Oct 12 09:34:56 2021 From: boris at codesynthesis.com (Boris Kolpackov) Date: Tue Oct 12 09:33:06 2021 Subject: [xsd-users] Howto avoid slicing with xsd-types to std::string In-Reply-To: References: Message-ID: Tempelaar E. (Erik) writes: > As a result we get warnings for code that interacts with xsd-generated > types with assignments where the resulting type is simply a std::string. Can you show an example of the code that produces such a warning and the warning diagnostics that you are getting for it? From Erik.Tempelaar at vanoord.com Wed Oct 13 01:55:06 2021 From: Erik.Tempelaar at vanoord.com (Tempelaar E. (Erik)) Date: Thu Oct 14 12:53:04 2021 Subject: [xsd-users] Howto avoid slicing with xsd-types to std::string In-Reply-To: References: Message-ID: Thank you for getting back to me so soon. Here's the exact message for the code below: " Use pointer or reference to avoid slicing from "string>" to "basic_string" if (cfg->Configuration().ProjectFolder().present()) { m_projectFolder = cfg->Configuration().ProjectFolder().get(); with: m_projectFolder: std::string m_projectFolder{""}; But there's others like this one (not only the optionals), i.e.: "Use pointer or reference to avoid slicing from "Title_t" to "basic_string" Best regards, Erik -----Original Message----- From: Boris Kolpackov Sent: 12 October 2021 15:35 To: Tempelaar E. (Erik) Cc: xsd-users@codesynthesis.com Subject: Re: [xsd-users] Howto avoid slicing with xsd-types to std::string Tempelaar E. (Erik) writes: > As a result we get warnings for code that interacts with xsd-generated > types with assignments where the resulting type is simply a std::string. Can you show an example of the code that produces such a warning and the warning diagnostics that you are getting for it? Disclaimer: This mail transmission and any attached files are confidential and are intended for the addressee only. If you are not the person or organization to whom it is addressed, you must not copy, disclose, distribute or take any action in reliance upon it. If you have received this message in error, please contact the sender by email and delete all copies of this message and all copies of any attached files. From boris at codesynthesis.com Thu Oct 14 13:09:30 2021 From: boris at codesynthesis.com (Boris Kolpackov) Date: Thu Oct 14 13:07:41 2021 Subject: [xsd-users] Howto avoid slicing with xsd-types to std::string In-Reply-To: References: Message-ID: Tempelaar E. (Erik) writes: > " Use pointer or reference to avoid slicing from > "string>" > to "basic_string" > > if (cfg->Configuration().ProjectFolder().present()) { > > m_projectFolder = cfg->Configuration().ProjectFolder().get(); > > > with: m_projectFolder: > std::string m_projectFolder{""}; > > > What happens here is xsd:string XML Schema type is mapped to a C++ type that derives from std::string (it also contains some other things to implement the XML Schema builtin type hierarchy). And what you are doing here seems pretty normal to me: you copy the string part to a separate data member. I suspect you can suppress this warning by explicitly casting to std::string&, though that's admittedly quite ugly: m_projectFolder = static_cast( cfg->Configuration().ProjectFolder().get()); Another thing that may make sense to look into is if there is an ability in your static analyzer to disable this warning for a specific. From Erik.Tempelaar at vanoord.com Fri Oct 22 02:49:03 2021 From: Erik.Tempelaar at vanoord.com (Tempelaar E. (Erik)) Date: Fri Oct 22 03:13:32 2021 Subject: [xsd-users] Howto avoid slicing with xsd-types to std::string In-Reply-To: References: Message-ID: Thanks for looking into it. I figured this much, but I was hoping there was a library-call that explicitly returns the std::string that I had missed. Good to know it is pretty normal. I was able to resolved the issues as "won't fix" and I feel like that's the best solution here. Thanks again. -----Original Message----- From: Boris Kolpackov Sent: 14 October 2021 19:10 To: Tempelaar E. (Erik) Cc: xsd-users@codesynthesis.com Subject: Re: [xsd-users] Howto avoid slicing with xsd-types to std::string Tempelaar E. (Erik) writes: > " Use pointer or reference to avoid slicing from "string xsd::cxx::tree::simple_type>" > to "basic_string" > > if (cfg->Configuration().ProjectFolder().present()) { > > m_projectFolder = cfg->Configuration().ProjectFolder().get(); > > > with: m_projectFolder: > std::string m_projectFolder{""}; > > > What happens here is xsd:string XML Schema type is mapped to a C++ type that derives from std::string (it also contains some other things to implement the XML Schema builtin type hierarchy). And what you are doing here seems pretty normal to me: you copy the string part to a separate data member. I suspect you can suppress this warning by explicitly casting to std::string&, though that's admittedly quite ugly: m_projectFolder = static_cast( cfg->Configuration().ProjectFolder().get()); Another thing that may make sense to look into is if there is an ability in your static analyzer to disable this warning for a specific. Disclaimer: This mail transmission and any attached files are confidential and are intended for the addressee only. If you are not the person or organization to whom it is addressed, you must not copy, disclose, distribute or take any action in reliance upon it. If you have received this message in error, please contact the sender by email and delete all copies of this message and all copies of any attached files. From boris at codesynthesis.com Tue Oct 26 06:04:12 2021 From: boris at codesynthesis.com (Boris Kolpackov) Date: Tue Oct 26 06:02:13 2021 Subject: [xsd-users] Howto avoid slicing with xsd-types to std::string In-Reply-To: References: Message-ID: <20211026100412.GA6702@codesynthesis.com> Tempelaar E. (Erik) writes: > I figured this much, but I was hoping there was a library-call that > explicitly returns the std::string that I had missed. I've added[1] the base_string() function to xml_schema::{string,uri} classes for the next release. [1] https://git.codesynthesis.com/cgit/xsd/xsd/commit/?id=06e68339a849