Skip to content

Commit

Permalink
Allow multiline comments before preprocessor directives.
Browse files Browse the repository at this point in the history
Microsoft's preprocessor allows this, even though C/C++ doesn't.
  • Loading branch information
icculus committed Oct 2, 2014
1 parent d1421a7 commit 8961fa5
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 6 deletions.
5 changes: 5 additions & 0 deletions mojoshader_internal.h
Expand Up @@ -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...

Expand Down
12 changes: 12 additions & 0 deletions mojoshader_lexer.c
Expand Up @@ -1164,6 +1164,18 @@ Token preprocessor_lexer(IncludeState *s)
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;
}
}
Expand Down
12 changes: 12 additions & 0 deletions mojoshader_lexer.re
Expand Up @@ -172,6 +172,18 @@ multilinecomment:
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 {
Expand Down
6 changes: 0 additions & 6 deletions mojoshader_preprocessor.c
Expand Up @@ -60,12 +60,6 @@ typedef struct Context
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...

Expand Down
@@ -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
@@ -0,0 +1 @@
RIGHT
@@ -0,0 +1 @@
/*comment*/RIGHT/*comment*/
@@ -0,0 +1 @@
RIGHT
@@ -0,0 +1,3 @@
/*comment*/
/*comment*/
RIGHT
@@ -0,0 +1 @@
RIGHT

0 comments on commit 8961fa5

Please sign in to comment.