From: Michael Wallner Date: Mon, 10 Dec 2018 09:09:13 +0000 (+0100) Subject: CPP builtins X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=195175bc433f6b3167dc24d2c17ecea975a016e9;p=m6w6%2Fext-psi CPP builtins * __DATE__ * __TIME__ --- diff --git a/TODO b/TODO index 8823a08..c60442f 100644 --- a/TODO +++ b/TODO @@ -21,3 +21,4 @@ * real integral type of enums * #pragma PSI lib "foo" instead of lib "foo"; +* #line directive diff --git a/src/builtin.c b/src/builtin.c index 5d9d7e4..9ed930c 100644 --- a/src/builtin.c +++ b/src/builtin.c @@ -45,9 +45,11 @@ static bool has_feature(struct psi_cpp *cpp, struct psi_token *target, struct ps static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res); static bool BASE_FILE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res); static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res); +static bool DATE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res); static bool FILE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res); static bool INCLUDE_LEVEL__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res); static bool LINE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res); +static bool TIME__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res); static bool TIMESTAMP__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res); static inline struct psi_plist *builtin_sig(token_t typ, ...) @@ -109,9 +111,11 @@ PHP_MINIT_FUNCTION(psi_builtin) PSI_BUILTIN(BASE_FILE__, -1); PSI_BUILTIN(COUNTER__, -1); + PSI_BUILTIN(DATE__, -1); PSI_BUILTIN(FILE__, -1); PSI_BUILTIN(INCLUDE_LEVEL__, -1); PSI_BUILTIN(LINE__, -1); + PSI_BUILTIN(TIME__, -1); PSI_BUILTIN(TIMESTAMP__, -1); return SUCCESS; @@ -214,7 +218,7 @@ static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target, #define ADD_UNSIGNED_NUMBER(u) do { \ char buf[0x20]; \ unsigned u_ = u; \ - size_t len = sprintf(buf, "%u", u_); \ + size_t len = snprintf(buf, sizeof(buf), "%u", u_); \ struct psi_token *tok_ = NEW_TOKEN(PSI_T_NUMBER, buf, len); \ tok_->flags |= PSI_NUMBER_INT | PSI_NUMBER_U; \ ADD_TOKEN(tok_); \ @@ -234,6 +238,24 @@ static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target, return true; } +static bool DATE__(struct psi_cpp *cpp, struct psi_token *target, + struct psi_plist **args, struct psi_plist **res) +{ + char buf[12]; + struct tm *tp; + time_t t = time(NULL); +#if HAVE_LOCALTIME_R + struct tm tm = {0}; + tp = localtime_r(&t, &tm); +#else + tp = localtime(&t); +#endif + + strftime(buf, sizeof(buf), "%b %e %Y", tp); + ADD_QUOTED_STRING(buf, 11); + return true; +} + static bool FILE__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res) { @@ -255,6 +277,24 @@ static bool LINE__(struct psi_cpp *cpp, struct psi_token *target, return true; } +static bool TIME__(struct psi_cpp *cpp, struct psi_token *target, + struct psi_plist **args, struct psi_plist **res) +{ + char buf[9]; + struct tm *tp; + time_t t = time(NULL); +#if HAVE_LOCALTIME_R + struct tm tm = {0}; + tp = localtime_r(&t, &tm); +#else + tp = localtime(&t); +#endif + + strftime(buf, sizeof(buf), "%H:%M:%S", tp); + ADD_QUOTED_STRING(buf, 8); + return true; +} + static bool TIMESTAMP__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res) { diff --git a/tests/parser/cpp004.phpt b/tests/parser/cpp004.phpt index 69d448f..da46c2d 100644 --- a/tests/parser/cpp004.phpt +++ b/tests/parser/cpp004.phpt @@ -12,7 +12,7 @@ LC_TIME=C ===TEST=== ===DONE=== --EXPECTF-- @@ -28,4 +28,6 @@ int(2) string(24) "%s %s %w%d %d:%d:%d %d" string(54) "/%s/tests/parser/cpp004/include_level.h" int(3) +string(11) "%s %d %d" +string(8) "%d:%d:%d" ===DONE=== diff --git a/tests/parser/cpp004/builtins.psi b/tests/parser/cpp004/builtins.psi index 24f79d4..5055846 100644 --- a/tests/parser/cpp004/builtins.psi +++ b/tests/parser/cpp004/builtins.psi @@ -11,3 +11,6 @@ const \CPP_TEST5 = c(500); #include "base_file.h" // includes include_level.h #include "timestamp.h" + +const \date = __DATE__; +const \time = __TIME__;