Skip to content

Commit

Permalink
[SELF INCOMPATIBILITY] CPreProcessor: fix #1514
Browse files Browse the repository at this point in the history
Old behavior:

When --language=C++ is specified, C preprocessor macro definitions in a source
code is not captured.

Because CPreProcessor parser captures the definitions and is disabled
with --language=C++ option.

As reported in #1514, this behavior makes use confused.

    $ ./ctags --list-kinds=C++
    d  macro definitions
    e  enumerators (values inside an enumeration)
    f  function definitions

Though CPreProcessor parser captures macro definitions, macro kind is
listed in --list-kinds=C++. A user may understand "macro kind" is
part of C++ parser, and the understanding looks reasonable.

New behavior:

In the following condition, CPreProcessor related kind language objects are recorded
even if CPreProcessor is disabled:

     The kinds are listed in --list-kinds output of a parser, that
     uses CPreProcessor parser.

C and C++ meet the condition.
DTS doesn't meet the condition.

Close #1514.

Signed-off-by: Masatake YAMATO <[email protected]>
  • Loading branch information
masatake committed Oct 1, 2017
1 parent bd40ebd commit 90da35f
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See #1514.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--languages=-CPreProcessor
--kinds-C=-d

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
int
main(int argc)
{
return X;
return X;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See #1514.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--languages=-CPreProcessor
--kinds-CPreProcessor=-d

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
X input.c /^#define X /;" d file:
main input.c /^main(int argc)$/;" f typeref:typename:int
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define X 1

int
main(int argc)
{
return X;
}
1 change: 1 addition & 0 deletions Units/parser-cpreprocessor.r/disable-cpp-client.d/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
See #1514.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
X input.c /^#define X /;" d file:
main input.c /^main(int argc)$/;" f typeref:typename:int
7 changes: 7 additions & 0 deletions Units/parser-cpreprocessor.r/disable-cpp-client.d/input.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define X 1

int
main(int argc)
{
return X;
}
1 change: 1 addition & 0 deletions Units/parser-cpreprocessor.r/disable-cpp-cpp.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--languages=-CPreProcessor
3 changes: 3 additions & 0 deletions Units/parser-cpreprocessor.r/disable-cpp-cpp.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0x00 input.dts /^ phandle = <0x00>;$/;" p label:label
label input.dts /^label: test {$/;" l
label2 input.dts /^ label2: chosen { } ;$/;" l label:label
12 changes: 12 additions & 0 deletions Units/parser-cpreprocessor.r/disable-cpp-cpp.d/input.dts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/dts-v1/;

#include "foo.dtsi"
#define bar 98

label: test {
dummy {
label2: chosen { } ;
phandle = <0x00>;
};
};
#undef bar
42 changes: 35 additions & 7 deletions parsers/cpreprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enum eState {
*/
typedef struct sCppState {
langType lang;
langType clientLang;

int * ungetBuffer; /* memory buffer for unget characters */
int ungetBufferSize; /* the current unget buffer size */
Expand Down Expand Up @@ -158,6 +159,7 @@ void cppPopExternalParserBlock()

static cppState Cpp = {
.lang = LANG_IGNORE,
.clientLang = LANG_IGNORE,
.ungetBuffer = NULL,
.ungetBufferSize = 0,
.ungetPointer = NULL,
Expand Down Expand Up @@ -201,7 +203,8 @@ extern unsigned int cppGetDirectiveNestLevel (void)
return Cpp.directive.nestLevel;
}

extern void cppInit (const bool state, const bool hasAtLiteralStrings,
static void cppInitCommon(langType clientLang,
const bool state, const bool hasAtLiteralStrings,
const bool hasCxxRawLiteralStrings,
const bool hasSingleQuoteLiteralNumbers,
const kindDefinition *defineMacroKind,
Expand All @@ -223,6 +226,7 @@ extern void cppInit (const bool state, const bool hasAtLiteralStrings,
initializeParser (t);
}

Cpp.clientLang = clientLang;
Cpp.ungetBuffer = NULL;
Cpp.ungetPointer = NULL;

Expand Down Expand Up @@ -253,6 +257,22 @@ extern void cppInit (const bool state, const bool hasAtLiteralStrings,
Cpp.directive.name = vStringNewOrClear (Cpp.directive.name);
}

extern void cppInit (const bool state, const bool hasAtLiteralStrings,
const bool hasCxxRawLiteralStrings,
const bool hasSingleQuoteLiteralNumbers,
const kindDefinition *defineMacroKind,
int macroUndefRoleIndex,
const kindDefinition *headerKind,
int headerSystemRoleIndex, int headerLocalRoleIndex)
{
langType client = getInputLanguage ();

cppInitCommon (client, state, hasAtLiteralStrings,
hasCxxRawLiteralStrings, hasSingleQuoteLiteralNumbers,
defineMacroKind, macroUndefRoleIndex, headerKind,
headerSystemRoleIndex, headerLocalRoleIndex);
}

extern void cppTerminate (void)
{
if (Cpp.directive.name != NULL)
Expand All @@ -266,6 +286,8 @@ extern void cppTerminate (void)
eFree(Cpp.ungetBuffer);
Cpp.ungetBuffer = NULL;
}

Cpp.clientLang = LANG_IGNORE;
}

extern void cppBeginStatement (void)
Expand Down Expand Up @@ -584,8 +606,11 @@ static int makeDefineTag (const char *const name, const char* const signature, b
{
const bool isFileScope = (bool) (! isInputHeaderFile ());

if (!isLanguageEnabled (Cpp.lang))
return CORK_NIL;
if (!isLanguageEnabled (
doesCPreProRunAsStandaloneParser(CPREPRO_MACRO)
? Cpp.lang
: Cpp.clientLang))
return CORK_NIL;

if (!Cpp.defineMacroKind)
return CORK_NIL;
Expand Down Expand Up @@ -633,8 +658,11 @@ static void makeIncludeTag (const char *const name, bool systemHeader)
tagEntryInfo e;
int role_index;

if (!isLanguageEnabled (Cpp.lang))
return;
if (!isLanguageEnabled (
doesCPreProRunAsStandaloneParser(CPREPRO_HEADER)
? Cpp.lang
: Cpp.clientLang))
return;

role_index = systemHeader? Cpp.headerSystemRoleIndex: Cpp.headerLocalRoleIndex;
if (role_index == ROLE_INDEX_DEFINITION)
Expand Down Expand Up @@ -1254,8 +1282,8 @@ extern int cppGetc (void)

static void findCppTags (void)
{
cppInit (0, false, false, false,
NULL, 0, NULL, 0, 0);
cppInitCommon (Cpp.lang, 0, false, false, false,
NULL, 0, NULL, 0, 0);

findRegexTagsMainloop (cppGetc);

Expand Down

0 comments on commit 90da35f

Please sign in to comment.