[clang] `__noop` not marked as constexpr #102064 (PR #105983)

via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 25 05:32:02 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: ofAlpaca (ofAlpaca)

<details>
<summary>Changes</summary>

Resolves #<!-- -->102064 
I think the problem is that the function `bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp)` return default `false` directly when visiting expression of `__noop`.
Thus, `__noop` cannot be evaluated as constant initializer in here.
https://github.com/llvm/llvm-project/blob/b6603e1bf11dee4761e49af6581c8b8f074b705d/clang/lib/Sema/SemaDecl.cpp#L14402
By adding a `BI__noop` case in `bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp)` should fix the problem.
Correct me if there is anything I misunderstood.

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


4 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+2) 
- (modified) clang/include/clang/Basic/Builtins.td (+1-1) 
- (modified) clang/lib/AST/ExprConstant.cpp (+4) 
- (added) clang/test/SemaCXX/GH102064.cpp (+3) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 971df672b6ca1e..e60407061ccd3b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -861,6 +861,8 @@ Bug Fixes to Compiler Builtins
 - Clang now allows pointee types of atomic builtin arguments to be complete template types
   that was not instantiated elsewhere.
 
+- Fix ``__noop`` not marked as constexpr. (#GH102064)
+
 Bug Fixes to Attribute Support
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index f5b15cf90d1f83..b42f7ea1d9de68 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -2516,7 +2516,7 @@ def IsoVolatileStore : MSLangBuiltin, Int8_16_32_64Template {
 
 def Noop : MSLangBuiltin {
   let Spellings = ["__noop"];
-  let Attributes = [NoThrow];
+  let Attributes = [NoThrow, Constexpr];
   let Prototype = "int(...)";
 }
 
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 5af712dd7257b1..d505346bccd9b3 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -12586,6 +12586,10 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
     return false;
   }
 
+  case Builtin::BI__noop:
+  // __noop always evaluates successfully
+    return true;
+
   case Builtin::BI__builtin_is_constant_evaluated: {
     const auto *Callee = Info.CurrentCall->getCallee();
     if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression &&
diff --git a/clang/test/SemaCXX/GH102064.cpp b/clang/test/SemaCXX/GH102064.cpp
new file mode 100644
index 00000000000000..0ed930439e3d75
--- /dev/null
+++ b/clang/test/SemaCXX/GH102064.cpp
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 -std=c++20 -fms-extensions %s
+// expected-no-diagnostics
+constexpr int x = []{ __noop; return 0; }();

``````````

</details>


https://github.com/llvm/llvm-project/pull/105983


More information about the cfe-commits mailing list