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

CPreProcessor,C: skip #ifdef __cplusplus ~ #endif branch #2648

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 15 additions & 3 deletions parsers/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,21 @@ static void findAsmTags (void)
vString *operator = vStringNew ();
const unsigned char *line;

cppInit (false, false, false, false,
KIND_GHOST_INDEX, 0, KIND_GHOST_INDEX, KIND_GHOST_INDEX, 0, 0,
FIELD_UNKNOWN);
const struct cppInitData initData = {
.state = false,
.hasAtLiteralStrings = false,
.hasCxxRawLiteralStrings = false,
.hasSingleQuoteLiteralNumbers = false,
.defineMacroKindIndex = KIND_GHOST_INDEX,
.macroUndefRoleIndex = 0,
.macroParamKindIndex = KIND_GHOST_INDEX,
.macrodefFieldIndex = FIELD_UNKNOWN,
.headerKindIndex = KIND_GHOST_INDEX,
.headerSystemRoleIndex = 0,
.headerLocalRoleIndex = 0,
.skip__cplusplus_branch = false,
};
cppInit (&initData);

int lastMacroCorkIndex = CORK_NIL;

Expand Down
20 changes: 15 additions & 5 deletions parsers/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -3496,11 +3496,21 @@ static rescanReason findCTags (const unsigned int passCount)
role_for_header_local = VR_HEADER_LOCAL;
}

cppInit ((bool) (passCount > 1), isInputLanguage (Lang_csharp), isInputLanguage(Lang_cpp),
isInputLanguage(Lang_vera),
kind_for_define, role_for_macro_undef, kind_for_param,
kind_for_header, role_for_header_system, role_for_header_local,
FIELD_UNKNOWN);
const struct cppInitData initData = {
.state = (bool) (passCount > 1),
.hasAtLiteralStrings = isInputLanguage (Lang_csharp),
.hasCxxRawLiteralStrings = isInputLanguage(Lang_cpp),
.hasSingleQuoteLiteralNumbers = isInputLanguage(Lang_vera),
.defineMacroKindIndex = kind_for_define,
.macroUndefRoleIndex = role_for_macro_undef,
.macroParamKindIndex = kind_for_param,
.macrodefFieldIndex = FIELD_UNKNOWN,
.headerKindIndex = kind_for_header,
.headerSystemRoleIndex = role_for_header_system,
.headerLocalRoleIndex = role_for_header_local,
.skip__cplusplus_branch = !isInputLanguage (Lang_cpp),
};
cppInit (&initData);

Signature = vStringNew ();

Expand Down
104 changes: 60 additions & 44 deletions parsers/cpreprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ typedef struct sCppState {

int macrodefFieldIndex;

bool skip__cplusplus_branch;

struct sDirective {
enum eState state; /* current directive being processed */
bool accept; /* is a directive syntactically permitted? */
Expand Down Expand Up @@ -244,18 +246,9 @@ extern unsigned int cppGetDirectiveNestLevel (void)
return Cpp.directive.nestLevel;
}

static void cppInitCommon(langType clientLang,
const bool state, const bool hasAtLiteralStrings,
const bool hasCxxRawLiteralStrings,
const bool hasSingleQuoteLiteralNumbers,
int defineMacroKindIndex,
int macroUndefRoleIndex,
int macroParamKindIndex,
int headerKindIndex,
int headerSystemRoleIndex, int headerLocalRoleIndex,
int macrodefFieldIndex)
static void cppInitCommon(langType clientLang, const struct cppInitData *initData)
{
BraceFormat = state;
BraceFormat = initData->state;

CXX_DEBUG_PRINT("cppInit: brace format is %d",BraceFormat);

Expand All @@ -277,17 +270,17 @@ static void cppInitCommon(langType clientLang,
Cpp.charOrStringContents = vStringNew();

Cpp.resolveRequired = false;
Cpp.hasAtLiteralStrings = hasAtLiteralStrings;
Cpp.hasCxxRawLiteralStrings = hasCxxRawLiteralStrings;
Cpp.hasSingleQuoteLiteralNumbers = hasSingleQuoteLiteralNumbers;
Cpp.hasAtLiteralStrings = initData->hasAtLiteralStrings;
Cpp.hasCxxRawLiteralStrings = initData->hasCxxRawLiteralStrings;
Cpp.hasSingleQuoteLiteralNumbers = initData->hasSingleQuoteLiteralNumbers;

if (defineMacroKindIndex != KIND_GHOST_INDEX)
if (initData->defineMacroKindIndex != KIND_GHOST_INDEX)
{
Cpp.defineMacroKindIndex = defineMacroKindIndex;
Cpp.defineMacroKindIndex = initData->defineMacroKindIndex;
Cpp.useClientLangDefineMacroKindIndex = true;

Cpp.macroUndefRoleIndex = macroUndefRoleIndex;
Cpp.macrodefFieldIndex = macrodefFieldIndex;
Cpp.macroUndefRoleIndex = initData->macroUndefRoleIndex;
Cpp.macrodefFieldIndex = initData->macrodefFieldIndex;
}
else
{
Expand All @@ -298,9 +291,9 @@ static void cppInitCommon(langType clientLang,
Cpp.macrodefFieldIndex = CPreProFields [F_MACRODEF].ftype;
}

if (macroParamKindIndex != KIND_GHOST_INDEX)
if (initData->macroParamKindIndex != KIND_GHOST_INDEX)
{
Cpp.macroParamKindIndex = macroParamKindIndex;
Cpp.macroParamKindIndex = initData->macroParamKindIndex;
Cpp.useClientLangMacroParamKindIndex = true;
}
else
Expand All @@ -309,13 +302,13 @@ static void cppInitCommon(langType clientLang,
Cpp.useClientLangMacroParamKindIndex = false;
}

if (headerKindIndex != KIND_GHOST_INDEX)
if (initData->headerKindIndex != KIND_GHOST_INDEX)
{
Cpp.headerKindIndex = headerKindIndex;
Cpp.headerKindIndex = initData->headerKindIndex;
Cpp.useClientLangHeaderKindIndex = true;

Cpp.headerSystemRoleIndex = headerSystemRoleIndex;
Cpp.headerLocalRoleIndex = headerLocalRoleIndex;
Cpp.headerSystemRoleIndex = initData->headerSystemRoleIndex;
Cpp.headerLocalRoleIndex = initData->headerLocalRoleIndex;
}
else
{
Expand All @@ -326,6 +319,8 @@ static void cppInitCommon(langType clientLang,
Cpp.headerLocalRoleIndex = CPREPRO_HEADER_KIND_LOCAL_ROLE;
}

Cpp.skip__cplusplus_branch = initData->skip__cplusplus_branch;

Cpp.directive.state = DRCTV_NONE;
Cpp.directive.accept = true;
Cpp.directive.nestLevel = 0;
Expand All @@ -343,23 +338,10 @@ static void cppInitCommon(langType clientLang,
: NULL;
}

extern void cppInit (const bool state, const bool hasAtLiteralStrings,
const bool hasCxxRawLiteralStrings,
const bool hasSingleQuoteLiteralNumbers,
int defineMacroKindIndex,
int macroUndefRoleIndex,
int macroParamKindIndex,
int headerKindIndex,
int headerSystemRoleIndex, int headerLocalRoleIndex,
int macrodefFieldIndex)
extern void cppInit (const struct cppInitData *initData)
{
langType client = getInputLanguage ();

cppInitCommon (client, state, hasAtLiteralStrings,
hasCxxRawLiteralStrings, hasSingleQuoteLiteralNumbers,
defineMacroKindIndex, macroUndefRoleIndex, macroParamKindIndex,
headerKindIndex, headerSystemRoleIndex, headerLocalRoleIndex,
macrodefFieldIndex);
cppInitCommon (client, initData);
}

extern void cppTerminate (void)
Expand Down Expand Up @@ -953,10 +935,32 @@ static void directivePragma (int c)
Cpp.directive.state = DRCTV_NONE;
}

static bool isDefCondition (const int c, const char *condition)
{
if (*condition == '\0')
return true;
else if (c == EOF)
return false;

if (*condition != '\0' && c == condition[0])
{
const int next = cppGetcFromUngetBufferOrFile ();
return isDefCondition (next, condition + 1);
}

return false;
}

static bool directiveIf (const int c)
{
DebugStatement ( const bool ignore0 = isIgnore (); )
const bool ignore = pushConditional ((bool) (c != '0'));
bool firstBranchChosen = true;

if (c == '0'
|| (Cpp.skip__cplusplus_branch && isDefCondition (c, "__cplusplus")))
firstBranchChosen = false;

const bool ignore = pushConditional (firstBranchChosen);

Cpp.directive.state = DRCTV_NONE;
DebugStatement ( debugCppNest (true, Cpp.directive.nestLevel);
Expand Down Expand Up @@ -1634,10 +1638,22 @@ extern int cppGetc (void)

static void findCppTags (void)
{
cppInitCommon (Cpp.lang, 0, false, false, false,
KIND_GHOST_INDEX, 0, KIND_GHOST_INDEX,
KIND_GHOST_INDEX, 0, 0,
FIELD_UNKNOWN);
struct cppInitData initData = {
.state = 0,
.hasAtLiteralStrings = false,
.hasCxxRawLiteralStrings = false,
.hasSingleQuoteLiteralNumbers = false,
.defineMacroKindIndex = KIND_GHOST_INDEX,
.macroUndefRoleIndex = 0,
.macroParamKindIndex = KIND_GHOST_INDEX,
.macrodefFieldIndex = FIELD_UNKNOWN,
.headerKindIndex = KIND_GHOST_INDEX,
.headerSystemRoleIndex = 0,
.headerLocalRoleIndex = 0,
.skip__cplusplus_branch = false,
};

cppInitCommon (Cpp.lang, &initData);

findRegexTagsMainloop (cppGetc);

Expand Down
31 changes: 20 additions & 11 deletions parsers/cpreprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,28 @@
extern bool cppIsBraceFormat (void);
extern unsigned int cppGetDirectiveNestLevel (void);

/* Don't forget to set useCort true in your parser.
/* Don't forget to add CORK_QUEUE to useCork in your parser.
* The corkQueue is needed to capture macro parameters.
*/
extern void cppInit (const bool state,
const bool hasAtLiteralStrings,
const bool hasCxxRawLiteralStrings,
const bool hasSingleQuoteLiteralNumbers,
int defineMacroKindIndex,
int macroUndefRoleIndex,
int headerKindIndex,
int headerSystemRoleIndex, int headerLocalRoleIndex,
int macroParamKindIndex,
int macrodefFieldIndex);
struct cppInitData {
bool state;
bool hasAtLiteralStrings;
bool hasCxxRawLiteralStrings;
bool hasSingleQuoteLiteralNumbers;

int defineMacroKindIndex;
int macroUndefRoleIndex;
int macroParamKindIndex;
int macrodefFieldIndex;

int headerKindIndex;
int headerSystemRoleIndex;
int headerLocalRoleIndex;

bool skip__cplusplus_branch; /* #ifdef __cplusplus ... #endif */
};

extern void cppInit (const struct cppInitData *initData);

extern void cppTerminate (void);
extern void cppBeginStatement (void);
Expand Down
38 changes: 21 additions & 17 deletions parsers/cxx/cxx_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1821,7 +1821,8 @@ bool cxxParserParseIfForWhileSwitchCatchParenthesis(void)
return true;
}

static rescanReason cxxParserMain(const unsigned int passCount)
static rescanReason cxxParserMain(const unsigned int passCount,
bool cppSkip__cpluscplus_branch)
{
cxxScopeClear();
cxxTokenAPINewFile();
Expand All @@ -1836,19 +1837,22 @@ static rescanReason cxxParserMain(const unsigned int passCount)

Assert(passCount < 3);

cppInit(
(bool) (passCount > 1),
false,
true, // raw literals
false,
kind_for_define,
role_for_macro_undef,
kind_for_macro_param,
kind_for_header,
role_for_header_system,
role_for_header_local,
g_cxx.pFieldOptions[CXXTagFieldMacrodef].ftype
);
const struct cppInitData initData = {
.state = (bool) (passCount > 1),
.hasAtLiteralStrings = false,
.hasCxxRawLiteralStrings = true, // raw literals
.hasSingleQuoteLiteralNumbers = false,
.defineMacroKindIndex = kind_for_define,
.macroUndefRoleIndex = role_for_macro_undef,
.macroParamKindIndex = kind_for_macro_param,
.macrodefFieldIndex = g_cxx.pFieldOptions[CXXTagFieldMacrodef].ftype,
.headerKindIndex = kind_for_header,
.headerSystemRoleIndex = role_for_header_system,
.headerLocalRoleIndex = role_for_header_local,
.skip__cplusplus_branch = cppSkip__cpluscplus_branch,
};

cppInit(&initData);

g_cxx.iChar = ' ';

Expand Down Expand Up @@ -1883,7 +1887,7 @@ rescanReason cxxCParserMain(const unsigned int passCount)
g_cxx.bConfirmedCPPLanguage = false;
cxxKeywordEnablePublicProtectedPrivate(false);

rescanReason r = cxxParserMain(passCount);
rescanReason r = cxxParserMain(passCount, true);
CXX_DEBUG_LEAVE();
return r;
}
Expand All @@ -1897,7 +1901,7 @@ rescanReason cxxCUDAParserMain(const unsigned int passCount)
g_cxx.bConfirmedCPPLanguage = false;
cxxKeywordEnablePublicProtectedPrivate(false);

rescanReason r = cxxParserMain(passCount);
rescanReason r = cxxParserMain(passCount, true);
CXX_DEBUG_LEAVE();
return r;
}
Expand All @@ -1913,7 +1917,7 @@ rescanReason cxxCppParserMain(const unsigned int passCount)
g_cxx.bConfirmedCPPLanguage = !isInputHeaderFile();
cxxKeywordEnablePublicProtectedPrivate(g_cxx.bConfirmedCPPLanguage);

rescanReason r = cxxParserMain(passCount);
rescanReason r = cxxParserMain(passCount, false);
CXX_DEBUG_LEAVE();
return r;
}
Expand Down
19 changes: 15 additions & 4 deletions parsers/dts.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,21 @@ static tagRegexTable dtsTagRegexTable [] = {
*/
static void runCppGetc (void)
{
cppInit (false, false, false, false,
KIND_GHOST_INDEX, 0, KIND_GHOST_INDEX,
KIND_GHOST_INDEX, 0, 0,
FIELD_UNKNOWN);
const struct cppInitData initData = {
.state = false,
.hasAtLiteralStrings = false,
.hasCxxRawLiteralStrings = false,
.hasSingleQuoteLiteralNumbers = false,
.defineMacroKindIndex = KIND_GHOST_INDEX,
.macroUndefRoleIndex = 0,
.macroParamKindIndex = KIND_GHOST_INDEX,
.macrodefFieldIndex = FIELD_UNKNOWN,
.headerKindIndex = KIND_GHOST_INDEX,
.headerSystemRoleIndex = 0,
.headerLocalRoleIndex = 0,
.skip__cplusplus_branch = false,
};
cppInit (&initData);

findRegexTagsMainloop (cppGetc);

Expand Down
19 changes: 15 additions & 4 deletions parsers/ldscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,10 +696,21 @@ static void findLdScriptTags (void)
tokenInfo *const token = newLdScriptToken ();
tokenInfo *const tmp = newLdScriptToken ();

cppInit (false, false, false, false,
KIND_GHOST_INDEX, 0, KIND_GHOST_INDEX,
KIND_GHOST_INDEX, 0, 0,
FIELD_UNKNOWN);
const struct cppInitData initData = {
.state = false,
.hasAtLiteralStrings = false,
.hasCxxRawLiteralStrings = false,
.hasSingleQuoteLiteralNumbers = false,
.defineMacroKindIndex = KIND_GHOST_INDEX,
.macroUndefRoleIndex = 0,
.macroParamKindIndex = KIND_GHOST_INDEX,
.macrodefFieldIndex = FIELD_UNKNOWN,
.headerKindIndex = KIND_GHOST_INDEX,
.headerSystemRoleIndex = 0,
.headerLocalRoleIndex = 0,
.skip__cplusplus_branch = false,
};
cppInit (&initData);

do {
tokenRead (token);
Expand Down
Loading