Skip to content

Commit 5eecec8

Browse files
authored
[flang] Fix use of __has_builtin and formatting (#148746)
`__has_builtin` is not available on all compilers. Make sure it works when not defined. Also fix some formatting issues: - Use brace initialization where possible - Fix wrong capitalization of variables. - Add `std::` for `unit64_t` and `int64_t` as it is mostly done in this part of the codebase.
1 parent 49d2b5f commit 5eecec8

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

flang/include/flang/Common/format.h

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,64 +32,73 @@ namespace Fortran::common {
3232
// AddOverflow and MulOverflow are copied from
3333
// llvm/include/llvm/Support/MathExtras.h and specialised to int64_t.
3434

35+
// __has_builtin is not defined in some compilers. Make sure it is defined.
36+
#ifndef __has_builtin
37+
#define __has_builtin(x) 0
38+
#endif
39+
3540
/// Add two signed integers, computing the two's complement truncated result,
3641
/// returning true if overflow occurred.
37-
static inline bool AddOverflow(int64_t X, int64_t Y, int64_t &Result) {
42+
static inline bool AddOverflow(
43+
std::int64_t x, std::int64_t y, std::int64_t &result) {
3844
#if __has_builtin(__builtin_add_overflow)
39-
return __builtin_add_overflow(X, Y, &Result);
45+
return __builtin_add_overflow(x, y, &result);
4046
#else
4147
// Perform the unsigned addition.
42-
const uint64_t UX = static_cast<uint64_t>(X);
43-
const uint64_t UY = static_cast<uint64_t>(Y);
44-
const uint64_t UResult = UX + UY;
48+
const std::uint64_t ux{static_cast<std::uint64_t>(x)};
49+
const std::uint64_t uy{static_cast<std::uint64_t>(y)};
50+
const std::uint64_t uresult{ux + uy};
4551

4652
// Convert to signed.
47-
Result = static_cast<int64_t>(UResult);
53+
result = static_cast<std::int64_t>(uresult);
4854

4955
// Adding two positive numbers should result in a positive number.
50-
if (X > 0 && Y > 0) {
51-
return Result <= 0;
56+
if (x > 0 && y > 0) {
57+
return result <= 0;
5258
}
5359
// Adding two negatives should result in a negative number.
54-
if (X < 0 && Y < 0) {
55-
return Result >= 0;
60+
if (x < 0 && y < 0) {
61+
return result >= 0;
5662
}
5763
return false;
5864
#endif
5965
}
6066

6167
/// Multiply two signed integers, computing the two's complement truncated
6268
/// result, returning true if an overflow occurred.
63-
static inline bool MulOverflow(int64_t X, int64_t Y, int64_t &Result) {
69+
static inline bool MulOverflow(
70+
std::int64_t x, std::int64_t y, std::int64_t &result) {
6471
#if __has_builtin(__builtin_mul_overflow)
65-
return __builtin_mul_overflow(X, Y, &Result);
72+
return __builtin_mul_overflow(x, y, &result);
6673
#else
6774
// Perform the unsigned multiplication on absolute values.
68-
const uint64_t UX =
69-
X < 0 ? (0 - static_cast<uint64_t>(X)) : static_cast<uint64_t>(X);
70-
const uint64_t UY =
71-
Y < 0 ? (0 - static_cast<uint64_t>(Y)) : static_cast<uint64_t>(Y);
72-
const uint64_t UResult = UX * UY;
75+
const std::uint64_t ux{x < 0 ? (0 - static_cast<std::uint64_t>(x))
76+
: static_cast<std::uint64_t>(x)};
77+
const std::uint64_t uy{y < 0 ? (0 - static_cast<std::uint64_t>(y))
78+
: static_cast<std::uint64_t>(y)};
79+
const std::uint64_t uresult{ux * uy};
7380

7481
// Convert to signed.
75-
const bool IsNegative = (X < 0) ^ (Y < 0);
76-
Result = IsNegative ? (0 - UResult) : UResult;
82+
const bool isNegative = (x < 0) ^ (y < 0);
83+
result = isNegative ? (0 - uresult) : uresult;
7784

7885
// If any of the args was 0, result is 0 and no overflow occurs.
79-
if (UX == 0 || UY == 0) {
86+
if (ux == 0 || uy == 0) {
8087
return false;
8188
}
8289

83-
// UX and UY are in [1, 2^n], where n is the number of digits.
90+
// ux and uy are in [1, 2^n], where n is the number of digits.
8491
// Check how the max allowed absolute value (2^n for negative, 2^(n-1) for
8592
// positive) divided by an argument compares to the other.
86-
if (IsNegative) {
87-
return UX > (static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) +
88-
uint64_t(1)) /
89-
UY;
93+
if (isNegative) {
94+
return ux >
95+
(static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max()) +
96+
std::uint64_t{1}) /
97+
uy;
9098
} else {
91-
return UX >
92-
(static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) / UY;
99+
return ux >
100+
(static_cast<std::uint64_t>(std::numeric_limits<std::int64_t>::max())) /
101+
uy;
93102
}
94103
#endif
95104
}
@@ -206,10 +215,10 @@ template <typename CHAR = char> class FormatValidator {
206215
Token token_{}; // current token
207216
Token knrToken_{}; // k, n, or r UnsignedInteger token
208217
Token scaleFactorToken_{}; // most recent scale factor token P
209-
int64_t integerValue_{-1}; // value of UnsignedInteger token
210-
int64_t knrValue_{-1}; // -1 ==> not present
211-
int64_t scaleFactorValue_{}; // signed k in kP
212-
int64_t wValue_{-1};
218+
std::int64_t integerValue_{-1}; // value of UnsignedInteger token
219+
std::int64_t knrValue_{-1}; // -1 ==> not present
220+
std::int64_t scaleFactorValue_{}; // signed k in kP
221+
std::int64_t wValue_{-1};
213222
char argString_[3]{}; // 1-2 character msg arg; usually edit descriptor name
214223
bool formatHasErrors_{false};
215224
bool unterminatedFormatError_{false};
@@ -280,18 +289,18 @@ template <typename CHAR> void FormatValidator<CHAR>::NextToken() {
280289
case '7':
281290
case '8':
282291
case '9': {
283-
const CHAR *lastCursor;
292+
const CHAR *lastCursor{};
284293
integerValue_ = 0;
285294
bool overflow{false};
286295
do {
287296
lastCursor = cursor_;
288297
if (!overflow) {
289-
overflow =
290-
MulOverflow(static_cast<int64_t>(10), integerValue_, integerValue_);
298+
overflow = MulOverflow(
299+
static_cast<std::int64_t>(10), integerValue_, integerValue_);
291300
}
292301
if (!overflow) {
293302
overflow = AddOverflow(
294-
integerValue_, static_cast<int64_t>(c - '0'), integerValue_);
303+
integerValue_, static_cast<std::int64_t>(c - '0'), integerValue_);
295304
}
296305
c = NextChar();
297306
} while (c >= '0' && c <= '9');

0 commit comments

Comments
 (0)