Skip to content

Commit

Permalink
Reland "Give NullabilityKind a printing operator<<"
Browse files Browse the repository at this point in the history
This reverts commit 5326c9e.

The problem that caused the revert was downstream
(missing dep in user of clang).
  • Loading branch information
sam-mccall committed May 8, 2023
1 parent 77bc5cc commit 14f0776
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/Specifiers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -333,6 +336,8 @@ namespace clang {
// 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.
Expand Down
28 changes: 6 additions & 22 deletions clang/lib/Basic/Diagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,12 @@ using namespace clang;

const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
DiagNullabilityKind nullability) {
StringRef string;
switch (nullability.first) {
case NullabilityKind::NonNull:
string = nullability.second ? "'nonnull'" : "'_Nonnull'";
break;

case NullabilityKind::Nullable:
string = nullability.second ? "'nullable'" : "'_Nullable'";
break;

case NullabilityKind::Unspecified:
string = nullability.second ? "'null_unspecified'" : "'_Null_unspecified'";
break;

case NullabilityKind::NullableResult:
assert(!nullability.second &&
"_Nullable_result isn't supported as context-sensitive keyword");
string = "_Nullable_result";
break;
}

DB.AddString(string);
DB.AddString(
("'" +
getNullabilitySpelling(nullability.first,
/*isContextSensitive=*/nullability.second) +
"'")
.str());
return DB;
}

Expand Down
14 changes: 14 additions & 0 deletions clang/lib/Basic/IdentifierTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,20 @@ StringRef clang::getNullabilitySpelling(NullabilityKind kind,
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) {
Expand Down
6 changes: 3 additions & 3 deletions clang/test/SemaObjC/nullable-result.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ void test_conversion(void) {
}

void test_dup(void) {
id _Nullable_result _Nullable_result a; // expected-warning {{duplicate nullability specifier _Nullable_result}}
id _Nullable _Nullable_result b; // expected-error{{nullability specifier _Nullable_result conflicts with existing specifier '_Nullable'}}
id _Nullable_result _Nonnull c; // expected-error{{nullability specifier '_Nonnull' conflicts with existing specifier _Nullable_result}}
id _Nullable_result _Nullable_result a; // expected-warning {{duplicate nullability specifier '_Nullable_result'}}
id _Nullable _Nullable_result b; // expected-error{{nullability specifier '_Nullable_result' conflicts with existing specifier '_Nullable'}}
id _Nullable_result _Nonnull c; // expected-error{{nullability specifier '_Nonnull' conflicts with existing specifier '_Nullable_result'}}
}

@interface NoContextSensitive
Expand Down

0 comments on commit 14f0776

Please sign in to comment.