[clang] [clang] replaced the usage of `asctime` with `strftime` (PR #99075)
Yi-Chi Lee via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 16 11:29:33 PDT 2024
https://github.com/yichi170 created https://github.com/llvm/llvm-project/pull/99075
In `clang/lib/Lex/PPMacroExpansion.cpp`, replaced the usage of the obsolete `asctime` function with `strftime` for generating timestamp strings.
This is my first time contributing to an open-source project. If there is anything that I can improve (such as code quality), please let me know!
Fixes: https://github.com/llvm/llvm-project/issues/98724
>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] [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.
More information about the cfe-commits
mailing list