Skip to content

Commit

Permalink
cxx: add a function for decoding CXXTokenType
Browse files Browse the repository at this point in the history
For making CXX_DO_DEBUGGING debug output more readable,
cxxDebugTypeDecode is introduced.

cxxDebugTypeDecode takes enum CXXTokenType typed parameter and returns
its C string representation. It can be used in CXX_DEBUG_PRINT.

misc/gencxxtypedumper.sh generates cxxDebugTypeDecode function from
enum CXXTokenType definition in cxx_token.h.

misc/gencxxtypedumper.sh is not integrated with ctags building scripts
because the script itself requires ctags internally. Generated source
code (cxx_debug_type.c) is committed to our git repository.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Nov 16, 2016
1 parent 0bbafae commit 869bb13
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 3 deletions.
43 changes: 43 additions & 0 deletions misc/gencxxtypedumper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/sh
#
# gencxxtypedumper - Generating cxxDebugTypeDecode function
#
# Copyright (C) 2014 Masatake YAMATO
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

INPUT=./parsers/cxx/cxx_token.h
CTAGS=./ctags
OUTPUT=./parsers/cxx/cxx_debug_type.c

exec > ${OUTPUT}
echo '/* Automatically generated by misc/gencxxtypedumper.sh */'
echo
echo '#include "cxx_token.h"'
echo '#include "cxx_debug.h"'
echo
echo '#ifdef CXX_DO_DEBUGGING'
echo 'const char * cxxDebugTypeDecode (enum CXXTokenType eType)'
echo '{'
echo ' switch (eType) {'
${CTAGS} -o - --language-force=C --kinds-C=e -x --_xformat="%N" "${INPUT}" \
| grep ^CXXTokenType \
| while read N; do
echo " case $N: return \"${N#CXXTokenType}\";"
done
echo ' default: return "REALLY-UNKNOWN";'
echo ' }'
echo '}'
echo '#endif'
5 changes: 4 additions & 1 deletion parsers/cxx/cxx_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "general.h"
#include "debug.h"
#include "cxx_token.h"

//
// Uncomment this to enable extensive debugging to stderr in cxx code.
Expand All @@ -34,6 +35,8 @@ void cxxDebugLeave(const char * szFunction,const char * szFormat,...);
void cxxDebugPrint(const char * szFunction,const char * szFormat,...);
void cxxDebugInit(void);

const char* cxxDebugTypeDecode(enum CXXTokenType);

#define CXX_DEBUG_ENTER() cxxDebugEnter(__PRETTY_FUNCTION__,"")
#define CXX_DEBUG_LEAVE() cxxDebugLeave(__PRETTY_FUNCTION__,"")

Expand Down Expand Up @@ -75,4 +78,4 @@ void cxxDebugInit(void);
#endif //!CXX_DO_DEBUGGING


#endif //!ctags_cxx_debug_h_
#endif //!ctags_cxx_debug_h_
44 changes: 44 additions & 0 deletions parsers/cxx/cxx_debug_type.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Automatically generated by misc/gencxxtypedumper.sh */

#include "cxx_token.h"
#include "cxx_debug.h"

#ifdef CXX_DO_DEBUGGING
const char * cxxDebugTypeDecode (enum CXXTokenType eType)
{
switch (eType) {
case CXXTokenTypeAnd: return "And";
case CXXTokenTypeAngleBracketChain: return "AngleBracketChain";
case CXXTokenTypeAssignment: return "Assignment";
case CXXTokenTypeBracketChain: return "BracketChain";
case CXXTokenTypeCharacterConstant: return "CharacterConstant";
case CXXTokenTypeClosingBracket: return "ClosingBracket";
case CXXTokenTypeClosingParenthesis: return "ClosingParenthesis";
case CXXTokenTypeClosingSquareParenthesis: return "ClosingSquareParenthesis";
case CXXTokenTypeComma: return "Comma";
case CXXTokenTypeDotOperator: return "DotOperator";
case CXXTokenTypeEOF: return "EOF";
case CXXTokenTypeGreaterThanSign: return "GreaterThanSign";
case CXXTokenTypeIdentifier: return "Identifier";
case CXXTokenTypeKeyword: return "Keyword";
case CXXTokenTypeMultipleAnds: return "MultipleAnds";
case CXXTokenTypeMultipleColons: return "MultipleColons";
case CXXTokenTypeMultipleDots: return "MultipleDots";
case CXXTokenTypeNumber: return "Number";
case CXXTokenTypeOpeningBracket: return "OpeningBracket";
case CXXTokenTypeOpeningParenthesis: return "OpeningParenthesis";
case CXXTokenTypeOpeningSquareParenthesis: return "OpeningSquareParenthesis";
case CXXTokenTypeOperator: return "Operator";
case CXXTokenTypeParenthesisChain: return "ParenthesisChain";
case CXXTokenTypePointerOperator: return "PointerOperator";
case CXXTokenTypeSemicolon: return "Semicolon";
case CXXTokenTypeSingleColon: return "SingleColon";
case CXXTokenTypeSmallerThanSign: return "SmallerThanSign";
case CXXTokenTypeSquareParenthesisChain: return "SquareParenthesisChain";
case CXXTokenTypeStar: return "Star";
case CXXTokenTypeStringConstant: return "StringConstant";
case CXXTokenTypeUnknown: return "Unknown";
default: return "REALLY-UNKNOWN";
}
}
#endif
5 changes: 3 additions & 2 deletions parsers/cxx/cxx_parser_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ bool cxxParserExtractVariableDeclarations(CXXTokenChain * pChain,unsigned int uF
}

CXX_DEBUG_PRINT(
"Found notable token '%s' of type 0x%02x",
"Found notable token '%s' of type 0x%02x(%s)",
vStringValue(t->pszWord),
t->eType
t->eType,
cxxDebugTypeDecode(t->eType)
);

// Now before the notable token there MUST be an identifier
Expand Down
3 changes: 3 additions & 0 deletions parsers/cxx/cxx_token.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

// We assume that the compiler is capable of generating 32 bit wide enums
// This is used as enumeration but also as mask in several functions.
//
// DON'T FORGET TO RUN misc/gencxxtypedumper.sh after updating the elements.
//
enum CXXTokenType
{
CXXTokenTypeEOF = 1,
Expand Down
1 change: 1 addition & 0 deletions source.mak
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ PARSER_SRCS = \
parsers/cpreprocessor.c \
parsers/cxx/cxx.c \
parsers/cxx/cxx_debug.c \
parsers/cxx/cxx_debug_type.c \
parsers/cxx/cxx_keyword.c \
parsers/cxx/cxx_parser.c \
parsers/cxx/cxx_parser_block.c \
Expand Down
1 change: 1 addition & 0 deletions win32/ctags_vs2013.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
<ClCompile Include="..\main\read.c" />
<ClCompile Include="..\parsers\cxx\cxx.c" />
<ClCompile Include="..\parsers\cxx\cxx_debug.c" />
<ClCompile Include="..\parsers\cxx\cxx_debug_type.c" />
<ClCompile Include="..\parsers\cxx\cxx_keyword.c" />
<ClCompile Include="..\parsers\cxx\cxx_parser.c" />
<ClCompile Include="..\parsers\cxx\cxx_parser_block.c" />
Expand Down
3 changes: 3 additions & 0 deletions win32/ctags_vs2013.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@
<ClCompile Include="..\parsers\cxx\cxx_debug.c">
<Filter>Source Files\Main</Filter>
</ClCompile>
<ClCompile Include="..\parsers\cxx\cxx_debug_type.c">
<Filter>Source Files\Main</Filter>
</ClCompile>
<ClCompile Include="..\parsers\cxx\cxx_keyword.c">
<Filter>Source Files\Main</Filter>
</ClCompile>
Expand Down

0 comments on commit 869bb13

Please sign in to comment.