Skip to main content
summaryrefslogtreecommitdiffstats
blob: ff811d9821460fee6cfb491e52dabf1470993912 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation 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:
 * IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.internal.core.parser.ParserMessages;

/**
 * @author jcamelon
 */
public class CPPASTProblem extends CPPASTNode implements IASTProblem {
    private IASTNode parent;

    private ASTNodeProperty property;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.cdt.core.dom.ast.IASTNode#getParent()
     */
    public IASTNode getParent() {
        return parent;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.cdt.core.dom.ast.IASTNode#setParent(org.eclipse.cdt.core.dom.ast.IASTNode)
     */
    public void setParent(IASTNode node) {
        this.parent = node;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.cdt.core.dom.ast.IASTNode#getPropertyInParent()
     */
    public ASTNodeProperty getPropertyInParent() {
        return property;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.cdt.core.dom.ast.IASTNode#setPropertyInParent(org.eclipse.cdt.core.dom.ast.ASTNodeProperty)
     */
    public void setPropertyInParent(ASTNodeProperty property) {
        this.property = property;
    }

    private final char[] arg;

    private final int id;

    private final boolean isError;

    private final boolean isWarning;

    private String message = null;

    public CPPASTProblem(int id, char[] arg, boolean warn, boolean error) {
        this.id = id;
        this.arg = arg;
        this.isWarning = warn;
        this.isError = error;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.cdt.core.parser.IASTProblem#getID()
     */
    public int getID() {
        return id;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.cdt.core.parser.IASTProblem#isError()
     */
    public boolean isError() {
        return isError;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.cdt.core.parser.IASTProblem#isWarning()
     */
    public boolean isWarning() {
        return isWarning;
    }

    protected static final Map errorMessages;
    static {
        errorMessages = new HashMap();
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_POUND_ERROR),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.error")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_INCLUSION_NOT_FOUND),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.inclusionNotFound")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_DEFINITION_NOT_FOUND),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.definitionNotFound")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_INVALID_MACRO_DEFN),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.invalidMacroDefn")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_INVALID_MACRO_REDEFN),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.invalidMacroRedefn")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_UNBALANCE_CONDITION),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.unbalancedConditional")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(
                                IASTProblem.PREPROCESSOR_CONDITIONAL_EVAL_ERROR),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.conditionalEval")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_MACRO_USAGE_ERROR),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.macroUsage")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_CIRCULAR_INCLUSION),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.circularInclusion")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_INVALID_DIRECTIVE),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.invalidDirective")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_MACRO_PASTING_ERROR),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.macroPasting")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(
                                IASTProblem.PREPROCESSOR_MISSING_RPAREN_PARMLIST),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.missingRParen")); //$NON-NLS-1$       
        errorMessages
                .put(
                        new Integer(IASTProblem.PREPROCESSOR_INVALID_VA_ARGS),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.preproc.invalidVaArgs")); //$NON-NLS-1$       
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_INVALID_ESCAPECHAR),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.invalidEscapeChar")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_UNBOUNDED_STRING),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.unboundedString")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_BAD_FLOATING_POINT),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.badFloatingPoint")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_BAD_HEX_FORMAT),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.badHexFormat")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_BAD_OCTAL_FORMAT),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.badOctalFormat")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_BAD_DECIMAL_FORMAT),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.badDecimalFormat")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_ASSIGNMENT_NOT_ALLOWED),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.assignmentNotAllowed")); //$NON-NLS-1$        
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_DIVIDE_BY_ZERO),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.divideByZero")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_MISSING_R_PAREN),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.missingRParen")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_EXPRESSION_SYNTAX_ERROR),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.expressionSyntaxError")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_ILLEGAL_IDENTIFIER),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.illegalIdentifier")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_BAD_CONDITIONAL_EXPRESSION),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.badConditionalExpression")); //$NON-NLS-1$        
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_UNEXPECTED_EOF),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.unexpectedEOF")); //$NON-NLS-1$
        errorMessages
                .put(
                        new Integer(IASTProblem.SCANNER_BAD_CHARACTER),
                        ParserMessages
                                .getString("ScannerProblemFactory.error.scanner.badCharacter")); //$NON-NLS-1$
        errorMessages.put(new Integer(IASTProblem.SYNTAX_ERROR), ParserMessages
                .getString("ParserProblemFactory.error.syntax.syntaxError")); //$NON-NLS-1$
    }

    protected final static String AST_PROBLEM_PATTERN = "BaseProblemFactory.astProblemPattern"; //$NON-NLS-1$

    public String getMessage() {
        if (message != null)
            return message;

        String msg = (String) errorMessages.get(new Integer(id));
        if (msg == null)
            msg = ""; //$NON-NLS-1$

        if (arg != null) {
            msg = MessageFormat.format(msg, new Object[] { new String(arg) });
        }

        
        String file = null;
        int offset = 0;
        IASTFileLocation f = getFileLocation();
        if( f == null )
        {
            file = ""; //$NON-NLS-1$
            offset = 0;
        }
        else
        {
            file = f.getFileName();
            offset = f.getNodeOffset();
        }
        Object[] args = new Object[] { msg, file, new Integer(offset) }; //$NON-NLS-1$        
        message = ParserMessages.getFormattedString(AST_PROBLEM_PATTERN, args);
        return message;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.cdt.core.parser.IASTProblem#checkCategory(int)
     */
    public boolean checkCategory(int bitmask) {
        return ((id & bitmask) != 0);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.cdt.core.parser.IASTProblem#getArguments()
     */
    public String getArguments() {
        return arg != null ? String.valueOf(arg) : ""; //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.cdt.core.dom.ast.IASTNode#getTranslationUnit()
     */
    public IASTTranslationUnit getTranslationUnit() {
        if (this instanceof IASTTranslationUnit)
            return (IASTTranslationUnit) this;
        IASTNode node = getParent();
        while (!(node instanceof IASTTranslationUnit) && node != null) {
            node = node.getParent();
        }
        return (IASTTranslationUnit) node;
    }

    public boolean accept( ASTVisitor action ){
    	if( action.shouldVisitProblems ){
		    switch( action.visit( this ) ){
	            case ASTVisitor.PROCESS_ABORT : return false;
	            case ASTVisitor.PROCESS_SKIP  : return true;
	            default : break;
	        }
		}
        return true;
    }
}

Back to the top