From aa728ece671c7e135d6d6dc7ab299dc6bfc78308 Mon Sep 17 00:00:00 2001 From: William S Fulton Date: Wed, 1 Sep 2010 21:06:10 +0000 Subject: [PATCH] Fix __LINE__ and __FILE__ expansion in macros with no arguments git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12197 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- .../test-suite/java/preproc_line_file_runme.java | 3 +++ Examples/test-suite/preproc_line_file.i | 10 ++++++++++ Source/Preprocessor/cpp.c | 16 ++++++++++------ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Examples/test-suite/java/preproc_line_file_runme.java b/Examples/test-suite/java/preproc_line_file_runme.java index e390b0c19..7200e4a53 100644 --- a/Examples/test-suite/java/preproc_line_file_runme.java +++ b/Examples/test-suite/java/preproc_line_file_runme.java @@ -45,5 +45,8 @@ public class preproc_line_file_runme { if (SillyStruct.LINE_NUMBER != 41) throw new RuntimeException("preproc failure"); + + if (SillyMacroClass.LINE_NUM != 45) + throw new RuntimeException("preproc failure"); } } diff --git a/Examples/test-suite/preproc_line_file.i b/Examples/test-suite/preproc_line_file.i index 1a75be83a..2ef43b017 100644 --- a/Examples/test-suite/preproc_line_file.i +++ b/Examples/test-suite/preproc_line_file.i @@ -40,3 +40,13 @@ struct SillyStruct { int num; static const int LINE_NUMBER = __LINE__; /* This is a C test case, but we can still use a C++ feature to wrap a constant to test __LINE__ here */ }; + +#define SILLY_CLASS struct SillyMacroClass { int num; static const int LINE_NUM = __LINE__; }; +SILLY_CLASS + +%{ +#define SILLY_CLASS struct SillyMacroClass { int num; }; +SILLY_CLASS +%} + + diff --git a/Source/Preprocessor/cpp.c b/Source/Preprocessor/cpp.c index 38a52dc3f..f044e344c 100644 --- a/Source/Preprocessor/cpp.c +++ b/Source/Preprocessor/cpp.c @@ -699,9 +699,12 @@ static String *get_options(String *str) { * * Perform macro expansion and return a new string. Returns NULL if some sort * of error occurred. + * name - name of the macro + * args - arguments passed to the macro + * line_file - only used for line/file name when reporting errors * ----------------------------------------------------------------------------- */ -static String *expand_macro(String *name, List *args) { +static String *expand_macro(String *name, List *args, String *line_file) { String *ns; DOH *symbols, *macro, *margs, *mvalue, *temp, *tempa, *e; int i, l; @@ -718,8 +721,8 @@ static String *expand_macro(String *name, List *args) { if (macro_level == 0) { /* Store the start of the macro should the macro contain __LINE__ and __FILE__ for expansion */ - macro_start_line = Getline(args); - macro_start_file = Getfile(args); + macro_start_line = Getline(args ? args : line_file); + macro_start_file = Getfile(args ? args : line_file); } macro_level++; @@ -1085,7 +1088,7 @@ static DOH *Preprocessor_replace(DOH *s) { } else { args = 0; } - e = expand_macro(id, args); + e = expand_macro(id, args, s); if (e) { Append(ns, e); } @@ -1151,8 +1154,9 @@ static DOH *Preprocessor_replace(DOH *s) { /* if (Getattr(m,"args")) { Swig_error(Getfile(id),Getline(id),"Macro arguments expected.\n"); } */ - e = expand_macro(id, 0); - Append(ns, e); + e = expand_macro(id, 0, s); + if (e) + Append(ns, e); Delete(e); } else { Append(ns, id);