[clang] [clang] replaced the usage of `asctime` with `std::put_time` (PR #99075)
Yi-Chi Lee via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 23 20:16:51 PDT 2024
https://github.com/yichi170 updated https://github.com/llvm/llvm-project/pull/99075
>From 60a8a36851bcb0e73228ec4941f1906d0e5b7ba9 Mon Sep 17 00:00:00 2001
From: yichi170 <ilee900620 at gmail.com>
Date: Wed, 17 Jul 2024 02:15:43 +0800
Subject: [PATCH] [clang] replaced the usage of `asctime` with `std::put_time`
---
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 3913ff08c2eb5..879f01e87806e 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -52,7 +52,9 @@
#include <cstddef>
#include <cstring>
#include <ctime>
+#include <iomanip>
#include <optional>
+#include <sstream>
#include <string>
#include <tuple>
#include <utility>
@@ -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.
More information about the cfe-commits
mailing list