Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 503dc28ebdfb456ec1dd1cdff9b058735c286057 (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
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
/*******************************************************************************
 * Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik  
 * Rapperswil, University of applied sciences and others
 * 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: 
 * Institute for Software - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;

import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompositeTypeSpecifier;

/**
 * A visitor for the comments. Calls the NodeCommenter to assign the comments.
 * 
 * @see org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommenter 
 *   
 * @author Guido Zgraggen IFS 
 */
public class ASTCommenterVisitor extends CPPASTVisitor {
	
	protected CommentHandler commHandler;
	protected NodeCommentMap commentMap;
		
	private NodeCommenter nodeCommenter;
	
	{
		shouldVisitExpressions = true;
		shouldVisitStatements = true;
		shouldVisitNames = true;
		shouldVisitDeclarations = true;
		shouldVisitDeclSpecifiers = true;
		shouldVisitDeclarators = true;
		shouldVisitInitializers = true;
		shouldVisitBaseSpecifiers = true;
		shouldVisitNamespaces = true;
		shouldVisitTemplateParameters = true;
		shouldVisitParameterDeclarations = true;
	}
	
	public ASTCommenterVisitor(CommentHandler commHandler, NodeCommentMap commentMap) {
		this.commHandler = commHandler;
		this.commentMap = commentMap;
		init();
	}

	private void init() {
		nodeCommenter = new NodeCommenter(this, commHandler, commentMap);
	}
	
	
	public void addRemainingComments(IASTDeclaration declaration) {
		nodeCommenter.appendRemainingComments(declaration);
	}


	
	@Override
	public int visit(IASTName name) {
		return nodeCommenter.appendComments((ASTNode)name);
	}

	@Override
	public int visit(IASTDeclSpecifier declSpec) {
		return nodeCommenter.appendComments((ASTNode)declSpec);
	}

	@Override
	public int visit(IASTExpression expression) {
		return nodeCommenter.appendComments((ASTNode)expression);
	}

	@Override
	public int visit(IASTStatement statement) {
		return nodeCommenter.appendComments((ASTNode)statement);
	}

	@Override
	public int visit(IASTDeclaration declaration) {
		return nodeCommenter.appendComments((ASTNode)declaration);
	}

	@Override
	public int visit(IASTDeclarator declarator) {
		return nodeCommenter.appendComments((ASTNode)declarator);		
	}

	@Override
	public int visit(IASTInitializer initializer) {
		return nodeCommenter.appendComments((ASTNode)initializer);
	}

	@Override
	public int visit(IASTParameterDeclaration parameterDeclaration) {
		return nodeCommenter.appendComments((ASTNode)parameterDeclaration);
	}
	
	@Override
	public int visit(ICPPASTNamespaceDefinition namespace) {
		return nodeCommenter.appendComments((ASTNode)namespace);
	}

	@Override
	public int visit(ICPPASTTemplateParameter parameter) {
		return nodeCommenter.appendComments((ASTNode)parameter);
	}

	@Override
	public int leave(IASTTranslationUnit tu) {
		nodeCommenter.appendComments((ASTNode)tu);
		return PROCESS_CONTINUE;
	}
	@Override
	public int leave(IASTName name) {
		nodeCommenter.appendComments((ASTNode)name);
		return PROCESS_CONTINUE;
	}
	@Override
	public int leave(IASTDeclaration declaration) {
		nodeCommenter.appendComments((ASTNode)declaration);
		return PROCESS_CONTINUE;
	}
	@Override
	public int leave(ICPPASTNamespaceDefinition namespaceDefinition) {
		return nodeCommenter.appendFreestandingComments((ASTNode)namespaceDefinition);
	}
	@Override
	public int leave(IASTInitializer initializer) {
		nodeCommenter.appendComments((ASTNode)initializer);
		return PROCESS_CONTINUE;
	}
	@Override
	public int leave(IASTParameterDeclaration parameterDeclaration) {
		nodeCommenter.appendComments((ASTNode)parameterDeclaration);
		return PROCESS_CONTINUE;
	}
	@Override
	public int leave(IASTDeclarator declarator) {
		nodeCommenter.appendComments((ASTNode)declarator);
		return PROCESS_CONTINUE;
	}
	@Override
	public int leave(IASTDeclSpecifier declSpec) {
		if(declSpec instanceof CPPASTCompositeTypeSpecifier) {
			return nodeCommenter.appendFreestandingComments((ASTNode)declSpec);
		}
		nodeCommenter.appendComments((ASTNode)declSpec);
		return PROCESS_CONTINUE;
	}
	@Override
	public int leave(IASTExpression expression) {
		nodeCommenter.appendComments((ASTNode)expression);
		return PROCESS_CONTINUE;
	}
	@Override
	public int leave(IASTStatement statement) {
		if(statement instanceof IASTCompoundStatement) {
			return nodeCommenter.appendFreestandingComments((ASTNode)statement);
		}
		nodeCommenter.appendComments((ASTNode)statement);
		return PROCESS_CONTINUE;
	}
	@Override
	public int leave(IASTTypeId typeId) {
		nodeCommenter.appendComments((ASTNode)typeId);
		return PROCESS_CONTINUE;
	}
	@Override
	public int leave(IASTEnumerator enumerator) {
		nodeCommenter.appendComments((ASTNode)enumerator);
		return PROCESS_CONTINUE;
	}	
	@Override
	public int leave(IASTProblem problem){
		nodeCommenter.appendComments((ASTNode)problem);
		return PROCESS_CONTINUE;
	}	
	@Override
	public int leave( IASTComment comment){
		nodeCommenter.appendComments((ASTNode)comment);
		return PROCESS_CONTINUE;
	}	
}

Back to the top