clang-tools  14.0.0git
ConfigTesting.h
Go to the documentation of this file.
1 //===-- ConfigTesting.h - Helpers for configuration tests -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_CONFIGTESTING_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_CONFIGTESTING_H
11 
12 #include "Protocol.h"
13 #include "llvm/Support/ScopedPrinter.h"
14 #include "llvm/Support/SourceMgr.h"
15 #include "gmock/gmock.h"
16 #include <functional>
17 
18 namespace clang {
19 namespace clangd {
20 namespace config {
21 
22 // Provides a DiagnosticsCallback that records diganostics.
23 // Unlike just pushing them into a vector, underlying storage need not survive.
24 struct CapturedDiags {
25  std::function<void(const llvm::SMDiagnostic &)> callback() {
26  return [this](const llvm::SMDiagnostic &D) {
27  if (Files.empty() || Files.back() != D.getFilename())
28  Files.push_back(D.getFilename().str());
29 
30  if (D.getKind() > llvm::SourceMgr::DK_Warning)
31  return;
32 
33  Diagnostics.emplace_back();
34  Diag &Out = Diagnostics.back();
35  Out.Message = D.getMessage().str();
36  Out.Kind = D.getKind();
37  Out.Pos.line = D.getLineNo() - 1;
38  Out.Pos.character = D.getColumnNo(); // Zero-based - bug in SourceMgr?
39  if (!D.getRanges().empty()) {
40  const auto &R = D.getRanges().front();
41  Out.Rng.emplace();
42  Out.Rng->start.line = Out.Rng->end.line = Out.Pos.line;
43  Out.Rng->start.character = R.first;
44  Out.Rng->end.character = R.second;
45  }
46  };
47  }
48  struct Diag {
49  std::string Message;
50  llvm::SourceMgr::DiagKind Kind;
52  llvm::Optional<Range> Rng;
53 
54  friend void PrintTo(const Diag &D, std::ostream *OS) {
55  *OS << (D.Kind == llvm::SourceMgr::DK_Error ? "error: " : "warning: ")
56  << D.Message << "@" << llvm::to_string(D.Pos);
57  }
58  };
59  std::vector<Diag> Diagnostics; // Warning or higher.
60  std::vector<std::string> Files; // Filename from diagnostics including notes.
61 
62  void clear() {
63  Diagnostics.clear();
64  Files.clear();
65  }
66 };
67 
68 MATCHER_P(DiagMessage, M, "") { return arg.Message == M; }
69 MATCHER_P(DiagKind, K, "") { return arg.Kind == K; }
70 MATCHER_P(DiagPos, P, "") { return arg.Pos == P; }
71 MATCHER_P(DiagRange, R, "") { return arg.Rng == R; }
72 
73 inline Position toPosition(llvm::SMLoc L, const llvm::SourceMgr &SM) {
74  auto LineCol = SM.getLineAndColumn(L);
75  Position P;
76  P.line = LineCol.first - 1;
77  P.character = LineCol.second - 1;
78  return P;
79 }
80 
81 inline Range toRange(llvm::SMRange R, const llvm::SourceMgr &SM) {
82  return Range{toPosition(R.Start, SM), toPosition(R.End, SM)};
83 }
84 
85 } // namespace config
86 } // namespace clangd
87 } // namespace clang
88 
89 #endif
clang::clangd::config::CapturedDiags::Diag::Rng
llvm::Optional< Range > Rng
Definition: ConfigTesting.h:52
clang::clangd::config::CapturedDiags::Diag::Message
std::string Message
Definition: ConfigTesting.h:49
clang::clangd::config::CapturedDiags::callback
std::function< void(const llvm::SMDiagnostic &)> callback()
Definition: ConfigTesting.h:25
clang::clangd::config::CapturedDiags::Diag
Definition: ConfigTesting.h:48
SourceMgr
llvm::SourceMgr * SourceMgr
Definition: ConfigCompile.cpp:102
clang::clangd::Position::line
int line
Line position in a document (zero-based).
Definition: Protocol.h:150
Protocol.h
M
const google::protobuf::Message & M
Definition: Server.cpp:309
clang::clangd::Position
Definition: Protocol.h:148
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
clang::clangd::config::CapturedDiags::clear
void clear()
Definition: ConfigTesting.h:62
clang::clangd::config::MATCHER_P
MATCHER_P(DiagMessage, M, "")
Definition: ConfigTesting.h:68
clang::clangd::config::toPosition
Position toPosition(llvm::SMLoc L, const llvm::SourceMgr &SM)
Definition: ConfigTesting.h:73
clang::clangd::Position::character
int character
Character offset on a line in a document (zero-based).
Definition: Protocol.h:155
clang::clangd::config::CapturedDiags::Diag::Pos
Position Pos
Definition: ConfigTesting.h:51
clang::clangd::config::CapturedDiags
Definition: ConfigTesting.h:24
clang::clangd::config::CapturedDiags::Files
std::vector< std::string > Files
Definition: ConfigTesting.h:60
clang::clangd::Range
Definition: Protocol.h:177
clang::clangd::config::CapturedDiags::Diag::PrintTo
friend void PrintTo(const Diag &D, std::ostream *OS)
Definition: ConfigTesting.h:54
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
SM
const SourceManager & SM
Definition: IncludeCleaner.cpp:108
clang::clangd::config::CapturedDiags::Diag::Kind
llvm::SourceMgr::DiagKind Kind
Definition: ConfigTesting.h:50
clang::clangd::config::CapturedDiags::Diagnostics
std::vector< Diag > Diagnostics
Definition: ConfigTesting.h:59
clang::clangd::config::toRange
Range toRange(llvm::SMRange R, const llvm::SourceMgr &SM)
Definition: ConfigTesting.h:81
Out
CompiledFragmentImpl & Out
Definition: ConfigCompile.cpp:100
K
Kind K
Definition: Rename.cpp:442