[clang] [clang] replaced the usage of `asctime` with `strftime` (PR #99075)

Yi-Chi Lee via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 17 07:20:45 PDT 2024


https://github.com/yichi170 updated https://github.com/llvm/llvm-project/pull/99075

>From 0fcbd4a46bb05ad9829fcf33a9829fd5f5269c71 Mon Sep 17 00:00:00 2001
From: yichi170 <ilee900620 at gmail.com>
Date: Wed, 17 Jul 2024 02:15:43 +0800
Subject: [PATCH 1/2] [clang] replaced the usage of `asctime` with `strftime`

---
 clang/lib/Lex/PPMacroExpansion.cpp | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index 3913ff08c2eb5..f7e657be87932 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1722,10 +1722,12 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
     // 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;
+    char TimeString[std::size("Ddd Mmm dd hh:mm:ss yyyy")];
     if (getPreprocessorOpts().SourceDateEpoch) {
       time_t TT = *getPreprocessorOpts().SourceDateEpoch;
       std::tm *TM = std::gmtime(&TT);
-      Result = asctime(TM);
+      strftime(TimeString, std::size(TimeString), "%c", TM);
+      Result = TimeString;
     } 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 +1737,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
       if (CurFile) {
         time_t TT = CurFile->getModificationTime();
         struct tm *TM = localtime(&TT);
-        Result = asctime(TM);
+        strftime(TimeString, std::size(TimeString), "%c", TM);
+        Result = TimeString;
       } else {
-        Result = "??? ??? ?? ??:??:?? ????\n";
+        Result = "??? ??? ?? ??:??:?? ????";
       }
     }
-    // Surround the string with " and strip the trailing newline.
-    OS << '"' << StringRef(Result).drop_back() << '"';
+    OS << '"' << Result << '"';
     Tok.setKind(tok::string_literal);
   } else if (II == Ident__FLT_EVAL_METHOD__) {
     // __FLT_EVAL_METHOD__ is set to the default value.

>From 0352a494b6d0735ad83aa9189d33f2e50a1349d0 Mon Sep 17 00:00:00 2001
From: yichi170 <ilee900620 at gmail.com>
Date: Wed, 17 Jul 2024 22:17:48 +0800
Subject: [PATCH 2/2] [clang] handling zero return of `strgtime` in
 `clang/lib/Lex/PPMacroExpansion.cpp`

---
 clang/lib/Lex/PPMacroExpansion.cpp | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index f7e657be87932..89697872901d7 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1721,13 +1721,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;
-    char TimeString[std::size("Ddd Mmm dd hh:mm:ss yyyy")];
+    const char *Result = "??? ??? ?? ??:??:?? ????";
+    char TimeString[64];
     if (getPreprocessorOpts().SourceDateEpoch) {
       time_t TT = *getPreprocessorOpts().SourceDateEpoch;
       std::tm *TM = std::gmtime(&TT);
-      strftime(TimeString, std::size(TimeString), "%c", TM);
-      Result = TimeString;
+      if (strftime(TimeString, sizeof TimeString, "%a %b %e %T %Y", TM) != 0)
+        Result = TimeString;
     } else {
       // Get the file that we are lexing out of.  If we're currently lexing from
       // a macro, dig into the include stack.
@@ -1737,10 +1737,8 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
       if (CurFile) {
         time_t TT = CurFile->getModificationTime();
         struct tm *TM = localtime(&TT);
-        strftime(TimeString, std::size(TimeString), "%c", TM);
-        Result = TimeString;
-      } else {
-        Result = "??? ??? ?? ??:??:?? ????";
+        if (strftime(TimeString, sizeof TimeString, "%a %b %e %T %Y", TM) != 0)
+          Result = TimeString;
       }
     }
     OS << '"' << Result << '"';



More information about the cfe-commits mailing list