diff --git a/README.md b/README.md index 35cf7cc..26b309f 100644 --- a/README.md +++ b/README.md @@ -569,6 +569,17 @@ This sample generates an array with pointers to screen rows: #for(i,SCREEN_HEIGHT) Screen + SCREEN_WIDTH * i, }; +The preprocessor keeps track of the source location while parsing a file to generate error messages and debug symbols. This is problematic for generated code, where the C source file position does not reflect the actual source position. + +The source position can be set with the #line directiv + + #line linenum + + sets the line number + + #line linenum "sourcefile" + + sets the line number and source file name ### Linker control diff --git a/oscar64/Preprocessor.cpp b/oscar64/Preprocessor.cpp index 75bfb0e..787a048 100644 --- a/oscar64/Preprocessor.cpp +++ b/oscar64/Preprocessor.cpp @@ -608,6 +608,7 @@ bool SourceFile::Open(const char* name, const char* path, SourceFileMode mode) *p = '/'; p++; } + strcpy_s(mLocationFileName, mFileName); mMode = mode; mLimit = 0x10000; mFill = 0; @@ -731,7 +732,7 @@ bool Preprocessor::EmbedData(const char* reason, const char* name, bool local, i source->mUp = mSource; mSource = source; - mLocation.mFileName = mSource->mFileName; + mLocation.mFileName = mSource->mLocationFileName; mLocation.mLine = 0; mLine[0] = 0; @@ -791,7 +792,7 @@ bool Preprocessor::OpenSource(const char * reason, const char* name, bool local) source->mUp = mSource; mSource = source; - mLocation.mFileName = mSource->mFileName; + mLocation.mFileName = mSource->mLocationFileName; mLocation.mLine = 0; mLine[0] = 0; diff --git a/oscar64/Preprocessor.h b/oscar64/Preprocessor.h index 19e1d54..a849ba6 100644 --- a/oscar64/Preprocessor.h +++ b/oscar64/Preprocessor.h @@ -39,7 +39,7 @@ enum SourceFileDecoder class SourceFile { public: - char mFileName[MAXPATHLEN]; + char mFileName[MAXPATHLEN], mLocationFileName[MAXPATHLEN]; SourceFile * mUp, * mNext; Location mLocation; diff --git a/oscar64/Scanner.cpp b/oscar64/Scanner.cpp index 2a48e37..b761cd9 100644 --- a/oscar64/Scanner.cpp +++ b/oscar64/Scanner.cpp @@ -143,6 +143,7 @@ const char* TokenNames[] = "'#ifdef'", "'#ifndef'", "'#pragma'", + "'#line'", "'#assign'", "'#repeat'", @@ -597,6 +598,18 @@ void Scanner::NextPreToken(void) mPreprocessor->CloseSource(); } } + else if (mToken == TK_PREP_LINE) + { + NextPreToken(); + int l = mLocation.mLine; + int64 v = PrepParseConditional(); + if (mLocation.mLine == l && mToken == TK_STRING) + { + strcpy_s(mPreprocessor->mSource->mLocationFileName, mTokenString); + NextRawToken(); + } + mPreprocessor->mLocation.mLine = v - 1; + } else if (mToken == TK_PREP_FOR) { NextRawToken(); @@ -1384,6 +1397,8 @@ void Scanner::NextRawToken(void) mToken = TK_PREP_ENDIF; else if (!strcmp(tkprep, "pragma")) mToken = TK_PREP_PRAGMA; + else if (!strcmp(tkprep, "line")) + mToken = TK_PREP_LINE; else if (!strcmp(tkprep, "assign")) mToken = TK_PREP_ASSIGN; else if (!strcmp(tkprep, "repeat")) diff --git a/oscar64/Scanner.h b/oscar64/Scanner.h index 8db05ac..1f41adc 100644 --- a/oscar64/Scanner.h +++ b/oscar64/Scanner.h @@ -141,6 +141,7 @@ enum Token TK_PREP_IFDEF, TK_PREP_IFNDEF, TK_PREP_PRAGMA, + TK_PREP_LINE, TK_PREP_ASSIGN, TK_PREP_REPEAT,