[clang] [clang] replaced the usage of `asctime` with `strftime` (PR #99075)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 16 11:30:22 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Yi-Chi Lee (yichi170)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/99075.diff
1 Files Affected:
- (modified) clang/lib/Lex/PPMacroExpansion.cpp (+7-5)
``````````diff
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.
``````````
</details>
https://github.com/llvm/llvm-project/pull/99075
More information about the cfe-commits
mailing list