From f5d64bb8bc16170fcb224c5cb7f6785ddf0acebb Mon Sep 17 00:00:00 2001 From: Yi-Chi Lee <55395582+yichi170@users.noreply.github.com> Date: Wed, 24 Jul 2024 14:56:37 +0800 Subject: [PATCH] [clang] replaced the usage of `asctime` with `std::put_time` (#99075) In `clang/lib/Lex/PPMacroExpansion.cpp`, replaced the usage of the obsolete `asctime` function with `std::put_time` for generating timestamp strings. Fixes: https://github.com/llvm/llvm-project/issues/98724 --- clang/lib/Lex/PPMacroExpansion.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 3913ff08c2eb55b..879f01e87806e21 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -52,7 +52,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -1721,11 +1723,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { Diag(Tok.getLocation(), diag::warn_pp_date_time); // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. - const char *Result; + std::string Result; + std::stringstream TmpStream; + TmpStream.imbue(std::locale("C")); if (getPreprocessorOpts().SourceDateEpoch) { time_t TT = *getPreprocessorOpts().SourceDateEpoch; std::tm *TM = std::gmtime(&TT); - Result = asctime(TM); + TmpStream << std::put_time(TM, "%a %b %e %T %Y"); } else { // Get the file that we are lexing out of. If we're currently lexing from // a macro, dig into the include stack. @@ -1735,13 +1739,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) { if (CurFile) { time_t TT = CurFile->getModificationTime(); struct tm *TM = localtime(&TT); - Result = asctime(TM); - } else { - Result = "??? ??? ?? ??:??:?? ????\n"; + TmpStream << std::put_time(TM, "%a %b %e %T %Y"); } } - // Surround the string with " and strip the trailing newline. - OS << '"' << StringRef(Result).drop_back() << '"'; + Result = TmpStream.str(); + if (Result.empty()) + Result = "??? ??? ?? ??:??:?? ????"; + OS << '"' << Result << '"'; Tok.setKind(tok::string_literal); } else if (II == Ident__FLT_EVAL_METHOD__) { // __FLT_EVAL_METHOD__ is set to the default value.