[PATCH] D157296: [AST][Coroutine] Fix CoyieldExpr missing end loc

Ding Fei via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 7 10:26:33 PDT 2023


danix800 updated this revision to Diff 547843.
danix800 added a comment.

Use `getEndLoc()` instead of `getBeginLoc()`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157296/new/

https://reviews.llvm.org/D157296

Files:
  clang/lib/Sema/SemaCoroutine.cpp
  clang/test/AST/coroutine-co_yield-source-range.cpp


Index: clang/test/AST/coroutine-co_yield-source-range.cpp
===================================================================
--- /dev/null
+++ clang/test/AST/coroutine-co_yield-source-range.cpp
@@ -0,0 +1,77 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -std=c++20 \
+// RUN:    -fsyntax-only -ast-dump | FileCheck %s
+
+namespace std {
+template <class Ret, typename... T>
+struct coroutine_traits { using promise_type = typename Ret::promise_type; };
+
+template <class Promise = void>
+struct coroutine_handle {
+  static coroutine_handle from_address(void *) noexcept;
+  static coroutine_handle from_promise(Promise &promise);
+  constexpr void* address() const noexcept;
+};
+template <>
+struct coroutine_handle<void> {
+  template <class PromiseType>
+  coroutine_handle(coroutine_handle<PromiseType>) noexcept;
+  static coroutine_handle from_address(void *);
+  constexpr void* address() const noexcept;
+};
+
+struct suspend_always {
+  bool await_ready() noexcept { return false; }
+  void await_suspend(coroutine_handle<>) noexcept {}
+  void await_resume() noexcept {}
+};
+
+struct suspend_never {
+  bool await_ready() noexcept { return true; }
+  void await_suspend(coroutine_handle<>) noexcept {}
+  void await_resume() noexcept {}
+};
+} // namespace std
+
+struct Chat {
+  struct promise_type {
+    std::suspend_always initial_suspend() { return {}; }
+    Chat get_return_object() {
+      return std::coroutine_handle<promise_type>::from_promise(*this);
+    }
+    std::suspend_always yield_value(int m) {
+      return {};
+    }
+    std::suspend_always final_suspend() noexcept { return {}; }
+    std::suspend_always return_value(int) noexcept { return {}; }
+    void unhandled_exception() noexcept {}
+
+    auto await_transform(int s) noexcept {
+      struct awaiter {
+        promise_type *promise;
+        bool await_ready() const {
+          return true;
+        }
+        int await_resume() const {
+          return promise->message;
+        }
+        void await_suspend(std::coroutine_handle<>) {
+        }
+      };
+      return awaiter{this};
+    }
+    int message;
+  };
+
+  Chat(std::coroutine_handle<promise_type> promise);
+
+  std::coroutine_handle<promise_type> handle;
+};
+
+Chat f(int s) {
+  // CHECK: CoyieldExpr {{.*}} <col:3, col:12>
+  co_yield s;
+  // CHECK: CoreturnStmt {{.*}} <line:74:3, col:13>
+  co_return s;
+  // CHECK: CoawaitExpr {{.*}} <col:3, col:12>
+  co_await s;
+}
Index: clang/lib/Sema/SemaCoroutine.cpp
===================================================================
--- clang/lib/Sema/SemaCoroutine.cpp
+++ clang/lib/Sema/SemaCoroutine.cpp
@@ -318,7 +318,8 @@
     return ExprError();
   }
 
-  return S.BuildCallExpr(nullptr, Result.get(), Loc, Args, Loc, nullptr);
+  auto EndLoc = Args.empty() ? Loc : Args.back()->getEndLoc();
+  return S.BuildCallExpr(nullptr, Result.get(), Loc, Args, EndLoc, nullptr);
 }
 
 // See if return type is coroutine-handle and if so, invoke builtin coro-resume


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157296.547843.patch
Type: text/x-patch
Size: 2997 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230807/ebd9a3b9/attachment.bin>


More information about the cfe-commits mailing list