Skip to content

Commit fad17b4

Browse files
authored
[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: #98724
1 parent ef1c70d commit fad17b4

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
#include <cstddef>
5353
#include <cstring>
5454
#include <ctime>
55+
#include <iomanip>
5556
#include <optional>
57+
#include <sstream>
5658
#include <string>
5759
#include <tuple>
5860
#include <utility>
@@ -1721,11 +1723,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
17211723
Diag(Tok.getLocation(), diag::warn_pp_date_time);
17221724
// MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be
17231725
// of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime.
1724-
const char *Result;
1726+
std::string Result;
1727+
std::stringstream TmpStream;
1728+
TmpStream.imbue(std::locale("C"));
17251729
if (getPreprocessorOpts().SourceDateEpoch) {
17261730
time_t TT = *getPreprocessorOpts().SourceDateEpoch;
17271731
std::tm *TM = std::gmtime(&TT);
1728-
Result = asctime(TM);
1732+
TmpStream << std::put_time(TM, "%a %b %e %T %Y");
17291733
} else {
17301734
// Get the file that we are lexing out of. If we're currently lexing from
17311735
// a macro, dig into the include stack.
@@ -1735,13 +1739,13 @@ void Preprocessor::ExpandBuiltinMacro(Token &Tok) {
17351739
if (CurFile) {
17361740
time_t TT = CurFile->getModificationTime();
17371741
struct tm *TM = localtime(&TT);
1738-
Result = asctime(TM);
1739-
} else {
1740-
Result = "??? ??? ?? ??:??:?? ????\n";
1742+
TmpStream << std::put_time(TM, "%a %b %e %T %Y");
17411743
}
17421744
}
1743-
// Surround the string with " and strip the trailing newline.
1744-
OS << '"' << StringRef(Result).drop_back() << '"';
1745+
Result = TmpStream.str();
1746+
if (Result.empty())
1747+
Result = "??? ??? ?? ??:??:?? ????";
1748+
OS << '"' << Result << '"';
17451749
Tok.setKind(tok::string_literal);
17461750
} else if (II == Ident__FLT_EVAL_METHOD__) {
17471751
// __FLT_EVAL_METHOD__ is set to the default value.

0 commit comments

Comments
 (0)