diff options
author | Sven Gothel <[email protected]> | 2010-11-07 07:24:32 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2010-11-07 07:24:32 +0100 |
commit | e7fe471e5899cfb22d40234242c7baea738d87fc (patch) | |
tree | ac70667492abfda92da21dbbdd81835fcf04f946 | |
parent | ed83567444bd803918b8904eb71b155d4eff2de4 (diff) |
PCPP: Fix ifdef/ifndef, if, elif and endif correct ; Enhanced PCPP tests
-rw-r--r-- | src/java/com/jogamp/gluegen/pcpp/PCPP.java | 55 | ||||
-rw-r--r-- | test/junit/com/jogamp/gluegen/PCPPTest.java | 29 | ||||
-rw-r--r-- | test/junit/com/jogamp/gluegen/pcpptest.h | 37 |
3 files changed, 97 insertions, 24 deletions
diff --git a/src/java/com/jogamp/gluegen/pcpp/PCPP.java b/src/java/com/jogamp/gluegen/pcpp/PCPP.java index ab2c8b2..966c860 100644 --- a/src/java/com/jogamp/gluegen/pcpp/PCPP.java +++ b/src/java/com/jogamp/gluegen/pcpp/PCPP.java @@ -744,27 +744,35 @@ public class PCPP { private void handleIfdef(boolean isIfdef) throws IOException { // Next token is the name of the #ifdef String symbolName = nextWord(); - debugPrint(true, (isIfdef ? "#ifdef " : "#ifndef ") + symbolName); + + boolean enabledStatusBefore = enabled(); // condition or true boolean symbolIsDefined = defineMap.get(symbolName) != null; - debugPrint(true, (isIfdef ? "#ifdef " : "#ifndef ") + symbolName + "(defined: "+symbolIsDefined+")"); - pushEnableBit(enabled() && symbolIsDefined == isIfdef); + + debugPrint(false, "#" + (isIfdef ? "ifdef " : "ifndef ") + symbolName + ", enabledBefore " + enabledStatusBefore + ", isDefined " + symbolIsDefined + ", file \"" + filename() + " line " + lineNumber()); + + boolean enabledNow = enabled() && symbolIsDefined == isIfdef ; + pushEnableBit( enabledNow ) ; // condition + pushEnableBit( enabledNow ) ; // block } /** Handles #else directives */ private void handleElse() throws IOException { - boolean enabledStatusBeforeElse = enabled(); - popEnableBit(); - pushEnableBit(enabled() && !enabledStatusBeforeElse); - debugPrint(true, "#else "); + popEnableBit(); // block + boolean enabledStatusBefore = enabled(); // condition or true + debugPrint(false, "#else, enabledBefore " + enabledStatusBefore + ", file \"" + filename() + " line " + lineNumber()); + popEnableBit(); // condition + pushEnableBit(!enabledStatusBefore); // don't care + pushEnableBit(!enabledStatusBefore); // block } private void handleEndif() { - boolean enabledBeforePopping = enabled(); - popEnableBit(); + popEnableBit(); // block + boolean enabledStatusBefore = enabled(); + popEnableBit(); // condition // print the endif if we were enabled prior to popEnableBit() (sending // false to debugPrint means "print regardless of current enabled() state). - debugPrint(!enabledBeforePopping, "#endif/end-else"); + debugPrint(false, "#endif, enabledBefore " + enabledStatusBefore); } /** @@ -772,14 +780,26 @@ public class PCPP { * processing #elif. */ private void handleIf(boolean isIf) throws IOException { - //System.out.println("IN HANDLE_" + (isIf ? "IF" : "ELIF") + " file \"" + filename() + " line " + lineNumber()); - debugPrint(true, (isIf ? "#if" : "#elif")); - boolean defineEvaluatedToTrue = handleIfRecursive(true); if (!isIf) { - popEnableBit(); + popEnableBit(); // block + } + boolean enabledStatusBefore = enabled(); // condition or true + boolean defineEvaluatedToTrue = handleIfRecursive(true); + + debugPrint(false, "#" + (isIf ? "if" : "elif") + ", enabledBefore " + enabledStatusBefore + ", eval " + defineEvaluatedToTrue + ", file \"" + filename() + " line " + lineNumber()); + + boolean enabledNow; + + if(isIf) { + enabledNow = enabledStatusBefore && defineEvaluatedToTrue ; + pushEnableBit( enabledNow ) ; // condition + pushEnableBit( enabledNow ) ; // block + } else { + popEnableBit(); // condition + enabledNow = !enabledStatusBefore && defineEvaluatedToTrue ; + pushEnableBit( enabledStatusBefore || enabledNow ) ; // condition: pass prev true condition + pushEnableBit( enabledNow ) ; // block } - pushEnableBit(enabled() && defineEvaluatedToTrue); - //System.out.println("OUT HANDLE_" + (isIf ? "IF" : "ELIF") +" (evaluated to " + defineEvaluatedToTrue + ")"); } //static int tmp = -1; @@ -1005,8 +1025,7 @@ public class PCPP { private void popEnableBit() { if (enabledBits.isEmpty()) { - LOG.warning("mismatched #ifdef/endif pairs"); - return; + throw new RuntimeException("mismatched #ifdef/endif pairs (line " + lineNumber() + " file " + filename() + ")"); } enabledBits.remove(enabledBits.size() - 1); --debugPrintIndentLevel; diff --git a/test/junit/com/jogamp/gluegen/PCPPTest.java b/test/junit/com/jogamp/gluegen/PCPPTest.java index c1f12ab..fc59f9a 100644 --- a/test/junit/com/jogamp/gluegen/PCPPTest.java +++ b/test/junit/com/jogamp/gluegen/PCPPTest.java @@ -47,7 +47,7 @@ public class PCPPTest { @Test public void pcppMacroDefinitionTest() throws FileNotFoundException, IOException { - PCPP pp = new PCPP(Collections.<String>emptyList()); + PCPP pp = new PCPP(Collections.<String>emptyList(), false); ByteArrayOutputStream output = new ByteArrayOutputStream(); pp.setOut(output); @@ -58,12 +58,30 @@ public class PCPPTest { String expected = "# 1 \"pcpptest.h\""+ "# define CL_SCHAR_MIN (-127-1)"+ - " cl_char __attribute__(( aligned(2))) s[ 2];"+ - "# 7 \"pcpptest.h\""; + "# define __YES__ 1"+ + "# 16 \"pcpptest.h\""+ + "# 26 \"pcpptest.h\""+ + "# 36 \"pcpptest.h\""+ + " cl_char GOOD_A;"+ + " int GOOD_B;"+ + " int GOOD_C;"+ + "# 40 \"pcpptest.h\""; + output.flush(); String result = output.toString(); output.close(); + System.err.println("Expected: "); + System.err.println("-------------------------------"); + System.err.println(killWhitespace(expected)); + System.err.println("-------------------------------"); + System.err.println(); + System.err.println("Result: "); + System.err.println("-------------------------------"); + System.err.println(killWhitespace(result)); + System.err.println("-------------------------------"); + System.err.println(); + assertEquals(killWhitespace(expected), killWhitespace(result)); } @@ -72,5 +90,8 @@ public class PCPPTest { return a.replaceAll("\\p{javaWhitespace}+", ""); } - + public static void main(String args[]) throws IOException { + String tstname = PCPPTest.class.getName(); + org.junit.runner.JUnitCore.main(tstname); + } } diff --git a/test/junit/com/jogamp/gluegen/pcpptest.h b/test/junit/com/jogamp/gluegen/pcpptest.h index 58b8935..5009d04 100644 --- a/test/junit/com/jogamp/gluegen/pcpptest.h +++ b/test/junit/com/jogamp/gluegen/pcpptest.h @@ -1,6 +1,39 @@ #define CL_SCHAR_MIN (-127-1) -#define CL_ALIGNED(_x) __attribute__ ((aligned(_x))) -cl_char CL_ALIGNED(2) s[2]; +#define __YES__ 1 + +#if defined( __YES__ ) + #define TEST_A(_x) GOOD_A +#elif defined( _WIN32) && (_MSC_VER) + #define TEST_A(_x) ERR_A_1 +#elif defined( __unix__) || ( __sun__ ) + #define TEST_A(_x) ERR_A_2 +#else + #define TEST_A(_x) ERR_A_3 +#endif + +#if defined( __NO__ ) + #define TEST_B ERR_B_1 +#elif defined( __YES__) + #define TEST_B GOOD_B +#elif defined( __unix__) || ( __sun__ ) + #define TEST_B ERR_B_2 +#else + #define TEST_B ERR_B_3 +#endif + +#if defined( __NO__ ) + #define TEST_C ERR_C_1 +#elif defined( __NO__ ) + #define TEST_C ERR_C_2 +#elif defined( __unix__) || ( __sun__ ) + #define TEST_C ERR_C_3 +#else + #define TEST_C GOOD_C +#endif + +cl_char TEST_A(2); +int TEST_B; +int TEST_C; |