[clang] [Clang][FPEnv] Don't expand macros in #pragma STDC FENV_ROUND (PR #245535)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 24 00:16:46 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Mészáros Gergely (Maetveis)
<details>
<summary>Changes</summary>
C23 Section 6.10.8 Pragma directive, paragraph 2 says the following
> If the preprocessing token STDC does immediately follow pragma in
> the directive (prior to any macro replacement), then no macro
> replacement is performed on the directive
The GNU C library fenv.h `#defines` the rounding direction macros such
as FE_DOWNWARD, FE_UPWARD, FE_TONEAREST, and FE_TOWARDZERO to integer
constant expressions. If that file is included in the compilation unit
we used to fail to match the macro names and ended up reporting a
misleading warning "expected identifier in '#pragma FENV_ROUND' - ignored".
Fixes: #<!-- -->173098
---
Full diff: https://github.com/llvm/llvm-project/pull/245535.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.md (+1)
- (modified) clang/lib/Parse/ParsePragma.cpp (+2-2)
- (modified) clang/test/Parser/pragma-fenv_round.c (+8)
``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index dce8b7992b72f3..f39c0eaf77526a 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -972,6 +972,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Fixed conversion from `float` to `bool` in `__builtin_convertvector`. (#GH228649)
- Fixed `-print-dependency-directives-minimized-source` output for object-like macros starting with '(' so that it is not incorrectly turned into a function-like macro.
- Fixed an exponential time-increase bug when collecting argument dependent lookup candidates when the same type is used several times at each level.
+- Clang no longer performs macro expansion on the direction argument to `#pragma STDC FENV_ROUND`. (#GH173098)
#### Bug Fixes to Compiler Builtins
diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp
index dd05bd3c21442a..2af4cbce49b37a 100644
--- a/clang/lib/Parse/ParsePragma.cpp
+++ b/clang/lib/Parse/ParsePragma.cpp
@@ -3529,7 +3529,7 @@ void PragmaSTDC_FENV_ROUNDHandler::HandlePragma(Preprocessor &PP,
return;
}
- PP.Lex(Tok);
+ PP.LexUnexpandedToken(Tok);
if (Tok.isNot(tok::identifier)) {
PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
<< PragmaName.getIdentifierInfo()->getName();
@@ -3550,7 +3550,7 @@ void PragmaSTDC_FENV_ROUNDHandler::HandlePragma(Preprocessor &PP,
PP.Diag(Tok.getLocation(), diag::warn_stdc_unknown_rounding_mode);
return;
}
- PP.Lex(Tok);
+ PP.LexUnexpandedToken(Tok);
if (Tok.isNot(tok::eod)) {
PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
diff --git a/clang/test/Parser/pragma-fenv_round.c b/clang/test/Parser/pragma-fenv_round.c
index b2f6f7ed41a4a3..e3fe5b06267447 100644
--- a/clang/test/Parser/pragma-fenv_round.c
+++ b/clang/test/Parser/pragma-fenv_round.c
@@ -9,3 +9,11 @@ float func_01(int x, float y) {
// expected-warning at -1{{pragma STDC FENV_ROUND is not supported}}
return x + y;
}
+
+// Rounding direction names are macros in <fenv.h>; STDC pragmas are not macro-expanded.
+#define FE_DOWNWARD 0x400
+#define FE_UPWARD 0x800
+#pragma STDC FENV_ROUND FE_DOWNWARD // expected-warning{{pragma STDC FENV_ROUND is not supported}}
+
+#define MY_ROUND FE_UPWARD
+#pragma STDC FENV_ROUND MY_ROUND // expected-warning{{invalid or unsupported rounding mode}}
``````````
</details>
https://github.com/llvm/llvm-project/pull/245535
More information about the cfe-commits
mailing list