@@ -84,6 +84,22 @@ rewriteInverseDurationCall(const MatchFinder::MatchResult &Result,
84
84
return llvm::None;
85
85
}
86
86
87
+ // / If `Node` is a call to the inverse of `Scale`, return that inverse's
88
+ // / argument, otherwise None.
89
+ static llvm::Optional<std::string>
90
+ rewriteInverseTimeCall (const MatchFinder::MatchResult &Result,
91
+ DurationScale Scale, const Expr &Node) {
92
+ llvm::StringRef InverseFunction = getTimeInverseForScale (Scale);
93
+ if (const auto *MaybeCallArg = selectFirst<const Expr>(
94
+ " e" , match (callExpr (callee (functionDecl (hasName (InverseFunction))),
95
+ hasArgument (0 , expr ().bind (" e" ))),
96
+ Node, *Result.Context ))) {
97
+ return tooling::fixit::getText (*MaybeCallArg, *Result.Context ).str ();
98
+ }
99
+
100
+ return llvm::None;
101
+ }
102
+
87
103
// / Returns the factory function name for a given `Scale`.
88
104
llvm::StringRef getDurationFactoryForScale (DurationScale Scale) {
89
105
switch (Scale) {
@@ -103,6 +119,24 @@ llvm::StringRef getDurationFactoryForScale(DurationScale Scale) {
103
119
llvm_unreachable (" unknown scaling factor" );
104
120
}
105
121
122
+ llvm::StringRef getTimeFactoryForScale (DurationScale Scale) {
123
+ switch (Scale) {
124
+ case DurationScale::Hours:
125
+ return " absl::FromUnixHours" ;
126
+ case DurationScale::Minutes:
127
+ return " absl::FromUnixMinutes" ;
128
+ case DurationScale::Seconds:
129
+ return " absl::FromUnixSeconds" ;
130
+ case DurationScale::Milliseconds:
131
+ return " absl::FromUnixMillis" ;
132
+ case DurationScale::Microseconds :
133
+ return " absl::FromUnixMicros" ;
134
+ case DurationScale::Nanoseconds:
135
+ return " absl::FromUnixNanos" ;
136
+ }
137
+ llvm_unreachable (" unknown scaling factor" );
138
+ }
139
+
106
140
// / Returns the Time factory function name for a given `Scale`.
107
141
llvm::StringRef getTimeInverseForScale (DurationScale scale) {
108
142
switch (scale) {
@@ -250,9 +284,27 @@ std::string rewriteExprFromNumberToDuration(
250
284
.str ();
251
285
}
252
286
253
- bool isNotInMacro (const MatchFinder::MatchResult &Result, const Expr *E) {
287
+ std::string rewriteExprFromNumberToTime (
288
+ const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
289
+ const Expr *Node) {
290
+ const Expr &RootNode = *Node->IgnoreParenImpCasts ();
291
+
292
+ // First check to see if we can undo a complimentary function call.
293
+ if (llvm::Optional<std::string> MaybeRewrite =
294
+ rewriteInverseTimeCall (Result, Scale, RootNode))
295
+ return *MaybeRewrite;
296
+
297
+ if (IsLiteralZero (Result, RootNode))
298
+ return std::string (" absl::UnixEpoch()" );
299
+
300
+ return (llvm::Twine (getTimeFactoryForScale (Scale)) + " (" +
301
+ tooling::fixit::getText (RootNode, *Result.Context ) + " )" )
302
+ .str ();
303
+ }
304
+
305
+ bool isInMacro (const MatchFinder::MatchResult &Result, const Expr *E) {
254
306
if (!E->getBeginLoc ().isMacroID ())
255
- return true ;
307
+ return false ;
256
308
257
309
SourceLocation Loc = E->getBeginLoc ();
258
310
// We want to get closer towards the initial macro typed into the source only
@@ -264,7 +316,7 @@ bool isNotInMacro(const MatchFinder::MatchResult &Result, const Expr *E) {
264
316
// because Clang comment says it "should not generally be used by clients."
265
317
Loc = Result.SourceManager ->getImmediateMacroCallerLoc (Loc);
266
318
}
267
- return ! Loc.isMacroID ();
319
+ return Loc.isMacroID ();
268
320
}
269
321
270
322
} // namespace abseil
0 commit comments