Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
bdwgc
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm-project
clang
clang-tools-extra
change-namespace
clang-apply-replacements
clang-move
clang-query
clang-reorder-fields
clang-tidy
clang-tidy-vs
clangd
docs
include-fixer
modularize
pp-trace
test
tool-template
unittests
change-namespace
clang-apply-replacements
clang-move
clang-query
clang-tidy
clangd
Annotations.cpp
Annotations.h
CMakeLists.txt
ClangdTests.cpp
CodeCompleteTests.cpp
CodeCompletionStringsTests.cpp
ContextTests.cpp
FileIndexTests.cpp
FuzzyMatchTests.cpp
IndexTests.cpp
JSONExprTests.cpp
Matchers.h
SourceCodeTests.cpp
SymbolCollectorTests.cpp
TestFS.cpp
TestFS.h
TraceTests.cpp
XRefsTests.cpp
include
include-fixer
CMakeLists.txt
.arcconfig
.gitignore
CMakeLists.txt
CODE_OWNERS.TXT
LICENSE.TXT
README.txt
compiler-rt
eng
libcxx
libcxxabi
libunwind
lld
lldb
llvm
nuget
openmp
polly
Directory.Build.props
Directory.Build.targets
NuGet.config
azure-pipelines.yml
build.cmd
build.sh
dir.common.props
global.json
llvm.proj
mxe-Win64.cmake.in
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
how-to-bump-roslyn-binaries.md
ikvm-native
llvm
m4
man
mcs
mono
msvc
netcore
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h

88 lines
2.7 KiB
C++
Raw Normal View History

//===--- Annotations.cpp - Annotated source code for unit tests -*- C++-*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===---------------------------------------------------------------------===//
#include "Annotations.h"
#include "SourceCode.h"
namespace clang {
namespace clangd {
using namespace llvm;
// Crash if the assertion fails, printing the message and testcase.
// More elegant error handling isn't needed for unit tests.
static void require(bool Assertion, const char *Msg, llvm::StringRef Code) {
if (!Assertion) {
llvm::errs() << "Annotated testcase: " << Msg << "\n" << Code << "\n";
llvm_unreachable("Annotated testcase assertion failed!");
}
}
Annotations::Annotations(StringRef Text) {
auto Here = [this] { return offsetToPosition(Code, Code.size()); };
auto Require = [Text](bool Assertion, const char *Msg) {
require(Assertion, Msg, Text);
};
Optional<StringRef> Name;
SmallVector<std::pair<StringRef, Position>, 8> OpenRanges;
Code.reserve(Text.size());
while (!Text.empty()) {
if (Text.consume_front("^")) {
Points[Name.getValueOr("")].push_back(Here());
Name = None;
continue;
}
if (Text.consume_front("[[")) {
OpenRanges.emplace_back(Name.getValueOr(""), Here());
Name = None;
continue;
}
Require(!Name, "$name should be followed by ^ or [[");
if (Text.consume_front("]]")) {
Require(!OpenRanges.empty(), "unmatched ]]");
Ranges[OpenRanges.back().first].push_back(
{OpenRanges.back().second, Here()});
OpenRanges.pop_back();
continue;
}
if (Text.consume_front("$")) {
Name = Text.take_while(llvm::isAlnum);
Text = Text.drop_front(Name->size());
continue;
}
Code.push_back(Text.front());
Text = Text.drop_front();
}
Require(!Name, "unterminated $name");
Require(OpenRanges.empty(), "unmatched [[");
}
Position Annotations::point(llvm::StringRef Name) const {
auto I = Points.find(Name);
require(I != Points.end() && I->getValue().size() == 1,
"expected exactly one point", Code);
return I->getValue()[0];
}
std::vector<Position> Annotations::points(llvm::StringRef Name) const {
auto P = Points.lookup(Name);
return {P.begin(), P.end()};
}
Range Annotations::range(llvm::StringRef Name) const {
auto I = Ranges.find(Name);
require(I != Ranges.end() && I->getValue().size() == 1,
"expected exactly one range", Code);
return I->getValue()[0];
}
std::vector<Range> Annotations::ranges(llvm::StringRef Name) const {
auto R = Ranges.lookup(Name);
return {R.begin(), R.end()};
}
} // namespace clangd
} // namespace clang