1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Provide error markers for the pathentry ... Work in Progress.

This commit is contained in:
Alain Magloire 2004-05-17 12:30:43 +00:00
parent c3ff06502c
commit ad08ad2679
10 changed files with 455 additions and 13 deletions

View file

@ -821,10 +821,24 @@ public class CoreModel {
* the entries location are compatible, otherwise a status
* object indicating what is wrong with them
*/
//private static ICModelStatus validatePathEntry(ICProject cProject, IPathEntry[] entries) {
//pathEntryManager.validatePathEntry(cproject, entries)
// return null;
//}
public static ICModelStatus validatePathEntry(ICProject cProject, IPathEntry[] entries) {
return pathEntryManager.validatePathEntry(cProject, entries);
}
/**
* Returns a C model status describing the problem related to this entry if any,
* a status object with code <code>IStatus.OK</code> if the entry is fine (that is, if the
* given entry denotes a valid element).
*
* @param cProject the given C project
* @param entry the given entry
* @param checkSourceAttachment a flag to determine if source attachement should be checked
* @param recurseInContainers flag indicating whether validation should be applied to container entries recursively
* @return a c model status describing the problem related to this entry if any, a status object with code <code>IStatus.OK</code> if the entry is fine
*/
public static ICModelStatus validatePathEntry(ICProject cProject, IPathEntry entry, boolean checkSourceAttachment, boolean recurseInContainers){
return pathEntryManager.validatePathEntry(cProject, entry, checkSourceAttachment, recurseInContainers);
}
public static IPathEntryStore getPathEntryStore(IProject project) throws CoreException {
return CCorePlugin.getDefault().getPathEntryStore(project);

View file

@ -41,6 +41,22 @@ public interface ICModelMarker {
public static final String TASK_MARKER = CCorePlugin.PLUGIN_ID + ".task"; //$NON-NLS-1$
public static final String INDEXER_MARKER = CCorePlugin.PLUGIN_ID + ".indexermarker"; //$NON-NLS-1$
/**
* PatheEtnry problem marker type (value <code>"org.eclipse.cdt.core.pathentry_problem"</code>).
* This can be used to recognize those markers in the workspace that flag problems
* detected by the C tooling during pathEntry setting.
*/
public static final String PATHENTRY_PROBLEM_MARKER = CCorePlugin.PLUGIN_ID + ".pathentry_problem"; //$NON-NLS-1$
/**
* PathEntry file format marker attribute (value <code>"PathEntryFileFormat"</code>).
* Used only on pathentry store problem markers.
* The value of this attribute is either "true" or "false".
*
*/
public static final String PATHENTRY_FILE_FORMAT = "pathEntryFileFormat"; //$NON-NLS-1$
}

View file

@ -17,6 +17,11 @@ package org.eclipse.cdt.core.model;
*/
public interface ICModelStatusConstants {
/**
* Status constant indicating that a pathentry was invalid
*/
public static final int INVALID_PATHENTRY = 964;
/**
* Status constant indicating that a variable path was not resolvable
* indicating either the referred variable is undefined, unbound or the resolved

View file

@ -118,4 +118,14 @@ public abstract class APathEntry extends PathEntry {
return super.equals(obj);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(super.toString());
sb.append(" base-path:").append(basePath.toString()); //$NON-NLS-1$
sb.append(" base-ref:").append(baseRef.toString()); //$NON-NLS-1$
return sb.toString();
}
}

View file

@ -97,4 +97,14 @@ public class IncludeEntry extends APathEntry implements IIncludeEntry {
return p;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(super.toString());
sb.append(" isSystemInclude:").append(isSystemInclude); //$NON-NLS-1$
sb.append(" includePath:").append(includePath); //$NON-NLS-1
return sb.toString();
}
}

View file

@ -156,4 +156,13 @@ public class LibraryEntry extends APathEntry implements ILibraryEntry {
public IPath getLibraryPath() {
return libraryPath;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(super.toString());
sb.append(" librarypath:").append(libraryPath.toString()); //$NON-NLS-1$
return sb.toString();
}
}

View file

@ -74,4 +74,14 @@ public class MacroEntry extends APathEntry implements IMacroEntry {
return super.equals(obj);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(super.toString());
sb.append(" name:").append(macroName); //$NON-NLS-1$
sb.append(" value:").append(macroValue); //$NON-NLS-1$
return sb.toString();
}
}

View file

@ -12,6 +12,7 @@
***********************************************************************/
package org.eclipse.cdt.internal.core.model;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
@ -19,9 +20,12 @@ import java.util.Map;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.CoreModelUtil;
import org.eclipse.cdt.core.model.ElementChangedEvent;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICElementDelta;
import org.eclipse.cdt.core.model.ICModelMarker;
import org.eclipse.cdt.core.model.ICModelStatus;
import org.eclipse.cdt.core.model.ICModelStatusConstants;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IContainerEntry;
@ -38,8 +42,11 @@ import org.eclipse.cdt.core.model.PathEntryContainerInitializer;
import org.eclipse.cdt.core.resources.IPathEntryStore;
import org.eclipse.cdt.core.resources.IPathEntryStoreListener;
import org.eclipse.cdt.core.resources.PathEntryStoreChangedEvent;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
@ -95,8 +102,14 @@ public class PathEntryManager implements IPathEntryStoreListener, IElementChange
}
public IPathEntry[] getResolvedPathEntries(ICProject cproject) throws CModelException {
boolean markers = cproject.getProject().getWorkspace().isTreeLocked();
return getResolvedPathEntries(cproject, !markers);
}
public IPathEntry[] getResolvedPathEntries(ICProject cproject, boolean generateMarkers) throws CModelException {
IPathEntry[] entries = (IPathEntry[])resolvedMap.get(cproject);
if (entries == null) {
IPath projectPath = cproject.getPath();
entries = getRawPathEntries(cproject);
ArrayList list = new ArrayList();
for (int i = 0; i < entries.length; i++) {
@ -109,13 +122,14 @@ public class PathEntryManager implements IPathEntryStoreListener, IElementChange
IPathEntry[] containerEntries = container.getPathEntries();
if (containerEntries != null) {
for (int j = 0; j < containerEntries.length; j++) {
IPathEntry newEntry = cloneEntry(cproject.getPath(), containerEntries[j]);
IPathEntry newEntry = cloneEntry(projectPath, containerEntries[j]);
list.add(newEntry);
}
}
}
} else {
IPathEntry e = getExpandedPathEntry(entry, cproject);
IPathEntry clone = cloneEntry(projectPath, entry);
IPathEntry e = getExpandedPathEntry(clone, cproject);
if (e != null) {
list.add(e);
}
@ -123,6 +137,20 @@ public class PathEntryManager implements IPathEntryStoreListener, IElementChange
}
entries = new IPathEntry[list.size()];
list.toArray(entries);
if (generateMarkers) {
IProject project = cproject.getProject();
flushPathEntryProblemMarkers(project);
ICModelStatus status = validatePathEntry(cproject, entries);
if (!status.isOK()) {
createPathEntryProblemMarker(project, status);
}
for (int j = 0; j < entries.length; j++) {
status = validatePathEntry(cproject, entries[j], true, false);
if (!status.isOK()) {
createPathEntryProblemMarker(project, status);
}
}
}
resolvedMap.put(cproject, entries);
}
return entries;
@ -536,8 +564,92 @@ public class PathEntryManager implements IPathEntryStoreListener, IElementChange
return NO_PREREQUISITES;
}
public void saveRawPathEntries(ICProject cproject, IPathEntry[] newRawEntries) throws CModelException {
public void saveRawPathEntries(ICProject cproject, IPathEntry[] entries) throws CModelException {
ArrayList list = new ArrayList(entries.length);
IPath projectPath = cproject.getPath();
for (int i = 0; i < entries.length; i++) {
IPathEntry entry;
int kind = entries[i].getEntryKind();
// translate the project prefix.
IPath resourcePath = entries[i].getPath();
if (resourcePath == null) {
resourcePath = Path.EMPTY;
}
// Do not do this for container, the path is the ID.
if (kind != IPathEntry.CDT_CONTAINER) {
// translate to project relative from absolute (unless a device path)
if (resourcePath.isAbsolute()) {
if (projectPath != null && projectPath.isPrefixOf(resourcePath)) {
if (resourcePath.segment(0).equals(projectPath.segment(0))) {
resourcePath = resourcePath.removeFirstSegments(1);
resourcePath = resourcePath.makeRelative();
} else {
resourcePath = resourcePath.makeAbsolute();
}
}
}
}
// Specifics to the entries
switch(kind) {
case IPathEntry.CDT_INCLUDE: {
IIncludeEntry include = (IIncludeEntry)entries[i];
entry = CoreModel.newIncludeEntry(resourcePath, include.getBasePath(), include.getIncludePath(),
include.isSystemInclude(), include.getExclusionPatterns(), include.isExported());
break;
}
case IPathEntry.CDT_LIBRARY: {
ILibraryEntry library = (ILibraryEntry)entries[i];
IPath sourcePath = library.getSourceAttachmentPath();
if (sourcePath != null) {
// translate to project relative from absolute
if (projectPath != null && projectPath.isPrefixOf(sourcePath)) {
if (sourcePath.segment(0).equals(projectPath.segment(0))) {
sourcePath = sourcePath.removeFirstSegments(1);
sourcePath = sourcePath.makeRelative();
}
}
}
entry = CoreModel.newLibraryEntry(resourcePath, library.getBasePath(),
library.getLibraryPath(), sourcePath, library.getSourceAttachmentRootPath(),
library.getSourceAttachmentPrefixMapping(), library.isExported());
break;
}
case IPathEntry.CDT_MACRO: {
IMacroEntry macro = (IMacroEntry)entries[i];
entry = CoreModel.newMacroEntry(resourcePath, macro.getMacroName(), macro.getMacroValue(),
macro.getExclusionPatterns(), macro.isExported());
break;
}
case IPathEntry.CDT_OUTPUT: {
IOutputEntry out = (IOutputEntry)entries[i];
entry = CoreModel.newOutputEntry(resourcePath, out.getExclusionPatterns());
break;
}
case IPathEntry.CDT_PROJECT: {
IProjectEntry projEntry = (IProjectEntry)entries[i];
entry = CoreModel.newProjectEntry(projEntry.getPath(), projEntry.isExported());
break;
}
case IPathEntry.CDT_SOURCE: {
ISourceEntry source = (ISourceEntry)entries[i];
entry = CoreModel.newSourceEntry(resourcePath, source.getExclusionPatterns());
break;
}
case IPathEntry.CDT_CONTAINER:
entry = CoreModel.newContainerEntry(entries[i].getPath(), entries[i].isExported());
break;
default:
entry = entries[i];
}
list.add(entry);
}
try {
IPathEntry[] newRawEntries = new IPathEntry[list.size()];
list.toArray(newRawEntries);
IProject project = cproject.getProject();
IPathEntryStore store = getPathEntryStore(project, true);
store.setRawPathEntries(newRawEntries);
@ -801,8 +913,14 @@ public class PathEntryManager implements IPathEntryStoreListener, IElementChange
}
protected IPathEntry cloneEntry(IPath rpath, IPathEntry entry) {
// get the path
IPath entryPath = entry.getPath();
IPath resourcePath = (entryPath == null || entryPath.isEmpty()) ? rpath : entryPath;
if (entryPath == null) {
entryPath = Path.EMPTY;
}
IPath resourcePath = (entryPath.isAbsolute()) ? entryPath : rpath.append(entryPath);
switch(entry.getEntryKind()) {
case IPathEntry.CDT_INCLUDE: {
IIncludeEntry include = (IIncludeEntry)entry;
@ -822,7 +940,7 @@ public class PathEntryManager implements IPathEntryStoreListener, IElementChange
}
case IPathEntry.CDT_OUTPUT: {
IOutputEntry out = (IOutputEntry)entry;
return CoreModel.newOutputEntry(out.getPath(), out.getExclusionPatterns());
return CoreModel.newOutputEntry(resourcePath, out.getExclusionPatterns());
}
case IPathEntry.CDT_PROJECT: {
IProjectEntry projEntry = (IProjectEntry)entry;
@ -830,12 +948,248 @@ public class PathEntryManager implements IPathEntryStoreListener, IElementChange
}
case IPathEntry.CDT_SOURCE: {
ISourceEntry source = (ISourceEntry)entry;
return CoreModel.newSourceEntry(source.getPath(), source.getExclusionPatterns());
return CoreModel.newSourceEntry(resourcePath, source.getExclusionPatterns());
}
case IPathEntry.CDT_CONTAINER:
default:
return CoreModel.newContainerEntry(entry.getPath(), entry.isExported());
}
return entry;
}
public ICModelStatus validatePathEntry(ICProject cProject, IPathEntry[] entries) {
// Check duplication.
for (int i = 0; i < entries.length; i++) {
IPathEntry entry = entries[i];
if (entry == null) {
continue;
}
for (int j = 0; j < entries.length; j++) {
IPathEntry otherEntry = entries[j];
if (otherEntry == null) {
continue;
}
if (entry != otherEntry && otherEntry.equals(entry)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, "Duplication:" + entry.toString()); //$NON-NLS-1$
}
}
}
// allow nesting source entries in each other as long as the outer entry excludes the inner one
for (int i = 0; i < entries.length; i++) {
IPathEntry entry = entries[i];
if (entry == null) {
continue;
}
IPath entryPath = entry.getPath();
int kind = entry.getEntryKind();
if (kind == IPathEntry.CDT_SOURCE){
for (int j = 0; j < entries.length; j++){
IPathEntry otherEntry = entries[j];
if (otherEntry == null) {
continue;
}
int otherKind = otherEntry.getEntryKind();
IPath otherPath = otherEntry.getPath();
if (entry != otherEntry && (otherKind == IPathEntry.CDT_SOURCE)) {
char[][] exclusionPatterns = ((ISourceEntry)otherEntry).fullExclusionPatternChars();
if (otherPath.isPrefixOf(entryPath) && !otherPath.equals(entryPath)
&& !CoreModelUtil.isExcluded(entryPath.append("*"), exclusionPatterns)) { //$NON-NLS-1$
String exclusionPattern = entryPath.removeFirstSegments(otherPath.segmentCount()).segment(0);
if (CoreModelUtil.isExcluded(entryPath, exclusionPatterns)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, "Nested:" + entry.toString()); //$NON-NLS-1$
} else {
if (otherKind == IPathEntry.CDT_SOURCE) {
exclusionPattern += '/';
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, "Nested:" + entry.toString()); //$NON-NLS-1$
} else {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, "Nested:" + entry.toString()); //$NON-NLS-1$
}
}
}
}
}
}
}
return CModelStatus.VERIFIED_OK;
}
public ICModelStatus validatePathEntry(ICProject cProject, IPathEntry entry, boolean checkSourceAttachment, boolean recurseInContainers){
IProject project = cProject.getProject();
IPath projectPath = cProject.getPath();
String entryMesg = projectPath.toString() + ": " + entry.toString(); //$NON-NLS-1$
switch(entry.getEntryKind()) {
case IPathEntry.CDT_INCLUDE: {
IIncludeEntry include = (IIncludeEntry)entry;
IPath path = include.getPath();
if (!isValidWorkspacePath(project, path)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg); //$NON-NLS-1$
}
IPath includePath = include.getFullIncludePath();
if (!isValidExternalPath(includePath)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg); //$NON-NLS-1$
}
break;
}
case IPathEntry.CDT_LIBRARY: {
ILibraryEntry library = (ILibraryEntry)entry;
IPath path = library.getPath();
if (!isValidWorkspacePath(project, path)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg); //$NON-NLS-1$
}
if (checkSourceAttachment) {
IPath sourceAttach = library.getSourceAttachmentPath();
if (sourceAttach != null) {
if(!sourceAttach.isAbsolute()) {
if (!isValidWorkspacePath(project, sourceAttach)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg);
}
} else if (!isValidExternalPath(sourceAttach)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg);
}
}
}
IPath libraryPath = library.getFullLibraryPath();
if (!isValidExternalPath(libraryPath)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg); //$NON-NLS-1$
}
break;
}
case IPathEntry.CDT_MACRO: {
IMacroEntry macro = (IMacroEntry)entry;
IPath path = macro.getPath();
if (!isValidWorkspacePath(project, path)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg); //$NON-NLS-1$
}
break;
}
case IPathEntry.CDT_OUTPUT: {
IOutputEntry out = (IOutputEntry)entry;
IPath path = out.getPath();
if (!isValidWorkspacePath(project, path)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg); //$NON-NLS-1$
}
break;
}
case IPathEntry.CDT_PROJECT: {
IProjectEntry projEntry = (IProjectEntry)entry;
IPath path = projEntry.getPath();
if (path != null && path.isAbsolute() && !path.isEmpty()) {
IProject reqProject = project.getWorkspace().getRoot().getProject(path.segment(0));
if (!reqProject.exists() || !(CoreModel.hasCCNature(reqProject) || CoreModel.hasCCNature(reqProject))) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg); //$NON-NLS-1$
}
if (!reqProject.isOpen()){
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg); //$NON-NLS-1$
}
} else {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg); //$NON-NLS-1$
}
break;
}
case IPathEntry.CDT_SOURCE: {
ISourceEntry source = (ISourceEntry)entry;
IPath path = source.getPath();
if (!isValidWorkspacePath(project, path)) {
return new CModelStatus(ICModelStatusConstants.INVALID_PATHENTRY, entryMesg); //$NON-NLS-1$
}
break;
}
case IPathEntry.CDT_CONTAINER:
break;
}
return CModelStatus.VERIFIED_OK;
}
private boolean isValidWorkspacePath(IProject project, IPath path) {
if (path == null) {
return false;
}
IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();
// We accept empy path as the project
IResource res = null;
if (path.isAbsolute()) {
res = workspaceRoot.findMember(path);
} else {
res = project.findMember(path);
}
return (res != null && res.isAccessible());
}
private boolean isValidExternalPath(IPath path) {
if (path != null) {
File file = path.toFile();
if (file != null) {
return file.exists();
}
}
return false;
}
/**
* Record a new marker denoting a classpath problem
*/
void createPathEntryProblemMarker(IProject project, ICModelStatus status) {
IMarker marker = null;
int severity;
switch (status.getCode()) {
case ICModelStatusConstants.INVALID_PATHENTRY :
severity = IMarker.SEVERITY_WARNING;
break;
case ICModelStatusConstants.INVALID_PATH:
severity = IMarker.SEVERITY_WARNING;
break;
default:
severity = IMarker.SEVERITY_ERROR;
break;
}
try {
marker = project.createMarker(ICModelMarker.PATHENTRY_PROBLEM_MARKER);
marker.setAttributes(
new String[] {
IMarker.MESSAGE,
IMarker.SEVERITY,
IMarker.LOCATION,
ICModelMarker.PATHENTRY_FILE_FORMAT,
},
new Object[] {
status.getString(),
new Integer(severity),
"pathentry",//$NON-NLS-1$
"false",//$NON-NLS-1$
}
);
} catch (CoreException e) {
// could not create marker: cannot do much
e.printStackTrace();
}
}
/**
* Remove all markers denoting classpath problems
*/
protected void flushPathEntryProblemMarkers(IProject project) {
IWorkspace workspace = project.getWorkspace();
try {
if (project.isAccessible()) {
IMarker[] markers = project.findMarkers(ICModelMarker.PATHENTRY_PROBLEM_MARKER, false, IResource.DEPTH_ZERO);
if (markers != null) {
workspace.deleteMarkers(markers);
}
}
} catch (CoreException e) {
// could not flush markers: not much we can do
e.printStackTrace();
}
}
}

View file

@ -4,7 +4,7 @@ providerName=Eclipse.org
cnature.name=C Nature
ccnature.name=C++ Nature
CProblemMarker.name=C Problem
PathEntryProblemMarker.name=Path Entry Problem
CBuildCommand.name=C Builder Command
CBuildConsole.name=C Builder Console
CProject.name=C Project

View file

@ -371,5 +371,19 @@
type="org.eclipse.core.resources.textmarker">
</super>
</extension>
<!-- =================================================================================== -->
<!-- Marker for problems in the PathEntrie -->
<!-- =================================================================================== -->
<extension
id="pathentry_problem"
name="%PathEntryProblemMarker.name"
point="org.eclipse.core.resources.markers">
<super
type="org.eclipse.core.resources.problemmarker">
</super>
<persistent
value="true">
</persistent>
</extension>
</plugin>