From 344c30fe095f37e667bf70041e3cf4656e09c39f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Techet?= Date: Mon, 20 Dec 2021 15:53:29 +0100 Subject: [PATCH] Use asm parser from uctags --- ctags/Makefile.am | 2 +- ctags/parsers/{geany_asm.c => asm.c} | 196 +++++++++++++++++---------- src/tagmanager/tm_parser.c | 1 + 3 files changed, 130 insertions(+), 69 deletions(-) rename ctags/parsers/{geany_asm.c => asm.c} (66%) diff --git a/ctags/Makefile.am b/ctags/Makefile.am index 793ec45abc..aeb63f806e 100644 --- a/ctags/Makefile.am +++ b/ctags/Makefile.am @@ -46,7 +46,7 @@ parsers = \ parsers/abaqus.c \ parsers/abc.c \ parsers/asciidoc.c \ - parsers/geany_asm.c \ + parsers/asm.c \ parsers/basic.c \ parsers/bibtex.c \ parsers/geany_c.c \ diff --git a/ctags/parsers/geany_asm.c b/ctags/parsers/asm.c similarity index 66% rename from ctags/parsers/geany_asm.c rename to ctags/parsers/asm.c index 35de6456f9..a3679499e0 100644 --- a/ctags/parsers/geany_asm.c +++ b/ctags/parsers/asm.c @@ -15,18 +15,23 @@ #include +#include "cpreprocessor.h" #include "debug.h" +#include "entry.h" #include "keyword.h" #include "parse.h" #include "read.h" #include "routines.h" +#include "selectors.h" #include "vstring.h" /* * DATA DECLARATIONS */ typedef enum { - K_NONE = -1, K_DEFINE, K_LABEL, K_MACRO, K_TYPE + K_PSUEDO_MACRO_END = -2, + K_NONE = -1, K_DEFINE, K_LABEL, K_MACRO, K_TYPE, + K_SECTION, } AsmKind; typedef enum { @@ -45,11 +50,16 @@ typedef enum { OP_PROC, OP_RECORD, OP_SECTIONS, + OP_SECTION, OP_SET, OP_STRUCT, OP_LAST } opKeyword; +typedef enum { + ASM_SECTION_PLACEMENT, +} asmSectionRole; + typedef struct { opKeyword keyword; AsmKind kind; @@ -60,11 +70,17 @@ typedef struct { */ static langType Lang_asm; +static roleDefinition asmSectionRoles [] = { + { true, "placement", "placement where the assembled code goes" }, +}; + static kindDefinition AsmKinds [] = { { true, 'd', "define", "defines" }, { true, 'l', "label", "labels" }, { true, 'm', "macro", "macros" }, - { true, 't', "type", "types (structs and records)" } + { true, 't', "type", "types (structs and records)" }, + { true, 's', "section", "sections", + .referenceOnly = true, ATTACH_ROLES(asmSectionRoles)}, }; static const keywordTable AsmKeywords [] = { @@ -82,6 +98,12 @@ static const keywordTable AsmKeywords [] = { { "proc", OP_PROC }, { "record", OP_RECORD }, { "sections", OP_SECTIONS }, + + /* These are used in GNU as. */ + { "section", OP_SECTION }, + { "equiv", OP_EQU }, + { "eqv", OP_EQU }, + { "set", OP_SET }, { "struct", OP_STRUCT } }; @@ -91,7 +113,7 @@ static const opKind OpKinds [] = { { OP_ALIGN, K_NONE }, { OP_COLON_EQUAL, K_DEFINE }, { OP_END, K_NONE }, - { OP_ENDM, K_NONE }, + { OP_ENDM, K_PSUEDO_MACRO_END }, { OP_ENDMACRO, K_NONE }, { OP_ENDP, K_NONE }, { OP_ENDS, K_NONE }, @@ -102,6 +124,7 @@ static const opKind OpKinds [] = { { OP_PROC, K_LABEL }, { OP_RECORD, K_TYPE }, { OP_SECTIONS, K_NONE }, + { OP_SECTION, K_SECTION }, { OP_SET, K_DEFINE }, { OP_STRUCT, K_TYPE } }; @@ -131,33 +154,6 @@ static bool isSymbolCharacter (int c) return (bool) (c != '\0' && (isalnum (c) || strchr ("_$?", c) != NULL)); } -static bool readPreProc (const unsigned char *const line) -{ - bool result; - const unsigned char *cp = line; - vString *name = vStringNew (); - while (isSymbolCharacter ((int) *cp)) - { - vStringPut (name, *cp); - ++cp; - } - result = (bool) (strcmp (vStringValue (name), "define") == 0); - if (result) - { - while (isspace ((int) *cp)) - ++cp; - vStringClear (name); - while (isSymbolCharacter ((int) *cp)) - { - vStringPut (name, *cp); - ++cp; - } - makeSimpleTag (name, K_DEFINE); - } - vStringDelete (name); - return result; -} - static AsmKind operatorKind ( const vString *const operator, bool *const found) @@ -178,7 +174,7 @@ static AsmKind operatorKind ( static bool isDefineOperator (const vString *const operator) { const unsigned char *const op = - (unsigned char*) vStringValue (operator); + (unsigned char*) vStringValue (operator); const size_t length = vStringLength (operator); const bool result = (bool) (length > 0 && toupper ((int) *op) == 'D' && @@ -192,7 +188,9 @@ static void makeAsmTag ( const vString *const name, const vString *const operator, const bool labelCandidate, - const bool nameFollows) + const bool nameFollows, + const bool directive, + int *lastMacroCorkIndex) { if (vStringLength (name) > 0) { @@ -200,7 +198,7 @@ static void makeAsmTag ( const AsmKind kind = operatorKind (operator, &found); if (found) { - if (kind != K_NONE) + if (kind > K_NONE) makeSimpleTag (name, kind); } else if (isDefineOperator (operator)) @@ -214,6 +212,37 @@ static void makeAsmTag ( if (! found) makeSimpleTag (name, K_LABEL); } + else if (directive) + { + bool found_dummy; + const AsmKind kind_for_directive = operatorKind (name, &found_dummy); + tagEntryInfo *macro_tag; + + switch (kind_for_directive) + { + case K_NONE: + break; + case K_MACRO: + *lastMacroCorkIndex = makeSimpleTag (operator, + kind_for_directive); + if (*lastMacroCorkIndex != CORK_NIL) + registerEntry (*lastMacroCorkIndex); + break; + case K_PSUEDO_MACRO_END: + macro_tag = getEntryInCorkQueue (*lastMacroCorkIndex); + if (macro_tag) + macro_tag->extensionFields.endLine = getInputLineNumber (); + *lastMacroCorkIndex = CORK_NIL; + break; + case K_SECTION: + makeSimpleRefTag (operator, + kind_for_directive, + ASM_SECTION_PLACEMENT); + break; + default: + makeSimpleTag (operator, kind_for_directive); + } + } } } @@ -240,7 +269,7 @@ static const unsigned char *readOperator ( { const unsigned char *cp = start; vStringClear (operator); - while (*cp != '\0' && ! isspace ((int) *cp)) + while (*cp != '\0' && ! isspace ((int) *cp) && *cp != ',') { vStringPut (operator, *cp); ++cp; @@ -248,63 +277,85 @@ static const unsigned char *readOperator ( return cp; } +static const unsigned char *asmReadLineFromInputFile (void) +{ + static vString *line; + int c; + + line = vStringNewOrClear (line); + + while ((c = cppGetc()) != EOF) + { + if (c == '\n') + break; + else if (c == STRING_SYMBOL || c == CHAR_SYMBOL) + { + /* We cannot store these values to vString + * Store a whitespace as a dummy value for them. + */ + vStringPut (line, ' '); + } + else + vStringPut (line, c); + } + + if ((vStringLength (line) == 0)&& (c == EOF)) + return NULL; + else + return (unsigned char *)vStringValue (line); +} + static void findAsmTags (void) { vString *name = vStringNew (); vString *operator = vStringNew (); const unsigned char *line; - bool inCComment = false; - while ((line = readLineFromInputFile ()) != NULL) + cppInit (false, false, false, false, + KIND_GHOST_INDEX, 0, KIND_GHOST_INDEX, KIND_GHOST_INDEX, 0, 0, + FIELD_UNKNOWN); + + int lastMacroCorkIndex = CORK_NIL; + + while ((line = asmReadLineFromInputFile ()) != NULL) { const unsigned char *cp = line; bool labelCandidate = (bool) (! isspace ((int) *cp)); bool nameFollows = false; + bool directive = false; const bool isComment = (bool) (*cp != '\0' && strchr (";*@", *cp) != NULL); /* skip comments */ - if (strncmp ((const char*) cp, "/*", (size_t) 2) == 0) - { - inCComment = true; - cp += 2; - } - if (inCComment) - { - do - { - if (strncmp ((const char*) cp, "*/", (size_t) 2) == 0) - { - inCComment = false; - cp += 2; - break; - } - ++cp; - } while (*cp != '\0'); - } - if (isComment || inCComment) - continue; - - /* read preprocessor defines */ - if (*cp == '#') - { - ++cp; - readPreProc (cp); + if (isComment) continue; - } /* skip white space */ while (isspace ((int) *cp)) ++cp; /* read symbol */ - cp = readSymbol (cp, name); - if (vStringLength (name) > 0 && *cp == ':') + if (*cp == '.') { - labelCandidate = true; + directive = true; + labelCandidate = false; ++cp; } + cp = readSymbol (cp, name); + if (vStringLength (name) > 0) + { + if (*cp == ':') + { + labelCandidate = true; + ++cp; + } + else if (anyKindEntryInScope (CORK_NIL, + vStringValue (name), + K_MACRO)) + labelCandidate = false; + } + if (! isspace ((int) *cp) && *cp != '\0') continue; @@ -328,8 +379,12 @@ static void findAsmTags (void) cp = readSymbol (cp, name); nameFollows = true; } - makeAsmTag (name, operator, labelCandidate, nameFollows); + makeAsmTag (name, operator, labelCandidate, nameFollows, directive, + &lastMacroCorkIndex); } + + cppTerminate (); + vStringDelete (name); vStringDelete (operator); } @@ -351,8 +406,11 @@ extern parserDefinition* AsmParser (void) "*.[xX][68][68]", NULL }; + static selectLanguage selectors[] = { selectByArrowOfR, + NULL }; + parserDefinition* def = parserNew ("Asm"); - def->kindTable = AsmKinds; + def->kindTable = AsmKinds; def->kindCount = ARRAY_SIZE (AsmKinds); def->extensions = extensions; def->patterns = patterns; @@ -360,5 +418,7 @@ extern parserDefinition* AsmParser (void) def->initialize = initialize; def->keywordTable = AsmKeywords; def->keywordCount = ARRAY_SIZE (AsmKeywords); + def->selectLanguage = selectors; + def->useCork = CORK_QUEUE | CORK_SYMTAB; return def; } diff --git a/src/tagmanager/tm_parser.c b/src/tagmanager/tm_parser.c index af5ed8113c..9885d2fbbd 100644 --- a/src/tagmanager/tm_parser.c +++ b/src/tagmanager/tm_parser.c @@ -174,6 +174,7 @@ static TMParserMapEntry map_ASM[] = { {'l', tm_tag_namespace_t}, {'m', tm_tag_function_t}, {'t', tm_tag_struct_t}, + {'s', tm_tag_undef_t}, }; /* not in universal-ctags */