2019-04-12 18:09:14 +08:00
|
|
|
//===--- PrintASTTests.cpp ----------------------------------------- C++-*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AST.h"
|
|
|
|
#include "Annotations.h"
|
|
|
|
#include "Protocol.h"
|
|
|
|
#include "SourceCode.h"
|
|
|
|
#include "TestTU.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
2019-05-06 18:08:47 +08:00
|
|
|
using ::testing::ElementsAreArray;
|
2019-04-12 18:09:14 +08:00
|
|
|
|
|
|
|
struct Case {
|
|
|
|
const char *AnnotatedCode;
|
|
|
|
std::vector<const char *> Expected;
|
|
|
|
};
|
2019-05-06 18:08:47 +08:00
|
|
|
class ASTUtils : public ::testing::Test,
|
2019-04-12 18:09:14 +08:00
|
|
|
public ::testing::WithParamInterface<Case> {};
|
|
|
|
|
|
|
|
TEST_P(ASTUtils, PrintTemplateArgs) {
|
|
|
|
auto Pair = GetParam();
|
|
|
|
Annotations Test(Pair.AnnotatedCode);
|
|
|
|
auto AST = TestTU::withCode(Test.code()).build();
|
|
|
|
struct Visitor : RecursiveASTVisitor<Visitor> {
|
|
|
|
Visitor(std::vector<Position> Points) : Points(std::move(Points)) {}
|
|
|
|
bool VisitNamedDecl(const NamedDecl *ND) {
|
2019-04-13 00:40:54 +08:00
|
|
|
if (TemplateArgsAtPoints.size() == Points.size())
|
|
|
|
return true;
|
2019-04-12 18:09:14 +08:00
|
|
|
auto Pos = sourceLocToPosition(ND->getASTContext().getSourceManager(),
|
|
|
|
ND->getLocation());
|
|
|
|
if (Pos != Points[TemplateArgsAtPoints.size()])
|
|
|
|
return true;
|
|
|
|
TemplateArgsAtPoints.push_back(printTemplateSpecializationArgs(*ND));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
std::vector<std::string> TemplateArgsAtPoints;
|
|
|
|
const std::vector<Position> Points;
|
|
|
|
};
|
|
|
|
Visitor V(Test.points());
|
|
|
|
V.TraverseDecl(AST.getASTContext().getTranslationUnitDecl());
|
|
|
|
EXPECT_THAT(V.TemplateArgsAtPoints, ElementsAreArray(Pair.Expected));
|
|
|
|
}
|
|
|
|
|
2021-05-15 01:37:46 +08:00
|
|
|
INSTANTIATE_TEST_SUITE_P(ASTUtilsTests, ASTUtils,
|
|
|
|
::testing::ValuesIn(std::vector<Case>({
|
|
|
|
{
|
|
|
|
R"cpp(
|
2019-04-12 18:09:14 +08:00
|
|
|
template <class X> class Bar {};
|
|
|
|
template <> class ^Bar<double> {};)cpp",
|
2021-05-15 01:37:46 +08:00
|
|
|
{"<double>"}},
|
|
|
|
{
|
|
|
|
R"cpp(
|
2019-04-12 18:09:14 +08:00
|
|
|
template <class X> class Bar {};
|
|
|
|
template <class T, class U,
|
|
|
|
template<typename> class Z, int Q>
|
|
|
|
struct Foo {};
|
|
|
|
template struct ^Foo<int, bool, Bar, 8>;
|
|
|
|
template <typename T>
|
|
|
|
struct ^Foo<T *, T, Bar, 3> {};)cpp",
|
2021-05-15 01:37:46 +08:00
|
|
|
{"<int, bool, Bar, 8>", "<T *, T, Bar, 3>"}},
|
|
|
|
{
|
|
|
|
R"cpp(
|
2019-04-12 18:09:14 +08:00
|
|
|
template <int ...> void Foz() {};
|
|
|
|
template <> void ^Foz<3, 5, 8>() {};)cpp",
|
2021-05-15 01:37:46 +08:00
|
|
|
{"<3, 5, 8>"}},
|
|
|
|
{
|
|
|
|
R"cpp(
|
2019-04-12 18:09:14 +08:00
|
|
|
template <class X> class Bar {};
|
|
|
|
template <template <class> class ...>
|
|
|
|
class Aux {};
|
|
|
|
template <> class ^Aux<Bar, Bar> {};
|
[clangd] Errors in TestTU cause test failures unless suppressed with error-ok.
Summary:
The historic behavior of TestTU is to gather diagnostics and otherwise ignore
them. So if a test has a syntax error, and doesn't assert diagnostics, it
silently misbehaves.
This can be annoying when developing tests, as evidenced by various tests
gaining "assert no diagnostics" where that's not really the point of the test.
This patch aims to make that default behavior. For the first error
(not warning), TestTU will call ADD_FAILURE().
This can be suppressed with a comment containing "error-ok". For now that will
suppress any errors in the TU. We can make this stricter later -verify style.
(-verify itself is hard to reuse because of DiagnosticConsumer interfaces...)
A magic-comment was chosen over a TestTU option because of table-driven tests.
In addition to the behavior change, this patch:
- adds //error-ok where we're knowingly testing invalid code
(e.g. for diagnostics, crash-resilience, or token-level tests)
- fixes a bunch of errors in the checked-in tests, mostly trivial (missing ;)
- removes a bunch of now-redundant instances of "assert no diagnostics"
Reviewers: kadircet
Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D73199
2020-01-22 23:38:41 +08:00
|
|
|
template <template <class> class T>
|
2019-04-12 18:09:14 +08:00
|
|
|
class ^Aux<T, T> {};)cpp",
|
2021-05-15 01:37:46 +08:00
|
|
|
{"<Bar, Bar>", "<T, T>"}},
|
|
|
|
{
|
|
|
|
R"cpp(
|
2019-04-12 18:09:14 +08:00
|
|
|
template <typename T> T var = 1234;
|
|
|
|
template <> int ^var<int> = 1;)cpp",
|
2021-05-15 01:37:46 +08:00
|
|
|
{"<int>"}},
|
|
|
|
{
|
|
|
|
R"cpp(
|
2019-04-12 18:09:14 +08:00
|
|
|
template <typename T> struct Foo;
|
|
|
|
struct Bar { friend class Foo<int>; };
|
|
|
|
template <> struct ^Foo<int> {};)cpp",
|
2021-05-15 01:37:46 +08:00
|
|
|
{"<int>"}},
|
|
|
|
{
|
|
|
|
R"cpp(
|
2019-08-09 15:35:16 +08:00
|
|
|
template<class T>
|
|
|
|
T S = T(10);
|
|
|
|
template <class T>
|
|
|
|
int ^S<T*> = 0;
|
|
|
|
template <>
|
|
|
|
int ^S<double> = 0;)cpp",
|
2021-05-15 01:37:46 +08:00
|
|
|
{"<T *>", "<double>"}},
|
|
|
|
})));
|
2019-04-12 18:09:14 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|