Allow multiline comments before preprocessor directives.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 02 Oct 2014 15:54:31 -0400
changeset 1142 7b554e065321
parent 1141 77468a2a9165
child 1143 e4027cbe2116
Allow multiline comments before preprocessor directives. Microsoft's preprocessor allows this, even though C/C++ doesn't.
mojoshader_internal.h
mojoshader_lexer.c
mojoshader_lexer.re
mojoshader_preprocessor.c
unit_tests/preprocessor/output/comment-before-preprocessor-directive
unit_tests/preprocessor/output/comment-before-preprocessor-directive.correct
unit_tests/preprocessor/output/multiline-comment-no-whitespace
unit_tests/preprocessor/output/multiline-comment-no-whitespace.correct
unit_tests/preprocessor/output/multiline-comments-alone-on-line
unit_tests/preprocessor/output/multiline-comments-alone-on-line.correct
--- a/mojoshader_internal.h	Mon Jun 23 14:56:00 2014 -0400
+++ b/mojoshader_internal.h	Thu Oct 02 15:54:31 2014 -0400
@@ -68,6 +68,11 @@
 #error glsl120 profile requires glsl profile. Fix your build.
 #endif
 
+// Microsoft's preprocessor has some quirks. In some ways, it doesn't work
+//  like you'd expect a C preprocessor to function.
+#ifndef MATCH_MICROSOFT_PREPROCESSOR
+#define MATCH_MICROSOFT_PREPROCESSOR 1
+#endif
 
 // Other stuff you can disable...
 
--- a/mojoshader_lexer.c	Mon Jun 23 14:56:00 2014 -0400
+++ b/mojoshader_lexer.c	Thu Oct 02 15:54:31 2014 -0400
@@ -1164,6 +1164,18 @@
                             RET(TOKEN_MULTI_COMMENT);
                         else if (s->report_whitespace)
                             RET(' ');
+
+                        // Microsoft's preprocessor allows multiline comments
+                        //  before a preprocessor directive, even though C/C++
+                        //  doesn't. See if we've hit this case.
+                        #if MATCH_MICROSOFT_PREPROCESSOR
+                        if (s->tokenval == ((Token) '\n'))  // was start of line?
+                        {
+                            update_state(s, eoi, cursor, token, (Token) '\n');
+                            goto ppdirective;  // may jump back to scanner_loop.
+                        }
+                        #endif
+
                         goto scanner_loop;
                     }
 }
--- a/mojoshader_lexer.re	Mon Jun 23 14:56:00 2014 -0400
+++ b/mojoshader_lexer.re	Thu Oct 02 15:54:31 2014 -0400
@@ -172,6 +172,18 @@
                             RET(TOKEN_MULTI_COMMENT);
                         else if (s->report_whitespace)
                             RET(' ');
+
+                        // Microsoft's preprocessor allows multiline comments
+                        //  before a preprocessor directive, even though C/C++
+                        //  doesn't. See if we've hit this case.
+                        #if MATCH_MICROSOFT_PREPROCESSOR
+                        if (s->tokenval == ((Token) '\n'))  // was start of line?
+                        {
+                            update_state(s, eoi, cursor, token, (Token) '\n');
+                            goto ppdirective;  // may jump back to scanner_loop.
+                        }
+                        #endif
+
                         goto scanner_loop;
                     }
     NEWLINE         {
--- a/mojoshader_preprocessor.c	Mon Jun 23 14:56:00 2014 -0400
+++ b/mojoshader_preprocessor.c	Thu Oct 02 15:54:31 2014 -0400
@@ -60,12 +60,6 @@
     void *malloc_data;
 } Context;
 
-// Microsoft's preprocessor has some quirks. In some ways, it doesn't work
-//  like you'd expect a C preprocessor to function.
-#ifndef MATCH_MICROSOFT_PREPROCESSOR
-#define MATCH_MICROSOFT_PREPROCESSOR 1
-#endif
-
 
 // Convenience functions for allocators...
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/unit_tests/preprocessor/output/comment-before-preprocessor-directive	Thu Oct 02 15:54:31 2014 -0400
@@ -0,0 +1,10 @@
+// This shouldn't care that there's a multiline comment before a preprocessor
+//  directive. It should translate to whitespace, thrown away, making the
+//  "#if 1" the first thing on the line, and thus valid.
+// Note that this isn't legal in C/C++ preprocessing, but Microsoft's fxc.exe
+//  allows this quirk.
+/* comment! */ #if 1
+RIGHT
+#else
+WRONG
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/unit_tests/preprocessor/output/comment-before-preprocessor-directive.correct	Thu Oct 02 15:54:31 2014 -0400
@@ -0,0 +1,1 @@
+RIGHT
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/unit_tests/preprocessor/output/multiline-comment-no-whitespace	Thu Oct 02 15:54:31 2014 -0400
@@ -0,0 +1,1 @@
+/*comment*/RIGHT/*comment*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/unit_tests/preprocessor/output/multiline-comment-no-whitespace.correct	Thu Oct 02 15:54:31 2014 -0400
@@ -0,0 +1,1 @@
+RIGHT
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/unit_tests/preprocessor/output/multiline-comments-alone-on-line	Thu Oct 02 15:54:31 2014 -0400
@@ -0,0 +1,3 @@
+/*comment*/
+/*comment*/
+RIGHT
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/unit_tests/preprocessor/output/multiline-comments-alone-on-line.correct	Thu Oct 02 15:54:31 2014 -0400
@@ -0,0 +1,1 @@
+RIGHT
\ No newline at end of file