Skip to main content
summaryrefslogtreecommitdiffstats
blob: 766f4bb9136ca589ebfe69dd23064236f7094173 (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
package org.eclipse.cdt.internal.core.model;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICModelStatus;
import org.eclipse.cdt.core.model.IInclude;
import org.eclipse.cdt.core.model.ITranslationUnit;

/**
 * <p>This operation adds an include declaration to an existing translation unit.
 * If the translation unit already includes the specified include declaration,
 * the include is not generated (it does not generate duplicates).
 *
 * <p>Required Attributes:<ul>
 *  <li>Translation unit
 *  <li>Include name - the name of the include to add to the
 *      translation unit. For example: <code>"stdio.h"</code>
 * </ul>
 */
public class CreateFunctionDeclarationOperation extends CreateElementInTUOperation {

	/**
	 * The name of the include to be created.
	 */
	protected String fFunction;

	/**
	 * When executed, this operation will add an include to the given translation unit.
	 */
	public CreateFunctionDeclarationOperation(String function, ITranslationUnit parentElement) {
		super(parentElement);
		fFunction = function;
	}

	/**
	 * @see CreateElementInCUOperation#generateResultHandle
	 */
	protected ICElement generateResultHandle() {
		try {
			return getTranslationUnit().getElement(fFunction);
		} catch (CModelException e) {
		}
		return null;
	}

	/**
	 * @see CreateElementInCUOperation#getMainTaskName
	 */
	public String getMainTaskName(){
		return "operation.createIncludeProgress"; //$NON-NLS-1$
	}

	/**
	 * Sets the correct position for the new include:<ul>
	 * <li> after the last include
	 * <li> if no include, before the first type
	 * <li> if no type, after the package statement
	 * <li> and if no package statement - first thing in the CU
	 */
	protected void initializeDefaultPosition() {
		try {
			ITranslationUnit tu = getTranslationUnit();
			IInclude[] includes = tu.getIncludes();
			if (includes.length > 0) {
				createAfter(includes[includes.length - 1]);
				return;
			}
		} catch (CModelException npe) {
		}
	}

	/**
	 * Possible failures: <ul>
	 *  <li>NO_ELEMENTS_TO_PROCESS - the compilation unit supplied to the operation is
	 * 		<code>null</code>.
	 *  <li>INVALID_NAME - not a valid include declaration name.
	 * </ul>
	 * @see ICModelStatus
	 * @see CNamingConventions
	 */
	public ICModelStatus verify() {
		ICModelStatus status = super.verify();
		if (!status.isOK()) {
			return status;
		}
		//if (CConventions.validateInclude(fIncludeName).getSeverity() == IStatus.ERROR) {
		//	return new CModelStatus(ICModelStatusConstants.INVALID_NAME, fIncludeName);
		//}
		return CModelStatus.VERIFIED_OK;
	}
}

Back to the top