Skip to content

Commit

Permalink
#685 C23 preprocessor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
LADSoft committed Jul 26, 2023
1 parent 7f6cace commit 34217bb
Show file tree
Hide file tree
Showing 22 changed files with 167 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/oasm/AsmMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void AsmMain::CheckAssign(std::string& line, PreProcessor& pp)
}
else
{
ppExpr e(false);
ppExpr e(false, false);
std::string temp = line.substr(npos);
value = e.Eval(temp);
pp.Assign(name, value, caseInsensitive);
Expand Down Expand Up @@ -207,7 +207,7 @@ int AsmMain::Run(int argc, char* argv[])
{
inName = Utils::QualifiedFile(files[i].c_str(), ".asm");
}
PreProcessor pp(inName, srchPth, sysSrchPth, false, false, '%', false, false, true, false, "");
PreProcessor pp(inName, srchPth, sysSrchPth, false, false, '%', false, false, false, true, false, "");
int n = Defines.GetCount();
for (int i = 0; i < n; i++)
{
Expand Down
5 changes: 3 additions & 2 deletions src/occopt/configmsil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ namespace Optimizer
static char help_text[] =
"[options] [@response file] files\n"
"\n"
"/1 - C1x mode /8 - c89 mode\n"
"/9 - C99 mode /c - compile only\n"
"/1 - C1x mode /2 - C2x mode\n"
"/8 - c89 mode /9 - C99 mode\n"
"/c - compile only\n"
"-d - don't allow extensions +e - dump errors to file\n"
"+i - dump preprocessed file +l - dump listing file\n"
"/oxxx - specify output file name\n"
Expand Down
6 changes: 4 additions & 2 deletions src/occopt/configx86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ static char BackendIntrinsicPrototypes[] =
static char help_text[] =
"[options] [@response file] files\n"
"\n"
"/1 - C1x mode /8 - c89 mode\n"
"/9 - C99 mode /# - compile then assemble then link\n"
"/1 - C1x mode /2 - c2x mode\n"
"/8 - c89 mode /9 - C99 mode\n"
"/# - compile then assemble then link\n"
"/axxx - set assembler extension /c - compile only\n"
"+e - dump errors to file /f{flags} - set flags\n"
"/g - enable debug symbols +i - dump preprocessed file\n"
Expand Down Expand Up @@ -115,6 +116,7 @@ static char help_text[] =
" c89 1989 version of ansi c\n"
" c99 1999 version of ansi c\n"
" c11 2011 version of ansi c\n"
" c24 2024 version of ansi c\n"
" c++11 2011 version of C++\n"
" c++14 2014 version of C++\n"
" -nostdinc, nostdinc++ disable system include file path\n"
Expand Down
1 change: 1 addition & 0 deletions src/occparse/c.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class SymbolTableFactory
kw_char32_t, kw_mutable, kw_nullptr, kw_noexcept, kw_thread_local, kw_constexpr,
kw_rangefor,
/* Extended */
kw__has_c_attribute,
kw_atomic_flag_test_set, kw_atomic_flag_clear, kw_atomic_kill_dependency,
/* Clang compatibility for atomics */
kw_c11_atomic_init, kw_c11_atomic_thread_fence, kw_c11_atomic_signal_fence,
Expand Down
9 changes: 9 additions & 0 deletions src/occparse/declcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3527,6 +3527,9 @@ static const std::unordered_map<std::string, int, StringHash> gccStyleAttribName
{"constructor", 30 },
{"destructor", 31 },
{"stack_protect", 32 },
{"fallthrough", 33 },
{"maybe_unused", 34},
{"nodiscard", 35},
};
#define DEFAULT_CONSTRUCTOR_PRIORITY 101
#define DEFAULT_DESTRUCTOR_PRIORITY 101
Expand Down Expand Up @@ -3839,6 +3842,12 @@ void ParseOut__attribute__(LEXLIST** lex, SYMBOL* funcsp)
case 32: // stack-protect explicit
basisAttribs.uninheritable.stackProtect = true;
break;
case 33: // fallthrough
break;
case 34: // maybe_unused
break;
case 35: // nodiscard
break;
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/occparse/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ static LEXLIST* expression_pm(LEXLIST* lex, SYMBOL* funcsp, TYPE* atp, TYPE** tp
LEXLIST* expression_assign(LEXLIST* lex, SYMBOL* funcsp, TYPE* atp, TYPE** tp, EXPRESSION** exp, bool* ismutable, int flags);
static LEXLIST* expression_msilfunc(LEXLIST* lex, SYMBOL* funcsp, TYPE** tp, EXPRESSION** exp, int flags);

std::unordered_map<std::string, unsigned, StringHash> cattributes = {
{"deprecated" , 202311},
{"fallthrough" , 202311},
{"nodiscard" , 202311},
{"noreturn" , 202311},
{"maybe_unused" , 202311}
};

void expr_init(void)
{
packIndex = -1;
Expand Down Expand Up @@ -6106,6 +6114,24 @@ static LEXLIST* expression_primary(LEXLIST* lex, SYMBOL* funcsp, TYPE* atp, TYPE
case kw___typeid:
lex = expression___typeid(lex, funcsp, tp, exp);
break;
case kw__has_c_attribute:
lex = getsym();
*tp = &stdint;
*exp = intNode(en_c_i, 0);
if (needkw(&lex, openpa))
{
if (ISID(lex))
{
(*exp)->v.i = cattributes[lex->data->value.s.a];
lex = getsym();
}
else
{
error(ERR_IDENTIFIER_EXPECTED);
}
needkw(&lex, closepa);
}
break;
case kw_D0:
case kw_D1:
case kw_D2:
Expand Down
1 change: 1 addition & 0 deletions src/occparse/lex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ KEYWORD keywords[] = {
{"__finally", 9, kw___finally, KW_MSIL, TT_CONTROL},
{"__func__", 8, kw___func__, KW_C99 | KW_CPLUSPLUS, TT_UNARY | TT_OPERATOR},
{"__import", 8, kw__import, KW_NONANSI | KW_ALL, TT_LINKAGE},
{"__has_c_attribute", 17, kw__has_c_attribute, KW_C2X, TT_VAR},
{"__initblk", 9, kw__initblk, KW_MSIL, TT_OPERATOR | TT_UNARY},
{"__inline", 8, kw_inline, KW_NONANSI | KW_ALL, TT_LINKAGE},
{"__int16", 7, kw_short, KW_NONANSI | KW_386 | KW_MSIL, TT_BASETYPE | TT_INT},
Expand Down
10 changes: 9 additions & 1 deletion src/occparse/occparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,19 @@ int main(int argc, char* argv[])
{
Optimizer::cparams.prm_c99 = true;
Optimizer::cparams.prm_c1x = false;
Optimizer::cparams.prm_c2x = false;
}
else if (prm_std.GetValue() == "c11")
{
Optimizer::cparams.prm_c99 = true;
Optimizer::cparams.prm_c1x = true;
Optimizer::cparams.prm_c2x = false;
}
else if (prm_std.GetValue() == "c2x")
{
Optimizer::cparams.prm_c99 = true;
Optimizer::cparams.prm_c1x = true;
Optimizer::cparams.prm_c2x = true;
}
else if (prm_std.GetValue() == "c++11")
{
Expand Down Expand Up @@ -615,7 +623,7 @@ int main(int argc, char* argv[])
new PreProcessor(buffer, prm_cinclude.GetValue(),
Optimizer::cparams.prm_cplusplus ? prm_CPPsysinclude.GetValue() : prm_Csysinclude.GetValue(), true,
Optimizer::cparams.prm_trigraph, '#', Optimizer::cparams.prm_charisunsigned,
!Optimizer::cparams.prm_c99 && !Optimizer::cparams.prm_c1x && !Optimizer::cparams.prm_cplusplus,
!Optimizer::cparams.prm_c99 && !Optimizer::cparams.prm_c1x && !Optimizer::cparams.prm_c2x &&!Optimizer::cparams.prm_cplusplus, Optimizer::cparams.prm_c2x,
!Optimizer::cparams.prm_ansi,
(MakeStubsOption.GetValue() || MakeStubsUser.GetValue()) && MakeStubsMissingHeaders.GetValue(),
prm_pipe.GetValue() != "+" ? prm_pipe.GetValue() : "");
Expand Down
8 changes: 4 additions & 4 deletions src/ocpp/PreProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class PreProcessor
{
public:
PreProcessor(const std::string& FileName, const std::string& SrchPth, const std::string& SysSrchPth, bool fullName,
bool Trigraph, char PPStart, bool isunsignedchar, bool C89, bool extensions, bool NoErr,
bool Trigraph, char PPStart, bool isunsignedchar, bool C89, bool C2x, bool extensions, bool NoErr,
const std::string& pipeName) :
ppStart(PPStart),
lineno(0),
include(fullName, Trigraph, extensions, isunsignedchar, C89, SrchPth, SysSrchPth, PPStart == '%', NoErr, pipeName),
define(extensions, &include, C89, PPStart == '%'),
macro(include, define),
include(fullName, Trigraph, extensions, isunsignedchar, C89, C2x, SrchPth, SysSrchPth, PPStart == '%', NoErr, pipeName),
define(extensions, &include, C89, C2x, PPStart == '%'),
macro(include, define, C2x),
ctx(define),
trigraphs(Trigraph),
pragma(&include, &define)
Expand Down
4 changes: 2 additions & 2 deletions src/ocpp/ppCond.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class ppCtx;
class ppCond
{
public:
ppCond(bool isunsignedchar, bool C89, bool Extensions, bool AsmPP) :
define(nullptr), c89(C89), expr(isunsignedchar), extensions(Extensions), ctx(nullptr), asmpp(AsmPP){};
ppCond(bool isunsignedchar, bool C89, bool c2x, bool Extensions, bool AsmPP) :
define(nullptr), c89(C89), expr(isunsignedchar, c2x), extensions(Extensions), ctx(nullptr), asmpp(AsmPP){};
~ppCond();
void SetParams(ppDefine* Define, ppCtx* Ctx)
{
Expand Down
45 changes: 42 additions & 3 deletions src/ocpp/ppDefine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ ppDefine::Definition::Definition(const Definition& old) : Symbol(old.GetName())
}
}

ppDefine::ppDefine(bool UseExtensions, ppInclude* Include, bool C89, bool Asmpp) :
expr(false), include(Include), c89(C89), asmpp(Asmpp), ctx(nullptr), macro(nullptr), source_date_epoch((time_t)-1), counter_val(0)
ppDefine::ppDefine(bool UseExtensions, ppInclude* Include, bool C89, bool C2x, bool Asmpp) :
expr(false, C2x), include(Include), c89(C89), c2x(C2x), asmpp(Asmpp), ctx(nullptr), macro(nullptr), source_date_epoch((time_t)-1), counter_val(0)
{
char* sde = getenv("SOURCE_DATE_EPOCH");
if (sde)
Expand Down Expand Up @@ -676,6 +676,7 @@ bool ppDefine::ReplaceArgs(std::string& macro, const DefinitionArgList& oldargs,
}
else if (Tokenizer::IsSymbolChar(macro.c_str() + p, false))
{
bool doit = true;
int q = p;
name = defid(macro, q, p);
if (!c89 && name == "__VA_ARGS__")
Expand All @@ -700,8 +701,45 @@ bool ppDefine::ReplaceArgs(std::string& macro, const DefinitionArgList& oldargs,
else
p = q + rv - 1;
}
doit = false;
}
else
else if (c2x && name == "__VA_OPT__" && macro[p] == '(')
{
auto start = p+1;
auto end = start;
int count = 1;
while (end < macro.size())
{
if (macro[end] == '(') count ++;
if (macro[end++] == ')')
if (--count ==0)
break;
}
if (!count)
{
if (varargs.empty())
{
int rv;
if ((rv = InsertReplacementString(macro, end, q, "", "")) < -MACRO_REPLACE_SIZE)
return (false);
else
p = q + rv - 1;

}
else
{
std::string temp = macro.substr(p+1, end-p-2);
int rv;
if ((rv = InsertReplacementString(macro, end, q, temp, temp)) < -MACRO_REPLACE_SIZE)
return (false);
else
p = q;
}
doit = false;
}
}
if (doit)
{
for (int i = 0; i < oldargs.size(); i++)
{
if (name == oldargs[i])
Expand All @@ -716,6 +754,7 @@ bool ppDefine::ReplaceArgs(std::string& macro, const DefinitionArgList& oldargs,
}
}
}
}
}
}
return (true);
Expand Down
3 changes: 2 additions & 1 deletion src/ocpp/ppDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ppDefine
};

public:
ppDefine(bool UseExtensions, ppInclude* Include, bool C89, bool Asmpp);
ppDefine(bool UseExtensions, ppInclude* Include, bool C89, bool C2x, bool Asmpp);
~ppDefine() {}

void SetParams(ppCtx* Ctx, ppMacro* Macro)
Expand Down Expand Up @@ -172,6 +172,7 @@ class ppDefine
std::deque<TokenPos> tokenPositions;
static KeywordHash defTokens;
bool c89;
bool c2x;
ppExpr expr;
ppCtx* ctx;
ppMacro* macro;
Expand Down
36 changes: 36 additions & 0 deletions src/ocpp/ppExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ KeywordHash ppExpr::hash = {

ppExpr::CompilerExpression* ppExpr::expressionHandler;

std::unordered_map<std::string, unsigned> ppExpr::cattributes = {
{"deprecated" , 202311},
{"fallthrough" , 202311},
{"nodiscard" , 202311},
{"noreturn" , 202311},
{"maybe_unused" , 202311}
};

ppInclude* ppExpr::include;

PPINT ppExpr::Eval(std::string& line, bool fromConditional)
Expand Down Expand Up @@ -178,6 +186,34 @@ PPINT ppExpr::primary(std::string& line, bool& isunsigned)
}
}
}
else if (c2x && token->GetId() == "__has_c_attribute")
{
token = tokenizer->Next();
if (token->GetKeyword() == kw::openpa)
{
token = tokenizer->Next();
if (token->IsIdentifier())
{
std::string name = token->GetId();
if (name.size() >= 5 && name.substr(0,2) == "__" && name.substr(name.size()-2, 2) == "")
name = name.substr(2, name.size()-4);
rv = cattributes[name];
token = tokenizer->Next();
if (token->GetKeyword() != kw::closepa)
{
Errors::Error("Expected ')'");
}
}
else
{
Errors::Error("Expected identifier in defined statement");
}
}
else
{
Errors::Error("Expected '('");
}
}
else
{
rv = 0;
Expand Down
4 changes: 3 additions & 1 deletion src/ocpp/ppExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ppExpr
public:
typedef long long CompilerExpression(std::string& line);

ppExpr(bool isunsignedchar) : define(nullptr), unsignedchar(isunsignedchar), token(nullptr), floatWarned(false) {}
ppExpr(bool isunsignedchar, bool c2xMode) : define(nullptr), unsignedchar(isunsignedchar), token(nullptr), floatWarned(false), c2x(c2xMode) {}
~ppExpr() {}

void SetParams(ppDefine* Define) { define = Define; }
Expand Down Expand Up @@ -70,11 +70,13 @@ class ppExpr
private:
bool floatWarned;
bool unsignedchar;
bool c2x;
ppDefine* define;
std::unique_ptr<Tokenizer> tokenizer;
const Token* token;
static KeywordHash hash;
static ppInclude* include;
static CompilerExpression* expressionHandler;
static std::unordered_map<std::string, unsigned> cattributes;
};
#endif
4 changes: 2 additions & 2 deletions src/ocpp/ppFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class ppFile : public InputFile

public:
ppFile(bool fullname, bool Trigraph, bool extended, const std::string& Name, ppDefine* define, ppCtx& Ctx, bool isunsignedchar,
bool c89, bool asmpp, PipeArbitrator& piper, int directories_travelled = 0) :
bool c89, bool c2x, bool asmpp, PipeArbitrator& piper, int directories_travelled = 0) :
InputFile(fullname, Name, piper),
trigraphs(Trigraph),
extendedComment(extended),
cond(isunsignedchar, c89, extended, asmpp),
cond(isunsignedchar, c89, c2x, extended, asmpp),
ctx(Ctx),
directoriesTraversed(directories_travelled)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ocpp/ppInclude.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void ppInclude::pushFile(const std::string& name, const std::string& errname, bo
}
// this next line and the support code have been carefully crafted so that GetRealFile() should return a reference to the
// cached object.
current = std::make_unique<ppFile>(fullname, trigraphs, extendedComment, name, define, *ctx, unsignedchar, c89, asmpp,
current = std::make_unique<ppFile>(fullname, trigraphs, extendedComment, name, define, *ctx, unsignedchar, c89, c2x, asmpp,
piper, dirs_traversed);
// if (current)
if (!current->Open())
Expand Down
Loading

0 comments on commit 34217bb

Please sign in to comment.