[llvm] 227078b - [ADT, Support] Move operator<< to raw_ostream.h (NFC)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 6 09:10:31 PST 2022


Author: Kazu Hirata
Date: 2022-12-06T09:10:25-08:00
New Revision: 227078b7136c09f6505dab7972d7b65d5210209d

URL: https://github.com/llvm/llvm-project/commit/227078b7136c09f6505dab7972d7b65d5210209d
DIFF: https://github.com/llvm/llvm-project/commit/227078b7136c09f6505dab7972d7b65d5210209d.diff

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

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 to forward-declare
raw_ostream in those header files that were relying on the forward
declaration of raw_ostream in Optional.h.

Differential Revision: https://reviews.llvm.org/D139290

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/Optional.h b/llvm/include/llvm/ADT/Optional.h
index 56c0d9c29bf43..ec6c0b2483e1c 100644
--- a/llvm/include/llvm/ADT/Optional.h
+++ b/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 @@ constexpr bool operator>=(const T &X, const Optional<T> &Y) {
   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

diff  --git a/llvm/include/llvm/Support/raw_ostream.h b/llvm/include/llvm/Support/raw_ostream.h
index c4ae9dbd24e40..b5c3e463d69dc 100644
--- a/llvm/include/llvm/Support/raw_ostream.h
+++ b/llvm/include/llvm/Support/raw_ostream.h
@@ -13,6 +13,7 @@
 #ifndef LLVM_SUPPORT_RAW_OSTREAM_H
 #define LLVM_SUPPORT_RAW_OSTREAM_H
 
+#include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/DataTypes.h"
@@ -740,6 +741,28 @@ class Error;
 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


        


More information about the llvm-commits mailing list