summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc-Andre Laperle <marc-andre.laperle@ericsson.com>2018-04-10 17:34:46 +0000
committerMarc-Andre Laperle <marc-andre.laperle@ericsson.com>2018-04-10 17:34:46 +0000
commit90a937e3e11efc202865c86e008d9dca08eb5535 (patch)
tree5852b90ea7e3d9956f8a56b5a5f50c17fda7b607
parent3dca0bedbb769565787ca6bb120ea6cfb39aec19 (diff)
downloadbcm5719-llvm-90a937e3e11efc202865c86e008d9dca08eb5535.tar.gz
bcm5719-llvm-90a937e3e11efc202865c86e008d9dca08eb5535.zip
[clangd] Use operator<< to prevent printers issues in Gtest
Summary: It is possible that there will be two different instantiations of the printer template for a given type and some tests could end up calling the wrong (default) one. For example, it was seen in CodeCompleteTests.cpp when printing CompletionItems that it would use the wrong printer because the default is also instantiated in ClangdTests.cpp. With this change, objects that were previously printed with a custom Printer now get printed through the operator<< which is declared alongside the class. This rule of the thumb should make it less error-prone. Reviewers: simark, ilya-biryukov, sammccall Reviewed By: simark, ilya-biryukov, sammccall Subscribers: bkramer, hokein, sammccall, klimek, ilya-biryukov, jkorous-apple, ioeric, MaskRay, cfe-commits Differential Revision: https://reviews.llvm.org/D44764 llvm-svn: 329725
-rw-r--r--clang-tools-extra/clangd/Protocol.cpp21
-rw-r--r--clang-tools-extra/clangd/Protocol.h4
-rw-r--r--clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp27
-rw-r--r--clang-tools-extra/unittests/clangd/JSONExprTests.cpp4
-rw-r--r--clang-tools-extra/unittests/clangd/XRefsTests.cpp9
5 files changed, 26 insertions, 39 deletions
diff --git a/clang-tools-extra/clangd/Protocol.cpp b/clang-tools-extra/clangd/Protocol.cpp
index 566104ba258..ec81a22a8fc 100644
--- a/clang-tools-extra/clangd/Protocol.cpp
+++ b/clang-tools-extra/clangd/Protocol.cpp
@@ -446,6 +446,11 @@ json::Expr toJSON(const CompletionItem &CI) {
return std::move(Result);
}
+llvm::raw_ostream &operator<<(llvm::raw_ostream &O, const CompletionItem &I) {
+ O << I.label << " - " << toJSON(I);
+ return O;
+}
+
bool operator<(const CompletionItem &L, const CompletionItem &R) {
return (L.sortText.empty() ? L.label : L.sortText) <
(R.sortText.empty() ? R.label : R.sortText);
@@ -477,6 +482,12 @@ json::Expr toJSON(const SignatureInformation &SI) {
return std::move(Result);
}
+llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
+ const SignatureInformation &I) {
+ O << I.label << " - " << toJSON(I);
+ return O;
+}
+
json::Expr toJSON(const SignatureHelp &SH) {
assert(SH.activeSignature >= 0 &&
"Unexpected negative value for number of active signatures.");
@@ -502,6 +513,16 @@ json::Expr toJSON(const DocumentHighlight &DH) {
};
}
+llvm::raw_ostream &operator<<(llvm::raw_ostream &O,
+ const DocumentHighlight &V) {
+ O << V.range;
+ if (V.kind == DocumentHighlightKind::Read)
+ O << "(r)";
+ if (V.kind == DocumentHighlightKind::Write)
+ O << "(w)";
+ return O;
+}
+
bool fromJSON(const json::Expr &Params, DidChangeConfigurationParams &CCP) {
json::ObjectMapper O(Params);
return O && O.map("settings", CCP.settings);
diff --git a/clang-tools-extra/clangd/Protocol.h b/clang-tools-extra/clangd/Protocol.h
index 77cf8d24aad..fa57a0c0912 100644
--- a/clang-tools-extra/clangd/Protocol.h
+++ b/clang-tools-extra/clangd/Protocol.h
@@ -670,6 +670,7 @@ struct CompletionItem {
// between a completion and a completion resolve request.
};
json::Expr toJSON(const CompletionItem &);
+llvm::raw_ostream &operator<<(llvm::raw_ostream &, const CompletionItem &);
bool operator<(const CompletionItem &, const CompletionItem &);
@@ -708,6 +709,8 @@ struct SignatureInformation {
std::vector<ParameterInformation> parameters;
};
json::Expr toJSON(const SignatureInformation &);
+llvm::raw_ostream &operator<<(llvm::raw_ostream &,
+ const SignatureInformation &);
/// Represents the signature of a callable.
struct SignatureHelp {
@@ -761,6 +764,7 @@ struct DocumentHighlight {
}
};
json::Expr toJSON(const DocumentHighlight &DH);
+llvm::raw_ostream &operator<<(llvm::raw_ostream &, const DocumentHighlight &);
} // namespace clangd
} // namespace clang
diff --git a/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp b/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
index 48771a38b5c..b2747014c7d 100644
--- a/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
+++ b/clang-tools-extra/unittests/clangd/CodeCompleteTests.cpp
@@ -22,33 +22,6 @@
namespace clang {
namespace clangd {
-// Let GMock print completion items and signature help.
-void PrintTo(const CompletionItem &I, std::ostream *O) {
- llvm::raw_os_ostream OS(*O);
- OS << I.label << " - " << toJSON(I);
-}
-void PrintTo(const std::vector<CompletionItem> &V, std::ostream *O) {
- *O << "{\n";
- for (const auto &I : V) {
- *O << "\t";
- PrintTo(I, O);
- *O << "\n";
- }
- *O << "}";
-}
-void PrintTo(const SignatureInformation &I, std::ostream *O) {
- llvm::raw_os_ostream OS(*O);
- OS << I.label << " - " << toJSON(I);
-}
-void PrintTo(const std::vector<SignatureInformation> &V, std::ostream *O) {
- *O << "{\n";
- for (const auto &I : V) {
- *O << "\t";
- PrintTo(I, O);
- *O << "\n";
- }
- *O << "}";
-}
namespace {
using namespace llvm;
diff --git a/clang-tools-extra/unittests/clangd/JSONExprTests.cpp b/clang-tools-extra/unittests/clangd/JSONExprTests.cpp
index 2c5cab29ca4..1e24e0988e2 100644
--- a/clang-tools-extra/unittests/clangd/JSONExprTests.cpp
+++ b/clang-tools-extra/unittests/clangd/JSONExprTests.cpp
@@ -15,9 +15,7 @@
namespace clang {
namespace clangd {
namespace json {
-void PrintTo(const Expr &E, std::ostream *OS) {
- llvm::raw_os_ostream(*OS) << llvm::formatv("{0:2}", E);
-}
+
namespace {
std::string s(const Expr &E) { return llvm::formatv("{0}", E).str(); }
diff --git a/clang-tools-extra/unittests/clangd/XRefsTests.cpp b/clang-tools-extra/unittests/clangd/XRefsTests.cpp
index f4b518f5c61..7d245748241 100644
--- a/clang-tools-extra/unittests/clangd/XRefsTests.cpp
+++ b/clang-tools-extra/unittests/clangd/XRefsTests.cpp
@@ -24,15 +24,6 @@ namespace clang {
namespace clangd {
using namespace llvm;
-void PrintTo(const DocumentHighlight &V, std::ostream *O) {
- llvm::raw_os_ostream OS(*O);
- OS << V.range;
- if (V.kind == DocumentHighlightKind::Read)
- OS << "(r)";
- if (V.kind == DocumentHighlightKind::Write)
- OS << "(w)";
-}
-
namespace {
using testing::ElementsAre;
using testing::Field;
OpenPOWER on IntegriCloud