Skip to content

Commit

Permalink
Add required headers to generated code
Browse files Browse the repository at this point in the history
* Include cstdint header if integer types or octets are used
* Include array header for anonymous arrays
* Include string header if string constants are used
* Include dds/topic/TopicTraits.hpp from generated header
* Include org/eclipse/cyclonedds/topic/datatopic.hpp from generated header

This fixes #108.
This fixes #116.

Signed-off-by: Jeroen Koekkoek <jeroen@koekkoek.nl>
  • Loading branch information
k0ekk0ek committed Jul 1, 2021
1 parent d5d2bff commit ca478d5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <vector>

#include <org/eclipse/cyclonedds/topic/DataRepresentation.hpp>
#include "org/eclipse/cyclonedds/topic/DataRepresentation.hpp"

struct ddsi_sertype;

Expand Down
20 changes: 12 additions & 8 deletions src/idlcxx/src/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ register_types(
if (src && strcmp(loc->first.source->path->name, src) != 0)
return IDL_VISIT_DONT_RECURSE;

if (idl_is_array(type_spec))
if (idl_is_array(node) || idl_is_array(type_spec))
gen->uses_array = true;

type_spec = idl_unalias(idl_type_spec(node), IDL_UNALIAS_IGNORE_ARRAY);
Expand All @@ -601,6 +601,10 @@ register_types(
gen->uses_string = true;
} else if (idl_is_union(type_spec)) {
gen->uses_union = true;
} else if (idl_is_integer_type(type_spec)) {
gen->uses_integers = true;
} else if (idl_type(type_spec) == IDL_OCTET) {
gen->uses_integers = true;
}

/* FIXME: add support for @optional */
Expand All @@ -619,9 +623,10 @@ generate_includes(const idl_pstate_t *pstate, struct generator *generator)

/* determine which "system" headers to include */
memset(&visitor, 0, sizeof(visitor));
visitor.visit = IDL_DECLARATOR | IDL_SEQUENCE | IDL_UNION;
visitor.visit = IDL_DECLARATOR | IDL_SEQUENCE | IDL_UNION | IDL_CONST;
visitor.accept[IDL_ACCEPT_DECLARATOR] = &register_types;
visitor.accept[IDL_ACCEPT_SEQUENCE] = &register_types;
visitor.accept[IDL_ACCEPT_CONST] = &register_types;
visitor.accept[IDL_ACCEPT_UNION] = &register_union;
assert(pstate->sources);
sources[0] = pstate->sources->path->name;
Expand All @@ -631,11 +636,8 @@ generate_includes(const idl_pstate_t *pstate, struct generator *generator)

for (include = source->includes; include; include = include->next) {
int cnt;
const char *ext = NULL, *file = include->file->name;
for (const char *ptr = file; *ptr; ptr++) {
if (*ptr == '.')
ext = ptr;
}
const char *file = include->file->name;
const char *ext = strrchr(file, '.');
if (ext && idl_strcasecmp(ext, ".idl") == 0) {
const char *fmt = "#include \"%.*s.hpp\"\n";
int len = (int)(ext - file);
Expand All @@ -650,8 +652,10 @@ generate_includes(const idl_pstate_t *pstate, struct generator *generator)
}

{ int len = 0;
const char *incs[6];
const char *incs[7];

if (generator->uses_integers)
incs[len++] = "<cstdint>";
if (generator->uses_array)
incs[len++] = generator->array_include;
if (generator->uses_sequence)
Expand Down
1 change: 1 addition & 0 deletions src/idlcxx/src/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct generator {
char *union_format;
char *union_getter_format;
const char *union_include;
bool uses_integers;
bool uses_array;
bool uses_sequence;
bool uses_bounded_sequence;
Expand Down
6 changes: 4 additions & 2 deletions src/idlcxx/src/traits.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ generate_traits(const idl_pstate_t *pstate, struct generator *generator)
idl_visitor_t visitor;
const char *sources[] = { NULL, NULL };

if (idl_fprintf(generator->header.handle, "#include \"org/eclipse/cyclonedds/topic/TopicTraits.hpp\"\n"
"#include \"org/eclipse/cyclonedds/topic/DataRepresentation.hpp\"\n\n"
if (idl_fprintf(generator->header.handle,
"#include \"dds/topic/TopicTraits.hpp\"\n"
"#include \"org/eclipse/cyclonedds/topic/TopicTraits.hpp\"\n"
"#include \"org/eclipse/cyclonedds/topic/datatopic.hpp\"\n\n"
"namespace org {\n"
"namespace eclipse {\n"
"namespace cyclonedds {\n"
Expand Down
18 changes: 11 additions & 7 deletions src/idlcxx/src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,19 @@ emit_enum(
static idl_retcode_t
expand_typedef(
struct generator* gen,
const idl_declarator_t* declarator,
const idl_typedef_t* _typedef)
const idl_declarator_t* declarator)
{
const char* name = get_cpp11_name(declarator);
char* type = NULL;
char *type = NULL;
const char *name = get_cpp11_name(declarator);
const idl_type_spec_t *type_spec;

if (IDL_PRINTA(&type, get_cpp11_type, idl_is_array(declarator) ? declarator : _typedef->type_spec, gen) < 0)
return IDL_RETCODE_NO_MEMORY;
if (idl_is_array(declarator))
type_spec = declarator;
else
type_spec = idl_type_spec(declarator);

if (IDL_PRINTA(&type, get_cpp11_type, type_spec, gen) < 0)
return IDL_RETCODE_NO_MEMORY;
if (idl_fprintf(gen->header.handle, "typedef %s %s;\n\n", type, name) < 0)
return IDL_RETCODE_NO_MEMORY;

Expand All @@ -388,7 +392,7 @@ emit_typedef(

idl_retcode_t ret = IDL_RETCODE_OK;
IDL_FOREACH(declarator, _typedef->declarators) {
if ((ret = expand_typedef(gen, declarator, _typedef)) != IDL_RETCODE_OK)
if ((ret = expand_typedef(gen, declarator)) != IDL_RETCODE_OK)
break;
}

Expand Down

0 comments on commit ca478d5

Please sign in to comment.