Index: Preprocessor.cpp =================================================================== --- Preprocessor.cpp (revision 23013) +++ Preprocessor.cpp (working copy) @@ -1,66 +1,65 @@ /* - * This source file originally came from OGRE v1.7.2 - http://www.ogre3d.org/ - * with some tweaks as part of 0 A.D. - * All changes are released under the original license, as follows: - */ + ----------------------------------------------------------------------------- + This source file is part of OGRE + (Object-oriented Graphics Rendering Engine) + For the latest info, see http://www.ogre3d.org/ -/* ------------------------------------------------------------------------------ -This source file is part of OGRE -(Object-oriented Graphics Rendering Engine) -For the latest info, see http://www.ogre3d.org/ + Copyright (c) 2000-2014 Torus Knot Software Ltd -Copyright (c) 2000-2009 Torus Knot Software Ltd + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. ------------------------------------------------------------------------------ + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ----------------------------------------------------------------------------- */ -#include "precompiled.h" +#include "OgreGLSLPreprocessor.h" +#include "OgreLogManager.h" -#include "Preprocessor.h" +#include +#include +#include -#include "ps/CLogger.h" +namespace Ogre { -// Limit max number of macro arguments to this + // Limit max number of macro arguments to this #define MAX_MACRO_ARGS 16 -//---------------------------------------------------------------------------// +#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 && !defined( __MINGW32__ ) +#define snprintf _snprintf +#endif -/// Return closest power of two not smaller than given number -static size_t ClosestPow2 (size_t x) -{ + /// Return closest power of two not smaller than given number + static size_t ClosestPow2 (size_t x) + { if (!(x & (x - 1))) return x; while (x & (x + 1)) x |= (x + 1); return x + 1; -} + } -void CPreprocessor::Token::Append (const char *iString, size_t iLength) -{ + void CPreprocessor::Token::Append (const char *iString, size_t iLength) + { Token t (Token::TK_TEXT, iString, iLength); Append (t); -} + } -void CPreprocessor::Token::Append (const Token &iOther) -{ + void CPreprocessor::Token::Append (const Token &iOther) + { if (!iOther.String) return; @@ -97,10 +96,10 @@ if (Allocated) memcpy (Buffer + Length, iOther.String, iOther.Length); Length += iOther.Length; -} + } -bool CPreprocessor::Token::GetValue (long &oValue) const -{ + bool CPreprocessor::Token::GetValue (long &oValue) const + { long val = 0; size_t i = 0; @@ -118,7 +117,7 @@ for (; i < Length; i++) { - long c = long (String [i]); + int c = int (String [i]); if (isspace (c)) // Possible end of number break; @@ -146,19 +145,21 @@ oValue = val; return true; -} + } -void CPreprocessor::Token::SetValue (long iValue) -{ + + void CPreprocessor::Token::SetValue (long iValue) + { char tmp [21]; int len = snprintf (tmp, sizeof (tmp), "%ld", iValue); Length = 0; Append (tmp, len); Type = TK_NUMBER; -} + } -void CPreprocessor::Token::AppendNL (int iCount) -{ + + void CPreprocessor::Token::AppendNL (int iCount) + { static const char newlines [8] = { '\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n' }; @@ -169,15 +170,16 @@ } if (iCount > 0) Append (newlines, iCount); -} + } -int CPreprocessor::Token::CountNL () -{ + + int CPreprocessor::Token::CountNL () + { if (Type == TK_EOS || Type == TK_ERROR) return 0; const char *s = String; - int l = Length; + size_t l = Length; int c = 0; while (l > 0) { @@ -189,13 +191,12 @@ s = n + 1; } return c; -} + } -//---------------------------------------------------------------------------// -CPreprocessor::Token CPreprocessor::Macro::Expand ( + CPreprocessor::Token CPreprocessor::Macro::Expand ( int iNumArgs, CPreprocessor::Token *iArgs, Macro *iMacros) -{ + { Expanding = true; CPreprocessor cpp; @@ -222,49 +223,35 @@ cpp.MacroList = NULL; return xt; -} + } -//---------------------------------------------------------------------------// - -static void DefaultError (void *iData, int iLine, const char *iError, - const char *iToken, size_t iTokenLen) -{ - (void)iData; + void CPreprocessor::Error(int iLine, const char *iError, const Token *iToken) + { + char line [1000]; if (iToken) - LOGERROR("Preprocessor error: line %d: %s: '%s'\n", - iLine, iError, std::string (iToken, iTokenLen)); + snprintf (line, sizeof (line), "line %d: %s: `%.*s'\n", + iLine, iError, int (iToken->Length), iToken->String); else - LOGERROR("Preprocessor error: line %d: %s\n", iLine, iError); -} + snprintf (line, sizeof (line), "line %d: %s\n", iLine, iError); + LogManager::getSingleton ().logMessage (line, LML_CRITICAL); + } -//---------------------------------------------------------------------------// - -CPreprocessor::ErrorHandlerFunc CPreprocessor::ErrorHandler = DefaultError; - -CPreprocessor::CPreprocessor (const Token &iToken, int iLine) : MacroList (NULL) -{ + CPreprocessor::CPreprocessor (const Token &iToken, int iLine) : MacroList (NULL) + { Source = iToken.String; SourceEnd = iToken.String + iToken.Length; EnableOutput = 1; Line = iLine; BOL = true; -} + } -CPreprocessor::~CPreprocessor () -{ + CPreprocessor::~CPreprocessor () + { delete MacroList; -} + } -void CPreprocessor::Error (int iLine, const char *iError, const Token *iToken) -{ - if (iToken) - ErrorHandler (ErrorData, iLine, iError, iToken->String, iToken->Length); - else - ErrorHandler (ErrorData, iLine, iError, NULL, 0); -} - -CPreprocessor::Token CPreprocessor::GetToken (bool iExpand) -{ + CPreprocessor::Token CPreprocessor::GetToken (bool iExpand) + { if (Source >= SourceEnd) return Token (Token::TK_EOS); @@ -393,19 +380,21 @@ Source++; return Token (Token::TK_PUNCTUATION, begin, Source - begin); } -} + } -CPreprocessor::Macro *CPreprocessor::IsDefined (const Token &iToken) -{ + + CPreprocessor::Macro *CPreprocessor::IsDefined (const Token &iToken) + { for (Macro *cur = MacroList; cur; cur = cur->Next) if (cur->Name == iToken) return cur; return NULL; -} + } -CPreprocessor::Token CPreprocessor::ExpandMacro (const Token &iToken) -{ + + CPreprocessor::Token CPreprocessor::ExpandMacro (const Token &iToken) + { Macro *cur = IsDefined (iToken); if (cur && !cur->Expanding) { @@ -415,7 +404,7 @@ if (cur->NumArgs != 0) { - Token t = GetArguments (nargs, args, cur->ExpandFunc ? false : true); + Token t = GetArguments (nargs, args, cur->ExpandFunc ? false : true, false); if (t.Type == Token::TK_ERROR) { delete [] args; @@ -426,7 +415,7 @@ if (t.String) { // Returned token should never be allocated on heap - ENSURE (t.Allocated == 0); + assert (t.Allocated == 0); Source = t.String; Line -= t.CountNL (); } @@ -453,9 +442,10 @@ } return iToken; -} + } -/** + + /** * Operator priority: * 0: Whole expression * 1: '(' ')' @@ -471,9 +461,9 @@ * 11: '*' '/' '%' * 12: unary '+' '-' '!' '~' */ -CPreprocessor::Token CPreprocessor::GetExpression ( + CPreprocessor::Token CPreprocessor::GetExpression ( Token &oResult, int iLine, int iOpPriority) -{ + { char tmp [40]; do @@ -520,7 +510,7 @@ return Token (Token::TK_ERROR); } - ENSURE (op.Type == Token::TK_PUNCTUATION && + assert (op.Type == Token::TK_PUNCTUATION && op.Length == 1 && op.String [0] == ')'); op = GetToken (true); @@ -655,10 +645,11 @@ op = nextop; } -} + } -bool CPreprocessor::GetValue (const Token &iToken, long &oValue, int iLine) -{ + + bool CPreprocessor::GetValue (const Token &iToken, long &oValue, int iLine) + { Token r; const Token *vt = &iToken; @@ -693,6 +684,7 @@ vt = &r; } + Macro *m; switch (vt->Type) { case Token::TK_EOS: @@ -700,10 +692,8 @@ return false; case Token::TK_KEYWORD: - { // Try to expand the macro - Macro *m = IsDefined (*vt); - if (m != NULL && !m->Expanding) + if ((m = IsDefined (*vt)) && !m->Expanding) { Token x = ExpandMacro (*vt); m->Expanding = true; @@ -715,7 +705,7 @@ // Undefined macro, "expand" to 0 (mimic cpp behaviour) oValue = 0; break; - } + case Token::TK_TEXT: case Token::TK_NUMBER: if (!vt->GetValue (oValue)) @@ -731,10 +721,12 @@ } return true; -} + } -CPreprocessor::Token CPreprocessor::GetArgument (Token &oArg, bool iExpand) -{ + + CPreprocessor::Token CPreprocessor::GetArgument (Token &oArg, bool iExpand, + bool shouldAppendArg) + { do { oArg = GetToken (iExpand); @@ -763,7 +755,12 @@ } } - unsigned int len = oArg.Length; + size_t braceCount = 0; + + if( oArg.Type == Token::TK_PUNCTUATION && oArg.String[0] == '(' ) + ++braceCount; + + size_t len = oArg.Length; while (true) { Token t = GetToken (iExpand); @@ -771,17 +768,41 @@ { case Token::TK_EOS: Error (Line, "Unfinished list of arguments"); - FALLTHROUGH; + OGRE_FALLTHROUGH; case Token::TK_ERROR: return Token (Token::TK_ERROR); case Token::TK_PUNCTUATION: + if( t.String [0] == '(' ) + { + ++braceCount; + } + else if( !braceCount ) + { if (t.String [0] == ',' || t.String [0] == ')') { // Trim whitespaces at the end oArg.Length = len; + + //Append "__arg_" to all macro arguments, otherwise if user does: + // #define mad( a, b, c ) fma( a, b, c ) + // mad( x.s, y, a ); + //It will be translated to: + // fma( x.s, y, x.s ); + //instead of: + // fma( x.s, y, a ); + //This does not fix the problem by the root, but + //typing "__arg_" by the user is extremely rare. + if( shouldAppendArg ) + oArg.Append( "__arg_", 6 ); return t; } + } + else + { + if( t.String [0] == ')' ) + --braceCount; + } break; case Token::TK_LINECONT: case Token::TK_COMMENT: @@ -804,11 +825,12 @@ if (t.Type != Token::TK_WHITESPACE) len = oArg.Length; } -} + } -CPreprocessor::Token CPreprocessor::GetArguments (int &oNumArgs, Token *&oArgs, - bool iExpand) -{ + + CPreprocessor::Token CPreprocessor::GetArguments (int &oNumArgs, Token *&oArgs, + bool iExpand, bool shouldAppendArg) + { Token args [MAX_MACRO_ARGS]; int nargs = 0; @@ -816,15 +838,25 @@ oNumArgs = 0; oArgs = NULL; + bool isFirstTokenParsed = false; + bool isFirstTokenNotAnOpenBrace = false; + Token t; do { t = GetToken (iExpand); + + if( !isFirstTokenParsed && + (t.Type != Token::TK_PUNCTUATION || t.String [0] != '(') ) + { + isFirstTokenNotAnOpenBrace = true; + } + isFirstTokenParsed = true; } while (t.Type == Token::TK_WHITESPACE || t.Type == Token::TK_COMMENT || t.Type == Token::TK_LINECOMMENT); - if (t.Type != Token::TK_PUNCTUATION || t.String [0] != '(') + if( isFirstTokenNotAnOpenBrace ) { oNumArgs = 0; oArgs = NULL; @@ -839,13 +871,13 @@ return Token (Token::TK_ERROR); } - t = GetArgument (args [nargs++], iExpand); + t = GetArgument (args [nargs++], iExpand, shouldAppendArg); switch (t.Type) { case Token::TK_EOS: Error (Line, "Unfinished list of arguments"); - FALLTHROUGH; + OGRE_FALLTHROUGH; case Token::TK_ERROR: return Token (Token::TK_ERROR); @@ -863,16 +895,17 @@ } } -Done: + Done: oNumArgs = nargs; oArgs = new Token [nargs]; for (int i = 0; i < nargs; i++) oArgs [i] = args [i]; return t; -} + } -bool CPreprocessor::HandleDefine (Token &iBody, int iLine) -{ + + bool CPreprocessor::HandleDefine (Token &iBody, int iLine) + { // Create an additional preprocessor to process macro body CPreprocessor cpp (iBody, iLine); @@ -883,13 +916,9 @@ return false; } - bool output_enabled = ((EnableOutput & (EnableOutput + 1)) == 0); - if (!output_enabled) - return true; - Macro *m = new Macro (t); m->Body = iBody; - t = cpp.GetArguments (m->NumArgs, m->Args, false); + t = cpp.GetArguments (m->NumArgs, m->Args, false, true); while (t.Type == Token::TK_WHITESPACE) t = cpp.GetToken (false); @@ -907,19 +936,39 @@ default: t.Type = Token::TK_TEXT; - ENSURE (t.String + t.Length == cpp.Source); + assert (t.String + t.Length == cpp.Source); t.Length = cpp.SourceEnd - t.String; break; } + if( m->NumArgs > 0 ) + { + CPreprocessor cpp2; + + //We need to convert: + // #define mad( a__arg_, b__arg_, c__arg_ ) fma( a, b, c ) + //into: + // #define mad( a__arg_, b__arg_, c__arg_ ) fma( a__arg_, b__arg_, c__arg_ ) + for( int i = 0; i < m->NumArgs; ++i ) + { + cpp2.Define( m->Args[i].String, m->Args[i].Length - 6, + m->Args[i].String, m->Args[i].Length ); + } + + // Now run the macro expansion through the supplimentary preprocessor + Token xt = cpp2.Parse( t ); + t = xt; + } + m->Value = t; m->Next = MacroList; MacroList = m; return true; -} + } -bool CPreprocessor::HandleUnDef (Token &iBody, int iLine) -{ + + bool CPreprocessor::HandleUnDef (Token &iBody, int iLine) + { CPreprocessor cpp (iBody, iLine); Token t = cpp.GetToken (false); @@ -944,10 +993,10 @@ Error (iLine, "Warning: Ignoring garbage after directive", &t); return true; -} + } -bool CPreprocessor::HandleIfDef (Token &iBody, int iLine) -{ + bool CPreprocessor::HandleIfDef (Token &iBody, int iLine) + { if (EnableOutput & (1 << 31)) { Error (iLine, "Too many embedded #if directives"); @@ -979,10 +1028,11 @@ Error (iLine, "Warning: Ignoring garbage after directive", &t); return true; -} + } -CPreprocessor::Token CPreprocessor::ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs) -{ + + CPreprocessor::Token CPreprocessor::ExpandDefined (CPreprocessor *iParent, int iNumArgs, Token *iArgs) + { if (iNumArgs != 1) { iParent->Error (iParent->Line, "The defined() function takes exactly one argument"); @@ -991,10 +1041,11 @@ const char *v = iParent->IsDefined (iArgs [0]) ? "1" : "0"; return Token (Token::TK_NUMBER, v, 1); -} + } -bool CPreprocessor::HandleIf (Token &iBody, int iLine) -{ + + bool CPreprocessor::HandleIf (Token &iBody, int iLine) + { Macro defined (Token (Token::TK_KEYWORD, "defined", 7)); defined.Next = MacroList; defined.ExpandFunc = ExpandDefined; @@ -1018,12 +1069,48 @@ EnableOutput |= 1; return true; -} + } -bool CPreprocessor::HandleElse (Token &iBody, int iLine) -{ + + bool CPreprocessor::HandleElif (Token &iBody, int iLine) + { if (EnableOutput == 1) { + Error (iLine, "#elif without #if"); + return false; + } + + Macro defined (Token (Token::TK_KEYWORD, "defined", 7)); + defined.Next = MacroList; + defined.ExpandFunc = ExpandDefined; + defined.NumArgs = 1; + + // Temporary add the defined() function to the macro list + MacroList = &defined; + + long val; + bool rc = GetValue (iBody, val, iLine); + + // Restore the macro list + MacroList = defined.Next; + defined.Next = NULL; + + if (!rc) + return false; + + if (val) + EnableOutput |= 1; + else + EnableOutput &= ~1; + + return true; + } + + + bool CPreprocessor::HandleElse (Token &iBody, int iLine) + { + if (EnableOutput == 1) + { Error (iLine, "#else without #if"); return false; } @@ -1035,10 +1122,11 @@ Error (iLine, "Warning: Ignoring garbage after #else", &iBody); return true; -} + } -bool CPreprocessor::HandleEndIf (Token &iBody, int iLine) -{ + + bool CPreprocessor::HandleEndIf (Token &iBody, int iLine) + { EnableOutput >>= 1; if (EnableOutput == 0) { @@ -1050,13 +1138,14 @@ Error (iLine, "Warning: Ignoring garbage after #endif", &iBody); return true; -} + } -CPreprocessor::Token CPreprocessor::HandleDirective (Token &iToken, int iLine) -{ + + CPreprocessor::Token CPreprocessor::HandleDirective (Token &iToken, int iLine) + { // Analyze preprocessor directive const char *directive = iToken.String + 1; - int dirlen = iToken.Length - 1; + size_t dirlen = iToken.Length - 1; while (dirlen && isspace (*directive)) dirlen--, directive++; @@ -1109,15 +1198,17 @@ t.Append (last); t.Type = Token::TK_TEXT; } -Done: + Done: #define IS_DIRECTIVE(s) \ - ((dirlen == sizeof (s) - 1) && (strncmp (directive, s, sizeof (s) - 1) == 0)) + (dirlen == strlen(s) && (strncmp (directive, s, dirlen) == 0)) + bool outputEnabled = ((EnableOutput & (EnableOutput + 1)) == 0); bool rc; - if (IS_DIRECTIVE ("define")) + + if (IS_DIRECTIVE ("define") && outputEnabled) rc = HandleDefine (t, iLine); - else if (IS_DIRECTIVE ("undef")) + else if (IS_DIRECTIVE ("undef") && outputEnabled) rc = HandleUnDef (t, iLine); else if (IS_DIRECTIVE ("ifdef")) rc = HandleIfDef (t, iLine); @@ -1129,18 +1220,15 @@ } else if (IS_DIRECTIVE ("if")) rc = HandleIf (t, iLine); + else if (IS_DIRECTIVE ("elif")) + rc = HandleElif (t, iLine); + else if (IS_DIRECTIVE ("else")) rc = HandleElse (t, iLine); else if (IS_DIRECTIVE ("endif")) rc = HandleEndIf (t, iLine); - else { - // elif is tricky to support because the EnableOutput stack doesn't - // contain enough data to tell whether any previous branch matched - if (IS_DIRECTIVE ("elif")) - Error (iLine, "Unsupported preprocessor directive #elif"); - //Error (iLine, "Unknown preprocessor directive", &iToken); //return Token (Token::TK_ERROR); @@ -1156,38 +1244,31 @@ if (!rc) return Token (Token::TK_ERROR); return last; -} + } -void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen, + + void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen, const char *iMacroValue, size_t iMacroValueLen) -{ + { Macro *m = new Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen)); m->Value = Token (Token::TK_TEXT, iMacroValue, iMacroValueLen); m->Next = MacroList; MacroList = m; -} + } -void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen, + + void CPreprocessor::Define (const char *iMacroName, size_t iMacroNameLen, long iMacroValue) -{ + { Macro *m = new Macro (Token (Token::TK_KEYWORD, iMacroName, iMacroNameLen)); m->Value.SetValue (iMacroValue); m->Next = MacroList; MacroList = m; -} + } -void CPreprocessor::Define (const char *iMacroName, const char *iMacroValue) -{ - Define (iMacroName, strlen(iMacroName), iMacroValue, strlen(iMacroValue)); -} -void CPreprocessor::Define (const char *iMacroName, long iMacroValue) -{ - Define (iMacroName, strlen(iMacroName), iMacroValue); -} - -bool CPreprocessor::Undef (const char *iMacroName, size_t iMacroNameLen) -{ + bool CPreprocessor::Undef (const char *iMacroName, size_t iMacroNameLen) + { Macro **cur = &MacroList; Token name (Token::TK_KEYWORD, iMacroName, iMacroNameLen); while (*cur) @@ -1205,10 +1286,11 @@ } return false; -} + } -CPreprocessor::Token CPreprocessor::Parse (const Token &iSource) -{ + + CPreprocessor::Token CPreprocessor::Parse (const Token &iSource) + { Source = iSource.String; SourceEnd = Source + iSource.Length; Line = 1; @@ -1283,8 +1365,7 @@ output.AppendNL (empty_lines); empty_lines = 0; } - // Fallthrough to default - FALLTHROUGH; + OGRE_FALLTHROUGH; // to default case Token::TK_WHITESPACE: // Fallthrough to default default: @@ -1302,10 +1383,11 @@ } return output; -} + } -char *CPreprocessor::Parse (const char *iSource, size_t iLength, size_t &oLength) -{ + + char *CPreprocessor::Parse (const char *iSource, size_t iLength, size_t &oLength) + { Token retval = Parse (Token (Token::TK_TEXT, iSource, iLength)); if (retval.Type == Token::TK_ERROR) return NULL; @@ -1313,4 +1395,6 @@ oLength = retval.Length; retval.Allocated = 0; return retval.Buffer; -} + } + +} // namespace Ogre Index: Preprocessor.h =================================================================== --- Preprocessor.h (revision 23013) +++ Preprocessor.h (working copy) @@ -1,16 +1,10 @@ -/* - * This source file originally came from OGRE v1.7.2 - http://www.ogre3d.org/ - * with some tweaks as part of 0 A.D. - * All changes are released under the original license, as follows: - */ - -/* +/** ----------------------------------------------------------------------------- This source file is part of OGRE (Object-oriented Graphics Rendering Engine) For the latest info, see http://www.ogre3d.org/ -Copyright (c) 2000-2009 Torus Knot Software Ltd +Copyright (c) 2000-2011 Torus Knot Software Ltd Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -32,9 +26,14 @@ ----------------------------------------------------------------------------- */ -#ifndef INCLUDED_CPREPROCESSOR -#define INCLUDED_CPREPROCESSOR +#ifndef __OGRE_CPREPROCESSOR_H__ +#define __OGRE_CPREPROCESSOR_H__ +#include +#include + +namespace Ogre { + /** * This is a simplistic C/C++-like preprocessor. * It takes an non-zero-terminated string on input and outputs a @@ -46,18 +45,18 @@ * * Here's a list of supported features: *
    - *
  • Fast memory allocation-less operation (mostly).
  • - *
  • Line continuation (backslash-newline) is swallowed.
  • + *
  • Fast memory allocation-less operation (mostly). + *
  • Line continuation (backslash-newline) is swallowed. *
  • Line numeration is fully preserved by inserting empty lines where * required. This is crucial if, say, GLSL compiler reports you an error - * with a line number.
  • - *
  • \#define: Parametrized and non-parametrized macros. Invoking a macro with - * less arguments than it takes assignes empty values to missing arguments.
  • - *
  • \#undef: Forget defined macros
  • - *
  • \#ifdef/\#ifndef/\#else/\#endif: Conditional suppression of parts of code.
  • - *
  • \#if: Supports numeric expression of any complexity, also supports the - * defined() pseudo-function.
  • - *
+ * with a line number. + *
  • #define: Parametrized and non-parametrized macros. Invoking a macro with + * less arguments than it takes assignes empty values to missing arguments. + *
  • #undef: Forget defined macros + *
  • #ifdef/#ifndef/#else/#endif: Conditional suppression of parts of code. + *
  • #if: Supports numeric expression of any complexity, also supports the + * defined() pseudo-function. + *
      */ class CPreprocessor { @@ -108,10 +107,10 @@ /// Token length in bytes size_t Length; - Token () : Type (TK_ERROR), Allocated (0), String (NULL), Length (0) + Token () : Allocated (0), String (NULL) { } - Token (Kind iType) : Type (iType), Allocated (0), String (NULL), Length (0) + Token (Kind iType) : Type (iType), Allocated (0), String (NULL) { } Token (Kind iType, const char *iString, size_t iLength) : @@ -243,7 +242,7 @@ Token HandleDirective (Token &iToken, int iLine); /** - * Handle a \#define directive. + * Handle a #define directive. * @param iBody * The body of the directive (everything after the directive * until end of line). @@ -267,7 +266,7 @@ bool HandleUnDef (Token &iBody, int iLine); /** - * Handle an \#ifdef directive. + * Handle an #ifdef directive. * @param iBody * The body of the directive (everything after the directive * until end of line). @@ -279,7 +278,7 @@ bool HandleIfDef (Token &iBody, int iLine); /** - * Handle an \#if directive. + * Handle an #if directive. * @param iBody * The body of the directive (everything after the directive * until end of line). @@ -291,7 +290,7 @@ bool HandleIf (Token &iBody, int iLine); /** - * Handle an \#else directive. + * Handle an #else directive. * @param iBody * The body of the directive (everything after the directive * until end of line). @@ -303,7 +302,7 @@ bool HandleElse (Token &iBody, int iLine); /** - * Handle an \#endif directive. + * Handle an #endif directive. * @param iBody * The body of the directive (everything after the directive * until end of line). @@ -456,24 +455,6 @@ void Define (const char *iMacroName, size_t iMacroNameLen, long iMacroValue); /** - * Define a macro without parameters. - * @param iMacroName - * The name of the defined macro - * @param iMacroValue - * The value of the defined macro - */ - void Define (const char *iMacroName, const char *iMacroValue); - - /** - * Define a numerical macro. - * @param iMacroName - * The name of the defined macro - * @param iMacroValue - * The value of the defined macro - */ - void Define (const char *iMacroName, long iMacroValue); - - /** * Undefine a macro. * @param iMacroName * The name of the macro to undefine @@ -538,4 +519,6 @@ void *ErrorData; }; -#endif // INCLUDED_CPREPROCESSOR +} // namespace Ogre + +#endif // __OGRE_CPREPROCESSOR_H__