llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Hardik Kumar (hardikxk)

<details>
<summary>Changes</summary>

The patch updates the `TryEvaluateBuiltinNan` method to reject non-narrow 
string literals preventing the downstream assertion failure caused by 
`getString()` in cases where the user might pass a non-narrow / non 1 byte 
literal."


I think this requires tests as well but I haven't added them since when I tried 
to, I had some issues and the tests seem to keep failing. (I considered asking 
on discord before pushing this patch but I supposed this would be a better 
place instead).
I'm sharing the patch I tried for the tests. If I can get some feedback on how 
to correctly write them I will push the changes in the following commit if 
these tests are required.

```bash
diff --git a/clang/test/Sema/constexpr.c b/clang/test/Sema/constexpr.c
index 51ee62d249..843099e66e 100644
--- a/clang/test/Sema/constexpr.c
+++ b/clang/test/Sema/constexpr.c
@@ -343,6 +343,10 @@ void f7(int n, int array[n]) {
 #define FLT_NAN __builtin_nanf("1")
 #define DBL_NAN __builtin_nan("1")
 #define LD_NAN __builtin_nanf("1")
+#define WIDE_NAN __builtin_nanf(L"1")
+#define UTF32_NAN __builtin_nanf(U"1")
+#define UTF16_NAN __builtin_nanf(u"1")
+#define UTF8_NAN __builtin_nanf(u8"1")
 #define FLT_SNAN __builtin_nansf("1")
 #define DBL_SNAN __builtin_nans("1")
 #define LD_SNAN __builtin_nansl("1")
@@ -357,6 +361,14 @@ void infsNaNs() {
   constexpr float fl6 = LD_NAN;
   constexpr float fl7 = DBL_SNAN; // expected-error {{constexpr initializer 
evaluates to nan which is not exactly representable in type 'const float'}}
   constexpr float fl8 = LD_SNAN; // expected-error {{constexpr initializer 
evaluates to nan which is not exactly representable in type 'const float'}}
+  constexpr float fl9 = WIDE_NAN; // expected-error {{incompatible pointer 
types passing 'int[2]' to parameter of type 'const char *'}} \
+                                  // expected-error {{constexpr variable 'fl9' 
mustbe initialized by a constant expression}}
+  constexpr float fl10 = UTF32_NAN; // expected-error {{incompatible pointer 
types passing 'unsigned int[2]' to parameter of type 'const char *'}} \
+                                    // expected-error {{constexpr variable 
'fl10' must be initialized by a constant expression}}
+  constexpr float fl11 = UTF16_NAN;  // expected-error {{incompatible pointer 
types passing 'unsigned short[2]' to parameter of type 'const char *'}} \
+                                    // expected-error {{constexpr variable 
'fl11' must be initialized by a constant expression}}
+  constexpr float fl12 = UTF8_NAN; // expected-warning {{passing 'unsigned 
char[2]' to parameter of type 'const char *' converts between pointers to 
integer types whereone is of the unique plain 'char' type and the other is 
not}} \
+                                  // expected-error {{constexpr variable 
'fl12' must be initialized by a constant expression}}

   constexpr double db0 = FLT_NAN;
   constexpr double db2 = DBL_NAN;
```

related issue: #<!-- -->205306

---
Full diff: https://github.com/llvm/llvm-project/pull/212108.diff


1 Files Affected:

- (modified) clang/lib/AST/ExprConstant.cpp (+2-1) 


``````````diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 574dd8b04e779..a3385d3b7318f 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -20394,7 +20394,8 @@ static bool TryEvaluateBuiltinNaN(const ASTContext 
&Context,
                                   bool SNaN,
                                   llvm::APFloat &Result) {
   const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts());
-  if (!S) return false;
+  if (!S || !S->isOrdinary())
+    return false;
 
   const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy);
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/212108
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to