Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/main/java/com/eprosima/fastdds/idl/grammar/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ public void addTypeDeclaration(
{
super.addTypeDeclaration(typedecl);

String scope = typedecl.getScope();
if (scope != null && !scope.isEmpty())
{
last_declaration_scope_ = scope;
}

if (typedecl.getTypeCode().getKind() == Kind.KIND_STRUCT && typedecl.isInScope())
{
Annotation topicann = typedecl.getAnnotations().get("Topic");
Expand Down Expand Up @@ -604,15 +610,28 @@ public boolean isGenerateTypeObjectSupport()

public String getHeaderGuardName ()
{
if (m_lastStructure != null)
String decl_scope = null;
if ((m_lastStructure != null) && m_lastStructure.getHasScope())
{
if (m_lastStructure.getHasScope())
{
return m_lastStructure.getScope().replaceAll("::", "_").toUpperCase() +
"_" + m_fileNameUpper.replaceAll("\\.", "_");
}
decl_scope = m_lastStructure.getScope().replaceAll("::", "_");
}
return m_fileNameUpper;
else if (last_declaration_scope_ != null && !last_declaration_scope_.isEmpty())
{
decl_scope = last_declaration_scope_.replaceAll("::", "_");
}

String guard_base = getScopeFile().replaceAll("/", "__").replaceAll("\\\\", "__");
if (decl_scope != null && !decl_scope.isEmpty())
{
guard_base = guard_base + "_" + decl_scope;
}
return getValidGuardName(guard_base);
}

private String getValidGuardName(
String value)
{
return value.toUpperCase().replaceAll("[^A-Z0-9_]", "_");
}

public String getM_lastStructureTopicDataTypeName()
Expand Down Expand Up @@ -756,4 +775,6 @@ else if (name.equals(Annotation.external_str))
private boolean there_is_at_least_one_struct = false;

private boolean there_is_at_least_one_union = false;

private String last_declaration_scope_ = null;
}
78 changes: 78 additions & 0 deletions src/test/java/com/eprosima/fastdds/FastDDSGenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.junit.jupiter.api.Test;

import com.eprosima.fastdds.idl.grammar.Context;
import com.eprosima.idl.generator.manager.TemplateManager;
import com.eprosima.idl.parser.tree.TypeDeclaration;

import com.eprosima.integration.Command;

Expand Down Expand Up @@ -101,6 +103,82 @@ public void Context_getRelativeDir_Test()
}
}


@Test
public void Context_getHeaderGuardName_UsesFilePath_Test()
{
Context first_ctx = new Context(
new TemplateManager(),
"idl\\package_one\\common\\status_list.idl",
new ArrayList<String>(),
false,
false,
null,
false,
false,
false,
false);
Context second_ctx = new Context(
new TemplateManager(),
"idl/package_two/common/status_list.idl",
new ArrayList<String>(),
false,
false,
null,
false,
false,
false,
false);
Context third_ctx = new Context(
new TemplateManager(),
"idl/package_two/common/status/list.idl",
new ArrayList<String>(),
false,
false,
null,
false,
false,
false,
false);

assertEquals(
"IDL__PACKAGE_ONE__COMMON__STATUS_LIST_IDL",
first_ctx.getHeaderGuardName());
assertEquals(
"IDL__PACKAGE_TWO__COMMON__STATUS_LIST_IDL",
second_ctx.getHeaderGuardName());
assertEquals(
"IDL__PACKAGE_TWO__COMMON__STATUS__LIST_IDL",
third_ctx.getHeaderGuardName());
}

@Test
public void Context_getHeaderGuardName_DependsOnDeclarations_Test()
{
Context ctx = new Context(
new TemplateManager(),
"dir-one/impl-type.idl",
new ArrayList<String>(),
false,
false,
null,
false,
false,
false,
false);
TypeDeclaration typedecl = new TypeDeclaration(
ctx.getScopeFile(),
true,
"module::scope",
"ScopedType",
ctx.createAliasTypeCode("module::scope", "ScopedType"),
null);

ctx.addTypeDeclaration(typedecl);

assertEquals("DIR_ONE__IMPL_TYPE_IDL_MODULE_SCOPE", ctx.getHeaderGuardName());
}

@Test
public void runTests()
{
Expand Down
Loading