2019-05-19 13:03:48 +00:00
|
|
|
//===- unittests/AST/ASTTraverserTest.h------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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 "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/ASTNodeTraverser.h"
|
|
|
|
#include "clang/AST/TextNodeDumper.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace clang::tooling;
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
class NodeTreePrinter : public TextTreeStructure {
|
|
|
|
llvm::raw_ostream &OS;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NodeTreePrinter(llvm::raw_ostream &OS)
|
|
|
|
: TextTreeStructure(OS, /* showColors */ false), OS(OS) {}
|
|
|
|
|
2019-12-18 21:50:50 +00:00
|
|
|
void Visit(const Decl *D) {
|
|
|
|
OS << D->getDeclKindName() << "Decl";
|
|
|
|
if (auto *ND = dyn_cast<NamedDecl>(D)) {
|
|
|
|
OS << " '" << ND->getDeclName() << "'";
|
|
|
|
}
|
|
|
|
}
|
2019-05-19 13:03:48 +00:00
|
|
|
|
2019-12-18 21:50:50 +00:00
|
|
|
void Visit(const Stmt *S) {
|
2020-11-04 22:57:42 +00:00
|
|
|
if (!S) {
|
|
|
|
OS << "<<<NULL>>>";
|
|
|
|
return;
|
|
|
|
}
|
2019-12-18 21:50:50 +00:00
|
|
|
OS << S->getStmtClassName();
|
|
|
|
if (auto *E = dyn_cast<DeclRefExpr>(S)) {
|
|
|
|
OS << " '" << E->getDecl()->getDeclName() << "'";
|
|
|
|
}
|
|
|
|
}
|
2019-05-19 13:03:48 +00:00
|
|
|
|
|
|
|
void Visit(QualType QT) {
|
|
|
|
OS << "QualType " << QT.split().Quals.getAsString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Visit(const Type *T) { OS << T->getTypeClassName() << "Type"; }
|
|
|
|
|
|
|
|
void Visit(const comments::Comment *C, const comments::FullComment *FC) {
|
|
|
|
OS << C->getCommentKindName();
|
|
|
|
}
|
|
|
|
|
2020-11-04 22:57:42 +00:00
|
|
|
void Visit(const CXXCtorInitializer *Init) {
|
|
|
|
OS << "CXXCtorInitializer";
|
|
|
|
if (const auto *F = Init->getAnyMember()) {
|
|
|
|
OS << " '" << F->getNameAsString() << "'";
|
|
|
|
} else if (auto const *TSI = Init->getTypeSourceInfo()) {
|
2022-04-20 22:09:03 +01:00
|
|
|
OS << " '" << TSI->getType() << "'";
|
2020-11-04 22:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-19 13:03:48 +00:00
|
|
|
|
|
|
|
void Visit(const Attr *A) {
|
|
|
|
switch (A->getKind()) {
|
|
|
|
#define ATTR(X) \
|
|
|
|
case attr::X: \
|
|
|
|
OS << #X; \
|
|
|
|
break;
|
|
|
|
#include "clang/Basic/AttrList.inc"
|
|
|
|
}
|
|
|
|
OS << "Attr";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Visit(const OMPClause *C) { OS << "OMPClause"; }
|
|
|
|
void Visit(const TemplateArgument &A, SourceRange R = {},
|
|
|
|
const Decl *From = nullptr, const char *Label = nullptr) {
|
|
|
|
OS << "TemplateArgument";
|
2020-11-03 13:59:01 -08:00
|
|
|
switch (A.getKind()) {
|
|
|
|
case TemplateArgument::Type: {
|
2022-04-20 22:09:03 +01:00
|
|
|
OS << " type " << A.getAsType();
|
2020-11-03 13:59:01 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-05-19 13:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... T> void Visit(T...) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestASTDumper : public ASTNodeTraverser<TestASTDumper, NodeTreePrinter> {
|
|
|
|
|
|
|
|
NodeTreePrinter MyNodeRecorder;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TestASTDumper(llvm::raw_ostream &OS) : MyNodeRecorder(OS) {}
|
|
|
|
NodeTreePrinter &doGetNodeDelegate() { return MyNodeRecorder; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename... NodeType> std::string dumpASTString(NodeType &&... N) {
|
|
|
|
std::string Buffer;
|
|
|
|
llvm::raw_string_ostream OS(Buffer);
|
|
|
|
|
|
|
|
TestASTDumper Dumper(OS);
|
|
|
|
|
|
|
|
OS << "\n";
|
|
|
|
|
|
|
|
Dumper.Visit(std::forward<NodeType &&>(N)...);
|
|
|
|
|
2021-12-09 14:55:44 -08:00
|
|
|
return Buffer;
|
2019-05-19 13:03:48 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 16:51:58 +01:00
|
|
|
template <typename... NodeType>
|
2020-02-12 11:34:13 -08:00
|
|
|
std::string dumpASTString(TraversalKind TK, NodeType &&... N) {
|
2019-05-04 16:51:58 +01:00
|
|
|
std::string Buffer;
|
|
|
|
llvm::raw_string_ostream OS(Buffer);
|
|
|
|
|
|
|
|
TestASTDumper Dumper(OS);
|
|
|
|
Dumper.SetTraversalKind(TK);
|
|
|
|
|
|
|
|
OS << "\n";
|
|
|
|
|
|
|
|
Dumper.Visit(std::forward<NodeType &&>(N)...);
|
|
|
|
|
2021-12-09 14:55:44 -08:00
|
|
|
return Buffer;
|
2019-05-04 16:51:58 +01:00
|
|
|
}
|
|
|
|
|
2019-05-19 13:03:48 +00:00
|
|
|
const FunctionDecl *getFunctionNode(clang::ASTUnit *AST,
|
|
|
|
const std::string &Name) {
|
|
|
|
auto Result = ast_matchers::match(functionDecl(hasName(Name)).bind("fn"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(Result.size(), 1u);
|
|
|
|
return Result[0].getNodeAs<FunctionDecl>("fn");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T> struct Verifier {
|
|
|
|
static void withDynNode(T Node, const std::string &DumpString) {
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(DynTypedNode::create(Node)), DumpString);
|
2019-05-19 13:03:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> struct Verifier<T *> {
|
|
|
|
static void withDynNode(T *Node, const std::string &DumpString) {
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(DynTypedNode::create(*Node)), DumpString);
|
2019-05-19 13:03:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void verifyWithDynNode(T Node, const std::string &DumpString) {
|
|
|
|
EXPECT_EQ(dumpASTString(Node), DumpString);
|
|
|
|
|
|
|
|
Verifier<T>::withDynNode(Node, DumpString);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Traverse, Dump) {
|
|
|
|
|
|
|
|
auto AST = buildASTFromCode(R"cpp(
|
|
|
|
struct A {
|
|
|
|
int m_number;
|
|
|
|
|
|
|
|
/// CTor
|
|
|
|
A() : m_number(42) {}
|
|
|
|
|
|
|
|
[[nodiscard]] const int func() {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct templ
|
2019-12-18 22:12:10 +00:00
|
|
|
{
|
2019-05-19 13:03:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct templ<int>
|
2019-12-18 22:12:10 +00:00
|
|
|
{
|
2019-05-19 13:03:48 +00:00
|
|
|
};
|
|
|
|
|
2019-08-07 11:12:43 +00:00
|
|
|
void parmvardecl_attr(struct A __attribute__((address_space(19)))*);
|
|
|
|
|
2019-05-19 13:03:48 +00:00
|
|
|
)cpp");
|
|
|
|
|
|
|
|
const FunctionDecl *Func = getFunctionNode(AST.get(), "func");
|
|
|
|
|
|
|
|
verifyWithDynNode(Func,
|
|
|
|
R"cpp(
|
2019-12-18 21:50:50 +00:00
|
|
|
CXXMethodDecl 'func'
|
2019-05-19 13:03:48 +00:00
|
|
|
|-CompoundStmt
|
|
|
|
| `-ReturnStmt
|
|
|
|
| `-IntegerLiteral
|
|
|
|
`-WarnUnusedResultAttr
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
Stmt *Body = Func->getBody();
|
|
|
|
|
|
|
|
verifyWithDynNode(Body,
|
|
|
|
R"cpp(
|
|
|
|
CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
QualType QT = Func->getType();
|
|
|
|
|
|
|
|
verifyWithDynNode(QT,
|
|
|
|
R"cpp(
|
|
|
|
FunctionProtoType
|
|
|
|
`-QualType const
|
|
|
|
`-BuiltinType
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
const FunctionDecl *CTorFunc = getFunctionNode(AST.get(), "A");
|
|
|
|
|
|
|
|
verifyWithDynNode(CTorFunc->getType(),
|
|
|
|
R"cpp(
|
|
|
|
FunctionProtoType
|
|
|
|
`-BuiltinType
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
Attr *A = *Func->attr_begin();
|
|
|
|
|
|
|
|
{
|
|
|
|
std::string expectedString = R"cpp(
|
|
|
|
WarnUnusedResultAttr
|
|
|
|
)cpp";
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(A), expectedString);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *CTor = dyn_cast<CXXConstructorDecl>(CTorFunc);
|
|
|
|
const CXXCtorInitializer *Init = *CTor->init_begin();
|
|
|
|
|
|
|
|
verifyWithDynNode(Init,
|
|
|
|
R"cpp(
|
2020-11-04 22:57:42 +00:00
|
|
|
CXXCtorInitializer 'm_number'
|
2019-05-19 13:03:48 +00:00
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
const comments::FullComment *Comment =
|
|
|
|
AST->getASTContext().getLocalCommentForDeclUncached(CTorFunc);
|
|
|
|
{
|
|
|
|
std::string expectedString = R"cpp(
|
|
|
|
FullComment
|
|
|
|
`-ParagraphComment
|
|
|
|
`-TextComment
|
|
|
|
)cpp";
|
|
|
|
EXPECT_EQ(dumpASTString(Comment, Comment), expectedString);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Result = ast_matchers::match(
|
|
|
|
classTemplateSpecializationDecl(hasName("templ")).bind("fn"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(Result.size(), 1u);
|
|
|
|
auto Templ = Result[0].getNodeAs<ClassTemplateSpecializationDecl>("fn");
|
|
|
|
|
|
|
|
TemplateArgument TA = Templ->getTemplateArgs()[0];
|
|
|
|
|
|
|
|
verifyWithDynNode(TA,
|
|
|
|
R"cpp(
|
2020-11-03 13:59:01 -08:00
|
|
|
TemplateArgument type int
|
2020-06-23 00:01:38 -07:00
|
|
|
`-BuiltinType
|
2019-05-19 13:03:48 +00:00
|
|
|
)cpp");
|
2019-08-07 11:12:43 +00:00
|
|
|
|
|
|
|
Func = getFunctionNode(AST.get(), "parmvardecl_attr");
|
|
|
|
|
|
|
|
const auto *Parm = Func->getParamDecl(0);
|
|
|
|
const auto TL = Parm->getTypeSourceInfo()->getTypeLoc();
|
|
|
|
ASSERT_TRUE(TL.getType()->isPointerType());
|
|
|
|
|
|
|
|
const auto ATL = TL.getNextTypeLoc().getAs<AttributedTypeLoc>();
|
|
|
|
const auto *AS = cast<AddressSpaceAttr>(ATL.getAttr());
|
|
|
|
EXPECT_EQ(toTargetAddressSpace(static_cast<LangAS>(AS->getAddressSpace())),
|
|
|
|
19u);
|
2019-05-19 13:03:48 +00:00
|
|
|
}
|
2019-05-04 16:51:58 +01:00
|
|
|
|
2020-05-22 00:23:59 +01:00
|
|
|
TEST(Traverse, IgnoreUnlessSpelledInSourceVars) {
|
|
|
|
|
[test] Make tests pass regardless of gnu++14/gnu++17 default
GCC from 11 onwards defaults to -std=gnu++17 for C++ source files. We want to do the same
(https://discourse.llvm.org/t/c-objc-switch-to-gnu-17-as-the-default-dialect/64360).
Split RUN lines, adjust `-verify`, or add `__cplusplus < 201703L` or `-Wno-dynamic-exception-spec`,
so that tests will pass regardless of gnu++14/gnu++17 default.
We have a desire to mark a test compatible with multiple language standards.
There are ongoing discussions how to add markers in the long term:
* https://discourse.llvm.org/t/iterating-lit-run-lines/62596
* https://discourse.llvm.org/t/lit-run-a-run-line-multiple-times-with-different-replacements/64932
As a workaround in the short term, add lit substitutions `%std_cxx98-`,
`%std_cxx11-14`, etc. They can be used for tests which work across multiple
language standards. If a range has `n` standards, run lit multiple times, with
`LIT_CLANG_STD_GROUP=0`, `LIT_CLANG_STD_GROUP=1`, etc to cover all `n` standards.
Reviewed By: #clang-language-wg, aaron.ballman
Differential Revision: https://reviews.llvm.org/D131464
2022-09-04 05:29:32 +00:00
|
|
|
auto AST = buildASTFromCodeWithArgs(R"cpp(
|
2020-05-22 00:23:59 +01:00
|
|
|
|
|
|
|
struct String
|
|
|
|
{
|
|
|
|
String(const char*, int = -1) {}
|
2020-05-24 12:27:03 +01:00
|
|
|
|
|
|
|
int overloaded() const;
|
|
|
|
int& overloaded();
|
2020-05-22 00:23:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
void stringConstruct()
|
|
|
|
{
|
|
|
|
String s = "foo";
|
|
|
|
s = "bar";
|
|
|
|
}
|
|
|
|
|
2020-05-24 12:27:03 +01:00
|
|
|
void overloadCall()
|
|
|
|
{
|
|
|
|
String s = "foo";
|
|
|
|
(s).overloaded();
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:57:50 +01:00
|
|
|
struct C1 {};
|
|
|
|
struct C2 { operator C1(); };
|
|
|
|
|
|
|
|
void conversionOperator()
|
|
|
|
{
|
|
|
|
C2* c2;
|
|
|
|
C1 c1 = (*c2);
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:49:00 +01:00
|
|
|
template <unsigned alignment>
|
|
|
|
void template_test() {
|
|
|
|
static_assert(alignment, "");
|
|
|
|
}
|
|
|
|
void actual_template_test() {
|
|
|
|
template_test<4>();
|
|
|
|
}
|
2020-06-21 13:31:27 +01:00
|
|
|
|
|
|
|
struct OneParamCtor {
|
|
|
|
explicit OneParamCtor(int);
|
|
|
|
};
|
|
|
|
struct TwoParamCtor {
|
|
|
|
explicit TwoParamCtor(int, int);
|
|
|
|
};
|
|
|
|
|
|
|
|
void varDeclCtors() {
|
|
|
|
{
|
|
|
|
auto var1 = OneParamCtor(5);
|
|
|
|
auto var2 = TwoParamCtor(6, 7);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
OneParamCtor var3(5);
|
|
|
|
TwoParamCtor var4(6, 7);
|
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
{
|
|
|
|
auto var5 = OneParamCtor(i);
|
|
|
|
auto var6 = TwoParamCtor(i, 7);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
OneParamCtor var7(i);
|
|
|
|
TwoParamCtor var8(i, 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[test] Make tests pass regardless of gnu++14/gnu++17 default
GCC from 11 onwards defaults to -std=gnu++17 for C++ source files. We want to do the same
(https://discourse.llvm.org/t/c-objc-switch-to-gnu-17-as-the-default-dialect/64360).
Split RUN lines, adjust `-verify`, or add `__cplusplus < 201703L` or `-Wno-dynamic-exception-spec`,
so that tests will pass regardless of gnu++14/gnu++17 default.
We have a desire to mark a test compatible with multiple language standards.
There are ongoing discussions how to add markers in the long term:
* https://discourse.llvm.org/t/iterating-lit-run-lines/62596
* https://discourse.llvm.org/t/lit-run-a-run-line-multiple-times-with-different-replacements/64932
As a workaround in the short term, add lit substitutions `%std_cxx98-`,
`%std_cxx11-14`, etc. They can be used for tests which work across multiple
language standards. If a range has `n` standards, run lit multiple times, with
`LIT_CLANG_STD_GROUP=0`, `LIT_CLANG_STD_GROUP=1`, etc to cover all `n` standards.
Reviewed By: #clang-language-wg, aaron.ballman
Differential Revision: https://reviews.llvm.org/D131464
2022-09-04 05:29:32 +00:00
|
|
|
)cpp", {"-std=c++14"});
|
2020-05-22 00:23:59 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
auto FN =
|
|
|
|
ast_matchers::match(functionDecl(hasName("stringConstruct")).bind("fn"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(FN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("fn")),
|
|
|
|
R"cpp(
|
|
|
|
FunctionDecl 'stringConstruct'
|
|
|
|
`-CompoundStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 's'
|
|
|
|
| `-ExprWithCleanups
|
|
|
|
| `-CXXConstructExpr
|
|
|
|
| `-MaterializeTemporaryExpr
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-CXXConstructExpr
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-StringLiteral
|
|
|
|
| `-CXXDefaultArgExpr
|
|
|
|
`-ExprWithCleanups
|
|
|
|
`-CXXOperatorCallExpr
|
|
|
|
|-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr 'operator='
|
|
|
|
|-DeclRefExpr 's'
|
|
|
|
`-MaterializeTemporaryExpr
|
|
|
|
`-CXXConstructExpr
|
|
|
|
|-ImplicitCastExpr
|
|
|
|
| `-StringLiteral
|
|
|
|
`-CXXDefaultArgExpr
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
FN[0].getNodeAs<Decl>("fn")),
|
|
|
|
R"cpp(
|
|
|
|
FunctionDecl 'stringConstruct'
|
|
|
|
`-CompoundStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 's'
|
|
|
|
| `-StringLiteral
|
|
|
|
`-CXXOperatorCallExpr
|
|
|
|
|-DeclRefExpr 'operator='
|
|
|
|
|-DeclRefExpr 's'
|
|
|
|
`-StringLiteral
|
2020-05-22 00:57:50 +01:00
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
|
2020-05-24 12:27:03 +01:00
|
|
|
{
|
|
|
|
auto FN =
|
|
|
|
ast_matchers::match(functionDecl(hasName("overloadCall")).bind("fn"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(FN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("fn")),
|
|
|
|
R"cpp(
|
|
|
|
FunctionDecl 'overloadCall'
|
|
|
|
`-CompoundStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 's'
|
|
|
|
| `-ExprWithCleanups
|
|
|
|
| `-CXXConstructExpr
|
|
|
|
| `-MaterializeTemporaryExpr
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-CXXConstructExpr
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-StringLiteral
|
|
|
|
| `-CXXDefaultArgExpr
|
|
|
|
`-CXXMemberCallExpr
|
|
|
|
`-MemberExpr
|
|
|
|
`-ParenExpr
|
|
|
|
`-DeclRefExpr 's'
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
FN[0].getNodeAs<Decl>("fn")),
|
|
|
|
R"cpp(
|
|
|
|
FunctionDecl 'overloadCall'
|
|
|
|
`-CompoundStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 's'
|
|
|
|
| `-StringLiteral
|
|
|
|
`-CXXMemberCallExpr
|
|
|
|
`-MemberExpr
|
|
|
|
`-DeclRefExpr 's'
|
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
|
2020-05-22 00:57:50 +01:00
|
|
|
{
|
|
|
|
auto FN = ast_matchers::match(
|
|
|
|
functionDecl(hasName("conversionOperator"),
|
|
|
|
hasDescendant(varDecl(hasName("c1")).bind("var"))),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(FN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("var")),
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'c1'
|
|
|
|
`-ExprWithCleanups
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-MaterializeTemporaryExpr
|
|
|
|
`-ImplicitCastExpr
|
|
|
|
`-CXXMemberCallExpr
|
|
|
|
`-MemberExpr
|
|
|
|
`-ParenExpr
|
|
|
|
`-UnaryOperator
|
|
|
|
`-ImplicitCastExpr
|
|
|
|
`-DeclRefExpr 'c2'
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
FN[0].getNodeAs<Decl>("var")),
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'c1'
|
|
|
|
`-UnaryOperator
|
|
|
|
`-DeclRefExpr 'c2'
|
2020-05-24 22:49:00 +01:00
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto FN = ast_matchers::match(
|
|
|
|
functionDecl(hasName("template_test"),
|
|
|
|
hasDescendant(staticAssertDecl().bind("staticAssert"))),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(FN.size(), 2u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, FN[1].getNodeAs<Decl>("staticAssert")),
|
|
|
|
R"cpp(
|
|
|
|
StaticAssertDecl
|
|
|
|
|-ImplicitCastExpr
|
|
|
|
| `-SubstNonTypeTemplateParmExpr
|
2020-06-17 14:49:03 -07:00
|
|
|
| |-NonTypeTemplateParmDecl 'alignment'
|
2020-05-24 22:49:00 +01:00
|
|
|
| `-IntegerLiteral
|
|
|
|
`-StringLiteral
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
FN[1].getNodeAs<Decl>("staticAssert")),
|
|
|
|
R"cpp(
|
|
|
|
StaticAssertDecl
|
|
|
|
|-IntegerLiteral
|
|
|
|
`-StringLiteral
|
2020-05-22 00:23:59 +01:00
|
|
|
)cpp");
|
|
|
|
}
|
2020-06-21 13:31:27 +01:00
|
|
|
|
|
|
|
auto varChecker = [&AST](StringRef varName, StringRef SemanticDump,
|
|
|
|
StringRef SyntacticDump) {
|
|
|
|
auto FN = ast_matchers::match(
|
|
|
|
functionDecl(
|
|
|
|
hasName("varDeclCtors"),
|
|
|
|
forEachDescendant(varDecl(hasName(varName)).bind("varDeclCtor"))),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(FN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, FN[0].getNodeAs<Decl>("varDeclCtor")),
|
|
|
|
SemanticDump);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
FN[0].getNodeAs<Decl>("varDeclCtor")),
|
|
|
|
SyntacticDump);
|
|
|
|
};
|
|
|
|
|
|
|
|
varChecker("var1",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var1'
|
|
|
|
`-ExprWithCleanups
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-MaterializeTemporaryExpr
|
|
|
|
`-CXXFunctionalCastExpr
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var1'
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
varChecker("var2",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var2'
|
|
|
|
`-ExprWithCleanups
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-MaterializeTemporaryExpr
|
|
|
|
`-CXXTemporaryObjectExpr
|
|
|
|
|-IntegerLiteral
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var2'
|
|
|
|
`-CXXTemporaryObjectExpr
|
|
|
|
|-IntegerLiteral
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
varChecker("var3",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var3'
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var3'
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
varChecker("var4",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var4'
|
|
|
|
`-CXXConstructExpr
|
|
|
|
|-IntegerLiteral
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var4'
|
|
|
|
`-CXXConstructExpr
|
|
|
|
|-IntegerLiteral
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
varChecker("var5",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var5'
|
|
|
|
`-ExprWithCleanups
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-MaterializeTemporaryExpr
|
|
|
|
`-CXXFunctionalCastExpr
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-ImplicitCastExpr
|
|
|
|
`-DeclRefExpr 'i'
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var5'
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-DeclRefExpr 'i'
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
varChecker("var6",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var6'
|
|
|
|
`-ExprWithCleanups
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-MaterializeTemporaryExpr
|
|
|
|
`-CXXTemporaryObjectExpr
|
|
|
|
|-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr 'i'
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var6'
|
|
|
|
`-CXXTemporaryObjectExpr
|
|
|
|
|-DeclRefExpr 'i'
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
varChecker("var7",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var7'
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-ImplicitCastExpr
|
|
|
|
`-DeclRefExpr 'i'
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var7'
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-DeclRefExpr 'i'
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
varChecker("var8",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var8'
|
|
|
|
`-CXXConstructExpr
|
|
|
|
|-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr 'i'
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp",
|
|
|
|
R"cpp(
|
|
|
|
VarDecl 'var8'
|
|
|
|
`-CXXConstructExpr
|
|
|
|
|-DeclRefExpr 'i'
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
2020-05-22 00:23:59 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 12:09:45 +00:00
|
|
|
TEST(Traverse, IgnoreUnlessSpelledInSourceStructs) {
|
|
|
|
auto AST = buildASTFromCode(R"cpp(
|
|
|
|
|
|
|
|
struct MyStruct {
|
|
|
|
MyStruct();
|
|
|
|
MyStruct(int i) {
|
|
|
|
MyStruct();
|
|
|
|
}
|
|
|
|
~MyStruct();
|
|
|
|
};
|
|
|
|
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
auto BN = ast_matchers::match(
|
|
|
|
cxxConstructorDecl(hasName("MyStruct"),
|
|
|
|
hasParameter(0, parmVarDecl(hasType(isInteger()))))
|
|
|
|
.bind("ctor"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
2020-01-11 12:09:45 +00:00
|
|
|
BN[0].getNodeAs<Decl>("ctor")),
|
|
|
|
R"cpp(
|
|
|
|
CXXConstructorDecl 'MyStruct'
|
|
|
|
|-ParmVarDecl 'i'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-CXXTemporaryObjectExpr
|
|
|
|
)cpp");
|
|
|
|
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("ctor")),
|
|
|
|
R"cpp(
|
2020-01-11 12:09:45 +00:00
|
|
|
CXXConstructorDecl 'MyStruct'
|
|
|
|
|-ParmVarDecl 'i'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ExprWithCleanups
|
|
|
|
`-CXXBindTemporaryExpr
|
|
|
|
`-CXXTemporaryObjectExpr
|
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Traverse, IgnoreUnlessSpelledInSourceReturnStruct) {
|
|
|
|
|
|
|
|
auto AST = buildASTFromCode(R"cpp(
|
|
|
|
struct Retval {
|
|
|
|
Retval() {}
|
|
|
|
~Retval() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
Retval someFun();
|
|
|
|
|
|
|
|
void foo()
|
|
|
|
{
|
|
|
|
someFun();
|
|
|
|
}
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
auto BN = ast_matchers::match(functionDecl(hasName("foo")).bind("fn"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
2020-01-11 12:09:45 +00:00
|
|
|
BN[0].getNodeAs<Decl>("fn")),
|
|
|
|
R"cpp(
|
|
|
|
FunctionDecl 'foo'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-CallExpr
|
|
|
|
`-DeclRefExpr 'someFun'
|
|
|
|
)cpp");
|
|
|
|
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("fn")),
|
|
|
|
R"cpp(
|
2020-01-11 12:09:45 +00:00
|
|
|
FunctionDecl 'foo'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ExprWithCleanups
|
|
|
|
`-CXXBindTemporaryExpr
|
|
|
|
`-CallExpr
|
|
|
|
`-ImplicitCastExpr
|
|
|
|
`-DeclRefExpr 'someFun'
|
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Traverse, IgnoreUnlessSpelledInSourceReturns) {
|
2019-05-04 16:51:58 +01:00
|
|
|
|
[test] Make tests pass regardless of gnu++14/gnu++17 default
GCC from 11 onwards defaults to -std=gnu++17 for C++ source files. We want to do the same
(https://discourse.llvm.org/t/c-objc-switch-to-gnu-17-as-the-default-dialect/64360).
Split RUN lines, adjust `-verify`, or add `__cplusplus < 201703L` or `-Wno-dynamic-exception-spec`,
so that tests will pass regardless of gnu++14/gnu++17 default.
We have a desire to mark a test compatible with multiple language standards.
There are ongoing discussions how to add markers in the long term:
* https://discourse.llvm.org/t/iterating-lit-run-lines/62596
* https://discourse.llvm.org/t/lit-run-a-run-line-multiple-times-with-different-replacements/64932
As a workaround in the short term, add lit substitutions `%std_cxx98-`,
`%std_cxx11-14`, etc. They can be used for tests which work across multiple
language standards. If a range has `n` standards, run lit multiple times, with
`LIT_CLANG_STD_GROUP=0`, `LIT_CLANG_STD_GROUP=1`, etc to cover all `n` standards.
Reviewed By: #clang-language-wg, aaron.ballman
Differential Revision: https://reviews.llvm.org/D131464
2022-09-04 05:29:32 +00:00
|
|
|
auto AST = buildASTFromCodeWithArgs(R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
|
|
|
|
struct A
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
struct B
|
|
|
|
{
|
|
|
|
B(int);
|
|
|
|
B(A const& a);
|
|
|
|
B();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct C
|
|
|
|
{
|
|
|
|
operator B();
|
|
|
|
};
|
|
|
|
|
|
|
|
B func1() {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
|
|
|
|
B func2() {
|
|
|
|
return B{42};
|
|
|
|
}
|
|
|
|
|
|
|
|
B func3() {
|
|
|
|
return B(42);
|
|
|
|
}
|
|
|
|
|
|
|
|
B func4() {
|
|
|
|
return B();
|
|
|
|
}
|
|
|
|
|
|
|
|
B func5() {
|
|
|
|
return B{};
|
|
|
|
}
|
|
|
|
|
|
|
|
B func6() {
|
|
|
|
return C();
|
|
|
|
}
|
|
|
|
|
|
|
|
B func7() {
|
|
|
|
return A();
|
|
|
|
}
|
|
|
|
|
|
|
|
B func8() {
|
|
|
|
return C{};
|
|
|
|
}
|
|
|
|
|
|
|
|
B func9() {
|
|
|
|
return A{};
|
|
|
|
}
|
|
|
|
|
|
|
|
B func10() {
|
|
|
|
A a;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
B func11() {
|
|
|
|
B b;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
B func12() {
|
|
|
|
C c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
[test] Make tests pass regardless of gnu++14/gnu++17 default
GCC from 11 onwards defaults to -std=gnu++17 for C++ source files. We want to do the same
(https://discourse.llvm.org/t/c-objc-switch-to-gnu-17-as-the-default-dialect/64360).
Split RUN lines, adjust `-verify`, or add `__cplusplus < 201703L` or `-Wno-dynamic-exception-spec`,
so that tests will pass regardless of gnu++14/gnu++17 default.
We have a desire to mark a test compatible with multiple language standards.
There are ongoing discussions how to add markers in the long term:
* https://discourse.llvm.org/t/iterating-lit-run-lines/62596
* https://discourse.llvm.org/t/lit-run-a-run-line-multiple-times-with-different-replacements/64932
As a workaround in the short term, add lit substitutions `%std_cxx98-`,
`%std_cxx11-14`, etc. They can be used for tests which work across multiple
language standards. If a range has `n` standards, run lit multiple times, with
`LIT_CLANG_STD_GROUP=0`, `LIT_CLANG_STD_GROUP=1`, etc to cover all `n` standards.
Reviewed By: #clang-language-wg, aaron.ballman
Differential Revision: https://reviews.llvm.org/D131464
2022-09-04 05:29:32 +00:00
|
|
|
)cpp", {"-std=c++14"});
|
2019-05-04 16:51:58 +01:00
|
|
|
|
|
|
|
auto getFunctionNode = [&AST](const std::string &name) {
|
|
|
|
auto BN = ast_matchers::match(functionDecl(hasName(name)).bind("fn"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
return BN[0].getNodeAs<Decl>("fn");
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
auto FN = getFunctionNode("func1");
|
2020-01-13 12:38:39 +00:00
|
|
|
llvm::StringRef Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func1'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-ExprWithCleanups
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-MaterializeTemporaryExpr
|
|
|
|
`-ImplicitCastExpr
|
|
|
|
`-CXXConstructExpr
|
|
|
|
`-IntegerLiteral
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2019-05-04 16:51:58 +01:00
|
|
|
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, FN), Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func1'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-IntegerLiteral
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, FN), Expected);
|
2019-05-04 16:51:58 +01:00
|
|
|
}
|
|
|
|
|
2020-01-13 12:38:39 +00:00
|
|
|
llvm::StringRef Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func2'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-CXXTemporaryObjectExpr
|
|
|
|
`-IntegerLiteral
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func2")),
|
|
|
|
Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func3'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
2020-06-21 13:31:27 +01:00
|
|
|
`-CXXConstructExpr
|
2019-05-04 16:51:58 +01:00
|
|
|
`-IntegerLiteral
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func3")),
|
|
|
|
Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func4'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-CXXTemporaryObjectExpr
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func4")),
|
|
|
|
Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func5'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-CXXTemporaryObjectExpr
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func5")),
|
|
|
|
Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func6'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-CXXTemporaryObjectExpr
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func6")),
|
|
|
|
Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func7'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-CXXTemporaryObjectExpr
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func7")),
|
|
|
|
Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func8'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-CXXFunctionalCastExpr
|
|
|
|
`-InitListExpr
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func8")),
|
|
|
|
Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func9'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-CXXFunctionalCastExpr
|
|
|
|
`-InitListExpr
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func9")),
|
|
|
|
Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func10'
|
|
|
|
`-CompoundStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 'a'
|
|
|
|
| `-CXXConstructExpr
|
|
|
|
`-ReturnStmt
|
|
|
|
`-DeclRefExpr 'a'
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func10")),
|
|
|
|
Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func11'
|
|
|
|
`-CompoundStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 'b'
|
|
|
|
| `-CXXConstructExpr
|
|
|
|
`-ReturnStmt
|
|
|
|
`-DeclRefExpr 'b'
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func11")),
|
|
|
|
Expected);
|
2020-01-13 12:38:39 +00:00
|
|
|
|
|
|
|
Expected = R"cpp(
|
2019-05-04 16:51:58 +01:00
|
|
|
FunctionDecl 'func12'
|
|
|
|
`-CompoundStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 'c'
|
|
|
|
| `-CXXConstructExpr
|
|
|
|
`-ReturnStmt
|
|
|
|
`-DeclRefExpr 'c'
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_IgnoreUnlessSpelledInSource, getFunctionNode("func12")),
|
|
|
|
Expected);
|
2019-05-04 16:51:58 +01:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:35:46 +00:00
|
|
|
TEST(Traverse, LambdaUnlessSpelledInSource) {
|
|
|
|
|
|
|
|
auto AST =
|
|
|
|
buildASTFromCodeWithArgs(R"cpp(
|
|
|
|
|
|
|
|
void captures() {
|
|
|
|
int a = 0;
|
|
|
|
int b = 0;
|
|
|
|
int d = 0;
|
|
|
|
int f = 0;
|
|
|
|
|
|
|
|
[a, &b, c = d, &e = f](int g, int h = 42) {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void templated() {
|
|
|
|
int a = 0;
|
|
|
|
[a]<typename T>(T t) {};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SomeStruct {
|
|
|
|
int a = 0;
|
|
|
|
void capture_this() {
|
|
|
|
[this]() {};
|
|
|
|
}
|
|
|
|
void capture_this_copy() {
|
|
|
|
[self = *this]() {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
)cpp",
|
|
|
|
{"-Wno-unused-value", "-Wno-c++2a-extensions"});
|
|
|
|
|
|
|
|
auto getLambdaNode = [&AST](const std::string &name) {
|
|
|
|
auto BN = ast_matchers::match(
|
|
|
|
lambdaExpr(hasAncestor(functionDecl(hasName(name)))).bind("lambda"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
return BN[0].getNodeAs<LambdaExpr>("lambda");
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
auto L = getLambdaNode("captures");
|
|
|
|
|
2020-01-13 12:38:39 +00:00
|
|
|
llvm::StringRef Expected = R"cpp(
|
2019-12-18 22:35:46 +00:00
|
|
|
LambdaExpr
|
|
|
|
|-DeclRefExpr 'a'
|
|
|
|
|-DeclRefExpr 'b'
|
|
|
|
|-VarDecl 'c'
|
|
|
|
| `-DeclRefExpr 'd'
|
|
|
|
|-VarDecl 'e'
|
|
|
|
| `-DeclRefExpr 'f'
|
|
|
|
|-ParmVarDecl 'g'
|
|
|
|
|-ParmVarDecl 'h'
|
|
|
|
| `-IntegerLiteral
|
|
|
|
`-CompoundStmt
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected);
|
2019-12-18 22:35:46 +00:00
|
|
|
|
2020-01-13 12:38:39 +00:00
|
|
|
Expected = R"cpp(
|
2019-12-18 22:35:46 +00:00
|
|
|
LambdaExpr
|
|
|
|
|-CXXRecordDecl ''
|
|
|
|
| |-CXXMethodDecl 'operator()'
|
|
|
|
| | |-ParmVarDecl 'g'
|
|
|
|
| | |-ParmVarDecl 'h'
|
|
|
|
| | | `-IntegerLiteral
|
|
|
|
| | `-CompoundStmt
|
|
|
|
| |-FieldDecl ''
|
|
|
|
| |-FieldDecl ''
|
|
|
|
| |-FieldDecl ''
|
|
|
|
| |-FieldDecl ''
|
2022-10-14 08:17:16 -04:00
|
|
|
| `-CXXDestructorDecl '~(lambda at input.cc:9:3)'
|
2019-12-18 22:35:46 +00:00
|
|
|
|-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr 'a'
|
|
|
|
|-DeclRefExpr 'b'
|
|
|
|
|-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr 'd'
|
|
|
|
|-DeclRefExpr 'f'
|
|
|
|
`-CompoundStmt
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, L), Expected);
|
2019-12-18 22:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto L = getLambdaNode("templated");
|
|
|
|
|
2020-01-13 12:38:39 +00:00
|
|
|
llvm::StringRef Expected = R"cpp(
|
2019-12-18 22:35:46 +00:00
|
|
|
LambdaExpr
|
|
|
|
|-DeclRefExpr 'a'
|
|
|
|
|-TemplateTypeParmDecl 'T'
|
|
|
|
|-ParmVarDecl 't'
|
|
|
|
`-CompoundStmt
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected);
|
2019-12-18 22:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto L = getLambdaNode("capture_this");
|
|
|
|
|
2020-01-13 12:38:39 +00:00
|
|
|
llvm::StringRef Expected = R"cpp(
|
2019-12-18 22:35:46 +00:00
|
|
|
LambdaExpr
|
|
|
|
|-CXXThisExpr
|
|
|
|
`-CompoundStmt
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected);
|
2019-12-18 22:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto L = getLambdaNode("capture_this_copy");
|
|
|
|
|
2020-01-13 12:38:39 +00:00
|
|
|
llvm::StringRef Expected = R"cpp(
|
2019-12-18 22:35:46 +00:00
|
|
|
LambdaExpr
|
|
|
|
|-VarDecl 'self'
|
|
|
|
| `-UnaryOperator
|
|
|
|
| `-CXXThisExpr
|
|
|
|
`-CompoundStmt
|
2020-01-13 12:38:39 +00:00
|
|
|
)cpp";
|
2020-02-12 11:34:13 -08:00
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, L), Expected);
|
2019-12-18 22:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 22:57:42 +00:00
|
|
|
TEST(Traverse, IgnoreUnlessSpelledInSourceImplicit) {
|
|
|
|
{
|
|
|
|
auto AST = buildASTFromCode(R"cpp(
|
|
|
|
int i = 0;
|
|
|
|
)cpp");
|
|
|
|
const auto *TUDecl = AST->getASTContext().getTranslationUnitDecl();
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource, TUDecl),
|
|
|
|
R"cpp(
|
|
|
|
TranslationUnitDecl
|
|
|
|
`-VarDecl 'i'
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto AST2 = buildASTFromCodeWithArgs(R"cpp(
|
|
|
|
struct Simple {
|
|
|
|
};
|
|
|
|
struct Other {
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Record : Simple, Other {
|
|
|
|
Record() : Simple(), m_i(42) {}
|
|
|
|
private:
|
|
|
|
int m_i;
|
|
|
|
int m_i2 = 42;
|
|
|
|
Simple m_s;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct NonTrivial {
|
|
|
|
NonTrivial() {}
|
|
|
|
NonTrivial(NonTrivial&) {}
|
|
|
|
NonTrivial& operator=(NonTrivial&) { return *this; }
|
|
|
|
|
|
|
|
~NonTrivial() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ContainsArray {
|
|
|
|
NonTrivial arr[2];
|
|
|
|
int irr[2];
|
|
|
|
ContainsArray& operator=(ContainsArray &) = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
void copyIt()
|
|
|
|
{
|
|
|
|
ContainsArray ca;
|
|
|
|
ContainsArray ca2;
|
|
|
|
ca2 = ca;
|
|
|
|
}
|
|
|
|
|
|
|
|
void forLoop()
|
|
|
|
{
|
|
|
|
int arr[2];
|
|
|
|
for (auto i : arr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
for (auto& a = arr; auto i : a)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DefaultedAndDeleted {
|
|
|
|
NonTrivial nt;
|
|
|
|
DefaultedAndDeleted() = default;
|
|
|
|
~DefaultedAndDeleted() = default;
|
|
|
|
DefaultedAndDeleted(DefaultedAndDeleted &) = default;
|
|
|
|
DefaultedAndDeleted& operator=(DefaultedAndDeleted &) = default;
|
|
|
|
DefaultedAndDeleted(DefaultedAndDeleted &&) = delete;
|
|
|
|
DefaultedAndDeleted& operator=(DefaultedAndDeleted &&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
void copyIt2()
|
|
|
|
{
|
|
|
|
DefaultedAndDeleted ca;
|
|
|
|
DefaultedAndDeleted ca2;
|
|
|
|
ca2 = ca;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hasDefaultArg(int i, int j = 0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void callDefaultArg()
|
|
|
|
{
|
|
|
|
hasDefaultArg(42);
|
|
|
|
}
|
2021-01-30 16:19:43 +00:00
|
|
|
|
|
|
|
void decomposition()
|
|
|
|
{
|
|
|
|
int arr[3];
|
|
|
|
auto &[f, s, t] = arr;
|
|
|
|
|
|
|
|
f = 42;
|
|
|
|
}
|
|
|
|
|
2022-05-26 18:59:22 +02:00
|
|
|
typedef __typeof(sizeof(int)) size_t;
|
|
|
|
|
|
|
|
struct Pair
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note: these utilities are required to force binding to tuple like structure
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
template <typename E>
|
|
|
|
struct tuple_size
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct tuple_size<Pair>
|
|
|
|
{
|
|
|
|
static constexpr size_t value = 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t I, class T>
|
|
|
|
struct tuple_element
|
|
|
|
{
|
|
|
|
using type = int;
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <size_t I>
|
|
|
|
int &&get(Pair &&p);
|
|
|
|
|
|
|
|
void decompTuple()
|
|
|
|
{
|
|
|
|
Pair p{1, 2};
|
|
|
|
auto [a, b] = p;
|
|
|
|
|
|
|
|
a = 3;
|
|
|
|
}
|
|
|
|
|
2020-11-04 22:57:42 +00:00
|
|
|
)cpp",
|
|
|
|
{"-std=c++20"});
|
|
|
|
|
|
|
|
{
|
|
|
|
auto BN = ast_matchers::match(
|
|
|
|
cxxRecordDecl(hasName("Record"), unless(isImplicit())).bind("rec"),
|
|
|
|
AST2->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("rec")),
|
|
|
|
R"cpp(
|
|
|
|
CXXRecordDecl 'Record'
|
|
|
|
|-CXXRecordDecl 'Record'
|
|
|
|
|-CXXConstructorDecl 'Record'
|
[clang] Implement ElaboratedType sugaring for types written bare
Without this patch, clang will not wrap in an ElaboratedType node types written
without a keyword and nested name qualifier, which goes against the intent that
we should produce an AST which retains enough details to recover how things are
written.
The lack of this sugar is incompatible with the intent of the type printer
default policy, which is to print types as written, but to fall back and print
them fully qualified when they are desugared.
An ElaboratedTypeLoc without keyword / NNS uses no storage by itself, but still
requires pointer alignment due to pre-existing bug in the TypeLoc buffer
handling.
---
Troubleshooting list to deal with any breakage seen with this patch:
1) The most likely effect one would see by this patch is a change in how
a type is printed. The type printer will, by design and default,
print types as written. There are customization options there, but
not that many, and they mainly apply to how to print a type that we
somehow failed to track how it was written. This patch fixes a
problem where we failed to distinguish between a type
that was written without any elaborated-type qualifiers,
such as a 'struct'/'class' tags and name spacifiers such as 'std::',
and one that has been stripped of any 'metadata' that identifies such,
the so called canonical types.
Example:
```
namespace foo {
struct A {};
A a;
};
```
If one were to print the type of `foo::a`, prior to this patch, this
would result in `foo::A`. This is how the type printer would have,
by default, printed the canonical type of A as well.
As soon as you add any name qualifiers to A, the type printer would
suddenly start accurately printing the type as written. This patch
will make it print it accurately even when written without
qualifiers, so we will just print `A` for the initial example, as
the user did not really write that `foo::` namespace qualifier.
2) This patch could expose a bug in some AST matcher. Matching types
is harder to get right when there is sugar involved. For example,
if you want to match a type against being a pointer to some type A,
then you have to account for getting a type that is sugar for a
pointer to A, or being a pointer to sugar to A, or both! Usually
you would get the second part wrong, and this would work for a
very simple test where you don't use any name qualifiers, but
you would discover is broken when you do. The usual fix is to
either use the matcher which strips sugar, which is annoying
to use as for example if you match an N level pointer, you have
to put N+1 such matchers in there, beginning to end and between
all those levels. But in a lot of cases, if the property you want
to match is present in the canonical type, it's easier and faster
to just match on that... This goes with what is said in 1), if
you want to match against the name of a type, and you want
the name string to be something stable, perhaps matching on
the name of the canonical type is the better choice.
3) This patch could expose a bug in how you get the source range of some
TypeLoc. For some reason, a lot of code is using getLocalSourceRange(),
which only looks at the given TypeLoc node. This patch introduces a new,
and more common TypeLoc node which contains no source locations on itself.
This is not an inovation here, and some other, more rare TypeLoc nodes could
also have this property, but if you use getLocalSourceRange on them, it's not
going to return any valid locations, because it doesn't have any. The right fix
here is to always use getSourceRange() or getBeginLoc/getEndLoc which will dive
into the inner TypeLoc to get the source range if it doesn't find it on the
top level one. You can use getLocalSourceRange if you are really into
micro-optimizations and you have some outside knowledge that the TypeLocs you are
dealing with will always include some source location.
4) Exposed a bug somewhere in the use of the normal clang type class API, where you
have some type, you want to see if that type is some particular kind, you try a
`dyn_cast` such as `dyn_cast<TypedefType>` and that fails because now you have an
ElaboratedType which has a TypeDefType inside of it, which is what you wanted to match.
Again, like 2), this would usually have been tested poorly with some simple tests with
no qualifications, and would have been broken had there been any other kind of type sugar,
be it an ElaboratedType or a TemplateSpecializationType or a SubstTemplateParmType.
The usual fix here is to use `getAs` instead of `dyn_cast`, which will look deeper
into the type. Or use `getAsAdjusted` when dealing with TypeLocs.
For some reason the API is inconsistent there and on TypeLocs getAs behaves like a dyn_cast.
5) It could be a bug in this patch perhaps.
Let me know if you need any help!
Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>
Differential Revision: https://reviews.llvm.org/D112374
2021-10-11 18:15:36 +02:00
|
|
|
| |-CXXCtorInitializer 'Simple'
|
2020-11-04 22:57:42 +00:00
|
|
|
| | `-CXXConstructExpr
|
[clang] Implement ElaboratedType sugaring for types written bare
Without this patch, clang will not wrap in an ElaboratedType node types written
without a keyword and nested name qualifier, which goes against the intent that
we should produce an AST which retains enough details to recover how things are
written.
The lack of this sugar is incompatible with the intent of the type printer
default policy, which is to print types as written, but to fall back and print
them fully qualified when they are desugared.
An ElaboratedTypeLoc without keyword / NNS uses no storage by itself, but still
requires pointer alignment due to pre-existing bug in the TypeLoc buffer
handling.
---
Troubleshooting list to deal with any breakage seen with this patch:
1) The most likely effect one would see by this patch is a change in how
a type is printed. The type printer will, by design and default,
print types as written. There are customization options there, but
not that many, and they mainly apply to how to print a type that we
somehow failed to track how it was written. This patch fixes a
problem where we failed to distinguish between a type
that was written without any elaborated-type qualifiers,
such as a 'struct'/'class' tags and name spacifiers such as 'std::',
and one that has been stripped of any 'metadata' that identifies such,
the so called canonical types.
Example:
```
namespace foo {
struct A {};
A a;
};
```
If one were to print the type of `foo::a`, prior to this patch, this
would result in `foo::A`. This is how the type printer would have,
by default, printed the canonical type of A as well.
As soon as you add any name qualifiers to A, the type printer would
suddenly start accurately printing the type as written. This patch
will make it print it accurately even when written without
qualifiers, so we will just print `A` for the initial example, as
the user did not really write that `foo::` namespace qualifier.
2) This patch could expose a bug in some AST matcher. Matching types
is harder to get right when there is sugar involved. For example,
if you want to match a type against being a pointer to some type A,
then you have to account for getting a type that is sugar for a
pointer to A, or being a pointer to sugar to A, or both! Usually
you would get the second part wrong, and this would work for a
very simple test where you don't use any name qualifiers, but
you would discover is broken when you do. The usual fix is to
either use the matcher which strips sugar, which is annoying
to use as for example if you match an N level pointer, you have
to put N+1 such matchers in there, beginning to end and between
all those levels. But in a lot of cases, if the property you want
to match is present in the canonical type, it's easier and faster
to just match on that... This goes with what is said in 1), if
you want to match against the name of a type, and you want
the name string to be something stable, perhaps matching on
the name of the canonical type is the better choice.
3) This patch could expose a bug in how you get the source range of some
TypeLoc. For some reason, a lot of code is using getLocalSourceRange(),
which only looks at the given TypeLoc node. This patch introduces a new,
and more common TypeLoc node which contains no source locations on itself.
This is not an inovation here, and some other, more rare TypeLoc nodes could
also have this property, but if you use getLocalSourceRange on them, it's not
going to return any valid locations, because it doesn't have any. The right fix
here is to always use getSourceRange() or getBeginLoc/getEndLoc which will dive
into the inner TypeLoc to get the source range if it doesn't find it on the
top level one. You can use getLocalSourceRange if you are really into
micro-optimizations and you have some outside knowledge that the TypeLocs you are
dealing with will always include some source location.
4) Exposed a bug somewhere in the use of the normal clang type class API, where you
have some type, you want to see if that type is some particular kind, you try a
`dyn_cast` such as `dyn_cast<TypedefType>` and that fails because now you have an
ElaboratedType which has a TypeDefType inside of it, which is what you wanted to match.
Again, like 2), this would usually have been tested poorly with some simple tests with
no qualifications, and would have been broken had there been any other kind of type sugar,
be it an ElaboratedType or a TemplateSpecializationType or a SubstTemplateParmType.
The usual fix here is to use `getAs` instead of `dyn_cast`, which will look deeper
into the type. Or use `getAsAdjusted` when dealing with TypeLocs.
For some reason the API is inconsistent there and on TypeLocs getAs behaves like a dyn_cast.
5) It could be a bug in this patch perhaps.
Let me know if you need any help!
Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>
Differential Revision: https://reviews.llvm.org/D112374
2021-10-11 18:15:36 +02:00
|
|
|
| |-CXXCtorInitializer 'Other'
|
2020-11-04 22:57:42 +00:00
|
|
|
| | `-CXXConstructExpr
|
|
|
|
| |-CXXCtorInitializer 'm_i'
|
|
|
|
| | `-IntegerLiteral
|
|
|
|
| |-CXXCtorInitializer 'm_i2'
|
|
|
|
| | `-CXXDefaultInitExpr
|
|
|
|
| |-CXXCtorInitializer 'm_s'
|
|
|
|
| | `-CXXConstructExpr
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-AccessSpecDecl
|
|
|
|
|-FieldDecl 'm_i'
|
|
|
|
|-FieldDecl 'm_i2'
|
|
|
|
| `-IntegerLiteral
|
|
|
|
`-FieldDecl 'm_s'
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
BN[0].getNodeAs<Decl>("rec")),
|
|
|
|
R"cpp(
|
|
|
|
CXXRecordDecl 'Record'
|
|
|
|
|-CXXConstructorDecl 'Record'
|
[clang] Implement ElaboratedType sugaring for types written bare
Without this patch, clang will not wrap in an ElaboratedType node types written
without a keyword and nested name qualifier, which goes against the intent that
we should produce an AST which retains enough details to recover how things are
written.
The lack of this sugar is incompatible with the intent of the type printer
default policy, which is to print types as written, but to fall back and print
them fully qualified when they are desugared.
An ElaboratedTypeLoc without keyword / NNS uses no storage by itself, but still
requires pointer alignment due to pre-existing bug in the TypeLoc buffer
handling.
---
Troubleshooting list to deal with any breakage seen with this patch:
1) The most likely effect one would see by this patch is a change in how
a type is printed. The type printer will, by design and default,
print types as written. There are customization options there, but
not that many, and they mainly apply to how to print a type that we
somehow failed to track how it was written. This patch fixes a
problem where we failed to distinguish between a type
that was written without any elaborated-type qualifiers,
such as a 'struct'/'class' tags and name spacifiers such as 'std::',
and one that has been stripped of any 'metadata' that identifies such,
the so called canonical types.
Example:
```
namespace foo {
struct A {};
A a;
};
```
If one were to print the type of `foo::a`, prior to this patch, this
would result in `foo::A`. This is how the type printer would have,
by default, printed the canonical type of A as well.
As soon as you add any name qualifiers to A, the type printer would
suddenly start accurately printing the type as written. This patch
will make it print it accurately even when written without
qualifiers, so we will just print `A` for the initial example, as
the user did not really write that `foo::` namespace qualifier.
2) This patch could expose a bug in some AST matcher. Matching types
is harder to get right when there is sugar involved. For example,
if you want to match a type against being a pointer to some type A,
then you have to account for getting a type that is sugar for a
pointer to A, or being a pointer to sugar to A, or both! Usually
you would get the second part wrong, and this would work for a
very simple test where you don't use any name qualifiers, but
you would discover is broken when you do. The usual fix is to
either use the matcher which strips sugar, which is annoying
to use as for example if you match an N level pointer, you have
to put N+1 such matchers in there, beginning to end and between
all those levels. But in a lot of cases, if the property you want
to match is present in the canonical type, it's easier and faster
to just match on that... This goes with what is said in 1), if
you want to match against the name of a type, and you want
the name string to be something stable, perhaps matching on
the name of the canonical type is the better choice.
3) This patch could expose a bug in how you get the source range of some
TypeLoc. For some reason, a lot of code is using getLocalSourceRange(),
which only looks at the given TypeLoc node. This patch introduces a new,
and more common TypeLoc node which contains no source locations on itself.
This is not an inovation here, and some other, more rare TypeLoc nodes could
also have this property, but if you use getLocalSourceRange on them, it's not
going to return any valid locations, because it doesn't have any. The right fix
here is to always use getSourceRange() or getBeginLoc/getEndLoc which will dive
into the inner TypeLoc to get the source range if it doesn't find it on the
top level one. You can use getLocalSourceRange if you are really into
micro-optimizations and you have some outside knowledge that the TypeLocs you are
dealing with will always include some source location.
4) Exposed a bug somewhere in the use of the normal clang type class API, where you
have some type, you want to see if that type is some particular kind, you try a
`dyn_cast` such as `dyn_cast<TypedefType>` and that fails because now you have an
ElaboratedType which has a TypeDefType inside of it, which is what you wanted to match.
Again, like 2), this would usually have been tested poorly with some simple tests with
no qualifications, and would have been broken had there been any other kind of type sugar,
be it an ElaboratedType or a TemplateSpecializationType or a SubstTemplateParmType.
The usual fix here is to use `getAs` instead of `dyn_cast`, which will look deeper
into the type. Or use `getAsAdjusted` when dealing with TypeLocs.
For some reason the API is inconsistent there and on TypeLocs getAs behaves like a dyn_cast.
5) It could be a bug in this patch perhaps.
Let me know if you need any help!
Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>
Differential Revision: https://reviews.llvm.org/D112374
2021-10-11 18:15:36 +02:00
|
|
|
| |-CXXCtorInitializer 'Simple'
|
2020-11-04 22:57:42 +00:00
|
|
|
| | `-CXXConstructExpr
|
|
|
|
| |-CXXCtorInitializer 'm_i'
|
|
|
|
| | `-IntegerLiteral
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-AccessSpecDecl
|
|
|
|
|-FieldDecl 'm_i'
|
|
|
|
|-FieldDecl 'm_i2'
|
|
|
|
| `-IntegerLiteral
|
|
|
|
`-FieldDecl 'm_s'
|
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto BN = ast_matchers::match(
|
|
|
|
cxxRecordDecl(hasName("ContainsArray"), unless(isImplicit()))
|
|
|
|
.bind("rec"),
|
|
|
|
AST2->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("rec")),
|
|
|
|
R"cpp(
|
|
|
|
CXXRecordDecl 'ContainsArray'
|
|
|
|
|-CXXRecordDecl 'ContainsArray'
|
|
|
|
|-FieldDecl 'arr'
|
|
|
|
|-FieldDecl 'irr'
|
|
|
|
|-CXXMethodDecl 'operator='
|
|
|
|
| |-ParmVarDecl ''
|
|
|
|
| `-CompoundStmt
|
|
|
|
| |-ForStmt
|
|
|
|
| | |-DeclStmt
|
|
|
|
| | | `-VarDecl '__i0'
|
|
|
|
| | | `-IntegerLiteral
|
|
|
|
| | |-<<<NULL>>>
|
|
|
|
| | |-BinaryOperator
|
|
|
|
| | | |-ImplicitCastExpr
|
|
|
|
| | | | `-DeclRefExpr '__i0'
|
|
|
|
| | | `-IntegerLiteral
|
|
|
|
| | |-UnaryOperator
|
|
|
|
| | | `-DeclRefExpr '__i0'
|
|
|
|
| | `-CXXMemberCallExpr
|
|
|
|
| | |-MemberExpr
|
|
|
|
| | | `-ArraySubscriptExpr
|
|
|
|
| | | |-ImplicitCastExpr
|
|
|
|
| | | | `-MemberExpr
|
|
|
|
| | | | `-CXXThisExpr
|
|
|
|
| | | `-ImplicitCastExpr
|
|
|
|
| | | `-DeclRefExpr '__i0'
|
|
|
|
| | `-ArraySubscriptExpr
|
|
|
|
| | |-ImplicitCastExpr
|
|
|
|
| | | `-MemberExpr
|
|
|
|
| | | `-DeclRefExpr ''
|
|
|
|
| | `-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr '__i0'
|
|
|
|
| |-CallExpr
|
|
|
|
| | |-ImplicitCastExpr
|
|
|
|
| | | `-DeclRefExpr '__builtin_memcpy'
|
|
|
|
| | |-ImplicitCastExpr
|
|
|
|
| | | `-UnaryOperator
|
|
|
|
| | | `-MemberExpr
|
|
|
|
| | | `-CXXThisExpr
|
|
|
|
| | |-ImplicitCastExpr
|
|
|
|
| | | `-UnaryOperator
|
|
|
|
| | | `-MemberExpr
|
|
|
|
| | | `-DeclRefExpr ''
|
|
|
|
| | `-IntegerLiteral
|
|
|
|
| `-ReturnStmt
|
|
|
|
| `-UnaryOperator
|
|
|
|
| `-CXXThisExpr
|
|
|
|
|-CXXConstructorDecl 'ContainsArray'
|
|
|
|
| `-ParmVarDecl ''
|
|
|
|
|-CXXDestructorDecl '~ContainsArray'
|
|
|
|
| `-CompoundStmt
|
|
|
|
`-CXXConstructorDecl 'ContainsArray'
|
|
|
|
|-CXXCtorInitializer 'arr'
|
|
|
|
| `-CXXConstructExpr
|
|
|
|
`-CompoundStmt
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
BN[0].getNodeAs<Decl>("rec")),
|
|
|
|
R"cpp(
|
|
|
|
CXXRecordDecl 'ContainsArray'
|
|
|
|
|-FieldDecl 'arr'
|
|
|
|
|-FieldDecl 'irr'
|
|
|
|
`-CXXMethodDecl 'operator='
|
|
|
|
`-ParmVarDecl ''
|
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto BN = ast_matchers::match(functionDecl(hasName("forLoop")).bind("func"),
|
|
|
|
AST2->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("func")),
|
|
|
|
R"cpp(
|
|
|
|
FunctionDecl 'forLoop'
|
|
|
|
`-CompoundStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 'arr'
|
|
|
|
|-CXXForRangeStmt
|
|
|
|
| |-<<<NULL>>>
|
|
|
|
| |-DeclStmt
|
|
|
|
| | `-VarDecl '__range1'
|
|
|
|
| | `-DeclRefExpr 'arr'
|
|
|
|
| |-DeclStmt
|
|
|
|
| | `-VarDecl '__begin1'
|
|
|
|
| | `-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr '__range1'
|
|
|
|
| |-DeclStmt
|
|
|
|
| | `-VarDecl '__end1'
|
|
|
|
| | `-BinaryOperator
|
|
|
|
| | |-ImplicitCastExpr
|
|
|
|
| | | `-DeclRefExpr '__range1'
|
|
|
|
| | `-IntegerLiteral
|
|
|
|
| |-BinaryOperator
|
|
|
|
| | |-ImplicitCastExpr
|
|
|
|
| | | `-DeclRefExpr '__begin1'
|
|
|
|
| | `-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr '__end1'
|
|
|
|
| |-UnaryOperator
|
|
|
|
| | `-DeclRefExpr '__begin1'
|
|
|
|
| |-DeclStmt
|
|
|
|
| | `-VarDecl 'i'
|
|
|
|
| | `-ImplicitCastExpr
|
|
|
|
| | `-UnaryOperator
|
|
|
|
| | `-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr '__begin1'
|
|
|
|
| `-CompoundStmt
|
|
|
|
`-CXXForRangeStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 'a'
|
|
|
|
| `-DeclRefExpr 'arr'
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl '__range1'
|
|
|
|
| `-DeclRefExpr 'a'
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl '__begin1'
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr '__range1'
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl '__end1'
|
|
|
|
| `-BinaryOperator
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr '__range1'
|
|
|
|
| `-IntegerLiteral
|
|
|
|
|-BinaryOperator
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr '__begin1'
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr '__end1'
|
|
|
|
|-UnaryOperator
|
|
|
|
| `-DeclRefExpr '__begin1'
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 'i'
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-UnaryOperator
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr '__begin1'
|
|
|
|
`-CompoundStmt
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
BN[0].getNodeAs<Decl>("func")),
|
|
|
|
R"cpp(
|
|
|
|
FunctionDecl 'forLoop'
|
|
|
|
`-CompoundStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 'arr'
|
|
|
|
|-CXXForRangeStmt
|
|
|
|
| |-<<<NULL>>>
|
|
|
|
| |-VarDecl 'i'
|
|
|
|
| |-DeclRefExpr 'arr'
|
|
|
|
| `-CompoundStmt
|
|
|
|
`-CXXForRangeStmt
|
|
|
|
|-DeclStmt
|
|
|
|
| `-VarDecl 'a'
|
|
|
|
| `-DeclRefExpr 'arr'
|
|
|
|
|-VarDecl 'i'
|
|
|
|
|-DeclRefExpr 'a'
|
|
|
|
`-CompoundStmt
|
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto BN = ast_matchers::match(
|
|
|
|
cxxRecordDecl(hasName("DefaultedAndDeleted"), unless(isImplicit()))
|
|
|
|
.bind("rec"),
|
|
|
|
AST2->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("rec")),
|
|
|
|
R"cpp(
|
|
|
|
CXXRecordDecl 'DefaultedAndDeleted'
|
|
|
|
|-CXXRecordDecl 'DefaultedAndDeleted'
|
|
|
|
|-FieldDecl 'nt'
|
|
|
|
|-CXXConstructorDecl 'DefaultedAndDeleted'
|
|
|
|
| |-CXXCtorInitializer 'nt'
|
|
|
|
| | `-CXXConstructExpr
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-CXXDestructorDecl '~DefaultedAndDeleted'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-CXXConstructorDecl 'DefaultedAndDeleted'
|
|
|
|
| `-ParmVarDecl ''
|
|
|
|
|-CXXMethodDecl 'operator='
|
|
|
|
| |-ParmVarDecl ''
|
|
|
|
| `-CompoundStmt
|
|
|
|
| |-CXXMemberCallExpr
|
|
|
|
| | |-MemberExpr
|
|
|
|
| | | `-MemberExpr
|
|
|
|
| | | `-CXXThisExpr
|
|
|
|
| | `-MemberExpr
|
|
|
|
| | `-DeclRefExpr ''
|
|
|
|
| `-ReturnStmt
|
|
|
|
| `-UnaryOperator
|
|
|
|
| `-CXXThisExpr
|
|
|
|
|-CXXConstructorDecl 'DefaultedAndDeleted'
|
|
|
|
| `-ParmVarDecl ''
|
|
|
|
`-CXXMethodDecl 'operator='
|
|
|
|
`-ParmVarDecl ''
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
BN[0].getNodeAs<Decl>("rec")),
|
|
|
|
R"cpp(
|
|
|
|
CXXRecordDecl 'DefaultedAndDeleted'
|
|
|
|
|-FieldDecl 'nt'
|
|
|
|
|-CXXConstructorDecl 'DefaultedAndDeleted'
|
|
|
|
|-CXXDestructorDecl '~DefaultedAndDeleted'
|
|
|
|
|-CXXConstructorDecl 'DefaultedAndDeleted'
|
|
|
|
| `-ParmVarDecl ''
|
|
|
|
|-CXXMethodDecl 'operator='
|
|
|
|
| `-ParmVarDecl ''
|
|
|
|
|-CXXConstructorDecl 'DefaultedAndDeleted'
|
|
|
|
| `-ParmVarDecl ''
|
|
|
|
`-CXXMethodDecl 'operator='
|
|
|
|
`-ParmVarDecl ''
|
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto BN = ast_matchers::match(
|
|
|
|
callExpr(callee(functionDecl(hasName("hasDefaultArg"))))
|
|
|
|
.bind("funcCall"),
|
|
|
|
AST2->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<CallExpr>("funcCall")),
|
|
|
|
R"cpp(
|
|
|
|
CallExpr
|
|
|
|
|-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr 'hasDefaultArg'
|
|
|
|
|-IntegerLiteral
|
|
|
|
`-CXXDefaultArgExpr
|
|
|
|
)cpp");
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
BN[0].getNodeAs<CallExpr>("funcCall")),
|
|
|
|
R"cpp(
|
|
|
|
CallExpr
|
|
|
|
|-DeclRefExpr 'hasDefaultArg'
|
|
|
|
`-IntegerLiteral
|
2021-01-30 16:19:43 +00:00
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto FN = ast_matchers::match(
|
|
|
|
functionDecl(hasName("decomposition"),
|
|
|
|
hasDescendant(decompositionDecl().bind("decomp"))),
|
|
|
|
AST2->getASTContext());
|
|
|
|
EXPECT_EQ(FN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_AsIs, FN[0].getNodeAs<DecompositionDecl>("decomp")),
|
|
|
|
R"cpp(
|
|
|
|
DecompositionDecl ''
|
|
|
|
|-DeclRefExpr 'arr'
|
|
|
|
|-BindingDecl 'f'
|
|
|
|
| `-ArraySubscriptExpr
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr ''
|
|
|
|
| `-IntegerLiteral
|
|
|
|
|-BindingDecl 's'
|
|
|
|
| `-ArraySubscriptExpr
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr ''
|
|
|
|
| `-IntegerLiteral
|
|
|
|
`-BindingDecl 't'
|
|
|
|
`-ArraySubscriptExpr
|
|
|
|
|-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr ''
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
FN[0].getNodeAs<DecompositionDecl>("decomp")),
|
|
|
|
R"cpp(
|
|
|
|
DecompositionDecl ''
|
|
|
|
|-DeclRefExpr 'arr'
|
|
|
|
|-BindingDecl 'f'
|
|
|
|
|-BindingDecl 's'
|
|
|
|
`-BindingDecl 't'
|
2022-05-26 18:59:22 +02:00
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto FN = ast_matchers::match(
|
|
|
|
functionDecl(hasName("decompTuple"),
|
|
|
|
hasDescendant(decompositionDecl().bind("decomp"))),
|
|
|
|
AST2->getASTContext());
|
|
|
|
EXPECT_EQ(FN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
dumpASTString(TK_AsIs, FN[0].getNodeAs<DecompositionDecl>("decomp")),
|
|
|
|
R"cpp(
|
|
|
|
DecompositionDecl ''
|
|
|
|
|-CXXConstructExpr
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr 'p'
|
|
|
|
|-BindingDecl 'a'
|
|
|
|
| |-VarDecl 'a'
|
|
|
|
| | `-CallExpr
|
|
|
|
| | |-ImplicitCastExpr
|
|
|
|
| | | `-DeclRefExpr 'get'
|
|
|
|
| | `-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr ''
|
|
|
|
| `-DeclRefExpr 'a'
|
|
|
|
`-BindingDecl 'b'
|
|
|
|
|-VarDecl 'b'
|
|
|
|
| `-CallExpr
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr 'get'
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr ''
|
|
|
|
`-DeclRefExpr 'b'
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
FN[0].getNodeAs<DecompositionDecl>("decomp")),
|
|
|
|
R"cpp(
|
|
|
|
DecompositionDecl ''
|
|
|
|
|-DeclRefExpr 'p'
|
|
|
|
|-BindingDecl 'a'
|
|
|
|
`-BindingDecl 'b'
|
2020-11-04 22:57:42 +00:00
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 13:59:01 -08:00
|
|
|
TEST(Traverse, IgnoreUnlessSpelledInSourceTemplateInstantiations) {
|
|
|
|
|
|
|
|
auto AST = buildASTFromCode(R"cpp(
|
|
|
|
template<typename T>
|
|
|
|
struct TemplStruct {
|
|
|
|
TemplStruct() {}
|
|
|
|
~TemplStruct() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T m_t;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T timesTwo(T input)
|
|
|
|
{
|
|
|
|
return input * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void instantiate()
|
|
|
|
{
|
|
|
|
TemplStruct<int> ti;
|
|
|
|
TemplStruct<double> td;
|
|
|
|
(void)timesTwo<int>(2);
|
|
|
|
(void)timesTwo<double>(2);
|
|
|
|
}
|
2020-11-02 23:56:32 +00:00
|
|
|
|
|
|
|
template class TemplStruct<float>;
|
|
|
|
|
|
|
|
extern template class TemplStruct<long>;
|
|
|
|
|
|
|
|
template<> class TemplStruct<bool> {
|
|
|
|
TemplStruct() {}
|
|
|
|
~TemplStruct() {}
|
|
|
|
|
|
|
|
void foo() {}
|
|
|
|
private:
|
|
|
|
bool m_t;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Explicit instantiation of template functions do not appear in the AST
|
|
|
|
template float timesTwo(float);
|
|
|
|
|
|
|
|
template<> bool timesTwo<bool>(bool) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-03 13:59:01 -08:00
|
|
|
)cpp");
|
|
|
|
{
|
|
|
|
auto BN = ast_matchers::match(
|
|
|
|
classTemplateDecl(hasName("TemplStruct")).bind("rec"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
BN[0].getNodeAs<Decl>("rec")),
|
|
|
|
R"cpp(
|
|
|
|
ClassTemplateDecl 'TemplStruct'
|
|
|
|
|-TemplateTypeParmDecl 'T'
|
|
|
|
`-CXXRecordDecl 'TemplStruct'
|
|
|
|
|-CXXConstructorDecl 'TemplStruct<T>'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-CXXDestructorDecl '~TemplStruct<T>'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-AccessSpecDecl
|
|
|
|
`-FieldDecl 'm_t'
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("rec")),
|
|
|
|
R"cpp(
|
|
|
|
ClassTemplateDecl 'TemplStruct'
|
|
|
|
|-TemplateTypeParmDecl 'T'
|
|
|
|
|-CXXRecordDecl 'TemplStruct'
|
|
|
|
| |-CXXRecordDecl 'TemplStruct'
|
|
|
|
| |-CXXConstructorDecl 'TemplStruct<T>'
|
|
|
|
| | `-CompoundStmt
|
|
|
|
| |-CXXDestructorDecl '~TemplStruct<T>'
|
|
|
|
| | `-CompoundStmt
|
|
|
|
| |-AccessSpecDecl
|
|
|
|
| `-FieldDecl 'm_t'
|
|
|
|
|-ClassTemplateSpecializationDecl 'TemplStruct'
|
|
|
|
| |-TemplateArgument type int
|
|
|
|
| | `-BuiltinType
|
|
|
|
| |-CXXRecordDecl 'TemplStruct'
|
|
|
|
| |-CXXConstructorDecl 'TemplStruct'
|
|
|
|
| | `-CompoundStmt
|
|
|
|
| |-CXXDestructorDecl '~TemplStruct'
|
|
|
|
| | `-CompoundStmt
|
|
|
|
| |-AccessSpecDecl
|
|
|
|
| |-FieldDecl 'm_t'
|
|
|
|
| `-CXXConstructorDecl 'TemplStruct'
|
|
|
|
| `-ParmVarDecl ''
|
2020-11-02 23:56:32 +00:00
|
|
|
|-ClassTemplateSpecializationDecl 'TemplStruct'
|
|
|
|
| |-TemplateArgument type double
|
|
|
|
| | `-BuiltinType
|
|
|
|
| |-CXXRecordDecl 'TemplStruct'
|
|
|
|
| |-CXXConstructorDecl 'TemplStruct'
|
|
|
|
| | `-CompoundStmt
|
|
|
|
| |-CXXDestructorDecl '~TemplStruct'
|
|
|
|
| | `-CompoundStmt
|
|
|
|
| |-AccessSpecDecl
|
|
|
|
| |-FieldDecl 'm_t'
|
|
|
|
| `-CXXConstructorDecl 'TemplStruct'
|
|
|
|
| `-ParmVarDecl ''
|
|
|
|
|-ClassTemplateSpecializationDecl 'TemplStruct'
|
|
|
|
| |-TemplateArgument type float
|
|
|
|
| | `-BuiltinType
|
|
|
|
| |-CXXRecordDecl 'TemplStruct'
|
|
|
|
| |-CXXConstructorDecl 'TemplStruct'
|
|
|
|
| | `-CompoundStmt
|
|
|
|
| |-CXXDestructorDecl '~TemplStruct'
|
|
|
|
| | `-CompoundStmt
|
|
|
|
| |-AccessSpecDecl
|
|
|
|
| `-FieldDecl 'm_t'
|
|
|
|
|-ClassTemplateSpecializationDecl 'TemplStruct'
|
|
|
|
| |-TemplateArgument type long
|
|
|
|
| | `-BuiltinType
|
|
|
|
| |-CXXRecordDecl 'TemplStruct'
|
|
|
|
| |-CXXConstructorDecl 'TemplStruct'
|
|
|
|
| |-CXXDestructorDecl '~TemplStruct'
|
|
|
|
| |-AccessSpecDecl
|
|
|
|
| `-FieldDecl 'm_t'
|
2020-11-03 13:59:01 -08:00
|
|
|
`-ClassTemplateSpecializationDecl 'TemplStruct'
|
2020-11-02 23:56:32 +00:00
|
|
|
|-TemplateArgument type _Bool
|
2020-11-03 13:59:01 -08:00
|
|
|
| `-BuiltinType
|
|
|
|
|-CXXRecordDecl 'TemplStruct'
|
|
|
|
|-CXXConstructorDecl 'TemplStruct'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-CXXDestructorDecl '~TemplStruct'
|
|
|
|
| `-CompoundStmt
|
2020-11-02 23:56:32 +00:00
|
|
|
|-CXXMethodDecl 'foo'
|
|
|
|
| `-CompoundStmt
|
2020-11-03 13:59:01 -08:00
|
|
|
|-AccessSpecDecl
|
2020-11-02 23:56:32 +00:00
|
|
|
`-FieldDecl 'm_t'
|
2020-11-07 22:25:00 +00:00
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto BN = ast_matchers::match(
|
|
|
|
classTemplateSpecializationDecl(
|
|
|
|
hasTemplateArgument(
|
|
|
|
0, templateArgument(refersToType(asString("_Bool")))))
|
|
|
|
.bind("templSpec"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("templSpec")),
|
|
|
|
R"cpp(
|
|
|
|
ClassTemplateSpecializationDecl 'TemplStruct'
|
|
|
|
|-TemplateArgument type _Bool
|
|
|
|
| `-BuiltinType
|
|
|
|
|-CXXRecordDecl 'TemplStruct'
|
|
|
|
|-CXXConstructorDecl 'TemplStruct'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-CXXDestructorDecl '~TemplStruct'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-CXXMethodDecl 'foo'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-AccessSpecDecl
|
|
|
|
`-FieldDecl 'm_t'
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
BN[0].getNodeAs<Decl>("templSpec")),
|
|
|
|
R"cpp(
|
|
|
|
ClassTemplateSpecializationDecl 'TemplStruct'
|
|
|
|
|-TemplateArgument type _Bool
|
|
|
|
| `-BuiltinType
|
|
|
|
|-CXXConstructorDecl 'TemplStruct'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-CXXDestructorDecl '~TemplStruct'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-CXXMethodDecl 'foo'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-AccessSpecDecl
|
|
|
|
`-FieldDecl 'm_t'
|
2020-11-03 13:59:01 -08:00
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto BN = ast_matchers::match(
|
|
|
|
functionTemplateDecl(hasName("timesTwo")).bind("fn"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
BN[0].getNodeAs<Decl>("fn")),
|
|
|
|
R"cpp(
|
|
|
|
FunctionTemplateDecl 'timesTwo'
|
|
|
|
|-TemplateTypeParmDecl 'T'
|
|
|
|
`-FunctionDecl 'timesTwo'
|
|
|
|
|-ParmVarDecl 'input'
|
|
|
|
`-CompoundStmt
|
|
|
|
`-ReturnStmt
|
|
|
|
`-BinaryOperator
|
|
|
|
|-DeclRefExpr 'input'
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("fn")),
|
|
|
|
R"cpp(
|
|
|
|
FunctionTemplateDecl 'timesTwo'
|
|
|
|
|-TemplateTypeParmDecl 'T'
|
|
|
|
|-FunctionDecl 'timesTwo'
|
|
|
|
| |-ParmVarDecl 'input'
|
|
|
|
| `-CompoundStmt
|
|
|
|
| `-ReturnStmt
|
|
|
|
| `-BinaryOperator
|
|
|
|
| |-DeclRefExpr 'input'
|
|
|
|
| `-IntegerLiteral
|
|
|
|
|-FunctionDecl 'timesTwo'
|
|
|
|
| |-TemplateArgument type int
|
|
|
|
| | `-BuiltinType
|
|
|
|
| |-ParmVarDecl 'input'
|
|
|
|
| `-CompoundStmt
|
|
|
|
| `-ReturnStmt
|
|
|
|
| `-BinaryOperator
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr 'input'
|
|
|
|
| `-IntegerLiteral
|
2020-11-02 23:56:32 +00:00
|
|
|
|-FunctionDecl 'timesTwo'
|
|
|
|
| |-TemplateArgument type double
|
|
|
|
| | `-BuiltinType
|
|
|
|
| |-ParmVarDecl 'input'
|
|
|
|
| `-CompoundStmt
|
|
|
|
| `-ReturnStmt
|
|
|
|
| `-BinaryOperator
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr 'input'
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-IntegerLiteral
|
|
|
|
|-FunctionDecl 'timesTwo'
|
|
|
|
| |-TemplateArgument type float
|
|
|
|
| | `-BuiltinType
|
|
|
|
| |-ParmVarDecl 'input'
|
|
|
|
| `-CompoundStmt
|
|
|
|
| `-ReturnStmt
|
|
|
|
| `-BinaryOperator
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr 'input'
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-IntegerLiteral
|
|
|
|
|-FunctionDecl 'timesTwo'
|
|
|
|
| |-TemplateArgument type _Bool
|
|
|
|
| | `-BuiltinType
|
|
|
|
| |-ParmVarDecl ''
|
|
|
|
| `-CompoundStmt
|
|
|
|
| `-ReturnStmt
|
|
|
|
| `-CXXBoolLiteralExpr
|
2020-11-03 13:59:01 -08:00
|
|
|
`-FunctionDecl 'timesTwo'
|
2020-11-02 23:56:32 +00:00
|
|
|
|-TemplateArgument type _Bool
|
2020-11-03 13:59:01 -08:00
|
|
|
| `-BuiltinType
|
2020-11-02 23:56:32 +00:00
|
|
|
`-ParmVarDecl 'input'
|
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto BN = ast_matchers::match(
|
|
|
|
classTemplateSpecializationDecl(
|
|
|
|
hasName("TemplStruct"),
|
|
|
|
hasTemplateArgument(
|
|
|
|
0, templateArgument(refersToType(asString("float")))),
|
|
|
|
hasParent(translationUnitDecl()))
|
|
|
|
.bind("rec"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
BN[0].getNodeAs<Decl>("rec")),
|
|
|
|
R"cpp(
|
|
|
|
ClassTemplateSpecializationDecl 'TemplStruct'
|
|
|
|
`-TemplateArgument type float
|
|
|
|
`-BuiltinType
|
|
|
|
)cpp");
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Decl>("rec")),
|
|
|
|
R"cpp(
|
|
|
|
ClassTemplateSpecializationDecl 'TemplStruct'
|
|
|
|
|-TemplateArgument type float
|
|
|
|
| `-BuiltinType
|
|
|
|
|-CXXRecordDecl 'TemplStruct'
|
|
|
|
|-CXXConstructorDecl 'TemplStruct'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-CXXDestructorDecl '~TemplStruct'
|
|
|
|
| `-CompoundStmt
|
|
|
|
|-AccessSpecDecl
|
|
|
|
`-FieldDecl 'm_t'
|
2020-11-03 13:59:01 -08:00
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 23:04:31 +00:00
|
|
|
TEST(Traverse, CXXRewrittenBinaryOperator) {
|
|
|
|
|
|
|
|
auto AST = buildASTFromCodeWithArgs(R"cpp(
|
|
|
|
namespace std {
|
|
|
|
struct strong_ordering {
|
|
|
|
int n;
|
|
|
|
constexpr operator int() const { return n; }
|
|
|
|
static const strong_ordering equal, greater, less;
|
|
|
|
};
|
|
|
|
constexpr strong_ordering strong_ordering::equal = {0};
|
|
|
|
constexpr strong_ordering strong_ordering::greater = {1};
|
|
|
|
constexpr strong_ordering strong_ordering::less = {-1};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct HasSpaceshipMem {
|
|
|
|
int a;
|
|
|
|
constexpr auto operator<=>(const HasSpaceshipMem&) const = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
void binop()
|
|
|
|
{
|
|
|
|
HasSpaceshipMem hs1, hs2;
|
|
|
|
if (hs1 < hs2)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
)cpp",
|
|
|
|
{"-std=c++20"});
|
|
|
|
{
|
|
|
|
auto BN = ast_matchers::match(cxxRewrittenBinaryOperator().bind("binop"),
|
|
|
|
AST->getASTContext());
|
|
|
|
EXPECT_EQ(BN.size(), 1u);
|
|
|
|
|
|
|
|
EXPECT_EQ(dumpASTString(TK_AsIs, BN[0].getNodeAs<Stmt>("binop")),
|
|
|
|
R"cpp(
|
|
|
|
CXXRewrittenBinaryOperator
|
|
|
|
`-BinaryOperator
|
|
|
|
|-ImplicitCastExpr
|
|
|
|
| `-CXXMemberCallExpr
|
|
|
|
| `-MemberExpr
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-MaterializeTemporaryExpr
|
|
|
|
| `-CXXOperatorCallExpr
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr 'operator<=>'
|
|
|
|
| |-ImplicitCastExpr
|
|
|
|
| | `-DeclRefExpr 'hs1'
|
|
|
|
| `-ImplicitCastExpr
|
|
|
|
| `-DeclRefExpr 'hs2'
|
|
|
|
`-IntegerLiteral
|
|
|
|
)cpp");
|
|
|
|
EXPECT_EQ(dumpASTString(TK_IgnoreUnlessSpelledInSource,
|
|
|
|
BN[0].getNodeAs<Stmt>("binop")),
|
|
|
|
R"cpp(
|
|
|
|
CXXRewrittenBinaryOperator
|
|
|
|
|-DeclRefExpr 'hs1'
|
|
|
|
`-DeclRefExpr 'hs2'
|
|
|
|
)cpp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-19 13:03:48 +00:00
|
|
|
} // namespace clang
|