[clang] 14f0776 - Reland "Give NullabilityKind a printing operator<<"
Sam McCall via cfe-commits
cfe-commits at lists.llvm.org
Mon May 8 04:08:12 PDT 2023
Author: Sam McCall
Date: 2023-05-08T13:07:11+02:00
New Revision: 14f0776550b5a49e1c42f49a00213f7f3fa047bf
URL: https://github.com/llvm/llvm-project/commit/14f0776550b5a49e1c42f49a00213f7f3fa047bf
DIFF: https://github.com/llvm/llvm-project/commit/14f0776550b5a49e1c42f49a00213f7f3fa047bf.diff
LOG: Reland "Give NullabilityKind a printing operator<<"
This reverts commit 5326c9e480d70e16c2504cb5143524aff3ee2605.
The problem that caused the revert was downstream
(missing dep in user of clang).
Added:
Modified:
clang/include/clang/Basic/Specifiers.h
clang/lib/Basic/Diagnostic.cpp
clang/lib/Basic/IdentifierTable.cpp
clang/test/SemaObjC/nullable-result.m
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/Specifiers.h b/clang/include/clang/Basic/Specifiers.h
index a8c35fed9997e..06279a016a507 100644
--- a/clang/include/clang/Basic/Specifiers.h
+++ b/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 @@ 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.
diff --git a/clang/lib/Basic/Diagnostic.cpp b/clang/lib/Basic/Diagnostic.cpp
index 3474acbced195..b2106b0eb6611 100644
--- a/clang/lib/Basic/Diagnostic.cpp
+++ b/clang/lib/Basic/Diagnostic.cpp
@@ -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;
}
diff --git a/clang/lib/Basic/IdentifierTable.cpp b/clang/lib/Basic/IdentifierTable.cpp
index 97dc5cd2d9a6d..9be42c0f95ddc 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -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) {
diff --git a/clang/test/SemaObjC/nullable-result.m b/clang/test/SemaObjC/nullable-result.m
index 6125141e8c261..17cc1dd63810a 100644
--- a/clang/test/SemaObjC/nullable-result.m
+++ b/clang/test/SemaObjC/nullable-result.m
@@ -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
More information about the cfe-commits
mailing list