From a66fcdb8380ab8f322e177412915d08197a2fbca Mon Sep 17 00:00:00 2001 From: cxl Date: Wed, 3 Jun 2015 07:06:22 +0000 Subject: [PATCH] cpp: Fixed issue with #define inside the scope #1099 git-svn-id: svn://ultimatepp.org/upp/trunk@8501 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/CppBase/CppBase.h | 6 ++++- uppsrc/CppBase/Parser.cpp | 49 ++++++++++++++------------------------- uppsrc/CppBase/Pre.cpp | 6 +++-- 3 files changed, 27 insertions(+), 34 deletions(-) diff --git a/uppsrc/CppBase/CppBase.h b/uppsrc/CppBase/CppBase.h index 6cfbedacd..a6611d62b 100644 --- a/uppsrc/CppBase/CppBase.h +++ b/uppsrc/CppBase/CppBase.h @@ -311,7 +311,9 @@ struct SrcFile { int commentLinesRemoved; }; -SrcFile PreProcess(Stream& in); +struct Parser; + +SrcFile PreProcess(Stream& in, Parser& parser); enum Kind { STRUCT, @@ -579,6 +581,8 @@ struct Parser { typedef Parser CLASSNAME; public: + void AddMacro(int lineno, const String& macro); + struct FunctionStat { FunctionStat(const String & scope, diff --git a/uppsrc/CppBase/Parser.cpp b/uppsrc/CppBase/Parser.cpp index b2e1fc503..67037c854 100644 --- a/uppsrc/CppBase/Parser.cpp +++ b/uppsrc/CppBase/Parser.cpp @@ -743,8 +743,11 @@ void Parser::ParamList(Decl& d) { Elipsis(d); break; } - else - d.param.Add() = pick(Declaration(false, false, Null, Null).Top()); + else { + Array decl = Declaration(false, false, Null, Null); + if(decl.GetCount()) + d.param.Add() = pick(decl.Top()); + } if(Key(t_elipsis)) { Elipsis(d); break; @@ -1305,6 +1308,18 @@ CppItem& Parser::Item(const String& scope, const String& using_namespace, const return im; } +void Parser::AddMacro(int lineno, const String& macro) +{ + String name; + const char *s = macro; + while(*s && iscid(*s)) + name.Cat(*s++); + CppItem& im = Item("", "", macro, name); + im.kind = MACRO; + im.line = lineno; + im.access = PUBLIC; +} + CppItem& Parser::Item(const String& scope, const String& using_namespace, const String& item, const String& name) { @@ -1650,34 +1665,6 @@ void Parser::Do() Enum(); } else - if(Key('#')) { - if(lex.Code() == t_string) { - String n = lex.GetText(); - String name; - const char *s = n; - while(*s && iscid(*s)) - name.Cat(*s++); - CppItem& im = Item("", context.namespace_using, n, name); - im.kind = MACRO; - s = strchr(n, '('); - if(s) { - s++; - String p; - for(;;) { - if(iscid(*s)) - p.Cat(*s++); - else { - ScAdd(im.pname, p); - p.Clear(); - if(*s == ')' || *s == '\0') break; - s++; - } - } - } - im.access = context.access; - } - } - else if(!Scope(String(), String())) { if(Key(tk_public)) { context.access = PUBLIC; @@ -1750,7 +1737,7 @@ void Parser::Do(Stream& in, CppBase& _base, int filei_, int filetype_, { LLOG("= C++ Parser ==================================== " << fn); base = &_base; - file = PreProcess(in); + file = PreProcess(in, *this); lex.Init(~file.text); err = _err; filei = filei_; diff --git a/uppsrc/CppBase/Pre.cpp b/uppsrc/CppBase/Pre.cpp index 5c3d60b71..5c1133884 100644 --- a/uppsrc/CppBase/Pre.cpp +++ b/uppsrc/CppBase/Pre.cpp @@ -19,12 +19,14 @@ SrcFile::SrcFile() : { } -SrcFile PreProcess(Stream& in) // This is not really C preprocess, only removes (or processes) comment and directives +SrcFile PreProcess(Stream& in, Parser& parser) // This is not really C preprocess, only removes (or processes) comment and directives { SrcFile res; bool include = true; + int lineno = 0; while(!in.IsEof()) { String ln = in.GetLine(); + lineno++; SLPos(res); while(*ln.Last() == '\\') { ln.Trim(ln.GetLength() - 1); @@ -61,7 +63,7 @@ SrcFile PreProcess(Stream& in) // This is not really C preprocess, only removes macro << ')'; } if(include) - res.text << '#' << AsCString(macro); + parser.AddMacro(lineno, macro); } res.preprocessorLinesRemoved++; }