summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2016-01-28 19:25:00 +0000
committerNico Weber <nicolasweber@gmx.de>2016-01-28 19:25:00 +0000
commit22ffb44c7f484fd31ee76997b158de993811e920 (patch)
tree2bc11d94fb71b7124bf053c0418aea825fd5145e /unittests
parentc9d11f9bd2dd101d956a33d49d28676222fd3297 (diff)
downloadclang-22ffb44c7f484fd31ee76997b158de993811e920.tar.gz
Include RecordDecls from anonymous unions in the AST.
For void f() { union { int i; }; } clang used to omit the RecordDecl from the anonymous union from the AST. That's because the code creating it only called PushOnScopeChains(), which adds it to the current DeclContext, which here is the function's DeclContext. But RecursiveASTVisitor doesn't descent into all decls in a FunctionDecl. Instead, for DeclContexts that contain statements, return the RecordDecl so that it can be included in the DeclStmt containing the VarDecl for the union. Interesting bits from the AST before this change: |-FunctionDecl | `-CompoundStmt | |-DeclStmt | | `-VarDecl 0x589cd60 <col:3> col:3 implicit used 'union (anonymous at test.cc:3:3)' callinit After this change: -FunctionDecl | `-CompoundStmt | |-DeclStmt | | |-CXXRecordDecl 0x4612e48 <col:3, col:18> col:3 union definition | | | |-FieldDecl 0x4612f70 <col:11, col:15> col:15 referenced i 'int' | | `-VarDecl 0x4613010 <col:3> col:3 implicit used 'union (anonymous at test.cc:3:3)' callinit This is now closer to how anonymous struct and unions are represented as members of structs. It also enabled deleting some one-off code in the template instantiation code. Finally, it fixes a crash with ASTMatchers, see the included test case (this fixes http://crbug.com/580749). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@259079 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/ASTMatchers/ASTMatchersTest.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/unittests/ASTMatchers/ASTMatchersTest.cpp b/unittests/ASTMatchers/ASTMatchersTest.cpp
index 67d3de9f81..7fd933718f 100644
--- a/unittests/ASTMatchers/ASTMatchersTest.cpp
+++ b/unittests/ASTMatchers/ASTMatchersTest.cpp
@@ -4240,6 +4240,28 @@ TEST(HasAncestor, ImplicitArrayCopyCtorDeclRefExpr) {
declRefExpr(to(decl(hasAncestor(decl()))))));
}
+TEST(HasAncestor, AnonymousUnionMemberExpr) {
+ EXPECT_TRUE(matches("int F() {\n"
+ " union { int i; };\n"
+ " return i;\n"
+ "}\n",
+ memberExpr(member(hasAncestor(decl())))));
+ EXPECT_TRUE(matches("void f() {\n"
+ " struct {\n"
+ " struct { int a; int b; };\n"
+ " } s;\n"
+ " s.a = 4;\n"
+ "}\n",
+ memberExpr(member(hasAncestor(decl())))));
+ EXPECT_TRUE(matches("void f() {\n"
+ " struct {\n"
+ " struct { int a; int b; };\n"
+ " } s;\n"
+ " s.a = 4;\n"
+ "}\n",
+ declRefExpr(to(decl(hasAncestor(decl()))))));
+}
+
TEST(HasParent, MatchesAllParents) {
EXPECT_TRUE(matches(
"template <typename T> struct C { static void f() { 42; } };"