Skip to main content
summaryrefslogtreecommitdiffstats
blob: 258d2875f9c4cbc12227ca3670de6d8dad465c69 (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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*******************************************************************************
 * Copyright (c) 2013 CEA LIST.
 * 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:
 *     Ansgar Radermacher - ansgar.radermacher@cea.fr CEA LIST - initial API and implementation
 *
 *******************************************************************************/

package org.eclipse.papyrus.texteditor.cdt.sync;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.ExpansionOverlapsBoundaryException;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IFunctionDeclaration;
import org.eclipse.cdt.core.model.IMethodDeclaration;
import org.eclipse.cdt.core.model.IParent;
import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.ui.CDTUITools;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.EList;
import org.eclipse.papyrus.C_Cpp.Array;
import org.eclipse.papyrus.C_Cpp.Const;
import org.eclipse.papyrus.C_Cpp.Include;
import org.eclipse.papyrus.C_Cpp.Ptr;
import org.eclipse.papyrus.C_Cpp.Ref;
import org.eclipse.papyrus.cpp.codegen.Constants;
import org.eclipse.papyrus.cpp.profile.StUtils;
import org.eclipse.papyrus.infra.core.Activator;
import org.eclipse.papyrus.texteditor.cdt.CommandSupport;
import org.eclipse.papyrus.texteditor.cdt.Utils;
import org.eclipse.papyrus.texteditor.cdt.listener.ModelListener;
import org.eclipse.ui.IEditorInput;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.OpaqueBehavior;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Parameter;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;


public class SyncCDTtoModel implements Runnable {

	public static final String sAtParam = "@param"; //$NON-NLS-1$
	
	public static final String ansiCLib = "AnsiCLibrary"; //$NON-NLS-1$
	
	public SyncCDTtoModel(IEditorInput input, Classifier classifier, String projectName) {
		m_input = input;
		m_classifier = classifier;
		m_projectName = projectName;
	}

	public final String langID = "C/C++"; //$NON-NLS-1$

	public void syncCDTtoModel() {
		CommandSupport.exec("update model from CDT", this);
	}

	public void run() {
		ICElement ice = CDTUITools.getEditorInputCElement(m_input);
		ModelListener.syncFromEditor = true;

		if(ice instanceof ITranslationUnit) {
			ICProject project = CoreModel.getDefault().getCModel().getCProject(m_projectName);

			IIndex index = null;
			try {
				index = CCorePlugin.getIndexManager().getIndex(project);
				index.acquireReadLock();

				// index = CCorePlugin.getIndexManager().getIndex(project);
				ITranslationUnit itu = (ITranslationUnit)ice;
				// hack: force re-evaluation of AST node, requires modified CDT!
				// Seems to be no longer required.
				// ASTProvider.getASTProvider().fCache.setActiveElement(itu);

				IASTTranslationUnit ast = itu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
				IASTNodeSelector selector = ast.getNodeSelector(null);

				examineChildren(itu, selector, itu);
				updateCppInclude(itu);

				CUIPlugin.getDefault().getProblemMarkerManager();
				if(itu instanceof IWorkingCopy) {
					// ((IWorkingCopy)itu).commit(true, new NullProgressMonitor());
					// ((IWorkingCopy)itu).reconcile();
					((IWorkingCopy)itu).reconcile(true, new NullProgressMonitor());
					// ((IWorkingCopy)itu).reconcile(true, true, new NullProgressMonitor());
				}

			} catch (CModelException e) {
				Activator.getDefault().getLog().log(e.getStatus());
			} catch (Exception e) {
				System.err.println(e);
			} finally {
				if(index != null) {
					index.releaseReadLock();
				}
			}
		}
		ModelListener.syncFromEditor = false;
	}

	/**
	 * Examine the children of a translation unit in order to extract the methods that are defined within
	 * hte unit
	 * 
	 * @param itu
	 * @param selector
	 * @param parent
	 * @throws CModelException
	 */
	public void examineChildren(ITranslationUnit itu, IASTNodeSelector selector, IParent parent) throws CModelException {

		int position = 0;
		// if (parent instanceof Namespace) {
		for(ICElement child : parent.getChildren()) {
			if(child instanceof IParent) {
				examineChildren(itu, selector, (IParent)child);
			}
			ISourceRange range = null;
			if(child instanceof ISourceReference) {
				range = ((ISourceReference)child).getSourceRange();
			}
			if(child instanceof IFunctionDeclaration) {
				// function declaration is a superclass for method declaration (but need to trace functions differently?)
				String name = ((IFunctionDeclaration)child).getElementName();
				IASTNode node = selector.findEnclosingNode(range.getStartPos(), range.getLength());
				if(node instanceof IASTFunctionDefinition) {
					IASTFunctionDefinition definition = (IASTFunctionDefinition)node;
					IASTFunctionDeclarator declarator = definition.getDeclarator();
					String body = getBody(itu, definition);
					Operation operation = updateMethod(position, parent, name, body, declarator);
					updateComment(itu, definition, operation);
					// System.err.println("body source <" + body + ">");
				}
				position++;
			}
		}
	}

	/**
	 * update the contents of the CppInclude directive
	 * 
	 * @param itu
	 *        the translation unit
	 */
	public void updateCppInclude(ITranslationUnit itu) {
		String contents = new String(itu.getContents());
		int preBodyStart = contents.indexOf(Constants.cppIncPreBodyStart);
		int preBodyEnd = contents.indexOf(Constants.cppIncPreBodyEnd);
		String preBody = ""; //$NON-NLS-1$
		String body = ""; //$NON-NLS-1$
		if(preBodyStart != -1) {
			preBodyStart += Constants.cppIncPreBodyStart.length();
			if(preBodyEnd > preBodyStart) {
				preBody = contents.substring(preBodyStart, preBodyEnd).trim();
			}
		}

		int bodyStart = contents.indexOf(Constants.cppIncBodyStart);
		int bodyEnd = contents.indexOf(Constants.cppIncBodyEnd);

		if(bodyStart != -1) {
			bodyStart += Constants.cppIncBodyStart.length() + 1;
			if(bodyEnd > bodyStart) {
				body = contents.substring(bodyStart, bodyEnd).trim();
			}
		}
		if(body.length() > 0 || preBody.length() > 0) {
			Include Include = StUtils.applyApp(m_classifier, Include.class);
			Include.setPreBody(preBody);
			Include.setBody(body);
		}
	}

	/**
	 * Update a method in the model while passing the qualified name
	 * 
	 * @param position
	 *        The position of the method within the file. Used to identify renaming operations
	 * @param parent
	 *        the CDT parent which is used to get a list of children
	 * @param qualifiedName
	 *        the qualified name of a method
	 * @param body
	 *        the method body
	 * @param declarator
	 *        the declarator for the method
	 */
	public Operation updateMethod(int position, IParent parent, String qualifiedName, String body, IASTFunctionDeclarator declarator) {

		String names[] = qualifiedName.split(Utils.nsSep);
		String name = names[names.length - 1];

		Operation operation = m_classifier.getOperation(name, null, null);
		if(operation == null) {
			// operation is not found via name in the model. That does not necessarily mean that this is a new method.
			// It may also have been renamed.
			// Strategy: try to locate the operation in the model at the same "position" as the method in the file and
			// verify that this method does not have the same name as any method in the CDT file.
			if(position < m_classifier.getOperations().size()) {
				operation = m_classifier.getOperations().get(position);
				String modelName = operation.getName();
				try {
					for(ICElement child : parent.getChildren()) {
						if(child instanceof IMethodDeclaration) {
							String cdtName = ((IMethodDeclaration)child).getElementName();
							if(cdtName.equals(modelName)) {
								// an existing operation in the CDT file already has this name
								operation = null;
								break;
							}
						}
					}
				} catch (CModelException e) {
				}
			}
			if(operation != null) {
				operation.setName(name);
			}
			else {
				// still null => create new operation in model
				if(m_classifier instanceof Class) {
					operation = ((Class)m_classifier).createOwnedOperation(name, null, null);
				}
				else if(m_classifier instanceof DataType) {
					operation = ((DataType)m_classifier).createOwnedOperation(name, null, null);
				}
			}
		}

		OpaqueBehavior ob = null;
		if(operation.getMethods().size() == 0) {
			// does not exist, create

			if(m_classifier instanceof Class) {
				ob = (OpaqueBehavior)((Class)m_classifier).createOwnedBehavior(name, UMLPackage.eINSTANCE.getOpaqueBehavior());
			}
			else if(m_classifier instanceof DataType) {
				//ob = (OpaqueBehavior) ((DataType) m_classifier).createOwnedBehavior(name, UMLPackage.eINSTANCE.getOpaqueBehavior());
			}
			ob.setSpecification(operation);
			ob.setIsReentrant(false);
		}
		else {
			ob = (OpaqueBehavior)operation.getMethods().get(0);
			if(!ob.getName().equals(name)) {
				ob.setName(name);
			}
		}

		operation.getOwnedParameters().clear();
		ob.getOwnedParameters().clear();
		for(IASTNode declaratorChild : declarator.getChildren()) {
			if(declaratorChild instanceof IASTParameterDeclaration) {
				IASTParameterDeclaration parameter = (IASTParameterDeclaration)declaratorChild;
				IASTName parameterName = parameter.getDeclarator().getName();
				IASTDeclSpecifier parameterType = parameter.getDeclSpecifier();
				boolean isPointer = false;
				boolean isRef = false;
				String array = "";  //$NON-NLS-1$
				String parameterTypeName = ""; //$NON-NLS-1$
				try {
					IToken token = parameter.getDeclarator().getSyntax();
					while(token != null) {
						String tokenStr = token.toString();
						if(tokenStr.equals("*")) { //$NON-NLS-1$
							isPointer = true;
						}
						else if(tokenStr.equals("&")) { //$NON-NLS-1$
							isRef = true;
						}
						else if (tokenStr.equals("[")) { //$NON-NLS-1$
							while (token != null) {
								array += token.toString();
								token = token.getNext();
							}
							if (token == null) {
								break;
							}
						}
 						token = token.getNext();
					}

					token = parameterType.getSyntax();
					while(token != null) {
						String tokenStr = token.toString();
						if(tokenStr.equals("*")) { //$NON-NLS-1$
							// TODO: check, if this can be called (depending on * position with different semantics?)
							isPointer = true;
						}
						else if(tokenStr.equals("&")) { //$NON-NLS-1$
							isRef = true;
						}
						else if(tokenStr.equals("const")) { //$NON-NLS-1$
							// do nothing (use isConst() operation of parameterType)
							// is not part of parameter type
						}
						else {
							if(parameterTypeName.length() > 0) {
								parameterTypeName += " "; //$NON-NLS-1$
							}
							parameterTypeName += tokenStr;
						}
						token = token.getNext();
					}
				} catch (ExpansionOverlapsBoundaryException e) {
				}

				NamedElement umlParameterType = Utils.getQualifiedElement(Utils.getTop(m_classifier), parameterTypeName);
				if(umlParameterType == null) {
					umlParameterType = Utils.getQualifiedElement(Utils.getTop(m_classifier), ansiCLib + Utils.nsSep + parameterTypeName);
				}
				if(parameterType.isRestrict()) {
				}
				Parameter umlParameter = null;
				if(umlParameterType instanceof Type) {
					umlParameter = operation.createOwnedParameter(parameterName.toString(), (Type)umlParameterType);
					ob.createOwnedParameter(parameterName.toString(), (Type)umlParameterType);
				}
				else {
					umlParameter = operation.createOwnedParameter(parameterName.toString(), null);
					ob.createOwnedParameter(parameterName.toString(), null);
				}
				if(parameterType.isConst()) {
					StUtils.apply(umlParameter, Const.class);
				}
				if(isPointer) {
					StUtils.apply(umlParameter, Ptr.class);
				}
				else if(isRef) {
					StUtils.apply(umlParameter, Ref.class);
				}
				if(array.length() > 0) {
					Array arraySt = StUtils.applyApp(umlParameter, Array.class);
					if (!array.equals("[]") && (!array.equals("[ ]"))) {  //$NON-NLS-1$//$NON-NLS-2$
						arraySt.setDefinition(array);
					}
				}
			
			}
		}

		if(ob.getBodies().size() == 0) {
			ob.getLanguages().add(langID);
			ob.getBodies().add(""); //$NON-NLS-1$
		}
		for(int i = 0; i < ob.getLanguages().size(); i++) {
			if(ob.getLanguages().get(i).equals(langID)) {
				if(i < ob.getBodies().size()) { // should always be true, unless sync between languages/bodies is lost
					ob.getBodies().set(i, body);
				}
			}
		}
		return operation;
	}

	public static String getBody(ITranslationUnit itu, IASTFunctionDefinition definition) {
		IASTStatement body = definition.getBody();

		if(body instanceof IASTCompoundStatement) {
			IASTCompoundStatement bodyComp = (IASTCompoundStatement)body;

			IASTFileLocation bodyLoc = bodyComp.getFileLocation();
			int start = bodyLoc.getNodeOffset();
			int end = start + bodyLoc.getNodeLength();
			char contents[] = itu.getContents();
			// body contains enclosing { } which we need to remove (+2, -2). We cannot use the
			// first and last statement, since leading and trailing comments are not part
			// of the AST tree.
			return Utils.decreaseIndent(contents, start + 2, end - 2);
		}
		return ""; //$NON-NLS-1$
	}

	public void updateComment(ITranslationUnit itu, IASTFunctionDefinition definition, Operation operation) {
		IASTFileLocation bodyLoc = definition.getFileLocation();
		int start = bodyLoc.getNodeOffset() - 1;
		int end = start;
		char contents[] = itu.getContents();
		String comment = ""; //$NON-NLS-1$
		// backward scan for beginning /* 
		while(start > 0) {
			if(contents[start] == '/' && contents[start + 1] == '*') {
				start += "/**".length(); // TODO: common string constants with generator //$NON-NLS-1$
				for(int i = start; i < end; i++) {
					comment += contents[i];
				}
				comment = comment.replace("\n * ", "\n").	//$NON-NLS-1$//$NON-NLS-2$
						replace("*/", "").trim();  			//$NON-NLS-1$//$NON-NLS-2$
				break;
			}
			start--;
		}
		if(comment.length() > 0) {
			// filter @param
			int atParam = comment.indexOf(sAtParam);
			String commentMethodOnly = (atParam != -1) ? comment.substring(0, atParam).trim() : comment;

			EList<Comment> commentsUML = operation.getOwnedComments();
			Comment commentUML;
			if(commentsUML.size() == 0) {
				commentUML = operation.createOwnedComment();
				commentUML.getAnnotatedElements().add(commentUML);
			}
			else {
				commentUML = commentsUML.get(0);
			}
			while(atParam != -1) {
				int currentAtParam = atParam;
				atParam = comment.indexOf(sAtParam, atParam + 1);
				String commentParam = (atParam != -1) ? comment.substring(currentAtParam, atParam) : comment.substring(currentAtParam);
				Comment commentParamUML;
				int atParamName = sAtParam.length();

				while((atParamName < commentParam.length()) && Character.isWhitespace(commentParam.charAt(atParamName))) {
					atParamName++;
				}
				int atParamNameEnd = atParamName;
				while((atParamNameEnd < commentParam.length()) && !Character.isWhitespace(commentParam.charAt(atParamNameEnd))) {
					atParamNameEnd++;
				}
				if(atParamNameEnd < commentParam.length() - 1) {
					String parameterName = commentParam.substring(atParamName, atParamNameEnd);
					String commentParamText = commentParam.substring(atParamNameEnd).trim();
					Parameter parameter = operation.getOwnedParameter(parameterName, null, false, false);
					if(parameter != null) {
						EList<Comment> commentsParamUML = parameter.getOwnedComments();
						if(commentsParamUML.size() == 0) {
							commentParamUML = parameter.createOwnedComment();
							commentParamUML.getAnnotatedElements().add(commentParamUML);
						}
						else {
							commentParamUML = commentsParamUML.get(0);
						}
						commentParamUML.setBody(commentParamText);
					}
					else {
						// parameter is not found in model, e.g. either renamed or not yet existing
						// store comment in operation comment
						commentMethodOnly += "\n " + sAtParam + parameterName + //$NON-NLS-1$
								" not found(!) " + commentParamText; //$NON-NLS-1$
					}
				}
			}
			commentUML.setBody(commentMethodOnly);
		}
	}


	protected IEditorInput m_input;

	protected Classifier m_classifier;

	protected String m_projectName;
}

Back to the top