Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 43026431d9bc68bbbf312fafa14951e6c58f8185 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2009, 2012 QNX Software Systems
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     QNX Software Systems (Alena Laskavaia)  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.core.cxx;

import java.io.IOException;

import org.eclipse.cdt.codan.core.model.IChecker;
import org.eclipse.cdt.codan.core.test.CodanFastCxxAstTestCase;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBasicType.Kind;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.c.ICBasicType;

/**
 * Test CxxAstUtils
 */
public class CxxAstUtilsTest extends CodanFastCxxAstTestCase {
	@Override
	public IChecker getChecker() {
		return null; // not testing checker
	}

	// typedef int A;
	// typedef A B;
	// void main() {
	//    B x;
	// }
	public void testUnwindTypedef() throws IOException {
		String code = getAboveComment();
		IASTTranslationUnit tu = parse(code);
		final Object result[] = new Object[1];
		ASTVisitor astVisitor = new ASTVisitor() {
			{
				shouldVisitDeclarations = true;
			}

			@Override
			public int visit(IASTDeclaration decl) {
				if (decl instanceof IASTSimpleDeclaration) {
					IASTSimpleDeclaration sdecl = (IASTSimpleDeclaration) decl;
					IASTDeclSpecifier spec = sdecl.getDeclSpecifier();
					if (spec instanceof IASTNamedTypeSpecifier) {
						IASTName tname = ((IASTNamedTypeSpecifier) spec).getName();
						IType typeName = (IType) tname.resolveBinding();
						result[0] = CxxAstUtils.unwindTypedef(typeName);
					}
				}
				return PROCESS_CONTINUE;
			}
		};
		tu.accept(astVisitor);
		assertNotNull(result[0]);
		ICBasicType type = (ICBasicType) result[0];
		assertEquals(Kind.eInt, type.getKind());
	}

	// #define AAA a
	// void main (){
	//    AAA;
	//    b;
	//}
	public void testIsInMacro() throws IOException {
		String code = getAboveComment();
		IASTTranslationUnit tu = parse(code);
		final Object result[] = new Object[2];
		ASTVisitor astVisitor = new ASTVisitor() {
			int i;
			{
				shouldVisitStatements = true;
			}

			@Override
			public int visit(IASTStatement stmt) {
				if (stmt instanceof IASTExpressionStatement) {
					boolean check = CxxAstUtils.isInMacro(((IASTExpressionStatement) stmt).getExpression());
					result[i] = check;
					i++;
				}
				return PROCESS_CONTINUE;
			}
		};
		tu.accept(astVisitor);
		assertNotNull("Stmt not found", result[0]); //$NON-NLS-1$
		assertTrue((Boolean) result[0]);
		assertFalse((Boolean) result[1]);
	}

	//void f() __attribute__((noreturn));
	//
	//int test() {
	//  a();
	//  f();
	//  exit(0);
	//}
	public void testExitStatement() throws IOException {
		String code = getAboveComment();
		IASTTranslationUnit tu = parse(code);
		final Object result[] = new Object[4];
		ASTVisitor astVisitor = new ASTVisitor() {
			int i;
			{
				shouldVisitStatements = true;
			}

			@Override
			public int visit(IASTStatement stmt) {
				boolean check = CxxAstUtils.isExitStatement(stmt);
				result[i] = check;
				i++;
				return PROCESS_CONTINUE;
			}
		};
		tu.accept(astVisitor);
		assertNotNull("Stmt not found", result[0]); //$NON-NLS-1$
		assertFalse((Boolean) result[0]); // compound body
		assertFalse((Boolean) result[1]);
		assertTrue((Boolean) result[2]);
		assertTrue((Boolean) result[3]);
	}
}

Back to the top