Preprocessing now errors out if extra tokens appear after #else and #end. Add preprocessor errors when preprocessor expressions are missing

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12464 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2011-02-16 20:48:48 +00:00
commit efd06d0668
7 changed files with 132 additions and 32 deletions

View file

@ -5,6 +5,13 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 2.0.2 (in progress)
===========================
2011-02-16: wsfulton
Preprocessing now errors out if extra tokens appear after #else and #end.
2011-02-16: wsfulton
Fix #1653092 Preprocessor does not error out when #elif is missing an expression.
This and other cases of missing preprocessor expressions now result in an error.
2011-02-14: wsfulton
[Ocaml] Apply patch #3151788 from Joel Reymont. Brings Ocaml support a up to date
(ver 3.11 and 3.12), including std::string.

View file

@ -382,7 +382,7 @@ example.i(4) : Syntax error in input.
<ul>
<li>201. Unable to find 'filename'.
<li>202. Could not evaluate 'expr'.
<li>202. Could not evaluate expression 'expr'.
</ul>
<H3><a name="Warnings_nn12"></a>14.9.3 C/C++ Parser (300-399)</H3>

View file

@ -84,8 +84,7 @@ pp_constant.i:49: Warning 305: Bad constant value (ignored).
:::::::::::::::::::::::::::::::: pp_defined.i :::::::::::::::::::::::::::::::::::
pp_defined.i:6: Error: No arguments given to defined()
pp_defined.i:6: Warning 202: Could not evaluate 'defined'
pp_defined.i:6: Warning 202: Error: 'Expected an expression'
pp_defined.i:6: Error: Missing expression for #if.
:::::::::::::::::::::::::::::::: pp_deprecated.i :::::::::::::::::::::::::::::::::::
pp_deprecated.i:4: Warning 101: %extern is deprecated. Use %import instead.
@ -119,6 +118,20 @@ pp_macro_expansion_multiline.i:30: Warning 509: as it is shadowed by bar(int *).
pp_macro_inline_unterminated.i:9: Error: Unterminated call invoking macro 'foo'
pp_macro_inline_unterminated.i:12: Error: Syntax error in input(3).
:::::::::::::::::::::::::::::::: pp_macro_missing_expression.i :::::::::::::::::::::::::::::::::::
pp_macro_missing_expression.i:4: Error: Missing identifier for #ifdef.
pp_macro_missing_expression.i:7: Error: Missing identifier for #ifndef.
pp_macro_missing_expression.i:10: Error: Missing expression for #if.
pp_macro_missing_expression.i:14: Error: Missing expression for #elif.
pp_macro_missing_expression.i:21: Error: Missing expression for #elif.
:::::::::::::::::::::::::::::::: pp_macro_unexpected_tokens.i :::::::::::::::::::::::::::::::::::
pp_macro_unexpected_tokens.i:5: Error: Unexpected tokens after #endif.
pp_macro_unexpected_tokens.i:8: Error: Unexpected tokens after #endif.
pp_macro_unexpected_tokens.i:11: Error: Unexpected tokens after #else.
pp_macro_unexpected_tokens.i:18: Error: Unexpected tokens after #endif.
pp_macro_unexpected_tokens.i:21: Error: Unexpected tokens after #else.
:::::::::::::::::::::::::::::::: pp_macro_nargs.i :::::::::::::::::::::::::::::::::::
pp_macro_nargs.i:7: Error: Macro 'foo' expects 2 arguments
pp_macro_nargs.i:8: Error: Macro 'foo' expects 2 arguments

View file

@ -36,6 +36,8 @@ pp_macro_defined_unterminated
pp_macro_expansion
pp_macro_expansion_multiline
pp_macro_inline_unterminated
pp_macro_missing_expression
pp_macro_unexpected_tokens
pp_macro_nargs
pp_macro_redef
pp_macro_rparen

View file

@ -0,0 +1,22 @@
// Test "Missing identifier for ..." errrors
%module xxx
#ifdef
#endif
#ifndef
#endif
#if
#endif
#if defined(AAA)
#elif
#endif
#define BBB
#if !defined(BBB)
#elif
#endif

View file

@ -0,0 +1,23 @@
// Test "Unexpected tokens after ..." errors
%module xxx
#ifndef AAA
#endif rubbish
#ifdef AAA
#endif rubbish
#ifdef AAA
#else rubbish
#endif
#define BBB
#ifdef BBB
#else
#endif rubbish
#if !defined(BBB)
#else rubbish
#endif

View file

@ -1100,6 +1100,14 @@ static DOH *Preprocessor_replace(DOH *s) {
} else if (Equal(kpp_hash_if, id) || Equal(kpp_hash_elif, id)) {
expand_defined_operator = 1;
Append(ns, id);
/*
} else if (Equal("%#if", id) || Equal("%#ifdef", id)) {
Swig_warning(998, Getfile(s), Getline(s), "Found: %s preprocessor directive.\n", id);
Append(ns, id);
} else if (Equal("#ifdef", id) || Equal("#ifndef", id)) {
Swig_warning(998, Getfile(s), Getline(s), "The %s preprocessor directive does not work in macros, try #if instead.\n", id);
Append(ns, id);
*/
} else if ((m = Getattr(symbols, id))) {
/* See if the macro is defined in the preprocessor symbol table */
DOH *args = 0;
@ -1544,9 +1552,14 @@ String *Preprocessor_parse(String *s) {
level++;
if (allow) {
start_level = level;
/* See if the identifier is in the hash table */
if (!Getattr(symbols, value))
if (Len(value) > 0) {
/* See if the identifier is in the hash table */
if (!Getattr(symbols, value))
allow = 0;
} else {
Swig_error(Getfile(s), Getline(id), "Missing identifier for #ifdef.\n");
allow = 0;
}
mask = 1;
}
} else if (Equal(id, kpp_ifndef)) {
@ -1554,9 +1567,14 @@ String *Preprocessor_parse(String *s) {
level++;
if (allow) {
start_level = level;
/* See if the identifier is in the hash table */
if (Getattr(symbols, value))
if (Len(value) > 0) {
/* See if the identifier is in the hash table */
if (Getattr(symbols, value))
allow = 0;
} else {
Swig_error(Getfile(s), Getline(id), "Missing identifier for #ifndef.\n");
allow = 0;
}
mask = 1;
}
} else if (Equal(id, kpp_else)) {
@ -1564,6 +1582,8 @@ String *Preprocessor_parse(String *s) {
Swig_error(Getfile(s), Getline(id), "Misplaced #else.\n");
} else {
cond_lines[level - 1] = Getline(id);
if (Len(value) != 0)
Swig_error(Getfile(s), Getline(id), "Unexpected tokens after #else.\n");
if (allow) {
allow = 0;
mask = 0;
@ -1578,6 +1598,8 @@ String *Preprocessor_parse(String *s) {
level = 0;
} else {
if (level < start_level) {
if (Len(value) != 0)
Swig_error(Getfile(s), Getline(id), "Unexpected tokens after #endif.\n");
allow = 1;
start_level--;
}
@ -1593,17 +1615,22 @@ String *Preprocessor_parse(String *s) {
start_level = level;
Seek(sval, 0, SEEK_SET);
/* Printf(stdout,"Evaluating '%s'\n", sval); */
val = Preprocessor_expr(sval, &e);
if (e) {
char *msg = Preprocessor_expr_error();
Seek(value, 0, SEEK_SET);
Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "Could not evaluate '%s'\n", value);
if (msg)
Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "Error: '%s'\n", msg);
allow = 0;
} else {
if (val == 0)
if (Len(sval) > 0) {
val = Preprocessor_expr(sval, &e);
if (e) {
char *msg = Preprocessor_expr_error();
Seek(value, 0, SEEK_SET);
Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "Could not evaluate expression '%s'\n", value);
if (msg)
Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "Error: '%s'\n", msg);
allow = 0;
} else {
if (val == 0)
allow = 0;
}
} else {
Swig_error(Getfile(s), Getline(id), "Missing expression for #if.\n");
allow = 0;
}
expand_defined_operator = 0;
mask = 1;
@ -1613,30 +1640,36 @@ String *Preprocessor_parse(String *s) {
Swig_error(Getfile(s), Getline(id), "Misplaced #elif.\n");
} else {
cond_lines[level - 1] = Getline(id);
expand_defined_operator = 1;
if (allow) {
allow = 0;
mask = 0;
} else if (level == start_level) {
int val;
String *sval = Preprocessor_replace(value);
String *sval;
expand_defined_operator = 1;
sval = Preprocessor_replace(value);
Seek(sval, 0, SEEK_SET);
val = Preprocessor_expr(sval, &e);
if (e) {
char *msg = Preprocessor_expr_error();
Seek(value, 0, SEEK_SET);
Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "Could not evaluate '%s'\n", value);
if (msg)
Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "Error: '%s'\n", msg);
allow = 0;
} else {
if (val)
allow = 1 * mask;
else
if (Len(sval) > 0) {
val = Preprocessor_expr(sval, &e);
if (e) {
char *msg = Preprocessor_expr_error();
Seek(value, 0, SEEK_SET);
Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "Could not evaluate expression '%s'\n", value);
if (msg)
Swig_warning(WARN_PP_EVALUATION, Getfile(value), Getline(value), "Error: '%s'\n", msg);
allow = 0;
} else {
if (val)
allow = 1 * mask;
else
allow = 0;
}
} else {
Swig_error(Getfile(s), Getline(id), "Missing expression for #elif.\n");
allow = 0;
}
expand_defined_operator = 0;
}
expand_defined_operator = 0;
}
} else if (Equal(id, kpp_warning)) {
if (allow) {