[PATCH] D139290: [ADT, Support] Move operator<< to raw_ostream.h (NFC)

Kazu Hirata via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 4 22:05:32 PST 2022


kazu created this revision.
Herald added a project: All.
kazu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Without this patch, operator<< for Optional<T> and std::optional<T>
are in Optional.h.  This means that a C++ source file must include
Optional.h even if it just needs to stream std::optional<T> and has
nothing to do with Optional<T>, which is counter-intuitive.

This patch moves the operator<< to raw_ostream.h.

As a bonus, we get to resolve a circular dependency.  Optional.h no
longer needs to forward-declare raw_ostream.  That is, raw_ostream.h
depends on Optional.h, not vice versa.

As a preparation for this patch, I've checked in
77609717410372e8c43aca49a268511378f58297 <https://reviews.llvm.org/rG77609717410372e8c43aca49a268511378f58297> to forward-declare
raw_ostream in those header files that were relying on the forward
declaration of raw_ostream in Optional.h.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139290

Files:
  llvm/include/llvm/ADT/Optional.h
  llvm/include/llvm/Support/raw_ostream.h


Index: llvm/include/llvm/Support/raw_ostream.h
===================================================================
--- llvm/include/llvm/Support/raw_ostream.h
+++ llvm/include/llvm/Support/raw_ostream.h
@@ -741,6 +741,28 @@
 Error writeToOutput(StringRef OutputFileName,
                     std::function<Error(raw_ostream &)> Write);
 
+raw_ostream &operator<<(raw_ostream &OS, std::nullopt_t);
+
+template <typename T, typename = decltype(std::declval<raw_ostream &>()
+                                          << std::declval<const T &>())>
+raw_ostream &operator<<(raw_ostream &OS, const Optional<T> &O) {
+  if (O)
+    OS << *O;
+  else
+    OS << std::nullopt;
+  return OS;
+}
+
+template <typename T, typename = decltype(std::declval<raw_ostream &>()
+                                          << std::declval<const T &>())>
+raw_ostream &operator<<(raw_ostream &OS, const std::optional<T> &O) {
+  if (O)
+    OS << *O;
+  else
+    OS << std::nullopt;
+  return OS;
+}
+
 } // end namespace llvm
 
 #endif // LLVM_SUPPORT_RAW_OSTREAM_H
Index: llvm/include/llvm/ADT/Optional.h
===================================================================
--- llvm/include/llvm/ADT/Optional.h
+++ llvm/include/llvm/ADT/Optional.h
@@ -23,13 +23,10 @@
 #include "llvm/Support/type_traits.h"
 #include <cassert>
 #include <new>
-#include <optional>
 #include <utility>
 
 namespace llvm {
 
-class raw_ostream;
-
 namespace optional_detail {
 
 /// Storage for any type.
@@ -479,28 +476,6 @@
   return !(X < Y);
 }
 
-raw_ostream &operator<<(raw_ostream &OS, std::nullopt_t);
-
-template <typename T, typename = decltype(std::declval<raw_ostream &>()
-                                          << std::declval<const T &>())>
-raw_ostream &operator<<(raw_ostream &OS, const Optional<T> &O) {
-  if (O)
-    OS << *O;
-  else
-    OS << std::nullopt;
-  return OS;
-}
-
-template <typename T, typename = decltype(std::declval<raw_ostream &>()
-                                          << std::declval<const T &>())>
-raw_ostream &operator<<(raw_ostream &OS, const std::optional<T> &O) {
-  if (O)
-    OS << *O;
-  else
-    OS << std::nullopt;
-  return OS;
-}
-
 } // end namespace llvm
 
 #endif // LLVM_ADT_OPTIONAL_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139290.479981.patch
Type: text/x-patch
Size: 2219 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221205/4378dbe0/attachment.bin>


More information about the llvm-commits mailing list