[PATCH] D149650: Give NullabilityKind a printing operator<<

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 2 07:22:56 PDT 2023


sammccall created this revision.
sammccall added a reviewer: mboehme.
Herald added a project: All.
sammccall requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This is more useful for debug/test than getNullabilitySpelling:

- default form has uglifying underscores
- non-default form crashes on NullableResult
- both return unhelpfully verbose strings for Unspecified
- operator<< works with gtest, formatv, etc


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149650

Files:
  clang/include/clang/Basic/Specifiers.h
  clang/lib/Basic/IdentifierTable.cpp


Index: clang/lib/Basic/IdentifierTable.cpp
===================================================================
--- clang/lib/Basic/IdentifierTable.cpp
+++ clang/lib/Basic/IdentifierTable.cpp
@@ -849,6 +849,20 @@
   llvm_unreachable("Unknown nullability kind.");
 }
 
+llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, NullabilityKind NK) {
+  switch (NK) {
+  case NullabilityKind::NonNull:
+    return OS << "NonNull";
+  case NullabilityKind::Nullable:
+    return OS << "Nullable";
+  case NullabilityKind::NullableResult:
+    return OS << "NullableResult";
+  case NullabilityKind::Unspecified:
+    return OS << "Unspecified";
+  }
+  llvm_unreachable("Unknown nullability kind.");
+}
+
 diag::kind
 IdentifierTable::getFutureCompatDiagKind(const IdentifierInfo &II,
                                          const LangOptions &LangOpts) {
Index: clang/include/clang/Basic/Specifiers.h
===================================================================
--- clang/include/clang/Basic/Specifiers.h
+++ clang/include/clang/Basic/Specifiers.h
@@ -19,6 +19,9 @@
 #include "llvm/Support/DataTypes.h"
 #include "llvm/Support/ErrorHandling.h"
 
+namespace llvm {
+class raw_ostream;
+} // namespace llvm
 namespace clang {
 
   /// Define the meaning of possible values of the kind in ExplicitSpecifier.
@@ -333,6 +336,8 @@
     // parameters are assumed to only get null on error.
     NullableResult,
   };
+  /// Prints human-readable debug representation.
+  llvm::raw_ostream &operator<<(llvm::raw_ostream&, NullabilityKind);
 
   /// Return true if \p L has a weaker nullability annotation than \p R. The
   /// ordering is: Unspecified < Nullable < NonNull.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149650.518715.patch
Type: text/x-patch
Size: 1667 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230502/ef7f0db9/attachment-0001.bin>


More information about the cfe-commits mailing list