Skip to content

Commit 72f89a0

Browse files
Improve generated header guards (#587)
Signed-off-by: Miguel Company <miguelcompany@eprosima.com>
1 parent 724b278 commit 72f89a0

2 files changed

Lines changed: 106 additions & 7 deletions

File tree

src/main/java/com/eprosima/fastdds/idl/grammar/Context.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ public void addTypeDeclaration(
309309
{
310310
super.addTypeDeclaration(typedecl);
311311

312+
String scope = typedecl.getScope();
313+
if (scope != null && !scope.isEmpty())
314+
{
315+
last_declaration_scope_ = scope;
316+
}
317+
312318
boolean is_nested = typedecl.isAnnotatedAsNested();
313319

314320
if (typedecl.getTypeCode().getKind() == Kind.KIND_STRUCT && typedecl.isInScope() && !is_nested)
@@ -626,15 +632,28 @@ public boolean isGenerateTypeObjectSupport()
626632

627633
public String getHeaderGuardName ()
628634
{
629-
if (m_lastStructure != null)
635+
String decl_scope = null;
636+
if ((m_lastStructure != null) && m_lastStructure.getHasScope())
630637
{
631-
if (m_lastStructure.getHasScope())
632-
{
633-
return m_lastStructure.getScope().replaceAll("::", "_").toUpperCase() +
634-
"_" + m_fileNameUpper.replaceAll("\\.", "_");
635-
}
638+
decl_scope = m_lastStructure.getScope().replaceAll("::", "_");
636639
}
637-
return m_fileNameUpper;
640+
else if (last_declaration_scope_ != null && !last_declaration_scope_.isEmpty())
641+
{
642+
decl_scope = last_declaration_scope_.replaceAll("::", "_");
643+
}
644+
645+
String guard_base = getScopeFile().replaceAll("/", "__").replaceAll("\\\\", "__");
646+
if (decl_scope != null && !decl_scope.isEmpty())
647+
{
648+
guard_base = guard_base + "_" + decl_scope;
649+
}
650+
return getValidGuardName(guard_base);
651+
}
652+
653+
private String getValidGuardName(
654+
String value)
655+
{
656+
return value.toUpperCase().replaceAll("[^A-Z0-9_]", "_");
638657
}
639658

640659
public String getM_lastStructureTopicDataTypeName()
@@ -778,4 +797,6 @@ else if (name.equals(Annotation.external_str))
778797
protected boolean there_is_at_least_one_struct = false;
779798

780799
protected boolean there_is_at_least_one_union = false;
800+
801+
protected String last_declaration_scope_ = null;
781802
}

src/test/java/com/eprosima/fastdds/FastDDSGenTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.junit.jupiter.api.Test;
44

5+
import com.eprosima.fastdds.idl.grammar.Context;
56
import com.eprosima.idl.generator.manager.TemplateManager;
7+
import com.eprosima.idl.parser.tree.TypeDeclaration;
68

79
import com.eprosima.integration.Command;
810

@@ -101,6 +103,82 @@ public void Context_getRelativeDir_Test()
101103
}
102104
}
103105

106+
107+
@Test
108+
public void Context_getHeaderGuardName_UsesFilePath_Test()
109+
{
110+
Context first_ctx = new Context(
111+
new TemplateManager(),
112+
"idl\\package_one\\common\\status_list.idl",
113+
new ArrayList<String>(),
114+
false,
115+
false,
116+
null,
117+
false,
118+
false,
119+
false,
120+
false);
121+
Context second_ctx = new Context(
122+
new TemplateManager(),
123+
"idl/package_two/common/status_list.idl",
124+
new ArrayList<String>(),
125+
false,
126+
false,
127+
null,
128+
false,
129+
false,
130+
false,
131+
false);
132+
Context third_ctx = new Context(
133+
new TemplateManager(),
134+
"idl/package_two/common/status/list.idl",
135+
new ArrayList<String>(),
136+
false,
137+
false,
138+
null,
139+
false,
140+
false,
141+
false,
142+
false);
143+
144+
assertEquals(
145+
"IDL__PACKAGE_ONE__COMMON__STATUS_LIST_IDL",
146+
first_ctx.getHeaderGuardName());
147+
assertEquals(
148+
"IDL__PACKAGE_TWO__COMMON__STATUS_LIST_IDL",
149+
second_ctx.getHeaderGuardName());
150+
assertEquals(
151+
"IDL__PACKAGE_TWO__COMMON__STATUS__LIST_IDL",
152+
third_ctx.getHeaderGuardName());
153+
}
154+
155+
@Test
156+
public void Context_getHeaderGuardName_DependsOnDeclarations_Test()
157+
{
158+
Context ctx = new Context(
159+
new TemplateManager(),
160+
"dir-one/impl-type.idl",
161+
new ArrayList<String>(),
162+
false,
163+
false,
164+
null,
165+
false,
166+
false,
167+
false,
168+
false);
169+
TypeDeclaration typedecl = new TypeDeclaration(
170+
ctx.getScopeFile(),
171+
true,
172+
"module::scope",
173+
"ScopedType",
174+
ctx.createAliasTypeCode("module::scope", "ScopedType"),
175+
null);
176+
177+
ctx.addTypeDeclaration(typedecl);
178+
179+
assertEquals("DIR_ONE__IMPL_TYPE_IDL_MODULE_SCOPE", ctx.getHeaderGuardName());
180+
}
181+
104182
@Test
105183
public void runTests()
106184
{

0 commit comments

Comments
 (0)