1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
//===- SynthesisTest.cpp --------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file tests synthesis API for syntax trees.
//
//===----------------------------------------------------------------------===//
#include "TreeTestBase.h"
#include "clang/Tooling/Syntax/BuildTree.h"
#include "clang/Tooling/Syntax/Nodes.h"
#include "gtest/gtest.h"
using namespace clang;
using namespace clang::syntax;
namespace {
class SynthesisTest : public SyntaxTreeTest {
protected:
::testing::AssertionResult treeDumpEqual(syntax::Node *Root, StringRef Dump) {
if (!Root)
return ::testing::AssertionFailure()
<< "Root was not built successfully.";
auto Actual = StringRef(Root->dump(Arena->getSourceManager())).trim().str();
auto Expected = Dump.trim().str();
// EXPECT_EQ shows the diff between the two strings if they are different.
EXPECT_EQ(Expected, Actual);
if (Actual != Expected) {
return ::testing::AssertionFailure();
}
return ::testing::AssertionSuccess();
}
};
INSTANTIATE_TEST_SUITE_P(SynthesisTests, SynthesisTest,
::testing::ValuesIn(allTestClangConfigs()) );
TEST_P(SynthesisTest, Leaf_Punctuation) {
buildTree("", GetParam());
auto *Leaf = createLeaf(*Arena, tok::comma);
EXPECT_TRUE(treeDumpEqual(Leaf, R"txt(
',' Detached synthesized
)txt"));
}
TEST_P(SynthesisTest, Leaf_Punctuation_CXX) {
if (!GetParam().isCXX())
return;
buildTree("", GetParam());
auto *Leaf = createLeaf(*Arena, tok::coloncolon);
EXPECT_TRUE(treeDumpEqual(Leaf, R"txt(
'::' Detached synthesized
)txt"));
}
TEST_P(SynthesisTest, Leaf_Keyword) {
buildTree("", GetParam());
auto *Leaf = createLeaf(*Arena, tok::kw_if);
EXPECT_TRUE(treeDumpEqual(Leaf, R"txt(
'if' Detached synthesized
)txt"));
}
TEST_P(SynthesisTest, Leaf_Keyword_CXX11) {
if (!GetParam().isCXX11OrLater())
return;
buildTree("", GetParam());
auto *Leaf = createLeaf(*Arena, tok::kw_nullptr);
EXPECT_TRUE(treeDumpEqual(Leaf, R"txt(
'nullptr' Detached synthesized
)txt"));
}
TEST_P(SynthesisTest, Leaf_Identifier) {
buildTree("", GetParam());
auto *Leaf = createLeaf(*Arena, tok::identifier, "a");
EXPECT_TRUE(treeDumpEqual(Leaf, R"txt(
'a' Detached synthesized
)txt"));
}
TEST_P(SynthesisTest, Leaf_Number) {
buildTree("", GetParam());
auto *Leaf = createLeaf(*Arena, tok::numeric_constant, "1");
EXPECT_TRUE(treeDumpEqual(Leaf, R"txt(
'1' Detached synthesized
)txt"));
}
TEST_P(SynthesisTest, Tree_Empty) {
buildTree("", GetParam());
auto *Tree = createTree(*Arena, {}, NodeKind::UnknownExpression);
EXPECT_TRUE(treeDumpEqual(Tree, R"txt(
UnknownExpression Detached synthesized
)txt"));
}
TEST_P(SynthesisTest, Tree_Flat) {
buildTree("", GetParam());
auto *LeafLParen = createLeaf(*Arena, tok::l_paren);
auto *LeafRParen = createLeaf(*Arena, tok::r_paren);
auto *TreeParen = createTree(*Arena,
{{LeafLParen, NodeRole::LeftHandSide},
{LeafRParen, NodeRole::RightHandSide}},
NodeKind::ParenExpression);
EXPECT_TRUE(treeDumpEqual(TreeParen, R"txt(
ParenExpression Detached synthesized
|-'(' LeftHandSide synthesized
`-')' RightHandSide synthesized
)txt"));
}
TEST_P(SynthesisTest, Tree_OfTree) {
buildTree("", GetParam());
auto *Leaf1 = createLeaf(*Arena, tok::numeric_constant, "1");
auto *Int1 = createTree(*Arena, {{Leaf1, NodeRole::LiteralToken}},
NodeKind::IntegerLiteralExpression);
auto *LeafPlus = createLeaf(*Arena, tok::plus);
auto *Leaf2 = createLeaf(*Arena, tok::numeric_constant, "2");
auto *Int2 = createTree(*Arena, {{Leaf2, NodeRole::LiteralToken}},
NodeKind::IntegerLiteralExpression);
auto *TreeBinaryOperator = createTree(*Arena,
{{Int1, NodeRole::LeftHandSide},
{LeafPlus, NodeRole::OperatorToken},
{Int2, NodeRole::RightHandSide}},
NodeKind::BinaryOperatorExpression);
EXPECT_TRUE(treeDumpEqual(TreeBinaryOperator, R"txt(
BinaryOperatorExpression Detached synthesized
|-IntegerLiteralExpression LeftHandSide synthesized
| `-'1' LiteralToken synthesized
|-'+' OperatorToken synthesized
`-IntegerLiteralExpression RightHandSide synthesized
`-'2' LiteralToken synthesized
)txt"));
}
TEST_P(SynthesisTest, DeepCopy_Synthesized) {
buildTree("", GetParam());
auto *LeafContinue = createLeaf(*Arena, tok::kw_continue);
auto *LeafSemiColon = createLeaf(*Arena, tok::semi);
auto *StatementContinue = createTree(*Arena,
{{LeafContinue, NodeRole::LiteralToken},
{LeafSemiColon, NodeRole::Unknown}},
NodeKind::ContinueStatement);
auto *Copy = deepCopyExpandingMacros(*Arena, StatementContinue);
EXPECT_TRUE(
treeDumpEqual(Copy, StatementContinue->dump(Arena->getSourceManager())));
// FIXME: Test that copy is independent of original, once the Mutations API is
// more developed.
}
TEST_P(SynthesisTest, DeepCopy_Original) {
auto *OriginalTree = buildTree("int a;", GetParam());
auto *Copy = deepCopyExpandingMacros(*Arena, OriginalTree);
EXPECT_TRUE(treeDumpEqual(Copy, R"txt(
TranslationUnit Detached synthesized
`-SimpleDeclaration synthesized
|-'int' synthesized
|-DeclaratorList Declarators synthesized
| `-SimpleDeclarator ListElement synthesized
| `-'a' synthesized
`-';' synthesized
)txt"));
}
TEST_P(SynthesisTest, DeepCopy_Child) {
auto *OriginalTree = buildTree("int a;", GetParam());
auto *Copy = deepCopyExpandingMacros(*Arena, OriginalTree->getFirstChild());
EXPECT_TRUE(treeDumpEqual(Copy, R"txt(
SimpleDeclaration Detached synthesized
|-'int' synthesized
|-DeclaratorList Declarators synthesized
| `-SimpleDeclarator ListElement synthesized
| `-'a' synthesized
`-';' synthesized
)txt"));
}
TEST_P(SynthesisTest, DeepCopy_Macro) {
auto *OriginalTree = buildTree(R"cpp(
#define HALF_IF if (1+
#define HALF_IF_2 1) {}
void test() {
HALF_IF HALF_IF_2 else {}
})cpp",
GetParam());
auto *Copy = deepCopyExpandingMacros(*Arena, OriginalTree);
// The syntax tree stores already expanded Tokens, we can only see whether the
// macro was expanded when computing replacements. The dump does show that
// nodes in the copy are `modifiable`.
EXPECT_TRUE(treeDumpEqual(Copy, R"txt(
TranslationUnit Detached synthesized
`-SimpleDeclaration synthesized
|-'void' synthesized
|-DeclaratorList Declarators synthesized
| `-SimpleDeclarator ListElement synthesized
| |-'test' synthesized
| `-ParametersAndQualifiers synthesized
| |-'(' OpenParen synthesized
| `-')' CloseParen synthesized
`-CompoundStatement synthesized
|-'{' OpenParen synthesized
|-IfStatement Statement synthesized
| |-'if' IntroducerKeyword synthesized
| |-'(' synthesized
| |-ExpressionStatement Condition synthesized
| | `-BinaryOperatorExpression Expression synthesized
| | |-IntegerLiteralExpression LeftHandSide synthesized
| | | `-'1' LiteralToken synthesized
| | |-'+' OperatorToken synthesized
| | `-IntegerLiteralExpression RightHandSide synthesized
| | `-'1' LiteralToken synthesized
| |-')' synthesized
| |-CompoundStatement ThenStatement synthesized
| | |-'{' OpenParen synthesized
| | `-'}' CloseParen synthesized
| |-'else' ElseKeyword synthesized
| `-CompoundStatement ElseStatement synthesized
| |-'{' OpenParen synthesized
| `-'}' CloseParen synthesized
`-'}' CloseParen synthesized
)txt"));
}
TEST_P(SynthesisTest, Statement_EmptyStatement) {
buildTree("", GetParam());
auto *S = createEmptyStatement(*Arena);
EXPECT_TRUE(treeDumpEqual(S, R"txt(
EmptyStatement Detached synthesized
`-';' synthesized
)txt"));
}
} // namespace
|