From 4961729ef849e411e892b44673a89bd47790519d Mon Sep 17 00:00:00 2001 From: John Camelon Date: Fri, 4 Jul 2003 15:19:54 +0000 Subject: [PATCH] Patch for Victor Mozgin CTaskTagsReconciler refactoring. --- core/org.eclipse.cdt.core/ChangeLog | 3 + .../internal/core/model/CModelBuilder.java | 149 +++--------------- core/org.eclipse.cdt.core/parser/ChangeLog | 4 + .../cdt/core/parser/ITranslationResult.java | 4 +- .../cdt/core/parser/ParserFactory.java | 21 +++ .../parser/ITranslationResultRequestor.java | 24 --- .../core/parser/TranslationResult.java | 21 ++- .../core/parser/problem/DefaultProblem.java | 6 +- .../eclipse/cdt/core/CTaskTagsReconciler.java | 132 ++++++++++++++++ 9 files changed, 199 insertions(+), 165 deletions(-) delete mode 100644 core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ITranslationResultRequestor.java create mode 100644 core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CTaskTagsReconciler.java diff --git a/core/org.eclipse.cdt.core/ChangeLog b/core/org.eclipse.cdt.core/ChangeLog index 54bb68b379a..3e7152c0572 100644 --- a/core/org.eclipse.cdt.core/ChangeLog +++ b/core/org.eclipse.cdt.core/ChangeLog @@ -1,3 +1,6 @@ +2003-07-04 Victor Mozgin + Added CTaskTagsReconciler. + 2003-07-03 Bogdan Gheorghe Added support for adding individual source files to the index. diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder.java index 56ddb04463f..7fc22052559 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/CModelBuilder.java @@ -17,20 +17,18 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.HashSet; -import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.CTaskTagsReconciler; import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.ICElement; -import org.eclipse.cdt.core.model.ICModelMarker; import org.eclipse.cdt.core.model.INamespace; import org.eclipse.cdt.core.model.IParent; import org.eclipse.cdt.core.model.IStructure; import org.eclipse.cdt.core.model.ITemplate; import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.core.parser.ITranslationResult; import org.eclipse.cdt.core.parser.IParser; -import org.eclipse.cdt.core.parser.IProblem; +import org.eclipse.cdt.core.parser.IProblemReporter; +import org.eclipse.cdt.core.parser.ITranslationResult; import org.eclipse.cdt.core.parser.ParserFactory; import org.eclipse.cdt.core.parser.ParserMode; import org.eclipse.cdt.internal.core.dom.ArrayQualifier; @@ -57,20 +55,11 @@ import org.eclipse.cdt.internal.core.dom.TemplateDeclaration; import org.eclipse.cdt.internal.core.dom.TemplateParameter; import org.eclipse.cdt.internal.core.dom.TranslationUnit; import org.eclipse.cdt.internal.core.dom.TypeSpecifier; -import org.eclipse.cdt.internal.core.parser.DefaultErrorHandlingPolicies; -import org.eclipse.cdt.internal.core.parser.TranslationResult; -import org.eclipse.cdt.internal.core.parser.TranslationOptions; -import org.eclipse.cdt.internal.core.parser.ITranslationResultRequestor; import org.eclipse.cdt.internal.core.parser.Name; -import org.eclipse.cdt.internal.core.parser.problem.DefaultProblemFactory; -import org.eclipse.cdt.internal.core.parser.problem.ProblemReporter; -import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.CoreException; -public class CModelBuilder implements ITranslationResultRequestor { +public class CModelBuilder { protected org.eclipse.cdt.internal.core.model.TranslationUnit translationUnit; protected Map newElements; @@ -83,22 +72,22 @@ public class CModelBuilder implements ITranslationResultRequestor { public Map parse() throws Exception { DOMBuilder domBuilder = new DOMBuilder(); - - // create a problem reporter - Map options = null; - if (translationUnit != null && translationUnit.getCProject() != null) { - options = translationUnit.getCProject().getOptions(true); - } - TranslationOptions cOptions = new TranslationOptions(options); - ProblemReporter problemReporter = new ProblemReporter( - DefaultErrorHandlingPolicies.proceedWithAllProblems(), - cOptions, - new DefaultProblemFactory() - ); + + Map options = null; + IProject currentProject = null; + + if (translationUnit != null && translationUnit.getCProject() != null) { + options = translationUnit.getCProject().getOptions(true); + currentProject = translationUnit.getCProject().getProject(); + } + + // create problem reporter + IProblemReporter problemReporter = ParserFactory.createProblemReporter(options); // create translation result - TranslationResult unitResult = new TranslationResult(translationUnit); + ITranslationResult unitResult = ParserFactory.createTranslationResult(translationUnit.getPath().lastSegment()); + // create parser IParser parser = ParserFactory.createParser( ParserFactory.createScanner( new StringReader( @@ -113,12 +102,12 @@ public class CModelBuilder implements ITranslationResultRequestor { problemReporter, unitResult ); - if( translationUnit.getCProject() != null ) + if( currentProject != null ) { - IProject currentProject = translationUnit.getCProject().getProject(); boolean hasCppNature = CoreModel.getDefault().hasCCNature(currentProject); parser.setCppNature(hasCppNature); } + try { parser.parse(); @@ -140,7 +129,7 @@ public class CModelBuilder implements ITranslationResultRequestor { } // process translation results - acceptResult(unitResult); + CTaskTagsReconciler.getInstance().acceptResult(translationUnit, unitResult); // For the debuglog to take place, you have to call // Util.setDebugging(true); @@ -1054,101 +1043,5 @@ public class CModelBuilder implements ITranslationResultRequestor { } return name; - } - - - /** - * @see ITranslatorRequestor#acceptResult(ITranslationResult) - */ - public void acceptResult(ITranslationResult result) { - ITranslationUnit translationUnit = result.getTranslationUnit(); - - try { - updateTasksFor(translationUnit, result); // record tasks - } catch (CoreException e) { - System.out.println("Exception while accepting parse results"); - e.printStackTrace(); - } - } - - protected void updateTasksFor(ITranslationUnit sourceFile, ITranslationResult result) throws CoreException { - IProblem[] tasks = result.getTasks(); - - storeTasksFor(sourceFile, tasks); - } - - protected void storeTasksFor(ITranslationUnit sourceFile, IProblem[] tasks) throws CoreException { - if (sourceFile == null) return; - - if (tasks == null) tasks = new IProblem[0]; - - IResource resource = sourceFile.getResource(); - IMarker[] existingTaskMarkers = resource.findMarkers(ICModelMarker.TASK_MARKER, false, IResource.DEPTH_ONE); - HashSet taskSet = new HashSet(); - - if (existingTaskMarkers != null) - for (int i=0; i 0)) { - for (int j = 0; j < existingTaskMarkers.length; j++) { - if ( - (((Integer) existingTaskMarkers[j].getAttribute(IMarker.LINE_NUMBER)).intValue() == task.getSourceLineNumber()) - && (((Integer) existingTaskMarkers[j].getAttribute(IMarker.PRIORITY)).intValue() == priority) - && (((Integer) existingTaskMarkers[j].getAttribute(IMarker.CHAR_START)).intValue() == task.getSourceStart()) - && (((Integer) existingTaskMarkers[j].getAttribute(IMarker.CHAR_END)).intValue() == task.getSourceEnd()+1) - && (((String) existingTaskMarkers[j].getAttribute(IMarker.MESSAGE)).equals(task.getMessage())) - ) { - taskSet.remove(existingTaskMarkers[j]); - continue taskLoop; - } - } - } - - IMarker marker = resource.createMarker(ICModelMarker.TASK_MARKER); - - marker.setAttributes( - new String[] { - IMarker.MESSAGE, - IMarker.PRIORITY, - IMarker.DONE, - IMarker.CHAR_START, - IMarker.CHAR_END, - IMarker.LINE_NUMBER, - IMarker.USER_EDITABLE, - }, - new Object[] { - task.getMessage(), - new Integer(priority), - new Boolean(false), - new Integer(task.getSourceStart()), - new Integer(task.getSourceEnd() + 1), - new Integer(task.getSourceLineNumber()), - new Boolean(false), - }); - } - } - - // Remove all obsolete markers - Iterator setI = taskSet.iterator(); - while (setI.hasNext()) { - IMarker marker = (IMarker)setI.next(); - marker.delete(); - } - } + } } diff --git a/core/org.eclipse.cdt.core/parser/ChangeLog b/core/org.eclipse.cdt.core/parser/ChangeLog index 7cd063bf86c..60c02625495 100644 --- a/core/org.eclipse.cdt.core/parser/ChangeLog +++ b/core/org.eclipse.cdt.core/parser/ChangeLog @@ -1,3 +1,7 @@ +2003-07-04 Victor Mozgin + Added CTaskTagsReconciler. + Extended ParserFactory with createProblemReporter() and createTranslationResult(). + 2003-07-02 Victor Mozgin Fixed PR 39501 : Parser problems with throw clauses. diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ITranslationResult.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ITranslationResult.java index d0fa2cc9884..16c354ef19c 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ITranslationResult.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ITranslationResult.java @@ -7,7 +7,7 @@ package org.eclipse.cdt.core.parser; -import org.eclipse.cdt.core.model.ITranslationUnit; +//import org.eclipse.cdt.core.model.ITranslationUnit; /** * @author vmozgin @@ -23,7 +23,7 @@ public interface ITranslationResult { /** * Answer the initial translation unit corresponding to the present translation result */ - public abstract ITranslationUnit getTranslationUnit(); +// public abstract ITranslationUnit getTranslationUnit(); /** * Answer the initial file name diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ParserFactory.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ParserFactory.java index b822db5de12..48c3247f157 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ParserFactory.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/ParserFactory.java @@ -14,14 +14,20 @@ import java.io.Reader; import java.util.List; import java.util.Map; +//import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.parser.ast.IASTFactory; +import org.eclipse.cdt.internal.core.parser.DefaultErrorHandlingPolicies; import org.eclipse.cdt.internal.core.parser.LineOffsetReconciler; import org.eclipse.cdt.internal.core.parser.NullSourceElementRequestor; import org.eclipse.cdt.internal.core.parser.Parser; import org.eclipse.cdt.internal.core.parser.Preprocessor; import org.eclipse.cdt.internal.core.parser.Scanner; +import org.eclipse.cdt.internal.core.parser.TranslationOptions; +import org.eclipse.cdt.internal.core.parser.TranslationResult; import org.eclipse.cdt.internal.core.parser.ast.full.FullParseASTFactory; import org.eclipse.cdt.internal.core.parser.ast.quick.QuickParseASTFactory; +import org.eclipse.cdt.internal.core.parser.problem.DefaultProblemFactory; +import org.eclipse.cdt.internal.core.parser.problem.ProblemReporter; /** @@ -83,5 +89,20 @@ public class ParserFactory { return new LineOffsetReconciler( input ); } + public static IProblemReporter createProblemReporter( Map options ) + { + ITranslationOptions cOptions = new TranslationOptions(options); + IProblemReporter problemReporter = new ProblemReporter( + DefaultErrorHandlingPolicies.proceedWithAllProblems(), + cOptions, + new DefaultProblemFactory() + ); + + return problemReporter; + } + public static ITranslationResult createTranslationResult( String fileName/*ITranslationUnit tu*/ ) + { + return new TranslationResult( fileName /* tu */ ); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ITranslationResultRequestor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ITranslationResultRequestor.java deleted file mode 100644 index 497dbba564d..00000000000 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/ITranslationResultRequestor.java +++ /dev/null @@ -1,24 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2000, 2003 IBM Corporation and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Common Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/cpl-v10.html - * - * Contributors: - * IBM Corporation - initial API and implementation - *******************************************************************************/ -package org.eclipse.cdt.internal.core.parser; - -import org.eclipse.cdt.core.parser.ITranslationResult; - -/** - * A callback interface for receiving translation results. - */ -public interface ITranslationResultRequestor { - - /** - * Accept a translation result. - */ - public void acceptResult(ITranslationResult result); -} diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/TranslationResult.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/TranslationResult.java index d9a50b85063..4f3a3ee2048 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/TranslationResult.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/TranslationResult.java @@ -21,7 +21,7 @@ package org.eclipse.cdt.internal.core.parser; import org.eclipse.cdt.core.parser.*; import org.eclipse.cdt.core.parser.IProblem; -import org.eclipse.cdt.core.model.ITranslationUnit; +//import org.eclipse.cdt.core.model.ITranslationUnit; public class TranslationResult implements ITranslationResult { @@ -29,7 +29,7 @@ public class TranslationResult implements ITranslationResult { public IProblem tasks[]; public int problemCount; public int taskCount; - public ITranslationUnit translationUnit; +// public ITranslationUnit translationUnit; private int maxProblemPerUnit; public int unitIndex, totalUnitsKnown; @@ -49,12 +49,17 @@ public class TranslationResult implements ITranslationResult { } public static final int DEFAULT_MAX_PROBLEMS_PER_UNIT = 100; - +/* public TranslationResult( ITranslationUnit translationUnit) { this(translationUnit, 1, 1, DEFAULT_MAX_PROBLEMS_PER_UNIT); } - +*/ + public TranslationResult( + String fileName) { + this(fileName.toCharArray(), 1, 1, DEFAULT_MAX_PROBLEMS_PER_UNIT); + } +/* public TranslationResult( ITranslationUnit translationUnit, int unitIndex, @@ -67,7 +72,7 @@ public class TranslationResult implements ITranslationResult { this.totalUnitsKnown = totalUnitsKnown; this.maxProblemPerUnit = maxProblemPerUnit; } - +*/ private int computePriority(IProblem problem) { @@ -141,9 +146,9 @@ public class TranslationResult implements ITranslationResult { /** * Answer the initial translation unit corresponding to the present translation result */ - public ITranslationUnit getTranslationUnit() { - return translationUnit; - } +// public ITranslationUnit getTranslationUnit() { +// return translationUnit; +// } /** * Answer the initial file name diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/problem/DefaultProblem.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/problem/DefaultProblem.java index ff95768df78..fdb98d6b7dc 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/problem/DefaultProblem.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/problem/DefaultProblem.java @@ -10,7 +10,7 @@ *******************************************************************************/ package org.eclipse.cdt.internal.core.parser.problem; -import org.eclipse.cdt.core.model.ITranslationUnit; +//import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.parser.IProblem; @@ -43,7 +43,7 @@ public class DefaultProblem implements IProblem { this.line = line; } - public String errorReportSource(ITranslationUnit translationUnit) { + public String errorReportSource(char[] source) { //extra from the source the innacurate token //and "highlight" it using some underneath ^^^^^ //put some context around too. @@ -58,7 +58,7 @@ public class DefaultProblem implements IProblem { final char SPACE = '\u0020'; final char MARK = '^'; final char TAB = '\t'; - char[] source = translationUnit.getContents(); + //char[] source = translationUnit.getContents(); //the next code tries to underline the token..... //it assumes (for a good display) that token source does not //contain any \r \n. This is false on statements ! diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CTaskTagsReconciler.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CTaskTagsReconciler.java new file mode 100644 index 00000000000..9b10c61a52c --- /dev/null +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/CTaskTagsReconciler.java @@ -0,0 +1,132 @@ +package org.eclipse.cdt.core; + +/********************************************************************** + * Copyright (c) 2002,2003 Rational Software Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v0.5 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v05.html + * + * Contributors: + * IBM Rational Software - Initial API and implementation +***********************************************************************/ + +import java.util.HashSet; +import java.util.Iterator; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.model.ICModelMarker; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.parser.ITranslationResult; +import org.eclipse.cdt.core.parser.IProblem; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; + + +public class CTaskTagsReconciler { + + private static CTaskTagsReconciler instance = null; + + private CTaskTagsReconciler() { + } + + public static CTaskTagsReconciler getInstance() { + if (instance == null) { + instance = new CTaskTagsReconciler(); + } + return instance; + } + + + public void acceptResult(ITranslationUnit translationUnit, ITranslationResult result) { + try { + updateTasksFor(translationUnit, result); // record tasks + } catch (CoreException e) { + System.out.println("Exception while accepting parse results"); + e.printStackTrace(); + } + } + + protected void updateTasksFor(ITranslationUnit sourceFile, ITranslationResult result) throws CoreException { + IProblem[] tasks = result.getTasks(); + + storeTasksFor(sourceFile, tasks); + } + + protected void storeTasksFor(ITranslationUnit sourceFile, IProblem[] tasks) throws CoreException { + if (sourceFile == null) return; + + if (tasks == null) tasks = new IProblem[0]; + + IResource resource = sourceFile.getResource(); + IMarker[] existingTaskMarkers = resource.findMarkers(ICModelMarker.TASK_MARKER, false, IResource.DEPTH_ONE); + HashSet taskSet = new HashSet(); + + if (existingTaskMarkers != null) + for (int i=0; i 0)) { + for (int j = 0; j < existingTaskMarkers.length; j++) { + if ( + (((Integer) existingTaskMarkers[j].getAttribute(IMarker.LINE_NUMBER)).intValue() == task.getSourceLineNumber()) + && (((Integer) existingTaskMarkers[j].getAttribute(IMarker.PRIORITY)).intValue() == priority) + && (((Integer) existingTaskMarkers[j].getAttribute(IMarker.CHAR_START)).intValue() == task.getSourceStart()) + && (((Integer) existingTaskMarkers[j].getAttribute(IMarker.CHAR_END)).intValue() == task.getSourceEnd()+1) + && (((String) existingTaskMarkers[j].getAttribute(IMarker.MESSAGE)).equals(task.getMessage())) + ) { + taskSet.remove(existingTaskMarkers[j]); + continue taskLoop; + } + } + } + + IMarker marker = resource.createMarker(ICModelMarker.TASK_MARKER); + + marker.setAttributes( + new String[] { + IMarker.MESSAGE, + IMarker.PRIORITY, + IMarker.DONE, + IMarker.CHAR_START, + IMarker.CHAR_END, + IMarker.LINE_NUMBER, + IMarker.USER_EDITABLE, + }, + new Object[] { + task.getMessage(), + new Integer(priority), + new Boolean(false), + new Integer(task.getSourceStart()), + new Integer(task.getSourceEnd() + 1), + new Integer(task.getSourceLineNumber()), + new Boolean(false), + }); + } + } + + // Remove all obsolete markers + Iterator setI = taskSet.iterator(); + while (setI.hasNext()) { + IMarker marker = (IMarker)setI.next(); + marker.delete(); + } + } +}