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

Fix CDT parser include file handling and support recursion for includes #639

Draft
wants to merge 8 commits into
base: dev
Choose a base branch
from
19 changes: 19 additions & 0 deletions trunk/examples/CParser/include/directoryHeaderOne.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Header file that can be included as source file in test C programs where
* this source file is located using one of the
*
* # include "q-char-sequence" new-line
* # include <h-char-sequence> new-line
*
* preprocessor directives (see C standard, section 6.10.2).
*/

typedef struct ret {
int code;
} ret_t;

static inline ret_t funcFromDirectoryHeaderOne(void)
{
ret_t ret = { .code = 2 };
return ret;
}
15 changes: 15 additions & 0 deletions trunk/examples/CParser/include/directoryHeaderTwo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Header file that can be included as source file in test C programs where
* this source file is located using one of the
*
* # include "q-char-sequence" new-line
* # include <h-char-sequence> new-line
*
* preprocessor directives (see C standard, section 6.10.2).
*/

static inline int funcFromDirectoryHeaderTwo(void)
{
int var = 4;
return var;
}
11 changes: 11 additions & 0 deletions trunk/examples/CParser/include/subdirectory/additionalHeader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Header file that can be included as source file in test C programs where
* this source file is located using one of the
*
* # include "q-char-sequence" new-line
* # include <h-char-sequence> new-line
*
* preprocessor directives (see C standard, section 6.10.2).
*/

int groundTruth = 17;
32 changes: 32 additions & 0 deletions trunk/examples/CParser/includeHeadersLocal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//#Safe
/**
* This test program includes a named header file that is searched for in an
* implementation-defined manner (see C standard, section 6.10.2):
*
* # include <h-char-sequence> new-line
* # include "q-char-sequence" new-line
*
* The first directive searches a sequence of implementation-defined places for
* a header identified uniquely by the specified sequence between the '<' and
* '>' delimiters. The second directive causes the replacement of that
* directive by the entire contents of the source file identified by the
* specified sequence between the '"' delimiters. The named source file is
* searched for in an implementation-defined manner.
*
* As usual, a C preprocessor searches for the named source file first in the
* directory containing the current file, then in the same directories
* specified with a sequence between the '<' and '>' delimiters.
*/

#include "localHeaderOne.h"
#include "localHeaderTwo.h"

int main(void)
{
status_t funcOneRet = funcFromLocalHeaderOne();
int funcOneVal = funcOneRet.retval;
//@ assert(funcOneVal == 3);
int funcTwoVal = funcFromLocalHeaderTwo();
//@ assert(funcTwoVal == 5);
return funcOneVal + funcTwoVal;
}
32 changes: 32 additions & 0 deletions trunk/examples/CParser/includeHeadersNonRecursiveSimple.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//#Safe
/**
* This test program includes a named header file that is searched for in an
* implementation-defined manner (see C standard, section 6.10.2):
*
* # include <h-char-sequence> new-line
* # include "q-char-sequence" new-line
*
* The first directive searches a sequence of implementation-defined places for
* a header identified uniquely by the specified sequence between the '<' and
* '>' delimiters. The second directive causes the replacement of that
* directive by the entire contents of the source file identified by the
* specified sequence between the '"' delimiters. The named source file is
* searched for in an implementation-defined manner.
*
* As usual, a C preprocessor searches for the named source file first in the
* directory containing the current file, then in the same directories
* specified with a sequence between the '<' and '>' delimiters.
*/

#include <directoryHeaderOne.h>
#include <directoryHeaderTwo.h>

int main(void)
{
ret_t funcOneRet = funcFromDirectoryHeaderOne();
int funcOneVal = funcOneRet.code;
//@ assert(funcOneVal == 2);
int funcTwoVal = funcFromDirectoryHeaderTwo();
//@ assert(funcTwoVal == 4);
return funcOneVal + funcTwoVal;
}
34 changes: 34 additions & 0 deletions trunk/examples/CParser/includeHeadersNonRecursiveSubdirectory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//#Safe
/**
* This test program includes a named header file that is searched for in an
* implementation-defined manner (see C standard, section 6.10.2):
*
* # include <h-char-sequence> new-line
* # include "q-char-sequence" new-line
*
* The first directive searches a sequence of implementation-defined places for
* a header identified uniquely by the specified sequence between the '<' and
* '>' delimiters. The second directive causes the replacement of that
* directive by the entire contents of the source file identified by the
* specified sequence between the '"' delimiters. The named source file is
* searched for in an implementation-defined manner.
*
* As usual, a C preprocessor searches for the named source file first in the
* directory containing the current file, then in the same directories
* specified with a sequence between the '<' and '>' delimiters.
*/

#include <directoryHeaderOne.h>
#include <directoryHeaderTwo.h>
#include <additionalHeader.h>

int main(void)
{
ret_t funcOneRet = funcFromDirectoryHeaderOne();
int funcOneVal = funcOneRet.code;
//@ assert(funcOneVal == 2);
int funcTwoVal = funcFromDirectoryHeaderTwo();
//@ assert(funcTwoVal == 4);
//@ assert(groundTruth == 17);
return funcOneVal + funcTwoVal + groundTruth;
}
34 changes: 34 additions & 0 deletions trunk/examples/CParser/includeHeadersRecursive.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//#Safe
/**
* This test program includes a named header file that is searched for in an
* implementation-defined manner (see C standard, section 6.10.2):
*
* # include <h-char-sequence> new-line
* # include "q-char-sequence" new-line
*
* The first directive searches a sequence of implementation-defined places for
* a header identified uniquely by the specified sequence between the '<' and
* '>' delimiters. The second directive causes the replacement of that
* directive by the entire contents of the source file identified by the
* specified sequence between the '"' delimiters. The named source file is
* searched for in an implementation-defined manner.
*
* As usual, a C preprocessor searches for the named source file first in the
* directory containing the current file, then in the same directories
* specified with a sequence between the '<' and '>' delimiters.
*/

#include <directoryHeaderOne.h>
#include <directoryHeaderTwo.h>
#include <additionalHeader.h>

int main(void)
{
ret_t funcOneRet = funcFromDirectoryHeaderOne();
int funcOneVal = funcOneRet.code;
//@ assert(funcOneVal == 2);
int funcTwoVal = funcFromDirectoryHeaderTwo();
//@ assert(funcTwoVal == 4);
//@ assert(groundTruth == 17);
return funcOneVal + funcTwoVal + groundTruth;
}
18 changes: 18 additions & 0 deletions trunk/examples/CParser/localHeaderOne.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Header file that can be included as source file in test C programs where
* this source file is located using the
*
* # include "q-char-sequence" new-line
*
* preprocessor directive (see C standard, section 6.10.2).
*/

typedef struct status {
int retval;
} status_t;

static inline status_t funcFromLocalHeaderOne(void)
{
status_t ret = { .retval = 3 };
return ret;
}
14 changes: 14 additions & 0 deletions trunk/examples/CParser/localHeaderTwo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Header file that can be included as source file in test C programs where
* this source file is located using the
*
* # include "q-char-sequence" new-line
*
* preprocessor directive (see C standard, section 6.10.2).
*/

static inline int funcFromLocalHeaderTwo(void)
{
int var = 5;
return var;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//#Safe
/*-----------------------------------------------------------------------------
* C program with preprocessor macros to test macro parsing and substitution
*-----------------------------------------------------------------------------
* Author: Manuel Bentele
* Date: 2023-09-25
*---------------------------------------------------------------------------*/

#undef SELECT_FUNC_ONE

#define VALUE 100

int compute_one(int num)
{
return num - VALUE;
}

int compute_two(int num)
{
return num + VALUE;
}

#ifdef SELECT_FUNC_ONE
#define FUNC(VAL) compute_one(VAL)
#else
#define FUNC(VAL) compute_two(VAL)
#endif

int main(void)
{
int ret = FUNC(VALUE);
//@ assert(ret == 100 + 100);
return ret;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//#Unafe
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//#Unsafe

Copy link
Member Author

@bahnwaerter bahnwaerter Nov 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out the invalid program correctness specifier.

/*-----------------------------------------------------------------------------
* C program with preprocessor macros to test macro parsing and substitution
*-----------------------------------------------------------------------------
* Author: Manuel Bentele
* Date: 2023-09-25
*---------------------------------------------------------------------------*/

#define SELECT_FUNC_ONE

#define VALUE 100

int compute_one(int num)
{
return num - VALUE;
}

int compute_two(int num)
{
return num + VALUE;
}

#ifdef SELECT_FUNC_ONE
#define FUNC(VAL) compute_one(VAL)
#else
#define FUNC(VAL) compute_two(VAL)
#endif

int main(void)
{
int ret = FUNC(VALUE);
//@ assert(ret == 100 + 100);
return ret;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//#Safe
/*-----------------------------------------------------------------------------
* C program with preprocessor macro to test macro parsing and substitution
*-----------------------------------------------------------------------------
* Author: Manuel Bentele
* Date: 2023-09-25
*---------------------------------------------------------------------------*/

#define VALUE 100

int main(void)
{
int ret = VALUE;
//@ assert(ret == 100);
return ret;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//#Unsafe
/*-----------------------------------------------------------------------------
* C program with preprocessor macro to test macro parsing and substitution
*-----------------------------------------------------------------------------
* Author: Manuel Bentele
* Date: 2023-09-25
*---------------------------------------------------------------------------*/

#define VALUE 100

int main(void)
{
int ret = VALUE;
//@ assert(ret == 200);
return ret;
}
Loading