Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: support integer field in the writers #4177

Merged
merged 5 commits into from
Jan 17, 2025
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
4 changes: 4 additions & 0 deletions Tmain/json-output-typed-fields.d/stdout-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
{"_type": "tag", "name": "etag", "path": "input.ctst", "pattern": "/^f$/", "kind": "fieldMaker", "sbField": false}
{"_type": "tag", "name": "ftag", "path": "input.ctst", "pattern": "/^f$/", "kind": "fieldMaker", "sField": "val"}
{"_type": "tag", "name": "gtag", "path": "input.ctst", "pattern": "/^f$/", "kind": "fieldMaker", "sField": ""}
{"_type": "tag", "name": "htag", "path": "input.ctst", "pattern": "/^f$/", "kind": "fieldMaker", "iField": 23}
{"_type": "tag", "name": "itag", "path": "input.ctst", "pattern": "/^f$/", "kind": "fieldMaker", "iField": -3}
{"_type": "tag", "name": "jtag", "path": "input.ctst", "pattern": "/^f$/", "kind": "fieldMaker", "iField": 1}
{"_type": "tag", "name": "ktag", "path": "input.ctst", "pattern": "/^f$/", "kind": "fieldMaker", "iField": 0}
{"_type": "tag", "name": "TITLE", "path": "input.rst", "pattern": "/^TITLE$/", "kind": "title", "overline": true}
{"_type": "tag", "name": "section", "path": "input.rst", "pattern": "/^section$/", "kind": "subtitle", "scope": "TITLE", "scopeKind": "title"}
{"_type": "tag", "name": "global", "path": "input.c", "pattern": "/^int global;$/", "typeref": "typename:int", "kind": "variable"}
Expand Down
4 changes: 4 additions & 0 deletions Tmain/tags-output-typed-fields.d/stdout-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ dtag input.ctst /^f$/;" f sbField:val
etag input.ctst /^f$/;" f sbField:
ftag input.ctst /^f$/;" f sField:val
gtag input.ctst /^f$/;" f sField:
htag input.ctst /^f$/;" f iField:23
itag input.ctst /^f$/;" f iField:-3
jtag input.ctst /^f$/;" f iField:1
ktag input.ctst /^f$/;" f iField:0
TITLE input.rst /^TITLE$/;" H overline:
section input.rst /^section$/;" h title:TITLE
global input.c /^int global;$/;" v typeref:typename:int
Expand Down
2 changes: 1 addition & 1 deletion Tmain/xref-output-typed-fields.d/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CTAGS=$1

echo '# input.ctst'
${CTAGS} --quiet --options=NONE -o - \
--output-format=xref --_xformat='%{name} -> b=%{CTagsSelfTest.bField},sb=%{CTagsSelfTest.sbField},s=%{CTagsSelfTest.sField}' \
--output-format=xref --_xformat='%{name} -> b=%{CTagsSelfTest.bField},sb=%{CTagsSelfTest.sbField},s=%{CTagsSelfTest.sField},i=%{CTagsSelfTest.iField}' \
--language-force=CTagsSelfTest input.ctst

echo '# input.rst'
Expand Down
18 changes: 11 additions & 7 deletions Tmain/xref-output-typed-fields.d/stdout-expected.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# input.ctst
atag -> b=-,sb=,s=
btag -> b=bField,sb=,s=
ctag -> b=bField,sb=,s=
dtag -> b=-,sb=val,s=
etag -> b=-,sb=-,s=
ftag -> b=-,sb=,s=val
gtag -> b=-,sb=,s=
atag -> b=-,sb=,s=,i=
btag -> b=bField,sb=,s=,i=
ctag -> b=bField,sb=,s=,i=
dtag -> b=-,sb=val,s=,i=
etag -> b=-,sb=-,s=,i=
ftag -> b=-,sb=,s=val,i=
gtag -> b=-,sb=,s=,i=
htag -> b=-,sb=,s=,i=23
itag -> b=-,sb=,s=,i=-3
jtag -> b=-,sb=,s=,i=1
ktag -> b=-,sb=,s=,i=0
# input.rst
TITLE -> overline
section -> -
Expand Down
39 changes: 39 additions & 0 deletions main/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,45 @@ typedef enum eFieldDataType {
* json | (nothing)
*
*
* For FIELDTYPE_INTEGER
* ---------------------
* If the value points "" (empty C string), the all writers print it as
* 0. If the value points a string which cannot be converted to an integer with
* strtol(3), the all writers print it as 1.
*
* Consider if you set "99" to the field "foo":
*
* WRITER | OUTPUT
* -------+-----------
* ctags | foo:99
* xref | 99
* json | "foo": 99
*
* Consider if you set "str" to the field "foo":
*
* WRITER | OUTPUT
* -------+-----------
* ctags | foo:1
* xref | 1
* json | "foo": 1
*
* Consider if you set "" to the field "foo":
*
* WRITER | OUTPUT
* -------+-----------
* ctags | foo:0
* xref | 0
* json | "foo": 0
*
* Consider if you don't set the field "foo":
*
* WRITER | OUTPUT
* -------+-----------
* ctags | (nothing)
* xref | (nothing)
* json | (nothing)
*
*
* The other data type and the combination of types are not implemented yet.
*
*/
Expand Down
10 changes: 8 additions & 2 deletions main/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,18 @@ static int printTagField (fmtSpec* fspec, MIO* fp, const tagEntryInfo * tag)
{
str = renderField (f->ftype, tag, findex);
if ((dt & FIELDTYPE_BOOL) && str[0] == '\0')
{
str = FIELD_NULL_LETTER_STRING;
}
}
else if (dt & FIELDTYPE_BOOL)
str = getFieldName (f->ftype);
else if (dt & FIELDTYPE_INTEGER)
{
long unused;

str = renderField (f->ftype, tag, findex);
if (!strToLong (str, 10, &unused))
str = (str[0] == '\0'? "0": "1");
}
else
{
/* Not implemented */
Expand Down
30 changes: 30 additions & 0 deletions main/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -5641,6 +5641,7 @@ typedef enum {
F_BOOLEAN_FIELD,
F_BOOLEAN_AND_STRING_FIELD,
F_STRING_FIELD,
F_INTEGER_FIELD,
COUNT_FIELD
} CTSTField;

Expand All @@ -5660,6 +5661,11 @@ static fieldDefinition CTSTFields[COUNT_FIELD] = {
.dataType = FIELDTYPE_STRING,
.enabled = true,
},
{ .name = "iField",
.description = "field for testing integer type",
.dataType = FIELDTYPE_INTEGER,
.enabled = true,
},
};

static void createCTSTTags (void)
Expand Down Expand Up @@ -5843,6 +5849,30 @@ static void createCTSTTags (void)
CTSTFields[F_STRING_FIELD].ftype, "");
makeTagEntry (&e);

name [0] = c++;
initTagEntry (&e, name, i);
attachParserField (&e,
CTSTFields[F_INTEGER_FIELD].ftype, "23");
makeTagEntry (&e);

name [0] = c++;
initTagEntry (&e, name, i);
attachParserField (&e,
CTSTFields[F_INTEGER_FIELD].ftype, "-3");
makeTagEntry (&e);

name [0] = c++;
initTagEntry (&e, name, i);
attachParserField (&e,
CTSTFields[F_INTEGER_FIELD].ftype, "poison");
makeTagEntry (&e);

name [0] = c++;
initTagEntry (&e, name, i);
attachParserField (&e,
CTSTFields[F_INTEGER_FIELD].ftype, "");
makeTagEntry (&e);

break;
}
case K_TRIGGER_NOTICE:
Expand Down
25 changes: 21 additions & 4 deletions main/writer-ctags.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,27 @@
continue;

unsigned int dt = getFieldDataType (ftype);
const char *val = ((dt & FIELDTYPE_STRING) == 0
&& (dt & FIELDTYPE_BOOL))
? ""
: escapeFieldValueFull (writer, tag, ftype, i);
const char *val;

if (dt & FIELDTYPE_STRING)
val = escapeFieldValueFull (writer, tag, ftype, i);
else if (dt & FIELDTYPE_BOOL)
val = "";
else if (dt & FIELDTYPE_INTEGER)
{
long tmp;

val = escapeFieldValueFull (writer, tag, ftype, i);
if (!strToLong (val, 10, &tmp))
val = (val[0] == '\0')? "0": "1";
}
else
{
/* Not implemented */
AssertNotReached ();

Check warning on line 234 in main/writer-ctags.c

View check run for this annotation

Codecov / codecov/patch

main/writer-ctags.c#L234

Added line #L234 was not covered by tests
val = "CTAGS INTERNAL BUG!";
}


length += mio_printf(mio, "\t%s:%s",
getFieldName (ftype), val);
Expand Down
10 changes: 7 additions & 3 deletions main/writer-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,13 @@ static void addParserFields (json_t *response, const tagEntryInfo *const tag)
}
else if (dt & FIELDTYPE_INTEGER)
{
/* NOT IMPLEMENTED YET */
AssertNotReached ();
o = json_null ();
const char *str = escapeFieldValueRaw (tag, ftype, i);
long tmp;

if (strToLong (str, 10, &tmp))
o = json_integer (tmp);
else
o = json_integer(str[0] == '\0'? 0: 1);
}
else if (dt & FIELDTYPE_BOOL)
o = json_true ();
Expand Down
Loading