Skip to content

Commit caf3827

Browse files
committed
Saving work on <ref> field in commsdsl2wireshark.
1 parent 1c68485 commit caf3827

26 files changed

Lines changed: 195 additions & 90 deletions

app/commsdsl2wireshark/src/WiresharkField.cpp

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,7 @@ bool WiresharkField::wiresharkPrepare()
132132

133133
std::string WiresharkField::wiresharkDissectName(const WiresharkField* refField) const
134134
{
135-
const auto* genField = &m_genField;
136-
if (refField != nullptr) {
137-
genField = &(refField->wiresharkGenField());
138-
}
139-
140-
if (!genField->genIsReferenced()) {
141-
return strings::genEmptyString();
142-
}
143-
144-
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genField->genGenerator());
145-
return wiresharkGenerator.wiresharkDissectNameFor(*genField);
135+
return wiresharkDissectNameImpl(refField);
146136
}
147137

148138
std::string WiresharkField::wiresharkDissectCode(const WiresharkField* refField) const
@@ -153,6 +143,7 @@ std::string WiresharkField::wiresharkDissectCode(const WiresharkField* refField)
153143
}
154144

155145
if (!genField->genIsReferenced()) {
146+
genField->genGenerator().genLogger().genDebug("Field " + genField->genName() + " is not really referenced, skipping dissect code generation");
156147
return strings::genEmptyString();
157148
}
158149

@@ -199,19 +190,39 @@ std::string WiresharkField::wiresharkDissectCode(const WiresharkField* refField)
199190
}
200191

201192
std::string WiresharkField::wiresharkFieldObjName(const WiresharkField* refField) const
193+
{
194+
return wiresharkFieldObjNameImpl(refField);
195+
}
196+
197+
std::string WiresharkField::wiresharkFieldRegistration(const WiresharkField* refField) const
198+
{
199+
return wiresharkFieldRegistrationImpl(refField);
200+
}
201+
202+
std::string WiresharkField::wiresharkDissectNameImpl(const WiresharkField* refField) const
202203
{
203204
const auto* genField = &m_genField;
204205
if (refField != nullptr) {
205206
genField = &(refField->wiresharkGenField());
206207
}
208+
209+
if (!genField->genIsReferenced()) {
210+
return strings::genEmptyString();
211+
}
212+
207213
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genField->genGenerator());
208-
auto scope = comms::genScopeFor(*genField, wiresharkGenerator, false);
209-
return Wireshark::wiresharkProtocolObjName(wiresharkGenerator) + '_' + util::genStrReplace(scope, "::", "_");
214+
return wiresharkGenerator.wiresharkDissectNameFor(*genField);
210215
}
211216

212-
std::string WiresharkField::wiresharkFieldRegistration(const WiresharkField* refField) const
217+
std::string WiresharkField::wiresharkFieldObjNameImpl(const WiresharkField* refField) const
213218
{
214-
return wiresharkFieldRegistrationImpl(refField);
219+
const auto* genField = &m_genField;
220+
if (refField != nullptr) {
221+
genField = &(refField->wiresharkGenField());
222+
}
223+
auto& wiresharkGenerator = WiresharkGenerator::wiresharkCast(genField->genGenerator());
224+
auto scope = comms::genScopeFor(*genField, wiresharkGenerator, false);
225+
return Wireshark::wiresharkProtocolObjName(wiresharkGenerator) + '_' + util::genStrReplace(scope, "::", "_");
215226
}
216227

217228
std::string WiresharkField::wiresharkFieldRegistrationImpl([[maybe_unused]] const WiresharkField* refField) const
@@ -236,6 +247,11 @@ std::string WiresharkField::wiresharkFieldRefName(const WiresharkField* refField
236247
return Wireshark::wiresharkProtocolObjName(wiresharkGenerator) + '.' + util::genStrReplace(scope, "::", ".");
237248
}
238249

250+
bool WiresharkField::wiresharkIsBitfieldMember() const
251+
{
252+
return wiresharkParentBitfieldInternal(*this) != nullptr;
253+
}
254+
239255
std::string WiresharkField::wiresharkForcedIntegralFieldMask(const WiresharkField* refField) const
240256
{
241257
if (refField == nullptr) {
@@ -342,6 +358,14 @@ std::string WiresharkField::wiresharkFieldNameVarNameStr(const WiresharkField* r
342358
return wiresharkFieldObjName(refField) + strings::genNameSuffixStr();
343359
}
344360

361+
bool WiresharkField::wiresharkHasOverrideCode() const
362+
{
363+
return
364+
m_customCode.m_hasRead ||
365+
m_customCode.m_hasValid ||
366+
m_customCode.m_hasName;
367+
}
368+
345369
bool WiresharkField::wiresharkCopyCodeFromInternal()
346370
{
347371
auto obj = m_genField.genParseObj();

app/commsdsl2wireshark/src/WiresharkField.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,21 @@ class WiresharkField
5656
std::string wiresharkFieldRegistration(const WiresharkField* refField) const;
5757

5858
protected:
59+
virtual std::string wiresharkDissectNameImpl(const WiresharkField* refField) const;
60+
virtual std::string wiresharkFieldObjNameImpl(const WiresharkField* refField) const;
5961
virtual std::string wiresharkFieldRegistrationImpl(const WiresharkField* refField) const;
6062
virtual std::string wiresharkMembersDissectCodeImpl(const WiresharkField* refField) const;
6163

6264
std::string wiresharkFieldRefName(const WiresharkField* refField) const;
65+
bool wiresharkIsBitfieldMember() const;
6366
std::string wiresharkForcedIntegralFieldMask(const WiresharkField* refField) const;
6467
std::string wiresharkForcedIntegralFieldType(const WiresharkField* refField) const;
6568
unsigned wiresharkForcedMaskShift(const WiresharkField* refField) const;
6669
unsigned wiresharkForcedBitLength(const WiresharkField* refField) const;
6770
std::string wiresharkFieldDescriptionStr(const WiresharkField* refField) const;
6871
std::string wiresharkFieldDisplayNameStr(const WiresharkField* refField) const;
6972
std::string wiresharkFieldNameVarNameStr(const WiresharkField* refField) const;
73+
bool wiresharkHasOverrideCode() const;
7074

7175
private:
7276
using WiresharkCustomCodeFunc = std::string (WiresharkField::*)(bool& hasCode) const;

app/commsdsl2wireshark/src/WiresharkRefField.cpp

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,34 @@
1717

1818
#include "WiresharkGenerator.h"
1919

20+
#include "commsdsl/gen/strings.h"
21+
2022
#include <cassert>
23+
#include <functional>
24+
#include <tuple>
25+
26+
namespace strings = commsdsl::gen::strings;
2127

2228
namespace commsdsl2wireshark
2329
{
2430

31+
namespace
32+
{
33+
34+
[[nodiscard]] auto wiresharkParseObjAsTupleInternal(const WiresharkRefField::ParseField& parseObj)
35+
{
36+
return
37+
std::make_tuple(
38+
std::cref(parseObj.parseName()),
39+
std::cref(parseObj.parseDisplayName()),
40+
std::cref(parseObj.parseDescription()),
41+
parseObj.parseSemanticType(),
42+
parseObj.parseIsPseudo(),
43+
parseObj.parseIsFailOnInvalid());
44+
}
45+
46+
} // namespace
47+
2548
WiresharkRefField::WiresharkRefField(WiresharkGenerator& generator, ParseField parseObj, GenElem* parent) :
2649
GenBase(generator, parseObj, parent),
2750
WiresharkBase(static_cast<GenBase&>(*this))
@@ -38,18 +61,61 @@ bool WiresharkRefField::genPrepareImpl()
3861
m_wiresharkField = WiresharkField::wiresharkCast(genReferencedField());
3962
assert(m_wiresharkField != nullptr);
4063

41-
// TODO: copy vs alias
64+
m_alias = wiresharkIsAliasInternal();
65+
66+
if (!m_alias) {
67+
genGenerator().genLogger().genDebug(m_wiresharkField->wiresharkGenField().genParseObj().parseExternalRef() + " field is not really referenced by " + genName());
68+
m_wiresharkField->wiresharkGenField().genSetReferenced(false);
69+
}
4270
return true;
4371
}
4472

73+
std::string WiresharkRefField::wiresharkDissectNameImpl(const WiresharkField* refField) const
74+
{
75+
if (!m_alias) {
76+
return WiresharkBase::wiresharkDissectNameImpl(refField);
77+
}
78+
79+
assert(m_wiresharkField != nullptr);
80+
return m_wiresharkField->wiresharkDissectName(refField);
81+
}
82+
83+
std::string WiresharkRefField::wiresharkFieldObjNameImpl(const WiresharkField* refField) const
84+
{
85+
if (!m_alias) {
86+
return WiresharkBase::wiresharkFieldObjNameImpl(refField);
87+
}
88+
89+
assert(m_wiresharkField != nullptr);
90+
return m_wiresharkField->wiresharkFieldObjName(refField);
91+
}
92+
4593
std::string WiresharkRefField::wiresharkFieldRegistrationImpl(const WiresharkField* refField) const
4694
{
95+
assert(m_wiresharkField != nullptr);
96+
if (m_alias) {
97+
genGenerator().genLogger().genDebug(genName() + " field is full alias to " + m_wiresharkField->wiresharkGenField().genParseObj().parseExternalRef() + ", not generating registration");
98+
return strings::genEmptyString();
99+
}
100+
47101
if (refField == nullptr) {
48102
refField = this;
49103
}
50104

51-
assert(m_wiresharkField != nullptr);
52105
return m_wiresharkField->wiresharkFieldRegistration(refField);
53106
}
54107

108+
bool WiresharkRefField::wiresharkIsAliasInternal() const
109+
{
110+
if (wiresharkIsBitfieldMember() ||
111+
wiresharkHasOverrideCode()) {
112+
return false;
113+
}
114+
115+
assert(m_wiresharkField != nullptr);
116+
auto& thisObj = wiresharkGenField().genParseObj();
117+
auto& refObj = m_wiresharkField->wiresharkGenField().genParseObj();
118+
return wiresharkParseObjAsTupleInternal(thisObj) == wiresharkParseObjAsTupleInternal(refObj);
119+
}
120+
55121
} // namespace commsdsl2wireshark

app/commsdsl2wireshark/src/WiresharkRefField.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ class WiresharkRefField final : public commsdsl::gen::GenRefField, public Wiresh
3737
protected:
3838
virtual bool genPrepareImpl() override;
3939

40+
virtual std::string wiresharkDissectNameImpl(const WiresharkField* refField) const override;
41+
virtual std::string wiresharkFieldObjNameImpl(const WiresharkField* refField) const override;
4042
virtual std::string wiresharkFieldRegistrationImpl(const WiresharkField* refField) const override;
4143

4244
private:
43-
const WiresharkField* m_wiresharkField = nullptr;
45+
bool wiresharkIsAliasInternal() const;
46+
47+
WiresharkField* m_wiresharkField = nullptr;
48+
bool m_alias = false;
4449
};
4550

4651
} // namespace commsdsl2wireshark

lib/include/commsdsl/gen/GenBitfieldField.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class COMMSDSL_API GenBitfieldField : public GenField
4444

4545
protected:
4646
virtual bool genPrepareImpl() override;
47-
virtual void genSetReferencedImpl() override;
47+
virtual void genSetReferencedImpl(bool referenced) override;
4848
virtual GenFieldRefInfo genProcessInnerRefImpl(const std::string& refStr) const override final;
4949

5050
ParseBitfieldField genBitfieldFieldParseObj() const;

lib/include/commsdsl/gen/GenBundleField.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class COMMSDSL_API GenBundleField : public GenField
4444

4545
protected:
4646
virtual bool genPrepareImpl() override;
47-
virtual void genSetReferencedImpl() override;
47+
virtual void genSetReferencedImpl(bool referenced) override;
4848
virtual GenFieldRefInfo genProcessInnerRefImpl(const std::string& refStr) const override final;
4949

5050
ParseBundleField genBundleFieldParseObj() const;

lib/include/commsdsl/gen/GenDataField.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class COMMSDSL_API GenDataField : public GenField
4848

4949
protected:
5050
virtual bool genPrepareImpl() override;
51-
virtual void genSetReferencedImpl() override;
51+
virtual void genSetReferencedImpl(bool referenced) override;
5252

5353
ParseDataField genDataFieldParseObj() const;
5454

lib/include/commsdsl/gen/GenField.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ class COMMSDSL_API GenField : public GenElem
7373
const GenGenerator& genGenerator() const;
7474

7575
bool genIsReferenced() const;
76-
void genSetReferenced();
76+
void genSetReferenced(bool referenced = true);
7777

78-
static void genSetFieldReferencedIfExists(GenField* field);
78+
static void genSetFieldReferencedIfExists(GenField* field, bool referenced = true);
7979

8080
std::string genTemplateScopeOfComms(const std::string& protOptionsStr) const;
8181

@@ -90,7 +90,7 @@ class COMMSDSL_API GenField : public GenElem
9090
virtual GenType genElemTypeImpl() const override final;
9191
virtual bool genPrepareImpl();
9292
virtual bool genWriteImpl() const;
93-
virtual void genSetReferencedImpl();
93+
virtual void genSetReferencedImpl(bool referenced);
9494
virtual GenFieldRefInfo genProcessInnerRefImpl(const std::string& refStr) const;
9595

9696
private:

lib/include/commsdsl/gen/GenListField.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class COMMSDSL_API GenListField : public GenField
7171

7272
protected:
7373
virtual bool genPrepareImpl() override;
74-
virtual void genSetReferencedImpl() override;
74+
virtual void genSetReferencedImpl(bool referenced) override;
7575

7676
ParseListField genListFieldParseObj() const;
7777

lib/include/commsdsl/gen/GenMessage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class COMMSDSL_API GenMessage : public GenElem
5151
bool genPrepare();
5252
bool genWrite() const;
5353
bool genIsReferenced() const;
54-
void genSetReferenced(bool value);
54+
void genSetReferenced(bool value = true);
5555

5656
ParseMessage genParseObj() const;
5757

0 commit comments

Comments
 (0)