Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c6d80c4422d37614aa447e56850b455bad4c39ba (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
/*******************************************************************************
 * Copyright (c) 2008 Wind River Systems, Inc. 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:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/ 
package org.eclipse.cdt.internal.ui.actions;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;

import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexFileLocation;
import org.eclipse.cdt.core.index.IIndexInclude;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.ExtendedScannerInfo;

import org.eclipse.cdt.internal.core.model.ASTCache;
import org.eclipse.cdt.internal.core.model.TranslationUnit;
import org.eclipse.cdt.internal.core.pdom.ASTFilePathResolver;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.indexer.ProjectIndexerInputAdapter;

import org.eclipse.cdt.internal.ui.editor.ASTProvider;

public class CreateParserLogAction implements IObjectActionDelegate {

	private static final class MyVisitor extends ASTVisitor {
		List<IASTProblem> fProblems= new ArrayList<IASTProblem>();
		List<IProblemBinding> fProblemBindings= new ArrayList<IProblemBinding>();
		List<Exception> fExceptions= new ArrayList<Exception>();

		MyVisitor() {
			shouldVisitProblems= true;
			shouldVisitNames= true;
		}

		@Override
		public int visit(IASTProblem problem) {
			fProblems.add(problem);
			return PROCESS_SKIP;
		}
		
		@Override
		public int visit(IASTName name) {
			if (name instanceof ICPPASTQualifiedName) {
				return PROCESS_CONTINUE;
			}
			try {
				IBinding binding= name.resolveBinding();
				if (binding instanceof IProblemBinding) {
					fProblemBindings.add((IProblemBinding) binding);
				}
			} catch (RuntimeException e) {
				fExceptions.add(e);
			}
			return PROCESS_CONTINUE;
		}
	}

	private ISelection fSelection;
	private IWorkbenchPartSite fSite;
	
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		fSite= targetPart.getSite();
	}

	public void selectionChanged(IAction action, ISelection selection) {
		fSelection = selection;
	}

	public void run(IAction action) {
		if (!(fSelection instanceof IStructuredSelection))
			return;
		
		final String title= action.getText().replace("&", ""); //$NON-NLS-1$ //$NON-NLS-2$
		IStructuredSelection cElements= SelectionConverter.convertSelectionToCElements(fSelection);
		Iterator i= cElements.iterator();
		ArrayList<ITranslationUnit> tuSelection= new ArrayList<ITranslationUnit>();
		while (i.hasNext()) {
			Object o= i.next();
			if (o instanceof ITranslationUnit) {
				tuSelection.add((ITranslationUnit) o);
			}
		}
		ITranslationUnit[] tuArray= tuSelection.toArray(new ITranslationUnit[tuSelection.size()]);
		if (tuArray.length == 0) {
			return;
		}
		FileDialog dlg= new FileDialog(fSite.getShell(), SWT.SAVE);
		dlg.setText(title);
		dlg.setFilterExtensions(new String[]{"*.log"});  //$NON-NLS-1$
		String path= null;
		while(path == null) {
			path= dlg.open();
			if (path == null)
				return;

			File file= new File(path);		
			if (file.exists()) {
				if (!file.canWrite()) {
					final String msg= NLS.bind(ActionMessages.getString("CreateParserLogAction.readOnlyFile"), path); //$NON-NLS-1$
					MessageDialog.openError(fSite.getShell(), title, msg);
					path= null;
				}
				else {
					final String msg = NLS.bind(ActionMessages.getString("CreateParserLogAction.existingFile"), path); //$NON-NLS-1$
					if (!MessageDialog.openQuestion(fSite.getShell(), title, msg)) {
						path= null;
					}
				}
			}
		}
		
		try {
			PrintStream out= new PrintStream(path);
			try {
				boolean needsep= false;
				for (ITranslationUnit tu : tuArray) {
					if (needsep) {
						out.println(); out.println(); 
					}
					createLog(out, tu, new NullProgressMonitor());
					needsep= true;
				}
			}
			finally {
				out.close();
			}
		} catch (IOException e) {
			MessageDialog.openError(fSite.getShell(), action.getText(), e.getMessage());
		}
	}

	private void createLog(final PrintStream out, final ITranslationUnit tu, IProgressMonitor pm) {
		ASTProvider.getASTProvider().runOnAST(tu, ASTProvider.WAIT_YES, pm, new ASTCache.ASTRunnable() {
			public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreException {
				return createLog(out, tu, lang, ast);
			}
		});
	}

	protected IStatus createLog(PrintStream out, ITranslationUnit tu, ILanguage lang, IASTTranslationUnit ast) {
		IStatus status = Status.OK_STATUS;
		final ICProject cproject = tu.getCProject();
		final String projectName= cproject == null ? null : cproject.getElementName();
		
		ITranslationUnit ctx= tu;
		if (tu instanceof TranslationUnit) {
			TranslationUnit itu= (TranslationUnit) tu;
			ctx= itu.getSourceContextTU(ast.getIndex(), ITranslationUnit.AST_CONFIGURE_USING_SOURCE_CONTEXT);
		}
		final ExtendedScannerInfo scfg= new ExtendedScannerInfo(ctx.getScannerInfo(true));
		final String indent= "   "; //$NON-NLS-1$
		final MyVisitor visitor= new MyVisitor();
		ast.accept(visitor);
		
		out.println("Project:       " + projectName); //$NON-NLS-1$
		out.println("Index Version: " + PDOM.CURRENT_VERSION); //$NON-NLS-1$
		out.println("File:          " + tu.getLocationURI()); //$NON-NLS-1$
		out.println("Context:       " + ctx.getLocationURI()); //$NON-NLS-1$
		out.println("Language:      " + lang.getName()); //$NON-NLS-1$
		out.println();
		out.println("Include Search Path (option -I):");  //$NON-NLS-1$
		output(out, indent, scfg.getIncludePaths());
		out.println();
		out.println("Local Include Search Path (option -iquote):"); //$NON-NLS-1$
		output(out, indent, scfg.getLocalIncludePath());
		out.println();
		out.println("Preincluded files (option -include):"); //$NON-NLS-1$
		output(out, indent, scfg.getIncludeFiles());
		out.println();
		out.println("Preincluded macro files (option -imacros):"); //$NON-NLS-1$
		output(out, indent, scfg.getMacroFiles());
		out.println();
		out.println("Macro definitions (option -D):"); //$NON-NLS-1$
		HashSet<String> reported= new HashSet<String>();
		output(out, indent, scfg.getDefinedSymbols(), reported);
		out.println();
		out.println("Macro definitions (from configuration + headers in index):"); //$NON-NLS-1$
		output(out, indent, ast.getBuiltinMacroDefinitions(), reported);
		out.println();
		out.println("Macro definitions (from files actually parsed):"); //$NON-NLS-1$
		output(out, indent, ast.getMacroDefinitions(), reported);

		out.println();
		out.println("Unresolved includes (from headers in index):"); //$NON-NLS-1$
		try {
			outputUnresolvedIncludes(cproject, ast.getIndex(), out, indent, ast.getIncludeDirectives(), ast.getLinkage().getLinkageID());
		} catch (CoreException e) {
			status= e.getStatus();
		}
		
		out.println();
		out.println("Scanner problems:"); //$NON-NLS-1$
		output(out, indent, ast.getPreprocessorProblems());

		out.println();
		out.println("Parser problems:"); //$NON-NLS-1$
		output(out, indent, visitor.fProblems.toArray(new IASTProblem[visitor.fProblems.size()]));
		
		out.println();
		out.println("Unresolved names:"); //$NON-NLS-1$
		output(out, indent, visitor.fProblemBindings);

		out.println();
		out.println("Exceptions in name resolution:"); //$NON-NLS-1$
		output(out, visitor.fExceptions);

		return status;
	}

	private void outputUnresolvedIncludes(ICProject prj, IIndex index, PrintStream out, String indent, 
			IASTPreprocessorIncludeStatement[] includeDirectives, int linkageID) throws CoreException {
		ASTFilePathResolver resolver= new ProjectIndexerInputAdapter(prj);
		for (IASTPreprocessorIncludeStatement include : includeDirectives) {
			if (include.isActive() && include.isResolved()) {
				outputUnresolvedIncludes(index, out, indent, resolver.resolveASTPath(include.getPath()), linkageID);
			}
		}
	}

	private void outputUnresolvedIncludes(IIndex index, PrintStream out, String indent, 
			IIndexFileLocation ifl, int linkageID) throws CoreException {
		IIndexFile ifile= index.getFile(linkageID, ifl);
		if (ifile == null) {
			out.println(indent + ifl.getURI() + " is not indexed"); //$NON-NLS-1$
		}
		else {
			IIndexInclude[] includes = ifile.getIncludes();
			for (IIndexInclude inc : includes) {
				if (inc.isActive()) {
					if (inc.isResolved()) {
						outputUnresolvedIncludes(index, out, indent, inc.getIncludesLocation(), linkageID);
					}
					else {
						out.println(indent + "Unresolved inclusion: " + inc.getName() + " in file " +  //$NON-NLS-1$//$NON-NLS-2$
								inc.getIncludedByLocation().getURI());
					}
				}
			}
		}
	}

	private void output(PrintStream out, String indent, String[] list) {
		for (String line : list) {
			out.println(indent + line);
		}
	}

	private void output(PrintStream out, String indent, Map<String, String> definedSymbols, HashSet<String> reported) {
		for (Entry<String, String> entry : definedSymbols.entrySet()) {
			final String macro = entry.getKey() + '=' + entry.getValue();
			if (reported.add(macro)) {
				out.println(indent + macro);
			}
		}
	}
	
	private void output(PrintStream out, String indent,	IASTPreprocessorMacroDefinition[] defs, HashSet<String> reported) {
		for (IASTPreprocessorMacroDefinition def : defs) {
			String macro= def.toString();
			if (reported.add(macro)) {
				out.println(indent + macro);
			}
		}
	}
	
	private void output(PrintStream out, String indent,	IASTProblem[] preprocessorProblems) {
		for (IASTProblem problem : preprocessorProblems) {
			out.println(indent + problem.getMessage());
		}
	}
	
	private void output(PrintStream out, String indent, List<IProblemBinding> list) {
		for (IProblemBinding problem : list) {
	        String file= problem.getFileName();
	        int line = problem.getLineNumber();
			out.println(indent + problem.getMessage() + " in file " + file + ':' + line); //$NON-NLS-1$
		}
	}

	private void output(PrintStream out, List<Exception> list) {
		for (Exception problem : list) {
			problem.printStackTrace(out);
		}
	}
}

Back to the top