Skip to content

Commit 25a694c

Browse files
Parse IDL file on DynamicTypeBuilderFactory::create_type_w_uri (#4943)
* Implement idl parser (in progress) Signed-off-by: Yangbo Long <[email protected]> * Adapt literals and expressions parsing to new xtype APIs Signed-off-by: Yangbo Long <[email protected]> * Compile code in Windows Signed-off-by: Yangbo Long <[email protected]> * Fix alias parsing Signed-off-by: Yangbo Long <[email protected]> * Fix code to run through example in Linux Signed-off-by: Yangbo Long <[email protected]> * Fix format and compile warnings Signed-off-by: Yangbo Long <[email protected]> * Fix type for parsing integer literals Signed-off-by: Yangbo Long <[email protected]> * Fix union default label parsing Signed-off-by: Yangbo Long <[email protected]> * Support create_type_w_document Signed-off-by: Yangbo Long <[email protected]> * Remove example Signed-off-by: Yangbo Long <[email protected]> * Capture exceptions before return to user code Signed-off-by: Yangbo Long <[email protected]> * Implement get_temporary_file Signed-off-by: Yangbo Long <[email protected]> * Build with EPROSIMA_BUILD_TESTS=ON Signed-off-by: Yangbo Long <[email protected]> * Add unit tests Signed-off-by: Yangbo Long <[email protected]> * Build and run tests on Linux Signed-off-by: Yangbo Long <[email protected]> * Parse basic_inner_types.idl Signed-off-by: Yangbo Long <[email protected]> * Parse structures.idl (in progress) Signed-off-by: Yangbo Long <[email protected]> * Fix struct member type/name parsing and const parsing Signed-off-by: Yangbo Long <[email protected]> * Fix struct parsing when struct members have arrays Example input: struct StructMixedArray { short var_short; int32 var_array[10]; float var_multi_array[5][15]; }; Signed-off-by: Yangbo Long <[email protected]> * Skip sequence and map parsing Signed-off-by: Yangbo Long <[email protected]> * Test structures.idl with proper comments Signed-off-by: Yangbo Long <[email protected]> * Test alases.idl Signed-off-by: Yangbo Long <[email protected]> * Try fixing ci/cd Signed-off-by: Yangbo Long <[email protected]> * Fix struct parsing with member being array Handle the case when array size being an identifier instead of integer. For example: const long size_1 = 5; struct ArraySingleDimensionLiteralsShort { short var_array_short[size_1]; }; Signed-off-by: Yangbo Long <[email protected]> * Allow positive_int_const to be const_expr Signed-off-by: Yangbo Long <[email protected]> * Parse arrays.idl (in progress) Signed-off-by: Yangbo Long <[email protected]> * Handle scoped names in arithmetic expressions Signed-off-by: Yangbo Long <[email protected]> * Finish arrays.idl testing Signed-off-by: Yangbo Long <[email protected]> * Test relative_path_include.idl Signed-off-by: Yangbo Long <[email protected]> * Start to test unions.idl Signed-off-by: Yangbo Long <[email protected]> * Fix union case label parsing Signed-off-by: Yangbo Long <[email protected]> * Finish unions.idl testing Signed-off-by: Yangbo Long <[email protected]> * Fix internal error spews Signed-off-by: Yangbo Long <[email protected]> * Try fixing ci/cd errors Signed-off-by: Yangbo Long <[email protected]> * Address PR comments Signed-off-by: Yangbo Long <[email protected]> * Escape separator in CMake path variable Signed-off-by: Miguel Company <[email protected]> * Add API to set preprocessor path. Signed-off-by: Miguel Company <[email protected]> * Split preprocessor and flags. Signed-off-by: Miguel Company <[email protected]> * Pass preprocessor to IDL parser. Signed-off-by: Miguel Company <[email protected]> * Set preprocessor on tests Signed-off-by: Miguel Company <[email protected]> * Move idl parser implementation to create_type_w_uri Signed-off-by: Yangbo Long <[email protected]> --------- Signed-off-by: Yangbo Long <[email protected]> Signed-off-by: Miguel Company <[email protected]> Co-authored-by: Miguel Company <[email protected]>
1 parent 165d64e commit 25a694c

File tree

20 files changed

+5404
-8
lines changed

20 files changed

+5404
-8
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ if(MSVC OR MSVC_IDE)
6262
# C4715: 'Test': not all control paths return a value
6363
# C5038 data member 'member1' will be initialized after data member 'member2'
6464
# C4100 'identifier' : unreferenced formal parameter (matches clang -Wunused-lambda-capture)
65-
add_compile_options(/w34101 /w34189 /w34555 /w34715 /w35038 /w44100)
65+
add_compile_options(/w34101 /w34189 /w34555 /w34715 /w35038 /w44100 /bigobj)
6666

6767
if(EPROSIMA_BUILD)
6868
string(REPLACE "/DNDEBUG" "" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")

include/fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilderFactory.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,15 @@ class DynamicTypeBuilderFactory : public std::enable_shared_from_this<DynamicTyp
145145
FASTDDS_EXPORTED_API virtual traits<DynamicTypeBuilder>::ref_type create_bitmask_type(
146146
uint32_t bound) = 0;
147147

148+
/*!
149+
* Sets the path to the preprocessor executable to be used when parsing type descriptions.
150+
* @param [in] preprocessor path to the preprocessor executable.
151+
*/
152+
FASTDDS_EXPORTED_API virtual void set_preprocessor(
153+
const std::string& preprocessor) = 0;
154+
148155
/*!
149156
* Creates a new @ref DynamicTypeBuilder reference by parsing the type description at the given URL.
150-
* @remark Not implemented yet.
151157
* @param [in] document_url pointing to the url containing the type description.
152158
* @param [in] type_name Fully qualified name of the type to be loaded from the document.
153159
* @param [in] include_paths A collection of URLs to directories to be searched for additional type description

src/cpp/fastdds/xtypes/dynamic_types/DynamicTypeBuilderFactoryImpl.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,24 @@
3131
#include <fastdds/xtypes/type_representation/TypeObjectRegistry.hpp>
3232
#include <rtps/RTPSDomainImpl.hpp>
3333

34+
#include "idl_parser/Idl.hpp"
35+
3436
namespace eprosima {
3537
namespace fastdds {
3638
namespace dds {
3739

3840
traits<DynamicTypeBuilderFactoryImpl>::ref_type DynamicTypeBuilderFactoryImpl::instance_;
3941

42+
//{{{ Utility functions
43+
44+
void DynamicTypeBuilderFactoryImpl::set_preprocessor(
45+
const std::string& preprocessor)
46+
{
47+
preprocessor_ = preprocessor;
48+
}
49+
50+
//}}}
51+
4052
//{{{ Functions to create types
4153

4254
traits<DynamicTypeBuilder>::ref_type DynamicTypeBuilderFactoryImpl::create_type(
@@ -175,11 +187,20 @@ traits<DynamicTypeBuilder>::ref_type DynamicTypeBuilderFactoryImpl::create_type_
175187
const std::string& type_name,
176188
const IncludePathSeq& include_paths) noexcept
177189
{
178-
traits<DynamicTypeBuilder>::ref_type nil;
179-
static_cast<void>(document_url);
180-
static_cast<void>(type_name);
181-
static_cast<void>(include_paths);
182-
return nil;
190+
traits<DynamicTypeBuilder>::ref_type ret_val;
191+
192+
try
193+
{
194+
idlparser::Context context = idlparser::parse_file(document_url, type_name, include_paths, preprocessor_);
195+
ret_val = context.builder;
196+
}
197+
catch (const std::exception& e)
198+
{
199+
EPROSIMA_LOG_ERROR(IDLPARSER, e.what());
200+
ret_val.reset();
201+
}
202+
203+
return ret_val;
183204
}
184205

185206
//}}}

src/cpp/fastdds/xtypes/dynamic_types/DynamicTypeBuilderFactoryImpl.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class DynamicTypeBuilderFactoryImpl : public traits<DynamicTypeBuilderFactory>::
4141
{
4242
public:
4343

44+
//{{{ Utility functions
45+
46+
void set_preprocessor(
47+
const std::string& preprocessor) override;
48+
49+
//}}}
50+
4451
//{{{ Functions to create types
4552

4653
traits<DynamicTypeBuilder>::ref_type create_type(
@@ -197,6 +204,9 @@ class DynamicTypeBuilderFactoryImpl : public traits<DynamicTypeBuilderFactory>::
197204
traits<DynamicTypeImpl>::ref_type char16_type_ {std::make_shared<DynamicTypeImpl>(TypeDescriptorImpl{TK_CHAR16,
198205
""})};
199206
//}}}
207+
208+
//! Path to the preprocessor executable to be used when parsing type descriptions.
209+
std::string preprocessor_{};
200210
};
201211

202212
} // namespace dds
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef FASTDDS_XTYPES_DYNAMIC_TYPES_IDL_PARSER_IDL_HPP
16+
#define FASTDDS_XTYPES_DYNAMIC_TYPES_IDL_PARSER_IDL_HPP
17+
18+
#include "IdlParser.hpp"
19+
20+
#include <sstream>
21+
22+
namespace eprosima {
23+
namespace fastdds {
24+
namespace dds {
25+
namespace idlparser {
26+
27+
/// \brief Parse IDL string input, conforming to IDL specification V4.2.
28+
/// \param[in] idl An IDL string to parse into DynamicTypes
29+
/// \return A Context object with data related to the parse output
30+
inline Context parse(
31+
const std::string& idl)
32+
{
33+
return Parser::instance()->parse_string(idl);
34+
}
35+
36+
/// \brief Parse IDL string input with an existent context, conforming to IDL specification V4.2.
37+
/// \param[in] idl An IDL string to parse into DynamicTypes
38+
/// \param[in] context An existent Context object
39+
/// \return A Context object with data related to the parse output
40+
inline Context& parse(
41+
const std::string& idl,
42+
Context& context)
43+
{
44+
Parser::instance()->parse_string(idl, context);
45+
return context;
46+
}
47+
48+
/// \brief Parse IDL file input, conforming to IDL specification V4.2.
49+
/// \param[in] idl_file Path to the IDL file
50+
/// \return A Context object with data related to the parse output
51+
inline Context parse_file(
52+
const std::string& idl_file)
53+
{
54+
return Parser::instance()->parse_file(idl_file);
55+
}
56+
57+
/// \brief Parse IDL file input with an existent context, conforming to IDL specification V4.2.
58+
/// \param[in] idl_file Path to the IDL file
59+
/// \param[in] context An existent Context object
60+
/// \return A Context object with data related to the parse output
61+
inline Context& parse_file(
62+
const std::string& idl_file,
63+
Context& context)
64+
{
65+
Parser::instance()->parse_file(idl_file, context);
66+
return context;
67+
}
68+
69+
/// \brief Parse IDL file input and save the DynamicTypeBuilder object corresponding to the given target type name,
70+
/// conforming to IDL specification V4.2.
71+
/// \param[in] idl_file Path to the IDL file
72+
/// \param[in] type_name Fully qualified target type name to load from the IDL file
73+
/// \param[in] include_paths A collection of directories to search for additional type description
74+
/// \return A Context object with data related to the parse output
75+
inline Context parse_file(
76+
const std::string& idl_file,
77+
const std::string& type_name,
78+
const IncludePathSeq& include_paths,
79+
const std::string& preprocessor)
80+
{
81+
return Parser::instance()->parse_file(idl_file, type_name, include_paths, preprocessor);
82+
}
83+
84+
/// \brief Preprocess IDL file.
85+
/// \param[in] idl_file Path to the IDL file
86+
/// \param[in] includes A collection of directories to search for additional type description
87+
/// \return Preprocessed IDL string
88+
inline std::string preprocess(
89+
const std::string& idl_file,
90+
const std::vector<std::string>& includes)
91+
{
92+
return Parser::preprocess(idl_file, includes);
93+
}
94+
95+
} //namespace idlparser
96+
} //namespace dds
97+
} //namespace fastdds
98+
} //namespace eprosima
99+
100+
#endif //FASTDDS_XTYPES_DYNAMIC_TYPES_IDL_PARSER_IDL_HPP

0 commit comments

Comments
 (0)